fcamuso

Corso Python - lezione 25 OOP - 3 (Ereditarietá)

Jul 3rd, 2026
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.26 KB | None | 0 0
  1. class Stregone:
  2.  
  3.     danno_base: int = 500
  4.  
  5.     def __init__(self, nome: str, livello_salute: int) -> None:
  6.         self.nome: str = nome
  7.         self.livello_salute: int = livello_salute
  8.  
  9.     def attacca(self) -> int:
  10.         return 300*self.danno_base
  11.  
  12. class Pezzente:
  13.     def attacca(self) -> int:
  14.         return 1
  15.  
  16. class StregoneSupremo(Stregone):
  17.     def __init__(self, nome: str, danno_bonus: int = 1000) -> None:
  18.         super().__init__(nome, 2000)
  19.         self.danno_bonus: int = danno_bonus
  20.  
  21.     def attacca(self) -> int:
  22.         return super().attacca() + self.danno_bonus
  23.  
  24. stre_sup1 = StregoneSupremo("Sono il boss!", 10000)
  25. print(stre_sup1.nome)
  26. print(stre_sup1.livello_salute)
  27. print(stre_sup1.danno_bonus)
  28.  
  29. print(stre_sup1.attacca())
  30.  
  31. clan: list[Stregone] = [Stregone("Urgos", 100), stre_sup1, Stregone("Kandios", 300)]
  32.  
  33. danno_attacco_gruppo: int = 0
  34. for streg in clan:
  35.     danno_attacco_gruppo += streg.attacca()
  36. print(danno_attacco_gruppo)
  37.  
  38.  
  39. armata_brancaleone: list[Stregone | Pezzente] = \
  40.     [Stregone("Urgos", 100), Pezzente(), stre_sup1, Stregone("Kandios", 300)]
  41.  
  42. danno_attacco_gruppo = 0
  43. for personaggio in armata_brancaleone:
  44.     danno_attacco_gruppo += personaggio.attacca()
  45.  
  46. print(danno_attacco_gruppo)
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
Advertisement
Add Comment
Please, Sign In to add comment