【发布时间】:2019-09-28 19:40:35
【问题描述】:
class Parent:
def __init__(self):
self.__num = 100
def show(self):
print("Parent:",self.__num)
class Child(Parent):
def __init__(self):
self.__var = 10
def show(self):
super().show()
print("Child:",self.__var)
obj1 = Child()
obj1.show()
文件“main.py”,第 12 行,在节目中
超级().show()
文件“main.py”,第 6 行,在 show
print("父母:",self.__num)
AttributeError: 'Child' 对象没有属性 '_Parent__num'
【问题讨论】:
-
将
super().__init__()添加到您的构造函数中 -
不要使用双下划线变量,它们会受到名称修改的影响,您应该先熟悉它。
-
@KlausD。刚刚添加了这个作为答案......