作業(yè):人開槍射擊子彈
用面對(duì)對(duì)象寫
分析
有哪些類:
人:
類名:
屬性:
行為:
槍:
類名:
屬性:
行為:
彈夾:
類名:
屬性:
行為:
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
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 發(fā),' % self.bulletBox.bulletCount )
class BulletBox(object):
def __init__(self,bulletCount):
self.bulletCount = bulletCount
創(chuàng)建對(duì)象
彈夾
bulletBox = BulletBox(5)
槍
gun = Gun(bulletBox)
人
person = Person(gun)
per.fire()
per.fire()
per.fire()
per.fire()
per.fire()
per.fillBullet(2)