作业:人开枪射击子弹
用面对对象写
分析
有哪些类:
人:
类名:
属性:
行为:
枪:
类名:
属性:
行为:
弹夹:
类名:
属性:
行为:
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 发,' % self.bulletBox.bulletCount )
class BulletBox(object):
def __init__(self,bulletCount):
self.bulletCount = bulletCount
创建对象
弹夹
bulletBox = BulletBox(5)
枪
gun = Gun(bulletBox)
人
person = Person(gun)
per.fire()
per.fire()
per.fire()
per.fire()
per.fire()
per.fillBullet(2)