# 人
# 类名:Person
# 属性:gun
# 行为:fire
# 枪
# 类名:Gun
# 属性:bulletBox
# 行为:shoot
# 弹夹
# 类名 :bulletBox
# 属性:bullCount
class bulletBox():
def __init__(self, count):
self.bullCount = count
class Gun():
def __init__(self, bulletBox):
self.bulletBox = bulletBox
def shoot(self):
if self.bulletBox.bullCount == 0:
print("没有子弹了")
else:
self.bulletBox.bullCount -=1
print("剩余子弹: %d发" %self.bulletBox.bullCount)
class Person():
def __init__(self, gun):
self.gun = gun
def fire(self):
self.Gun.shoot()
def fillBullet(self, count):
self.gun.bulletBox.bullCount = count
if __name__ == '__main__':
bulletbox = bulletBox(5)
gun = Gun(bulletbox)
per = Person(gun)
per.fire()
per.fillBullet(3)
网友评论