组合

给一个类的对象封装一个属性,这个属性是另一个类的对象。

# 添加武器:斧子,刀,枪,棍,棒....

class GameRole:

    def __init__(self,name,ad,hp):

        self.name = name

        self.ad = ad

        self.hp = hp

    def attack(self,p):

       p.hp = p.hp - self.ad

       print("%s攻击%s,%s掉了%s血,还剩%s血” % (self.name,p.name,p.name,self.sd,p.hp))

class Weapon:

    def __init__(self,name,ad):

       self.name = name

       self.ad = ad

    def fight(self,p1,p2):

        p2.hp =p2.hp - self.ad

        print("%s用%s打了%s,掉了%s血,还剩%s血” % (p1.name,self.name,p2.name,p2.name,self.ad,p2.hp))

 p1 =GameRole('盖伦’,20,500)       

  p2 =GameRole('亚索’,50,200)            

axe = Weapon("三板斧",60)

broadsword = Weapon(“屠龙宝刀”,100)

axe.fight(p1,p2)

broadword.fight()

 

#第2版

 

类2

 

       

    

相关文章:

  • 2021-10-12
  • 2021-08-03
  • 2022-12-23
  • 2021-10-23
  • 2021-10-04
  • 2022-12-23
  • 2022-12-23
  • 2021-08-09
猜你喜欢
  • 2021-05-27
  • 2021-09-07
  • 2021-08-29
  • 2022-12-23
  • 2022-12-23
  • 2022-01-07
  • 2021-08-11
相关资源
相似解决方案