|
| 1 | +""" |
| 2 | +EJERCICIO: |
| 3 | + * Explora el concepto de herencia según tu lenguaje. Crea un ejemplo que |
| 4 | + * implemente una superclase Animal y un par de subclases Perro y Gato, |
| 5 | + * junto con una función que sirva para imprimir el sonido que emite cada Animal. |
| 6 | + * |
| 7 | +""" |
| 8 | + |
| 9 | +# Superclase |
| 10 | + |
| 11 | +class Animal: |
| 12 | + |
| 13 | + def __init__(self, name: str): |
| 14 | + self.name = name |
| 15 | + |
| 16 | + def sound(self): |
| 17 | + pass |
| 18 | + |
| 19 | +# Subclases |
| 20 | + |
| 21 | + |
| 22 | +class Dog(Animal): |
| 23 | + |
| 24 | + def sound(self): |
| 25 | + print("Guau!") |
| 26 | + |
| 27 | + |
| 28 | +class Cat(Animal): |
| 29 | + |
| 30 | + def sound(self): |
| 31 | + print("Miau!") |
| 32 | + |
| 33 | + |
| 34 | +def print_sound(animal: Animal): |
| 35 | + animal.sound() |
| 36 | + |
| 37 | + |
| 38 | +my_animal = Animal("Animal") |
| 39 | +print_sound(my_animal) |
| 40 | +my_dog = Dog("Perro") |
| 41 | +print_sound(my_dog) |
| 42 | +my_cat = Cat("Gato") |
| 43 | +print_sound(my_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 | +""" |
| 55 | + |
| 56 | + |
| 57 | +class Employee: |
| 58 | + |
| 59 | + def __init__(self, id: int, name: str): |
| 60 | + self.id = id |
| 61 | + self.name = name |
| 62 | + self.employees = [] |
| 63 | + |
| 64 | + def add(self, employee): |
| 65 | + self.employees.append(employee) |
| 66 | + |
| 67 | + def print_employees(self): |
| 68 | + for employee in self.employees: |
| 69 | + print(employee.name) |
| 70 | + |
| 71 | + |
| 72 | +class Manager(Employee): |
| 73 | + |
| 74 | + def coordinate_projects(self): |
| 75 | + print(f"{self.name} está coordinando todos los proyectos de la empresa.") |
| 76 | + |
| 77 | + |
| 78 | +class ProjectManager(Employee): |
| 79 | + |
| 80 | + def __init__(self, id: int, name: str, project: str): |
| 81 | + super().__init__(id, name) |
| 82 | + self.project = project |
| 83 | + |
| 84 | + def coordinate_project(self): |
| 85 | + print(f"{self.name} está coordinando su {self.project}.") |
| 86 | + |
| 87 | + |
| 88 | +class Programmer(Employee): |
| 89 | + |
| 90 | + def __init__(self, id: int, name: str, language: str): |
| 91 | + super().__init__(id, name) |
| 92 | + self.language = language |
| 93 | + |
| 94 | + def code(self): |
| 95 | + print(f"{self.name} está programando en {self.language}.") |
| 96 | + |
| 97 | + def add(self, employee: Employee): |
| 98 | + print( |
| 99 | + f"Un programador no tiene empleados a su cargo. {employee.name} no se añadirá.") |
| 100 | + |
| 101 | + |
| 102 | +my_manager = Manager(1, "MoureDev") |
| 103 | +my_project_manager = ProjectManager(2, "Brais", "Proyecto 1") |
| 104 | +my_project_manager2 = ProjectManager(3, "Moure", "Proyecto 2") |
| 105 | +my_programmer = Programmer(4, "Kontrol", "Swift") |
| 106 | +my_programmer2 = Programmer(5, "Ros", "Cobol") |
| 107 | +my_programmer3 = Programmer(6, "Bushi", "Dart") |
| 108 | +my_programmer4 = Programmer(7, "Nasos", "Python") |
| 109 | + |
| 110 | +my_manager.add(my_project_manager) |
| 111 | +my_manager.add(my_project_manager2) |
| 112 | + |
| 113 | +my_project_manager.add(my_programmer) |
| 114 | +my_project_manager.add(my_programmer2) |
| 115 | + |
| 116 | +my_project_manager2.add(my_programmer3) |
| 117 | +my_project_manager2.add(my_programmer4) |
| 118 | + |
| 119 | +my_programmer.add(my_programmer2) |
| 120 | + |
| 121 | +my_programmer.code() |
| 122 | +my_project_manager.coordinate_project() |
| 123 | +my_project_manager2.coordinate_project() |
| 124 | +my_manager.coordinate_projects() |
| 125 | +my_manager.print_employees() |
| 126 | +my_project_manager.print_employees() |
| 127 | +my_project_manager2.print_employees() |
| 128 | +my_programmer.print_employees() |
0 commit comments