如果子类是多重继承的话,两个父类都有一个同名方法,子类调用了那会怎样?
如果子类是多重继承的话,两个父类都有一个同名方法,子类调用了那会怎样?到底调用哪个
如果子类是多重继承的话,两个父类都有一个同名方法,子类调用了那会怎样?到底调用哪个
2016-07-21
class Person(object): def WhoFirst(selt): print 'Person first' class Student(Person): pass class Teacher(Person): pass class SkillMixin(object): def WhoFirst(selt): print 'SkillMixin first' class BasketballMixin(SkillMixin): def skill(self): return 'basketball' class FootballMixin(SkillMixin): def skill(self): return 'football' class BStudent(BasketballMixin,Student): pass class FTeacher(FootballMixin,Teacher): pass s = BStudent() print s.skill() print s.WhoFirst() t = FTeacher() print t.skill() print t.WhoFirst()
测试了一下,的确就是从左往右查找,网上看的说按广度优先,这里不知道到底咋回事
class Person(object): def WhoFirst(selt): print 'Person first'class Student(Person): passclass Teacher(Person): passclass SkillMixin(object): def WhoFirst(selt): print 'SkillMixin first'class BasketballMixin(SkillMixin): def skill(self): return 'basketball'class FootballMixin(SkillMixin): def skill(self): return 'football'class BStudent(BasketballMixin,Student): passclass FTeacher(FootballMixin,Teacher): passs = BStudent()print s.skill()print s.WhoFirst()t = FTeacher()print t.skill()print t.WhoFirst()
举报