这是一个采访示例问题我复制并从问题10改性: https://www.codementor.io/sheena/essential-python-interview-questions-du107ozr6蟒多重继承qustion
class A(object):
def go(self):
print("go A go!")
def stop(self):
print("stop A stop!")
class B(A):
def go(self):
super(B, self).go()
print("go B go!")
class C(A):
def go(self):
super(C, self).go()
print("go C go!")
def stop(self):
super(C, self).stop()
print("stop C stop!")
class D(B,C):
def go(self):
super(D, self).go()
print("go D go!")
def stop(self):
super(D, self).stop()
print("stop D stop!")
class E(B,C): pass
a = A()
b = B()
c = C()
d = D()
e = E()
print "call b.stop()......."
b.stop()
print "call d.stop()......."
d.stop()
print "call e.stop()......."
e.stop()
答案是:
call b.stop().......
stop A stop!
call d.stop().......
stop A stop!
stop C stop!
stop D stop! #?why not having b.stop() which leads to "stop A stop!"
call e.stop().......
stop A stop!
stop C stop!
我明白调用b.stop()示出了“停一停!”因为b不会覆盖stop(),所以会从A继承stop()。
但是我不明白为什么调用d.stop()只显示A,C,D, :D-> B-> C-> A?
我不明白为什么调用e.stop()只显示停止A和C,基于MRO:E-> B-> C-> A,我认为e.stop()应该继承B停止(),所以应该停止A停止,停止C停止,然后停止B停止?
我一定有误解......。关于超我猜。
好吧,所以“超级只关心看B本身不是祖先”,。得到它了。谢谢。 – user389955