Advertisement
ClearCode

inheritance_simple

Apr 27th, 2022
883
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.28 KB | None | 0 0
  1. class Monster:
  2.     def __init__(self,health,energy):
  3.         self.health = health
  4.         self.energy = energy
  5.  
  6.     # methods
  7.     def attack(self,amount):
  8.         print('The monster has attacked!')
  9.         print(f'{amount} damage was dealt')
  10.         self.energy -= 20
  11.        
  12.     def move(self,speed):
  13.         print('The monster has moved')
  14.         print(f'It has a speed of {speed}')
  15.  
  16. class Shark(Monster):
  17.     def __init__(self,speed, health, energy):
  18.         #Monster.__init__(self,health,energy)
  19.         super().__init__(health,energy)
  20.         self.speed = speed
  21.  
  22.     def bite(self):
  23.         print('The shark has bitten')
  24.  
  25.     def move(self):
  26.         print('The shark has moved')
  27.         print(f'The speed of the shark is {self.speed}')
  28.  
  29. # exercise
  30. # create scorpion class that inherits from monster
  31. class Scorpion(Monster):
  32.     def __init__(self,poison_damage,scorpion_health,scorpion_energy):
  33.         self.poison_damage = poison_damage
  34.         super().__init__(health = scorpion_health,energy = scorpion_energy)
  35.  
  36.     def attack(self):
  37.         print('The scorpion has attacked')
  38.         print(f'It has dealt {self.poison_damage} poison damage')
  39.  
  40. scorpion = Scorpion(poison_damage = 50, scorpion_health = 20, scorpion_energy = 10)
  41. print(scorpion.health)
  42. print(scorpion.energy)
  43.  
  44. # health and energy from the parent
  45. # poison_damage attribute
  46. # overwrite the damage method to show poison damage
  47.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement