python3 对象之间的交互

别说谁变了你拦得住时间么 提交于 2019-11-28 02:01:48
 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)
View Code
>>打印
100
is attacking <__main__.Riven object at 0x000002BF4FEF11D0> <__main__.Garen object at 0x000002BF4FEF1198>
50

  

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!