【发布时间】:2022-06-13 21:43:38
【问题描述】:
类 M 和 F 是两个独立的类。 C 类继承了 F 和 M 。我不明白为什么类 F 的 super() 关键字调用类 M 的 init 。有人可以解释这段代码背后的工作原理吗?
class M:
def __init__(self):
print("M constructor")
class F:
def __init__(self):
print("F constructor")
super().__init__()
super().__init__()
class C(F,M):
def __init__(self):
super().__init__()
print("C constructor")
C()
O/P
F 构造函数
M 构造函数
M 构造函数
C 构造函数
【问题讨论】:
-
super()并不真正意味着“这个类的父级”;它的意思是“方法解析顺序中的下一个类”,在多重继承的情况下可能完全不同。
标签: python python-3.x oop