第一個類:人開槍射擊子彈類
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