zoukankan      html  css  js  c++  java
  • python对象 -- 组合

    详解组合:
    #Demo1
    class Game_kind:
    def __init__(self,nickname,sex,hp,ad):
    self.nickname = nickname
    self.sex = sex
    self.hp = hp
    self.ad = ad

    def hit(self,people1):
    people1.hp -= self.ad
    print('%s有%s滴血'% (self.nickname,self.hp))
    print('%s有%s滴血' % (people1.nickname, people1.hp))
    print('%s攻击了%s,%s还剩%s滴血量' %(self.nickname,self.ad,people1.nickname,people1.hp))

    lvbu = Game_kind('吕布','男',200,100)
    hanxin = Game_kind('韩信','男',200,50)
    lvbu.hit(hanxin)


    #Demo2
    class Game_kind:
    def __init__(self,nickname,sex,hp,ad): # 构建初始人物的属性
    self.nickname = nickname
    self.sex = sex
    self.hp = hp
    self.ad = ad

    def hit(self,people1): # 人物1通过自身攻击力攻击了人物2
    people1.hp -= self.ad
    print('%s有%s滴血'% (self.nickname,self.hp))
    print('%s的攻击力为%s' %(self.nickname,self.ad))
    print('%s有%s滴血' % (people1.nickname, people1.hp))
    print('%s的攻击力为%s' % (people1.nickname, people1.ad))
    print('-'*50)
    print('第一次攻击')
    print('通过自身攻击力攻击的伤害')
    print('%s攻击了%s,%s还剩%s滴血量' %(self.nickname,self.ad,people1.nickname,people1.hp))

    def people_weapon(self,pweapon): # 人类通过在自己内部学会了使用武器的心法,从而可以直接使用武器
    self.pweapon = pweapon

    class Weapon:
    def __init__(self,weapon_name,weapon_ad): # 武器的攻击属性
    self.weapon_name = weapon_name
    self.weapon_ad = weapon_ad

    def weapon_hit(self,people1,people2): #人物1使用了武器攻击人物2
    people2.hp -= self.weapon_ad
    print('通过武器攻击造成的伤害')
    print('%s使用%s打了%s%s血,%s还剩%s滴血' % (people1.nickname,self.weapon_name,people2.nickname,self.weapon_ad,people2.nickname,people2.hp))

    lvbu = Game_kind('吕布','男',200,100)
    hanxin = Game_kind('韩信','男',200,50)
    fight = Weapon('狼牙棒',100)
    print('-'*50)
    lvbu.hit(hanxin) # 吕布通过自身攻击力攻击了韩信
    print('-'*50)
    print('第二次攻击')
    lvbu.people_weapon(fight) # 吕布使用了武器
    lvbu.pweapon.weapon_hit(lvbu,hanxin) # 吕布使用了狼牙棒打了韩信100滴血,韩信还剩100滴血

  • 相关阅读:
    GDI+常用操作、入门解析
    实用SQL语句大全
    SQL语句全解析
    ASP.NET MVC 浅析
    C#版文件分割器
    转一个orbitpanel GIS
    怎么给listbox 的item 添加动画1? GIS
    怎么给listbox 的item 添加动画? GIS
    关于查找可视树的一点问题 GIS
    判断gridview 滑动到最右端 GIS
  • 原文地址:https://www.cnblogs.com/ipyanthony/p/9163861.html
Copyright © 2011-2022 走看看