1 #class people: #经典类2 classpeople (object): #新式类3 def __init__(self,name,age):4Self.name=name5Self.age= Age6self.friends=[]7 defEat (self):8 Print("%s is eating."%self.name)9 defSleep (self):Ten Print("%s is sleeping."%self.name) One classRelation (object): A defmake_friends (self,obj): - Print('%s is making friends with%s'%(self.name,obj.name)) - self.friends.append (obj) the obj.friends.append (self) - - classMan (people,relation):#multiple inheritance, inheritance order from left to right - def __init__(Self,name,age,money): + #people.__init__ (self,name,age) -Super (man, self).__init__(Name,age)#new types of writing +self.property= Money A Print('%s had%s Yuan when he was born'%(self.name,self.property)) at defSleep (self):# Refactoring - #people.sleep (self) #继承父类 - Super (Man,self). Sleep () - Print('%s is dahaning'%self.name) - classWoman (Relation,People):
- #Multiple inheritance (constructors only need one), inheritance order from left to right, \
in #the first parent class has a constructor that does not execute the constructor of the second parent class, and if the first parent class does not have a constructor, it goes to the second parent class to find
- Pass
1 m1=man ('Alex', 33,10)2 w1=woman (' May ', +)34m1.make_friends (W1)5print (m1.friends[0].name) 6 Print (W1.friends[0].name)
Both the man and the woman classes inherit the two parent classes of people and relation, but there are no constructors in the relation class. When woman is instantiated, it goes to the relation class to find the constructor, because it is not found, so go to the people class to find the constructor (left to right order).
About Inheritance Order:
In Python2, the new class is inherited by the breadth first, and the classic class is inherited by the depth first.
In Python3 , both the new class and the classic class are inherited by breadth precedence .
Python Multiple inheritance