Bucky Roberts · September 9, 2014
class Enemy:
life = 3
def attack(self):
print('attack!')
self.life -= 1
def checkLife(self):
if self.life <= 0:
print("I am dead")
else:
print(str(self.life) + " life left")
enemy1 = Enemy()
enemy2 = Enemy()
# each object is independent of one another, they don't share variables
enemy1.attack()
enemy1.attack()
enemy1.checkLife()
enemy2.checkLife()
网友评论