python面向对象编程案例分享

python面向对象实例

最近整理的一些学习python的基础资料,下面用一个实例来解释面向对象编程,希望对才学习python的朋友有所帮助。题目是开枪打敌人的游戏。具体代码实现如下:

#!/usr/bin/python
# -*- coding: UTF-8 -*
"""
@author:li-boss
@file_name: mianx.py
@create date: 2019-10-31 22:27 
@blog https://leezhonglin.github.io
@csdn https://blog.csdn.net/qq_33196814
@file_description:
"""


class Person(object):
    """创建一个人类"""

    def __init__(self, name):
        self._name = name  # 人类绑定名字属性
        self._gun = None
        self._hp = 100

    def bulletToClip(self, bullet, clip):
        clip.preseBullet(bullet)

    def clipToGun(self, gun, clip):
        gun.preseClip(clip)

    def getGun(self, gun):
        self._gun = gun

    def __str__(self):
        if self._hp > 0:

            if self._gun:
                return '%s当前血量为:%s,%s' % (self._name, self._hp, self._gun)
            else:
                return '%s当前血量为:%s,没有枪' % (self._name, self._hp)

        else:
            return '%s已死亡' % self._name

    def fire(self, enemy):
        self._gun.hurt(enemy)

    def injured(self, hurt):
        self._hp -= hurt


class Gun(object):
    """创建一个枪类"""

    def __init__(self, name):

        self._name = name  # 枪类绑定名字属性
        self._clip = None

    def preseClip(self, clip):
        self._clip = clip

    def __str__(self):
        if self._clip:
            return '%s%s' % (self._name, self._clip)
        else:
            return '%s没有弹夹' % self._name

    def hurt(self, enemy):
        """枪从弹夹获取一发子弹"""
        if self._clip:
            bullet_temp = self._clip.toBullet()
            bullet_temp.lethalityToBullet(enemy)

        else:
            print('没有弹夹')


class Clip(object):
    """创建一个弹夹类"""

    def __init__(self, capacity):
        self._capacity = capacity  # 弹夹的容量
        self._listBullet = []

    def preseBullet(self, bullet):
        self._listBullet.append(bullet)

    @property
    def listBullet(self):
        return self._listBullet

    def __str__(self):
        return '当前弹夹信息为:%d/%d' % (len(self._listBullet), self._capacity)

    def toBullet(self):
        if self._listBullet:
            return self._listBullet.pop()
        else:
            return None


class Bullet(object):
    """创建一个子弹对象"""

    def __init__(self, lethailty):
        self._lethality = lethailty  # 子弹的杀伤力

    def lethalityToBullet(self, enemy):
        enemy.injured(self._lethality)


def main():
    """
    流程控制
    """
    # 创建一个战士
    warrior = Person('战士')
    # 创建一个枪的对象
    ak47 = Gun('AK47')
    # 创建一个弹夹的对象
    clip = Clip(50)
    # 创建一些子弹的对象
    for _ in range(20):
        bullet = Bullet(10)
        # 战士把子弹装进弹夹
        warrior.bulletToClip(bullet, clip)

    # 战士把弹夹装进枪
    warrior.clipToGun(ak47, clip)

    # 战士拿枪
    warrior.getGun(ak47)

    # 创建一个敌人对象
    enemy = Person('敌人')

    # 战士开枪打敌人
    for _ in range(len(clip.listBullet)):
        warrior.fire(enemy)
        print(warrior)
        print(enemy)


if __name__ == '__main__':
    main()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kujirashark

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值