【发布时间】:2020-12-28 05:27:03
【问题描述】:
我正在学习继承,遇到了这个问题
class A:
def test(self):
print("test of A called")
class B(A):
def test(self):
print("test of B called")
super().test()
class C(A):
def test(self):
print("test of C called")
super().test()
class D(B,C):
def test2(self):
print("test of D called")
obj=D()
obj.test()
根据发布此问题的网站,输出如下
test of B called
test of C called
test of A called
但在我看来,输出应该是
test of B called
test of A called
因为,B 类将首先被调用(Acc to MRO),然后从 B 类调用 super().test(),这将打印
test of A called.
我哪里错了?
【问题讨论】:
-
谷歌“python mro”-docs.python.org/3/glossary.html#term-method-resolution-order
-
D的 mro 是(__main__.D, __main__.B, __main__.C, __main__.A, object) -
@juanpa.arrivillaga 谢谢。
标签: python oop inheritance