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

推荐订阅源

Apple Machine Learning Research
Apple Machine Learning Research
AWS News Blog
AWS News Blog
Google DeepMind News
Google DeepMind News
U
Unit 42
博客园 - 叶小钗
博客园 - 聂微东
GbyAI
GbyAI
Stack Overflow Blog
Stack Overflow Blog
有赞技术团队
有赞技术团队
aimingoo的专栏
aimingoo的专栏
D
DataBreaches.Net
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Jina AI
Jina AI
美团技术团队
The Cloudflare Blog
M
MIT News - Artificial intelligence
Microsoft Azure Blog
Microsoft Azure Blog
I
InfoQ
S
Schneier on Security
C
Check Point Blog
Project Zero
Project Zero
The Hacker News
The Hacker News
Scott Helme
Scott Helme
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Cisco Talos Blog
Cisco Talos Blog
P
Privacy International News Feed
SecWiki News
SecWiki News
Latest news
Latest news
MongoDB | Blog
MongoDB | Blog
S
Secure Thoughts
Google Online Security Blog
Google Online Security Blog
F
Fortinet All Blogs
博客园 - 三生石上(FineUI控件)
H
Help Net Security
TaoSecurity Blog
TaoSecurity Blog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Last Week in AI
Last Week in AI
P
Privacy & Cybersecurity Law Blog
Forbes - Security
Forbes - Security
G
GRAHAM CLULEY
N
Netflix TechBlog - Medium
L
Lohrmann on Cybersecurity
A
About on SuperTechFans
T
The Exploit Database - CXSecurity.com
C
Cisco Blogs
PCI Perspectives
PCI Perspectives
大猫的无限游戏
大猫的无限游戏
T
Troy Hunt's Blog
H
Hacker News: Front Page
Vercel News
Vercel News

博客园 - 飘啊飘

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

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