【发布时间】:2021-08-19 21:46:25
【问题描述】:
class base():
def __init__(self):
self.var = 10
def add(self, num):
res = self.var+num
return res
class inherit(base):
def __init__(self, num=10):
x = super().add(num)
a = inherit()
print(a)
你好,
我正在学习继承和 super()。运行此程序时,将返回错误 AttributeError: 'inherit' object has no attribute 'var'。如何也继承 init 变量?
【问题讨论】:
-
首先调用
super()的构造函数初始化类实例,然后访问实例变量。
标签: python oop inheritance super