0
我的代码:多重继承()
class A():
def __init__(self, a = 100):
self.a = a
class B():
def __init__(self, b = 200):
self.b = b
class C(A,B):
def __init__(self, a, b, c = 300):
super().__init__(a)
super().__init__(b)
self.c = c
def output(self):
print(self.a)
print(self.b)
print(self.c)
def main():
c = C(1,2,3)`enter code here`
c.output()
main()
错误:
2
Traceback (most recent call last):
File "inheritance.py", line 25, in <module>
main()
File "inheritance.py", line 23, in main
c.output()
File "inheritance.py", line 17, in output
print(self.b)
AttributeError: 'C' object has no attribute 'b'
为什么不能继承在B? 这段代码有什么问题? 以及如何修改此代码?
如果我用A或B替换supper(),它可以正常运行。 那么是什么原因导致这个问题呢? 如果我不使用super(),我可以使用什么方法?
'超().__ init__'为您提供一流的'A'的构造。这意味着你用'A'的构造函数初始化你的'C'实例两次,并且永远不要调用'B'的构造函数。 –