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'的構造函數。 –