|
| 1 | +package Herencia; |
| 2 | +/* |
| 3 | + Crear una superclase "Animal" con atributos como especie, nombre y edad. |
| 4 | + También debe tener métodos para moverse y hacer sonidos. |
| 5 | + Crear dos subclases de "Animal": "Perro" y "Gato". |
| 6 | + Ambas deben tener atributos adicionales como raza o si es un gato doméstico o salvaje. |
| 7 | +*/ |
| 8 | +public class Animal { |
| 9 | + protected String especie; |
| 10 | + protected String nombre; |
| 11 | + protected int edad; |
| 12 | + //constructor |
| 13 | + public Animal(String especie, String nombre, int edad) { |
| 14 | + this.especie = especie; |
| 15 | + this.nombre = nombre; |
| 16 | + this.edad = edad; |
| 17 | + } |
| 18 | + //geters and setters |
| 19 | + public String getEspecie() { |
| 20 | + return especie; |
| 21 | + } |
| 22 | + public void setEspecie(String especie) { |
| 23 | + this.especie = especie; |
| 24 | + } |
| 25 | + public String getNombre() { |
| 26 | + return nombre; |
| 27 | + } |
| 28 | + public void setNombre(String nombre) { |
| 29 | + this.nombre = nombre; |
| 30 | + } |
| 31 | + public int getEdad() { |
| 32 | + return edad; |
| 33 | + } |
| 34 | + public void setEdad(int edad) { |
| 35 | + this.edad = edad; |
| 36 | + } |
| 37 | + //metodo moverse; |
| 38 | + public void moverse() { |
| 39 | + System.out.println(this.nombre+" esta moviendose"); |
| 40 | + } |
| 41 | + //metodo hacer sonidos; |
| 42 | + public void hacerSonido() { |
| 43 | + System.out.println(this.nombre + " está haciendo un sonido."); |
| 44 | + } |
| 45 | + //metodo toString() |
| 46 | + @Override |
| 47 | + public String toString() { |
| 48 | + StringBuilder builder = new StringBuilder(); |
| 49 | + builder.append("Animal [especie="); |
| 50 | + builder.append(this.especie); |
| 51 | + builder.append(", nombre="); |
| 52 | + builder.append(this.nombre); |
| 53 | + builder.append(", edad="); |
| 54 | + builder.append(this.edad); |
| 55 | + builder.append("]"); |
| 56 | + return builder.toString(); |
| 57 | + } |
| 58 | + |
| 59 | +} |
0 commit comments