阿克苏网站开发,工业软件开发,莱芜定制网站建设公司,美食门户网站源码1. 士兵 许三多 有一把 AK47
2. 士兵 可以开火
3. 枪 能够 发射 子弹
4. 枪 装填 子弹---增加子弹数量
class Gun:def __init__(self,type):self.type type# 刚开始枪没有子弹self.bullet_count 0def __str__(self):return (%s 已到位 % self.type)def shoot(sel…
1. 士兵 许三多 有一把 AK47
2. 士兵 可以开火
3. 枪 能够 发射 子弹
4. 枪 装填 子弹---增加子弹数量
class Gun:def __init__(self,type):self.type type# 刚开始枪没有子弹self.bullet_count 0def __str__(self):return (%s 已到位 % self.type)def shoot(self):# 1. 判断是否有子弹如果有发射如果没有返回if self.bullet_count is 0:print(%s 没有子弹请装填子弹 % self.type)returnself.bullet_count - 1print(突突突...%d % self.bullet_count)class Soldier:def __init__(self,name):self.name name# 新兵没有枪self.gun Nonedef fire(self,num):# 1 判断是否有枪if self.gun is None:print(%s 没有枪无法参与战斗耶 % self.name)return# 有枪发出口号self.gun.bullet_count numprint(冲啊*3)# 开火self.gun.shoot()# 创建枪对象
ak_47 Gun(AK47)
# 创建士兵对象
xu_san_duo Soldier(许三多)# 没有枪时
print(xu_san_duo.fire(50)) # 50 -- 装50发子弹# 有枪时
# 给许三多赋予一把枪
xu_san_duo.gun ak_47
print(xu_san_duo.fire(50))运行结果没有枪时运行结果有枪时