惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

H
Help Net Security
宝玉的分享
宝玉的分享
The Cloudflare Blog
Apple Machine Learning Research
Apple Machine Learning Research
V
Visual Studio Blog
Last Week in AI
Last Week in AI
Hugging Face - Blog
Hugging Face - Blog
博客园 - 司徒正美
博客园 - 三生石上(FineUI控件)
A
About on SuperTechFans
MyScale Blog
MyScale Blog
aimingoo的专栏
aimingoo的专栏
Microsoft Security Blog
Microsoft Security Blog
D
Docker
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Recent Announcements
Recent Announcements
大猫的无限游戏
大猫的无限游戏
IT之家
IT之家
P
Proofpoint News Feed
L
LangChain Blog
Blog — PlanetScale
Blog — PlanetScale
The GitHub Blog
The GitHub Blog
博客园 - 【当耐特】
Martin Fowler
Martin Fowler

博客园 - 飘啊飘

一个精简的vi源码(2000行) dtruss 粗糙的翻译 gdb常用命令[转] gdb中信号的处理[转] 在linux中通过进程名获得进程id busybox0.60.3源码学习开始 pygame做的贪吃蛇 python中使用struct模块处理二进制数据 使用PYGAME开发的坦克游戏[代码][思路] 一篇很好的讲/etc/inittab的文章[转] 哲学家吃空心粉问题 《代码整洁之道》笔记之函数 python生成文件树的代码 深入理解软件包的配置、编译与安装[转] pygame学习之对象移动 pywin32重启电脑 - 飘啊飘 - 博客园 解决LINUX和WINDOWS时间不一置 通过状态机实现的一个配置读取函数 UNIX基础知识--《APUE》第一章笔记
使用面向对象概念优化条件判断语句的一个小应用
飘啊飘 · 2011-04-12 · via 博客园 - 飘啊飘

以前就知道可以通过面向对象来优化条件语句,甚至看到过说面向对象概念可以完全替代条件语句,新想那得弄多少类啊,觉得太累了就没细研究,而且那时是在用C语言,没有面向对象的语法支持。

今天写代码时候,写出了这样的代码:

                if isinstance(b,Iron):#
                    if self._size == Bullet.BIG:
                        b.health 
-= 1
                    
elif self._size == Bullet.LARGE:
                        b.health 
-= 3
                    
elif self._size == Bullet.HUGE:
                        b.health 
-= 5if isinstance(b,Soil):#
                    if self._size == Bullet.SMALL:
                        b.health 
-= 1
                    
elif self._size == Bullet.BIG:
                        b.health 
-= 5
                        
 

 又臭又长又丑,看了下,也没什么好办法改,条件总是要判断的呀,铁和转块受到攻击时效果不一样,受到不同大小子弹的攻击,效果不一样。
 突然灵光一闪,想起了还有继承这么回事。三下五除二,写下了这样的代码:

class Floor(pygame.sprite.Sprite):

    """

    Stage的组成部分。Stage向Game对象传递进来的pygame.sprite.Group对象引用进行填充。

    这是个基类,不同种类的格子通过这个来继承。

    """

    image = None

    def _on_small_bullet(self):

        """收到小子弹攻击"""

        raise TypeError,"not implement"

    def _on_big_bullet(self):

        """收到大子弹攻击"""

        raise TypeError,"not implement"

    def _on_large_bullet(self):

        """收到巨大子弹攻击"""

        raise TypeError,"not implement"

    def _on_huge_bullet(self):

        """收到炮弹攻击"""

        raise TypeError,"not implement"

class Soil(Floor):

    """土"""

    def __init__(self,pos):

        Floor.__init__(self)

        self.pos = pos

        self.health = 5

        self.image = Soil.image

        self.rect = self.image.get_rect(topleft = pos)

#        self.rect = Rect(self.rect)

        pass

    def _on_small_bullet(self):

        """收到小子弹攻击"""

        self.health -= 1

    def _on_big_bullet(self):

        """收到大子弹攻击"""

        self.health -= 2

    def _on_large_bullet(self):

        """收到巨大子弹攻击"""

        self.health -= 3

    def _on_huge_bullet(self):

        """收到炮弹攻击"""

        self.health -= 5 

啊哈,搞定了,而且代码好看很多。
所有砖块元素都继承自Floor,这样就不需要判断类型了。

                if self._size == Bullet.SMALL:

                    b._on_small_bullet()

                elif self._size == Bullet.BIG:

                    b._on_big_bullet()

                elif self._size == Bullet.LARGE:

                    b._on_large_bullet()

                elif self._size == Bullet.HUGE:

                    b._on_huge_bullet()

 代码好看了很多。
 还是得多写代码啊。。