# 组合 # 什么是组合 : 一个类对象的属性是另外一个类的对象 class Person: def __init__(self,name,sex,hp,mp,ad): self.name = name self.sex = sex self.hp = hp self.mp = mp self.ad = ad def attack(self,dog): print('%s攻击了%s'%(self.name,dog.name)) dog.hp -= self.ad class Dog: def __init__(self,name,kind,hp,ad): self.name = name self.kind = kind self.hp = hp self.ad = ad def bite(self,person): print('%s咬了%s'%(self.name,person.name)) person.hp -= self.ad alex = Person('alex','不详',10,10,0.1) hei = Dog('小黑','中华田园犬',999,1.1) hei.bite(alex) # 赚钱 # 充值 # 武器装备 增加人的攻击力ad # 武器类 : class Weapon: def __init__(self,name,price,ad): self.name = name self.price = price self.ad = ad def skill(self,dog): print('%s被%s攻击了'%(dog.name,self.name)) dog.hp -= self.ad sh = Weapon('鞋底子',9.99,66) # sh.skill(hei) alex.武器 = sh # 组合 # 什么是组合 : 一个类对象的属性是另外一个类的对象 alex.武器.skill(hei) # 组合的应用 sh.ad -= 10 alex.武器.skill(hei)