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

推荐订阅源

Scott Helme
Scott Helme
N
Netflix TechBlog - Medium
AI
AI
Security Latest
Security Latest
GbyAI
GbyAI
P
Proofpoint News Feed
Y
Y Combinator Blog
A
Arctic Wolf
G
Google Developers Blog
U
Unit 42
爱范儿
爱范儿
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
V
Vulnerabilities – Threatpost
Know Your Adversary
Know Your Adversary
Cisco Talos Blog
Cisco Talos Blog
T
Tor Project blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
T
Threatpost
L
Lohrmann on Cybersecurity
C
CERT Recently Published Vulnerability Notes
C
Check Point Blog
B
Blog RSS Feed
The GitHub Blog
The GitHub Blog
Microsoft Azure Blog
Microsoft Azure Blog
博客园 - 【当耐特】
博客园 - Franky
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
C
Cisco Blogs
云风的 BLOG
云风的 BLOG
NISL@THU
NISL@THU
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Microsoft Security Blog
Microsoft Security Blog
T
The Blog of Author Tim Ferriss
阮一峰的网络日志
阮一峰的网络日志
Latest news
Latest news
L
LINUX DO - 最新话题
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
美团技术团队
WordPress大学
WordPress大学
L
LangChain Blog
Stack Overflow Blog
Stack Overflow Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
酷 壳 – CoolShell
酷 壳 – CoolShell
大猫的无限游戏
大猫的无限游戏
The Hacker News
The Hacker News
Simon Willison's Weblog
Simon Willison's Weblog
V
V2EX
Project Zero
Project Zero
博客园_首页

博客园 - 飘啊飘

一个精简的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()

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