概述
Python3中只有新式类,所以他的多重继承采用C3搜索也可以把它看成就近搜索。
详解
多重继承采用 就近原则 搜索,class C4(C1,C2,C3) 先检索自身后检索C1、C2、C2的父类(甚至是父类的父类)、C3
代码
示例1
class C0: name = 'C0' def c(self): pass class C1: num = 1 def c1(self): pass class C2(C0): num = 2 def c2(self): pass class C3: name = 'C3' def c3(self): pass class C4(C1,C2,C3): pass c4 = C4() print(c4.name) print(c4.num)
示例2
class NewStyleClassA(object): var = 'New Style Class A' #多层继承 class NewStyleClassB(NewStyleClassA): pass class NewStyleClassC(object): var = 'New Style Class C' #多重继承,采用就近原则 class SubNewStyleClass(NewStyleClassB, NewStyleClassC): pass if __name__ == '__main__': print(SubNewStyleClass.mro()) print(SubNewStyleClass.var) [<class '__main__.SubNewStyleClass'>, <class '__main__.NewStyleClassB'>, <class '__main__.NewStyleClassA'>, <class '__main__.NewStyleClassC'>, <type 'object'>] New Style Class A
参考链接