forked from fiemcasals/Programacion_II
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path06_inheritance_polymorphism.py
More file actions
50 lines (38 loc) · 1.53 KB
/
06_inheritance_polymorphism.py
File metadata and controls
50 lines (38 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# 06_inheritance_polymorphism.py — Herencia, super(), polimorfismo
class Animal:
def __init__(self, nombre: str):
self.nombre = nombre
def hablar(self) -> str:
# Método "abstracto" por contrato (sin ABC)
raise NotImplementedError("Animal es una clase base; 'hablar' debe ser redefinido en las subclases.")
#return "falta definir el comportamiento en las clases hijas"
class Perro(Animal):
def __init__(self, nombre: str, raza: str):
# Llamo al inicializador de la clase base
super().__init__(nombre)
self.raza = raza
def hablar(self) -> str:
# Podrías usar super().hablar() si quisieras verificar/registrar algo antes,
# pero acá simplemente implementamos el comportamiento concreto.
return "guau"
class Gato(Animal):
def __init__(self, nombre: str, color: str):
# Reutilizo la inicialización de Animal
super().__init__(nombre)
self.color = color
def hablar(self) -> str:
return "miau"
def coro_animal(animals: list[Animal]) -> None:
for a in animals:
print(f"{a.nombre} dice {a.hablar()}")
if __name__ == "__main__":
p = Perro("Toby", raza="Labrador")
g = Gato("Mishi", color="gris")
coro_animal([p, g])
# Si intentás instanciar Animal y llamar a hablar(), lanzará NotImplementedError.
try:
z = Animal("Zuquer")
print(z.hablar())
except NotImplementedError as e:
print(f"Error esperado: {e}")
print("terminó el programa con éxito")