本帖最后由 wbzxz 于 2023-6-11 08:52 编辑
非常感谢您的指点,再深入的问一下,下面的两段代码,分别去掉了B类中的super()和C类中的super(), 为什么都导致了FishA不能输出
print('两层的类的继承, 去掉B的supper()')
class A:
def say(self):
print("FishA")
class B(A):
def say(self):
# super().say()
print("FishB")
class C(A):
def say(self):
super().say()
print("FishC")
class D(B, C):
def say(self):
super().say()
d = D()
d.say()
print(D.__mro__)
FishB
(<class '__main__.D'>, <class '__main__.B'>, <class '__main__.C'>, <class '__main__.A'>, <class 'object'>)
print('两层的类的继承, 去掉C的supper()')
class A:
def say(self):
print("FishA")
class B(A):
def say(self):
super().say()
print("FishB")
class C(A):
def say(self):
# super().say()
print("FishC")
class D(B, C):
def say(self):
super().say()
d = D()
d.say()
print(D.__mro__)
FishC
FishB
(<class '__main__.D'>, <class '__main__.B'>, <class '__main__.C'>, <class '__main__.A'>, <class 'object'>)
|