Python面向对象练习
class Enty:blood=100name=""atackvalue=100team=0domain=['1'] #1,land 2 airdef setTeam(self,team=0):self.team=teamdef atack(self,Enty):if self.team==Enty.team:print("不能向盟军开火")self.info()passelse :# print(self.domain)s=set(self.domain)es=set(Enty.domain)if s&es :print(f'{self.name}/{self.blood}/{self.atackvalue}攻击{Enty.name}/{Enty.blood}/{Enty.atackvalue}\n开始攻击')Enty.blood-=self.atackvalueprint(f'{Enty.name}被掉血{self.atackvalue},当前血量{Enty.blood}')else:print("不在火力范围")def info(self):print(f'血量{self.blood},名称{self.name}')class tank(Enty):def __init__(self,team=0):self.name="坦克"self.blood=1000super().setTeam(team)passclass Soldier(Enty):atackvalue = 20blood = 200def __init__(self,team=0):self.name="大兵"super().setTeam(team)passclass FlySoldier(Soldier):def __init__(self,team=0):self.name='飞行兵'self.domain=['2']super().setTeam(team)passclass DefenseSoldier(Soldier):def __init__(self,team=0):self.name="防空兵"self.domain=['1','2']super().setTeam(team)t=tank()
s=Soldier()
f=FlySoldier()
d=DefenseSoldier()
t1=tank(1)
s1=Soldier(1)
f1=FlySoldier(1)
d1=DefenseSoldier(1)s.atack(f)
t.atack(s)
s.atack(t)
s.atack(f1)
t.atack(f1)
d.atack(f1)