python3 对象之间的交互
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)
1 2 3 4 | >>打印 100 is attacking <__main__.Riven object at 0x000002BF4FEF11D0 > <__main__.Garen object at 0x000002BF4FEF1198 > 50 |
随心而动--momo
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步