【发布时间】:2020-07-14 12:17:04
【问题描述】:
我正在尝试在 python 中实现多重混合继承,它也给出了答案,但我想知道答案序列背后的原因是什么,对于这段代码:
class GrandPa:
def __init__(self):
print("Grand Pa")
class Father(GrandPa):
def __init__(self):
super().__init__()
print("Father")
class Mother(GrandPa):
def __init__(self):
super().__init__()
print("Mother")
class child(Father, Mother):
def __init__(self):
super().__init__()
print("Child")
c = child()
输出
Grand Pa
Mother
Father
Child
在子类中,我们在母亲之前采取了父亲类,那么父亲不应该在母亲之前打印吗?这个序列背后有什么逻辑吗?
【问题讨论】:
-
由于 MRO(方法解析顺序),它会发生这种情况。尝试拨打
c.__mro__ -
您可以在多重继承和MRO的文档中阅读更多内容:docs.python.org/3/tutorial/classes.html#multiple-inheritance
标签: python oop inheritance