【发布时间】:2019-11-02 14:27:13
【问题描述】:
每当我尝试在 vampire(monster) 类中运行 sleep 方法时,都会发生以下错误:
AttributeError: 'vampire' object has no attribute 'hasCastle'
该错误链接到代码的第 8 行。
我不明白为什么会发生这种情况,因为我是继承新手。
代码:
class vampire(monster):
def _init__(self, givenHasCastle, givenStrength, givenName):
super().__init__(givenStrength, givenName)
self.hasCastle = givenHasCastle
def drinkBlood(self):
print(self.name+" the vampire drinks the hero's blood.")
def sleep(self):
if self.hasCastle == True:
print("The vampire sleeps silently in their castle.")
else:
print("The vampire sleeps silently in the wilderness.")
【问题讨论】:
-
错字:
__init__缺少下划线
标签: python class object inheritance attributes