【发布时间】:2020-12-18 19:42:24
【问题描述】:
为什么在这段代码中没有调用 A:[是因为 mro : left to right then A class should be called?]
class A:
def __init__(self,name):
print('inside a',name)
class B:
def __init__(self,name):
print('inside b',name)
class C(B,A):
def __init__(self,name):
print('inside c',name)
super().__init__(name)
c = C('hello')
输出:
inside c hello
inside b hello
但是当我这样定义它基本上是一个父类时,它按预期正常工作。[为什么在这里调用一个类]代码:
class D:
def __init__(self,name):
print('inside d',name)
class A(D):
def __init__(self,name):
print('inside a',name)
super().__init__(name)
class B(D):
def __init__(self,name):
print('inside b',name)
super().__init__(name)
class C(B,A):
def __init__(self,name):
print('inside c',name)
super().__init__(name)
c = C('hello')
输出:
inside c hello
inside b hello
inside a hello
inside d hello
【问题讨论】:
-
要让
super()正确使用多重继承,您必须在层次结构中的每个 类中使用它——甚至是基类。
标签: python python-3.x inheritance multiple-inheritance