|
| 1 | +""" |
| 2 | +video en https://youtu.be/PVBs5PWjedA?si=gkpzPPTm0WUjsXAJ |
| 3 | +* EJERCICIO: |
| 4 | + * Explora el concepto de herencia según tu lenguaje. Crea un ejemplo que |
| 5 | + * implemente una superclase Animal y un par de subclases Perro y Gato, |
| 6 | + * junto con una función que sirva para imprimir el sonido que emite cada Animal. |
| 7 | + * |
| 8 | +""" |
| 9 | + |
| 10 | +class Animal: |
| 11 | + def __init__ (self, grupo: str, categoria: str, alimentacion: str, name: str): |
| 12 | + self.grupo = grupo # Vertebrados, Invertebrados |
| 13 | + self.categoria = categoria # vertebrados: Aves, Peces, Reptiles, Mamiferos #invertebrados: Insectos, |
| 14 | + self.alimentacion = alimentacion # Omnivoros, Herbivoros, Carnivoeros, Insectivoros |
| 15 | + self.name = name |
| 16 | + |
| 17 | + def sound(self): |
| 18 | + pass |
| 19 | + |
| 20 | +class Dog(Animal): |
| 21 | + |
| 22 | + def sound(self): |
| 23 | + print("Guau!") |
| 24 | + |
| 25 | +class Cat(Animal): |
| 26 | + def sound(self): |
| 27 | + print("Miau!") |
| 28 | + |
| 29 | +def print_sound(animal: Animal): |
| 30 | + animal.sound() |
| 31 | + |
| 32 | +animal = Animal("V", "M", "C", "Animal") |
| 33 | +animal.sound() |
| 34 | + |
| 35 | +dog = Dog("M", "M", "C", "Perro") |
| 36 | +dog.sound() |
| 37 | + |
| 38 | +cat = Cat("M", "M", "C", "Cat") |
| 39 | +cat.sound() |
| 40 | +print("> > > > > > > > > > >") |
| 41 | +print_sound(animal) |
| 42 | +print_sound(dog) |
| 43 | +print_sound(cat) |
| 44 | + |
| 45 | + |
| 46 | +""" |
| 47 | +* DIFICULTAD EXTRA (opcional): |
| 48 | + * Implementa la jerarquía de una empresa de desarrollo formada por Empleados que |
| 49 | + * pueden ser Gerentes, Gerentes de Proyectos o Programadores. |
| 50 | + * Cada empleado tiene un identificador y un nombre. |
| 51 | + * Dependiendo de su labor, tienen propiedades y funciones exclusivas de su |
| 52 | + * actividad, y almacenan los empleados a su cargo. |
| 53 | +""" |
| 54 | +print("DIFICULTAD EXTRA\n") |
| 55 | + |
| 56 | +class Employee: |
| 57 | + def __init__(self, id: str, name: str): |
| 58 | + self.id = id |
| 59 | + self.name = name |
| 60 | + self.employees = [] |
| 61 | + |
| 62 | + def add(self, employee): |
| 63 | + self.employees.append(employee) |
| 64 | + |
| 65 | + def get_employees(self): |
| 66 | + print(f"los colabores de {self.name} son:") |
| 67 | + for employee in self.employees: |
| 68 | + print(employee.name) |
| 69 | + |
| 70 | +class Manager(Employee): |
| 71 | + def coordina_proyectos(self): |
| 72 | + print(f"{self.name} esta coordinando los proyectos") |
| 73 | + |
| 74 | +class ProjectManager(Employee): |
| 75 | + |
| 76 | + def __init__(self, id: str, name: str, project: str): |
| 77 | + super().__init__(id, name) # reutiliza el inicialiador de la superclase |
| 78 | + self.project = project |
| 79 | + |
| 80 | + def coordina_proyecto(self): |
| 81 | + print(f"{self.name} esta coordinando un proyecto") |
| 82 | + |
| 83 | +class Programmer(Employee): |
| 84 | + def __init__(self, id: str, name: str, language: str): |
| 85 | + super().__init__(id, name) # reutiliza el inicialiador de la superclase |
| 86 | + self.language = language |
| 87 | + |
| 88 | + def code(self): |
| 89 | + print(f"{self.name} esta programando en {self.language}") |
| 90 | + |
| 91 | + def add(self, employee): |
| 92 | + print(f"{employee.name} No puede ser añadido porque {self.name} es programador y no puede tener empleados a su cargo") |
| 93 | + |
| 94 | +mgr = Manager("mgr1", "Tony") |
| 95 | +mgr2 = Manager("mgr2", "Katy") |
| 96 | + |
| 97 | + |
| 98 | +pm = ProjectManager("pm1", "Chucho", "Project 1") |
| 99 | +pm2 = ProjectManager("pm2", "Vic", "Project 2") |
| 100 | + |
| 101 | +prgmr = Programmer("pr1", "Gonza", "Java") |
| 102 | +prgmr2 = Programmer("pr2", "Luis", "Python") |
| 103 | +prgmr3 = Programmer("pr3", "Vero", "C#") |
| 104 | +prgmr4 = Programmer("pr4", "Pilar", "React") |
| 105 | +prgmr5 = Programmer("pr5", "Mary", "Python") |
| 106 | + |
| 107 | +mgr.add(pm) |
| 108 | +mgr2.add(pm2) |
| 109 | + |
| 110 | +pm.add(prgmr) |
| 111 | +pm.add(prgmr2) |
| 112 | +pm2.add(prgmr3) |
| 113 | +pm2.add(prgmr4) |
| 114 | +pm2.add(prgmr5) |
| 115 | + |
| 116 | +prgmr.add(prgmr3) |
| 117 | + |
| 118 | +prgmr2.code() |
| 119 | + |
| 120 | +pm.coordina_proyecto() |
| 121 | + |
| 122 | +mgr.coordina_proyectos() |
| 123 | + |
| 124 | +mgr.get_employees() |
| 125 | +mgr2.get_employees() |
| 126 | + |
| 127 | +pm.get_employees() |
| 128 | +pm2.get_employees() |
| 129 | + |
0 commit comments