1 class Garen: 2 camp='Demacia' 3 def __init__(self,nickname,aggresivity,life_value): 4 self.nickname=nickname #g1.nicknam=nickname 5 self.aggrv=aggresivity 6 self.life_value=life_value 7 8 def attack(self,enemy): 9 print('is attacking',self,enemy) 10 11 class Riven: 12 camp='Noxus' 13 def __init__(self,nickname,aggresivity,life_value): 14 self.nickname=nickname #g1.nicknam=nickname 15 self.aggrv=aggresivity 16 self.life_value=life_value 17 18 def attack(self,enemy): 19 print('is attacking',self,enemy) 20 enemy.life_value-=self.aggrv #g1.life_value-=r1.aggrv 21 22 g1=Garen('草丛伦',82,100) 23 r1=Riven('锐雯雯',50,200) 24 25 print(g1.life_value) 26 # 27 r1.attack(g1) 28 print(g1.life_value)
>>打印 100 is attacking <__main__.Riven object at 0x000002BF4FEF11D0> <__main__.Garen object at 0x000002BF4FEF1198> 50
来源:http://www.cnblogs.com/xp1005/p/6529847.html