组合
组合:不同类实例化的对象产生关联,使一个类的对象可以作为另一个类的属性
class Gamerole: def __init__(self,name,sex,age,ad,hp): self.name1 = name self.sex1 = sex self.age1 = age self.ad1 = ad self.hp1 = hp def fight(self,role): role.hp1 = role.hp1 - self.ad1 print('%s赤手空拳打了%s%s点血,%s还剩%s血' % (self.name1, role.name1,self.ad1,role.name1, role.hp1)) def weapon(self,w): self.weapon_1 = w r1 = Gamerole('苍井井','女',18,20,200) r2 = Gamerole('东尼木木','男',20,30,150) r3 = Gamerole('波多多','女',19,50,80) class Weapon: def __init__(self,wname,wad): self.wname1 = wname self.wad = wad def fighting(self,role2,role3): role3.hp1 = role3.hp1 - (role2.ad1 + self.wad) print('%s利用%s打了%s一%s,%s还剩%s血' % \ (role2.name1,self.wname1, role3.name1, self.wname1, role3.name1, role3.hp1)) def fight1(self, role4, role5,role6,role7): role7.hp1 = role7.hp1 - (role6.ad1 + self.wad) print('%s骑着%s打了骑着%s的%s一%s,%s哭了,还剩%s血' % \ (role6.name1,role4.mname1,role5.mname1,role7.name1,\ self.wname1,role7.name1,role7.hp1)) def car(self,c): self.cars = c def car1(self,c1): self.cars1 = c1 w1 = Weapon('平底锅',20) w2 = Weapon('斧子',50) w3 = Weapon('双节棍',65) class Traffic: def __init__(self,mname,speed): self.mname1 = mname self.speed1 = speed m1 = Traffic('小踏板',60) m2 = Traffic('雅马哈',80) m3 = Traffic('宝马',120) # 波多多骑着小踏板打了骑着雅马哈的东尼木木一斧子,东尼木木哭了,还剩xx血。 r3.weapon(w2) w3.car(m1) w3.car1(m2) r3.weapon_1.fight1(m1,m2,r3,r2)