#-*-coding:utf-8-*-
class Person():
def __init__(self,gun):
self.gun = gun
def fire(self):
self.gun.shoot()
#-*-coding:utf-8-*-
class Gun():
def __init__(self,bulletBox):
self.bulletBox = bulletBox
def shoot(self):
if self.bulletBox.bulletCount == 0:# 不是很好写,self为class Gun()的对象gun
形参,xxx.属性名[下个类的对象].属性名.[下个类的对象]
print("没有子弹了")
else
:
self
.bulletBox.bulletCount
-=
1
print
(
"剩余子弹:%d个"
%
self
.bulletBox.bulletCount)
class BulletBox():
def __init__(self,count):
self.bulletCount = count
#-*-coding:utf-8-*-
from bulletbox import BulletBox
from gun import Gun
from person import Person
'''
人
类名: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()