我是Python的新手,我正在努力学习如何使用类。有谁知道为什么这不起作用?关于关键字" self"的任何其他提示非常感谢。
代码:
class Enemy:
life = 3
def attack(self):
print('ouch!')
self.life -= 1
def checkLife(self):
if self.life <= 0:
print('I am dead')
else:
print(str(self.life) + "life left")
enemy1 = Enemy
enemy1.attack()
enemy1.checkLife()
错误:
C:\Users\Liam\AppData\Local\Programs\Python\Python36-32\python.exe C:/Users/Liam/PycharmProjects/YouTube/first.py
Traceback (most recent call last):
File "C:/Users/Liam/PycharmProjects/YouTube/first.py", line 16, in <module>
enemy1.attack()
TypeError: attack() missing 1 required positional argument: 'self'
Process finished with exit code 1
答案 0 :(得分:2)
cell.second_TableView.contentSize.height
是班级。 Enemy
是类Enemy()
的一个实例。
你需要初始化类,
Enemy