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
- C++類和異常例子C++
- 同步輔助類CountDownLatch的例子CountDownLatch
- 手持裝置掃描槍開發
- Python練手例子(16)Python
- Python練手例子(14)Python
- Python練手例子(13)Python
- Python練手例子(6)Python
- python字典的小例子Python
- python閉包小例子Python
- Python 檔案(file)處理 例子Python
- 常用移液槍配件——吸頭,PP、PFA、FEP移液槍槍頭該如何選擇?
- 開學季,教你用Python畫大學教室座位神分割槽圖!網友直呼“中槍”Python
- 最新安卓例子分類原始碼打包下載安卓原始碼
- 一個類資料型別的STL例子 (轉)資料型別
- 十幾個python小例子,從此愛上pythonPython
- Uniswap機槍池質押系統開發技術
- python 類的子類Python
- python類的子類Python
- python 類Python
- Python類Python
- C#操作Microsoft.Office.Interop.Word類庫完整例子C#ROS
- 用CSS實現類似導航翻轉功能例子CSS
- 3個例子教你重構 Python 程式碼Python
- 6 個例子教你重構 Python 程式碼Python
- python協程詳細解釋以及例子Python
- 類似Matlab的Python開發工具spyderMatlabPython
- 物以類聚,人以群分–走出軟體作坊:三五個人十來條槍 如何成為開發正規軍(十一)薦
- Android BLE 藍芽開發——掃碼槍基於BLESSEDAndroid藍芽
- python建立類和類方法Python
- Python實現工廠模式的兩個例子Python模式
- 由一個例子到python的名字空間Python
- loki: python3訪問loki介面的例子LokiPython
- Python簡單實現多執行緒例子Python執行緒
- python的類Python
- Python抽象類Python抽象
- Python 元類Python