相关代码 1:
class A(object):
def show(self):
print ('init A...')
class B(A):
def show(self):
super(B, self).show()
print('init B...')
class C(A):
def show(self):
# super(C, self).show()
print('init C...')
class D(B, C):
def show(self):
super(D, self).show()
print('init D...')
d = D()
d.show()
输出的结果是:
init C...
init B...
init D...
这里想问的是为什么没有经过 A,输出 init A...
相关代码 2:
class A(object):
def show(self):
print ('init A...')
class B(A):
def show(self):
super(B, self).show()
print('init B...')
class C(A):
def show(self):
# super(C, self).show()
print('init C...')
class D(C, B): #继承类和代码 1 中的顺序相反
def show(self):
super(D, self).show()
print('init D...')
d = D()
d.show()
输出的结果是:
init C...
init D...
这里想问的是为什么 B 中的方法没有被调用? 还有的就是新式类的 MRO 算法采用广度优先搜索。在这里是怎么调用的?
谢谢各位大佬