rangga_hrdme

methods: python

Apr 9th, 2021 (edited)
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.05 KB | None | 0 0
  1. # OOP: METHOD
  2.  
  3. class Hero:
  4.     # Class/ static variable
  5.     amount_hero = 0
  6.  
  7.     # Constructor __init__()
  8.     def __init__(self, inputName, inputHealth, inputPower, inputArmor):
  9.         # Instance variable
  10.         self.name       = inputName
  11.         self.health     = inputHealth
  12.         self.power      = inputPower
  13.         self.armor      = inputArmor
  14.         Hero.amount_hero    += 1
  15.  
  16.     # Void function, method without argument & return value
  17.     def who(self):
  18.         print("My Name: ", self.name)
  19.  
  20.     # Method with argument & without return
  21.     def healthUp(self, up):
  22.         self.health += up
  23.  
  24.     # Method with return value
  25.     def getHealth(self):
  26.         return self.health
  27.  
  28. hero1 = Hero("sniper", 100, 10, 4)
  29. hero2 = Hero("mario", 90, 15, 1)
  30. hero3 = Hero("ifa", 1000, 100, 0)
  31.  
  32. print(hero1.__dict__)
  33. print(hero2.__dict__)
  34. print(hero3.__dict__)
  35.  
  36. hero1.who()
  37. hero1.healthUp(11)
  38. print(hero1.health)
  39. print(hero1.getHealth())
  40.  
  41. # Link: https://www.youtube.com/watch?v=DE-h_oR8Nmo&list=PLZS-MHyEIRo7ab0-EveSvf4CLdyOECMm0&index=4
Add Comment
Please, Sign In to add comment