Python-物件之間互動——反恐精英(偽)

YangPotatoes發表於2020-12-07
# 類

class student:
    Class = "G20大資料1班"  #類屬性(大家都有,且一樣)
    def __init__(self,name,number):  #例項屬性(大家都有,但是不一樣)
        self.name = name
        self.no = number


student1 = student("張三","20200001")  #例項化物件



# 反恐精英\
# 人
class Person:
    def __init__(self,name):
        self.name = name
        self.blood = 100
    def install_bull(self,clip,bullet):  # 裝子彈方法
        clip.save_bullets(bullet)  # 呼叫彈夾安裝子彈方法來實現
    def install_clip(self, clip, Gun):
        Gun.install_clip(clip)
class Clip:
    def __init__(self,capacity):
        self.capacity = capacity
        self.current_list = []
    def save_bullets(self,bullet):#安裝子彈
        if len(self.current_list)<self.capacity:
            self.current_list.append(bullet)
            print("安裝子彈成功")
            print("當前子彈數目{}/{}".format(len(self.current_list),self.capacity))
class bullet:
    pass

class gun:
    def __init__(self):
        self.clip = None
    def install_clip(self,clip):
        if not self.clip:
            self.clip = clip
        print("槍已經裝上彈夾,目前彈夾容量{}/{}".format(len(clip.current_list),clip.capacity))


Solider1 = Person("牛戰士")
clip = Clip(30)
i = 0
while True:
    bullet1 = bullet()
    Solider1.install_bull(clip,bullet1)
    if i < 5:
        i = i+1
    else:
        break
Gun = gun() # 生成一把槍
#print(clip.current_list )
Solider1.install_clip(clip,Gun)

 

相關文章