classBase():""" My base class """
__nb_instances =0def__init__(self):
Base.__nb_instances +=1
self.id= Base.__nb_instances
classUser(Base):""" My User class """def__init__(self):super().__init__()
self.id+=99
u = User()print(u.id)
classRobot:def__init__(self, name):
self.name = name
defsay_hi(self):print("Hi, I am "+ self.name)classPhysicianRobot(Robot):defsay_hi(self):print("Everything will be okay! ")print(self.name +" takes care of you!")
y = PhysicianRobot("James")
y.say_hi()
# Python code to demonstrate how parent constructors# are called.# parent classclassPerson(object):# __init__ is known as the constructordef__init__(self, name, idnumber):
self.name = name
self.idnumber = idnumber
defdisplay(self):print(self.name)print(self.idnumber)# child classclassEmployee(Person):def__init__(self, name, idnumber, salary, post):
self.salary = salary
self.post = post
# invoking the __init__ of the parent class
Person.__init__(self, name, idnumber)# creation of an object variable or an instance
a = Employee('Rahul',886012,200000,"Intern")# calling a function of the class Person using its instance
a.display()
classRobot:def__init__(self, name):
self.name = name
defsay_hi(self):print("Hi, I am "+ self.name)classPhysicianRobot(Robot):pass
x = Robot("Marvin")
y = PhysicianRobot("James")print(x,type(x))print(y,type(y))
y.say_hi()
classCoordinates:def__init__(self, x, y):
self.__x = x #variables that start with double _ are private and only can be accessed by method
self.__y = y
defx(self):return self.__x
defy(self):return self.__y
defposition(self):return(self.__x, self.__y)classSquare(Coordinates):def__init__(self, x, y, w, h):super().__init__(x, y)
self.__w = w
self.__h = h
defw(self):return self.__w
defh(self):return self.__h
defarea(self):return self.__w * self.__h
classCube(Square):def__init__(self, x, y, w, h, d):super().__init__(x,y,w,h)
self.__d = d
defd(self):return self.__d
defarea(self):#overwrites square area functionreturn2*(self.__w * self.__h + self.__d * self.__h + self.__w * self.__d)defvolume(self):return self.__w * self.__h * self.__d
# A Python program to demonstrate inheritance# Base or Super class. Note object in bracket.# (Generally, object is made ancestor of all classes)# In Python 3.x "class Person" is# equivalent to "class Person(object)"classPerson(object):# Constructordef__init__(self, name):
self.name = name
# To get namedefgetName(self):return self.name
# To check if this person is an employeedefisEmployee(self):returnFalse# Inherited or Subclass (Note Person in bracket)classEmployee(Person):# Here we return truedefisEmployee(self):returnTrue# Driver code
emp = Person("Geek1")# An Object of Personprint(emp.getName(), emp.isEmployee())
emp = Employee("Geek2")# An Object of Employeeprint(emp.getName(), emp.isEmployee())
# A Python program to demonstrate inheritance# Base or Super class. Note object in bracket.# (Generally, object is made ancestor of all classes)# In Python 3.x "class Person" is# equivalent to "class Person(object)"classPerson(object):# Constructordef__init__(self, name):
self.name = name
# To get namedefgetName(self):return self.name
# To check if this person is an employeedefisEmployee(self):returnFalse# Inherited or Subclass (Note Person in bracket)classEmployee(Person):# Here we return truedefisEmployee(self):returnTrue# Driver code
emp = Person("Geek1")# An Object of Personprint(emp.getName(), emp.isEmployee())
emp = Employee("Geek2")# An Object of Employeeprint(emp.getName(), emp.isEmployee())
classPolygon:def__init__(self, no_of_sides):
self.n = no_of_sides
self.sides =[0for i inrange(no_of_sides)]definputSides(self):
self.sides =[float(input("Enter side "+str(i+1)+" : "))for i inrange(self.n)]defdispSides(self):for i inrange(self.n):print("Side",i+1,"is",self.sides[i])
<__main__.Robot object at 0x7fd0080b3ba8><class'__main__.Robot'><__main__.PhysicianRobot object at 0x7fd0080b3b70><class'__main__.PhysicianRobot'>
Hi, I am James
# A Python program to demonstrate inheritance# Base or Super class. Note object in bracket.# (Generally, object is made ancestor of all classes)# In Python 3.x "class Person" is# equivalent to "class Person(object)"classPerson(object):# Constructordef__init__(self, name):
self.name = name
# To get namedefgetName(self):return self.name
# To check if this person is an employeedefisEmployee(self):returnFalse# Inherited or Subclass (Note Person in bracket)classEmployee(Person):# Here we return truedefisEmployee(self):returnTrue# Driver code
emp = Person("Geek1")# An Object of Personprint(emp.getName(), emp.isEmployee())
emp = Employee("Geek2")# An Object of Employeeprint(emp.getName(), emp.isEmployee())