python 人開槍射擊子彈


第一個類:人開槍射擊子彈類
from person import Person
from gun import Gun
from bulletbox import BulletBox
'''

類名:Person
屬性:gun
行為:fire

類名:Gun
屬性:bulletBox
行為:shoot
彈夾
類名:BulletBox
屬性:bulletCount
行為:
'''
#彈夾
bulletBox = BulletBox(5)
#槍
gun = Gun(bulletBox)
#人
per = Person(gun)
per.fire()
per.fire()
per.fire()
per.fire()
per.fire()
per.fire()
per.fire()
per.fillBullet(2)
per.fire()
per.fire()
per.fire()

第二個類:Person人類
class Person(object):
def __init__(self, gun):
self.gun = gun
def fire(self):
self.gun.shoot()
def fillBullet(self, count):
self.gun.bulletBox.bulletCount = count
第三個類:gun槍類
class Gun(object):
def __init__(self, bulletBox):
self.bulletBox = bulletBox
def shoot(self):
if self.bulletBox.bulletCount == 0:
print("沒有子彈了")
else:
self.bulletBox.bulletCount -= 1
print("剩余子彈:%d發" % (self.bulletBox.bulletCount))
第四個類:BulletBox彈夾類
class BulletBox(object):
def __init__(self, count):
self.bulletCount = count


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM