我无法使用继承调用父类的变量和构造函数。
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...
请帮助,如何调用父类的构造函数
答案 0 :(得分:1)
您在示例中所做的不是多重继承。多重继承是指同一类从多个基类继承。
您遇到的问题是因为没有一个子类调用父构造函数,因此您需要这样做并传递所需的参数:
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')