|
| 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 | +abstract class Animal { |
| 9 | + protected readonly name: string |
| 10 | + protected readonly age: number |
| 11 | + protected readonly color: string |
| 12 | + |
| 13 | + constructor(name: string, age: number, color: string) { |
| 14 | + this.name = name |
| 15 | + this.age = age |
| 16 | + this.color = color |
| 17 | + } |
| 18 | + |
| 19 | + abstract sound(): void |
| 20 | +} |
| 21 | + |
| 22 | +class Cat extends Animal { |
| 23 | + sound(): void { |
| 24 | + console.log(`${this.name} el gato hace "Miau!"`) |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +class Dog extends Animal { |
| 29 | + sound(): void { |
| 30 | + console.log(`${this.name} el perro hace "Guau!"`) |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +const dog1: Dog = new Dog('Max', 2, 'café') |
| 35 | +dog1.sound() |
| 36 | + |
| 37 | +const cat1: Cat = new Cat('Michi', 1, 'negro') |
| 38 | +cat1.sound() |
| 39 | + |
| 40 | +/* |
| 41 | + * DIFICULTAD EXTRA (opcional): |
| 42 | + * Implementa la jerarquía de una empresa de desarrollo formada por Empleados que |
| 43 | + * pueden ser Gerentes, Gerentes de Proyectos o Programadores. |
| 44 | + * Cada empleado tiene un identificador y un nombre. |
| 45 | + * Dependiendo de su labor, tienen propiedades y funciones exclusivas de su |
| 46 | + * actividad, y almacenan los empleados a su cargo. |
| 47 | +*/ |
| 48 | + |
| 49 | +class Employee { |
| 50 | + protected readonly id: number |
| 51 | + protected name: string |
| 52 | + |
| 53 | + constructor(id: number, name: string) { |
| 54 | + this.id = id |
| 55 | + this.name = name |
| 56 | + } |
| 57 | + |
| 58 | + displayInfo(): void { |
| 59 | + console.log(`ID: ${this.id}, Name: ${this.name}`) |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +class Manager extends Employee { |
| 64 | + private employees: string[] = [] |
| 65 | + |
| 66 | + constructor(id: number, name: string, employees: string[] = []) { |
| 67 | + super(id, name) |
| 68 | + this.employees = employees |
| 69 | + } |
| 70 | + |
| 71 | + addEmployee(employee: string): void { |
| 72 | + this.employees.push(employee) |
| 73 | + } |
| 74 | + |
| 75 | + getEmployees(): string[] { |
| 76 | + return this.employees |
| 77 | + } |
| 78 | + |
| 79 | + displayInfo(): void { |
| 80 | + super.displayInfo() |
| 81 | + console.log(`Managed Employees: ${this.employees.join(', ') || 'No employees'}`) |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +class ProjectManager extends Employee { |
| 86 | + private projects: string[] = [] |
| 87 | + |
| 88 | + constructor(id: number, name: string, projects: string[] = []) { |
| 89 | + super(id, name) |
| 90 | + this.projects = projects |
| 91 | + } |
| 92 | + |
| 93 | + addProject(projectName: string): void { |
| 94 | + this.projects.push(projectName) |
| 95 | + } |
| 96 | + |
| 97 | + getProjects(): string[] { |
| 98 | + return this.projects |
| 99 | + } |
| 100 | + |
| 101 | + displayInfo(): void { |
| 102 | + super.displayInfo() |
| 103 | + console.log(`Projects: ${this.projects.join(', ') || 'No projects'}`) |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +class Developer extends Employee { |
| 108 | + private skills: string[] = [] |
| 109 | + |
| 110 | + constructor(id: number, name: string, skills: string[] = []) { |
| 111 | + super(id, name) |
| 112 | + this.skills = skills |
| 113 | + } |
| 114 | + |
| 115 | + addSkill(skill: string): void { |
| 116 | + if (!this.skills.includes(skill)) { |
| 117 | + this.skills.push(skill) |
| 118 | + } else { |
| 119 | + console.log(`${this.name} already has skill: ${skill}`) |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + getSkills(): string[] { |
| 124 | + return this.skills |
| 125 | + } |
| 126 | + |
| 127 | + displayInfo(): void { |
| 128 | + super.displayInfo() |
| 129 | + console.log(`Skills: ${this.skills.join(', ') || 'No skills'}`) |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | + |
| 134 | +const manager = new Manager(1, "Alice", ["Bob", "Charlie"]) |
| 135 | +manager.addEmployee("Dave") |
| 136 | +manager.displayInfo() |
| 137 | + |
| 138 | +const projectManager = new ProjectManager(2, "Eve", ["Project A", "Project B"]) |
| 139 | +projectManager.addProject("Project C") |
| 140 | +projectManager.displayInfo() |
| 141 | + |
| 142 | +const developer = new Developer(3, "Tom", ["JavaScript", "TypeScript"]) |
| 143 | +developer.addSkill("React") |
| 144 | +developer.addSkill("JavaScript") |
| 145 | +developer.displayInfo() |
0 commit comments