zoukankan      html  css  js  c++  java
  • Python笔记_第三篇_面向对象_5.一个关于类的实例(人开枪射击子弹)

    1. 我们学了类的这些东西,用这些类我们来操作一个关于类的实例。

    2. 题目:人开枪射击子弹,然后具有装弹动作,然后再开枪。

    第一步:设计类:


    类名:Person
    属性:gun
    行为:fire,fillBullet


    类名:Gun
    属性:bulletBox
    行为:shoot

    弹夹
    类名:BulletBox
    属性:bulletCount
    行为:

      分析:我们采取倒序的方式来设计这段面向对象的程序。人——枪——弹夹——发射,我们可以看出发射是最后一个动作。

    第二步:创建类

      我们用图示的方式来演示这么一段代码的过程。

    from person import Person
    from gun import Gun
    from bulletbox import BulletBox
    
    
    # 弹夹
    bulletBox = BulletBox(5)
    
    # 枪
    gun = Gun(bulletBox)
    
    # 人
    per = Person(gun)
    
    per.fire()
    per.fire()
    per.fire()
    per.fire()
    per.fire()
    per.fire()
    per.fire()
    
    per.fillBullet(2)
    per.fire()
    per.fire()
    per.fire()
    
    
    剩余子弹: 4发
    剩余子弹: 3发
    剩余子弹: 2发
    剩余子弹: 1发
    剩余子弹: 0发
    没有子弹了
    没有子弹了
    装入子弹2发
    剩余子弹: 1发
    剩余子弹: 0发
    没有子弹了
  • 相关阅读:
    类型转换
    new Overload函数输出
    快捷键加入属性代码段
    xp 下 安装Ubuntu 11.04 双系统
    native2ascii 用法解析
    apusic jconsole jmx connecitons url
    oracle 分页
    几条最基本的 sqlplus命令
    windows下plsql 设置 里面timestamp显示的格式
    oracle 时间差 做查询条件
  • 原文地址:https://www.cnblogs.com/noah0532/p/10860112.html
Copyright © 2011-2022 走看看