面向对象:人开枪射击子弹

# 人

#  类名: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)

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Python 面向对象Python从设计之初就已经是一门面向对象的语言,正因为如此,在Python中创建一个类和对...
    顺毛阅读 4,252评论 4 16
  • 别做“呸”偶,要做“配”偶 2017-07-27角子羽有句情羽 今天高温到走到路上,感觉自己成了一串烧烤。但是这仍...
    禾早茶阅读 250评论 0 0
  • 早起,看了许多今日头条上许多关于书法的帖子,突然意识到书法的背后体现是中国的传统文化博大精深,之所以博大是因为他既...
    mw568阅读 195评论 0 0
  • 我打门口走过 那等在晚霞下的面容如梧桐的飘落 流水不走,飘落的花瓣不去 你的心如圆圆的封闭的井 恰若木制的的门槛傍...
    翱蓝阅读 1,522评论 0 1