python類例子(開槍)
class Person(object): """人的類""" def __init__(self, name): super(Person, self).__init__() self.name = name self.gun = None # 用來儲存槍物件的引用 self.hp = 100 def anzhuang_zidan(self, dan_jia_temp, zi_dan_temp): """把子彈裝到彈夾中""" # 彈夾.儲存子彈(子彈) dan_jia_temp.baocun_zidan(zi_dan_temp) def anzhuang_danjia(self, gun_temp, dan_jia_temp): """把彈夾安裝到槍中""" # 槍.儲存彈夾(彈夾) gun_temp.baocun_danjia(dan_jia_temp) def naqiang(self, gun_temp): """拿起一把槍""" self.gun = gun_temp def __str__(self): if self.gun: return "%s的血量為:%d, 他有槍 %s" % (self.name, self.hp, self.gun) else: if self.hp > 0: return "%s的血量為%d, 他沒有槍" % (self.name, self.hp) else: return "%s 已掛...." % self.name def kou_ban_ji(self, diren): """讓槍發射子彈去打敵人""" # 槍.開火(敵人) self.gun.fire(diren) def xiao_xue(self, sha_shang_li): """根據殺傷力,掉相應的血量""" self.hp -= sha_shang_li class Gun(object): """槍類""" def __init__(self, name): super(Gun, self).__init__() self.name = name # 用來記錄槍的型別 self.danjia = None # 用來記錄彈夾物件的引用 def baocun_danjia(self, dan_jia_temp): """用一個屬性來儲存這個彈夾物件的引用""" self.danjia = dan_jia_temp def __str__(self): if self.danjia: return "槍的資訊為:%s, %s" % (self.name, self.danjia) else: return "槍的資訊為:%s,這把槍中沒有彈夾" % (self.name) def fire(self, diren): """槍從彈夾中獲取一發子彈,然後讓這發子彈去擊中敵人""" # 先從彈夾中取子彈 # 彈夾.彈出一發子彈() zidan_temp = self.danjia.tanchu_zidan() # 讓這個子彈去傷害敵人 if zidan_temp: # 子彈.打中敵人(敵人) zidan_temp.dazhong(diren) else: print("彈夾中沒有子彈了。。。。") class Danjia(object): """彈夾類""" def __init__(self, max_num): super(Danjia, self).__init__() self.max_num = max_num # 用來記錄彈夾的最大容量 self.zidan_list = [] # 用來記錄所有的子彈的引用 def baocun_zidan(self, zi_dan_temp): """將這顆子彈儲存""" self.zidan_list.append(zi_dan_temp) def __str__(self): return "彈夾的資訊為:%d/%d" % (len(self.zidan_list), self.max_num) def tanchu_zidan(self): """彈出最上面的那顆子彈""" if self.zidan_list: return self.zidan_list.pop() else: return None class Zidan(object): """子彈類""" def __init__(self, sha_shang_li): super(Zidan, self).__init__() self.sha_shang_li = sha_shang_li # 這顆子彈的威力 def dazhong(self, diren): """讓敵人掉血""" # 敵人.掉血(一顆子彈的威力) diren.xiao_xue(self.sha_shang_li) def main(): """用來控制整個程式的流程""" # 1. 建立老王物件 laowang = Person("老王") # 2. 建立一個槍物件 ak47 = Gun("AK47") # 3. 建立一個彈夾物件 dan_jia = Danjia(20) # 4. 建立一些子彈 for i in range(15): zi_dan = Zidan(10) # 5. 老王把子彈安裝到彈夾中 # 老王.安裝子彈到彈夾中(彈夾,子彈) laowang.anzhuang_zidan(dan_jia, zi_dan) # 6. 老王把彈夾安裝到槍中 # 老王.安裝彈夾到槍中(槍,彈夾) laowang.anzhuang_danjia(ak47, dan_jia) # test:測試彈夾的資訊 # print(dan_jia) # test:測試槍的資訊 # print(ak47) # 7. 老王拿槍 # 老王.拿槍(槍) laowang.naqiang(ak47) # test:測試老王物件 print(laowang) # 8. 建立一個敵人 gebi_laosong = Person("隔壁老宋") print(gebi_laosong) # 9. 老王開槍打敵人 # 老王.扣扳機(隔壁老宋) laowang.kou_ban_ji(gebi_laosong) print(gebi_laosong) print(laowang) laowang.kou_ban_ji(gebi_laosong) print(gebi_laosong) print(laowang) laowang.kou_ban_ji(gebi_laosong) print(gebi_laosong) print(laowang) laowang.kou_ban_ji(gebi_laosong) print(gebi_laosong) print(laowang) laowang.kou_ban_ji(gebi_laosong) print(gebi_laosong) print(laowang) laowang.kou_ban_ji(gebi_laosong) print(gebi_laosong) print(laowang) laowang.kou_ban_ji(gebi_laosong) print(gebi_laosong) print(laowang) laowang.kou_ban_ji(gebi_laosong) print(gebi_laosong) print(laowang) laowang.kou_ban_ji(gebi_laosong) print(gebi_laosong) print(laowang) laowang.kou_ban_ji(gebi_laosong) print(gebi_laosong) print(laowang) laowang.kou_ban_ji(gebi_laosong) print(gebi_laosong) print(laowang) if __name__ == '__main__': main() 執行結果: |
相關文章
- python3筆記---抽象類例子Python筆記抽象
- 【python小例子】小例子拾憶Python
- Python練手例子(14)Python
- Python練手例子(13)Python
- python字典的小例子Python
- Python練手例子(16)Python
- Python練手例子(6)Python
- 十幾個python小例子,從此愛上pythonPython
- python協程詳細解釋以及例子Python
- 3個例子教你重構 Python 程式碼Python
- 6 個例子教你重構 Python 程式碼Python
- 美國槍擊案後 沃爾瑪不禁售槍支開始下架遊戲遊戲
- C#操作Microsoft.Office.Interop.Word類庫完整例子C#ROS
- loki: python3訪問loki介面的例子LokiPython
- Python實現工廠模式的兩個例子Python模式
- Python簡單實現多執行緒例子Python執行緒
- Python類Python
- python 類Python
- python類的子類Python
- python 類的子類Python
- 常用移液槍配件——吸頭,PP、PFA、FEP移液槍槍頭該如何選擇?
- 開學季,教你用Python畫大學教室座位神分割槽圖!網友直呼“中槍”Python
- python建立類和類方法Python
- Python抽象類Python抽象
- python的類Python
- python 類篇Python
- Python 元類Python
- Uniswap機槍池質押系統開發技術
- Android BLE 藍芽開發——掃碼槍基於BLESSEDAndroid藍芽
- python基礎之 python實現PID演算法及測試的例子Python演算法
- 【Python】python類的繼承Python繼承
- [轉載] Python中協程的詳細用法和例子Python
- Python元類與列舉類Python
- awk例子
- 4.5.1.1.2 例子
- 熬夜加班,基因“躺槍”
- 從 例子 開始 入門 正則 表示式(-)
- Python的元類Python