以前就知道可以通过面向对象来优化条件语句,甚至看到过说面向对象概念可以完全替代条件语句,新想那得弄多少类啊,觉得太累了就没细研究,而且那时是在用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 -= 5
    
                    if 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()
    
    
       代码好看了很多。
       还是得多写代码啊。。