1.士兵 許三多 有一把 AK47
2.士兵 可以 開火
3.槍 能夠 發射 子彈
4.槍 裝填 裝填子彈——增加子彈數量
怎么用Python寫出來
uj5u.com熱心網友回復:
class Gun:def __init__(self, name):
self.name = name
self. count = 0
def launch(self):
if self.count == 0:
print("%沒有子彈,無法射擊" % self.name)
return
self.count -= 1
print("%s發射子彈,還有%d子彈" % (self.name, self.count))
def add_bullet(self, count):
self.count += count
print("%s增加了%d枚子彈,現在有%d枚子彈" % (self.name, count, self.count))
class Soilder:
def __init__(self, name):
self.name = name
self.gun = None
def fire(self):
if self.gun is None:
print("%s手上沒有槍" % self.name)
return
self.gun.add_bullet(30)
self.gun.launch()
# 1.士兵 許三多 有一把 AK47
xusanduo = Soilder("許三多")
xusanduo.gun = Gun("AK47")
# 2.士兵 可以 開火
xusanduo.fire()
# 3.槍 能夠 發射 子彈
# 4.槍 裝填 裝填子彈——增加子彈數量
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/57318.html
下一篇:各位大佬,能不能幫幫忙
