rangga_hrdme

class & instance variable

Apr 9th, 2021 (edited)
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.79 KB | None | 0 0
  1. # Object Oriented Programming (OOP)
  2. # Class/ template/ abstract
  3. # Main class
  4. class Hero:
  5.     # Class/ static variable
  6.     amount = 0
  7.  
  8.     # Constructor __init__()
  9.     def __init__(self, inputName, inputHealth, inputPower, inputArmor):
  10.         # Instance variable
  11.         self.name       = inputName
  12.         self.health     = inputHealth
  13.         self.power      = inputPower
  14.         self.armor      = inputArmor
  15.         Hero.amount     += 1 # Add 1 for each action
  16.         print("Create human that named by ", inputName)
  17.  
  18. hero1 = Hero("sniper", 100, 10, 4)
  19. print(Hero.amount)
  20. hero2 = Hero("mirana", 100, 15, 1)
  21. print(Hero.amount)
  22. hero3 = Hero("ucup", 1000, 100, 0)
  23. print(Hero.amount)
  24.  
  25. # Link: https://www.youtube.com/watch?v=upngNSC9FU8&list=PLZS-MHyEIRo7ab0-EveSvf4CLdyOECMm0&index=3
  26.  
  27.  
Add Comment
Please, Sign In to add comment