2021/1/15-2021/1/16 类的依赖(python)

依赖

一个类 调入 另外一个类

lass Game_role:

    def __init__(self, n, ad, hp):
        self.name = n
        self.ad = ad
        self.hp = hp

    def attack(self, wj):
        wj.xp = wj.hp - self.ad
        print(f'{self.name}攻击{wj.name}, {wj.name}掉了{self.ad}血, 还剩{wj.xp}')


gl = Game_role('盖伦', 10, 100)
jh = Game_role('剑豪', 20, 80)

gl.attack(jh)
jh.attack(gl)

class Boy:
    def __init__(self, name):
        self.name = name

    def meet(self, girl_friend=None):
        self.girl_friend = girl_friend

    def have_diner(self):
        if self.girl_friend:
            print(f'{self.name}请{self.girl_friend.name}一起吃六块钱的麻辣烫')
            self.girl_friend.shopping(self)

class Girl:
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def shopping(self, boy):
        print(f'{boy.name},{self.name}一起去购物')


ping = Boy('')

nvshen = Girl('女神', '25')

ping.meet(nvshen)

ping.have_diner()
class Gamep:
    def __init__(self, name, sex, pad, hp):
        self.name = name
        self.sex = sex
        self.pad = pad
        self.hp = hp

    def nwq(self, wq=None):
        self.wq = wq

    def baycar(self, car=None):
        self.car = car

    def pview(self):
        print(f'ID:{self.name} 性别:{self.sex} pad:{self.pad + self.wq.wad}(携带的武器是:{self.wq.wqname} 伤害是:{self.wq.wad}) HP:{self.hp}')
        print(f'携带的武器是:{self.wq.wqname} 伤害是:{self.wq.wad}')
        print(f'使用的载具是:{self.car.cname} 速度是:{self.car.v}迈')


class Wq:
    def __init__(self, wqname, wad):
        self.wqname = wqname
        self.wad = wad


class Car:
    def __init__(self, cname, v):
        self.cname = cname
        self.v = v


xh = Gamep('', '', 10, 100)
fz = Wq('斧子', 50)
tb = Car('小踏板', 60)
xh.nwq(fz)
xh.baycar(tb)
xh.pview()

 

posted @ 2021-01-17 14:45  ping_sen  阅读(58)  评论(0)    收藏  举报