Luninariel

Week 8 Examples

Oct 14th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.36 KB | None | 0 0
  1. class Animal:
  2.     """parent class for all animals"""
  3.     def __init__(self, name, weight, color, noise):
  4.         """initialies an Animal instance"""
  5.         self.__name = name
  6.         self.weight = weight
  7.         self.__color = color
  8.         self.__noise = noise
  9.  
  10.     def make_noise(self):
  11.         print(self.__noise)
  12.  
  13.     def eat(self, food):
  14.         print('munch... munch...')
  15.  
  16.     @property
  17.     def color(self):
  18.         return self.__color
  19.  
  20.     @property
  21.     def name(self):
  22.         return self.__name
  23.  
  24.     @name.setter
  25.     def name(self, new_name):
  26.         print('changed name')
  27.         self.__name = new_name
  28.  
  29.     def __del__(self):
  30.         print("bye")
  31.  
  32. class Carnivore:
  33.     def eat(self, food):
  34.         if isinstance(food, Animal):
  35.             food.make_noise()
  36.             super().eat(food)
  37.  
  38.  
  39. class TRex(Carnivore, Animal):
  40.     def __init__(self, name, weight, color):
  41.         super().__init__(name, weight, color, 'ROOAAAARR!!!!!!!!!')
  42.    
  43.  
  44. class Dog(Animal):
  45.     def __init__(self, name, weight, color):
  46.         super().__init__(name, weight, color, 'woof')
  47.  
  48.  
  49. class Horse(Animal):
  50.     def __init__(self, name, weight, color):
  51.         super().__init__(name, weight, color, 'neeeehh')
  52.        
  53.  
  54. class Squirrel(Animal):
  55.     def __init__(self, name, weight, color):
  56.         super().__init__(name, weight, color, 'squeek')
Add Comment
Please, Sign In to add comment