cannot do multiple inheritance using parameterized constructors
本问题已经有最佳答案,请猛点这里访问。
我无法使用继承调用父类的变量和构造函数。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | class base(): #age =24; def __init__(self,age): self.age = age; print("i am in base") class Date(base): def __init__(self,year,month,date): self.year = year self.month = month self.date = date class time(Date): def greet(self): print('i am in time') a = time(1,2,4) print(a.year) print(a.age) #this line is throwing error... |
请帮助,如何调用父类的构造函数
您在示例中所做的不是多重继承。多重继承是指同一类从多个基类继承。
您遇到的问题是,由于没有任何子类调用父构造函数,因此需要这样做并传递所需的参数:
1 2 3 4 5 6 7 8 9 10 11 12 13 | class Date(base): def __init__(self, age, year, month, date): super().__init__(age) self.year = year self.month = month self.date = date class time(Date): def __init__(self, age, year, month, date): super().__init__(age, year, month, date) def greet(self): print('i am in time') |