Guest User

Untitled

a guest
Jan 7th, 2016
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.26 KB | None | 0 0
  1. #mix-in demo
  2.  
  3. class BaseObject(object):
  4.     def __init__(self, *args, **kwargs):
  5.         pass
  6.        
  7.  
  8. class SolidObject(BaseObject):
  9.     def __init__(self, *args, **kwargs):
  10.         super(SolidObject, self).__init__(*args, **kwargs)
  11.         # field declarations
  12.  
  13.         self.x = kwargs.get('x')
  14.         self.y = kwargs.get('y')
  15.         self.visibility = True
  16.  
  17. #mix-in
  18. class DestroyableObject(BaseObject):
  19.     def __init__(self, *args, **kwargs):
  20.         super(DestroyableObject, self).__init__(*args, **kwargs)
  21.         self.obj_life = 100
  22.  
  23.     def hit(self):
  24.         if self.obj_life > 0:
  25.             self.obj_life -= 20
  26.             print 'hit! life left: ' + str(self.obj_life)
  27.         else:
  28.             self.visibility = False
  29.             print 'broken'
  30.  
  31.  
  32. class Crate(SolidObject, DestroyableObject):
  33.     def __init__(self, *args, **kwargs):
  34.        super(Crate, self).__init__(*args, **kwargs)
  35.        self.material = kwargs.get('material')
  36.  
  37.     def get_material(self):
  38.         print self.material
  39.  
  40.  
  41. wooden_crate = Crate(None, material='wood', x=0.1, y=-0.2)
  42. cardboard_crate = Crate(None, material='cardboard', x=0.3, y=0.5)
  43.  
  44. for _ in range(6):
  45.     wooden_crate.hit()
  46.  
  47. print wooden_crate.visibility
  48. print cardboard_crate.visibility
Advertisement
Add Comment
Please, Sign In to add comment