Veamos un ejemplo:
Clase principal persona
|
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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
// Persona is-a Object public class Persona extends Object{ private String nombre; private int edad; private char sexo; private float altura; public Persona() { // TODO Auto-generated constructor stub } public Persona(String nombre, int edad, char sexo, float altura) { this.nombre = nombre; this.edad = edad; this.sexo = sexo; this.altura = altura; } public String getNombre() { return nombre; } public void setNombre(String nombre) { this.nombre = nombre; } public int getEdad() { return edad; } public void setEdad(int edad) { this.edad = edad; } public char getSexo() { return sexo; } public void setSexo(char sexo) { this.sexo = sexo; } public float getAltura() { return altura; } public void setAltura(float altura) { this.altura = altura; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + Float.floatToIntBits(altura); result = prime * result + edad; result = prime * result + ((nombre == null) ? 0 : nombre.hashCode()); result = prime * result + sexo; return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Persona other = (Persona) obj; if (Float.floatToIntBits(altura) != Float.floatToIntBits(other.altura)) return false; if (edad != other.edad) return false; if (nombre == null) { if (other.nombre != null) return false; } else if (!nombre.equals(other.nombre)) return false; if (sexo != other.sexo) return false; return true; } @Override public String toString() { return "Persona [nombre=" + nombre + ", edad=" + edad + ", sexo=" + sexo + ", altura=" + altura + "]"; } } |
Ahora la clase empleado que hereda de la clase persona:
|
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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
// Empleado is-a Object // Empleado is-a Persona // Se hereda todo menos los constructores y los recursos privados public class Empleado extends Persona{ private double sueldo; private int antiguedad; public Empleado() { //this(null, 0, 'v',0,0,0); super(); } public Empleado(String nombre, int edad, char sexo, float altura, double sueldo, int antiguedad ) { // Invocando al constructor de la superclase super(nombre, edad, sexo, altura); // TODO Auto-generated constructor stub this.sueldo = sueldo; this.antiguedad = antiguedad; } public double getSueldo() { return sueldo; } public void setSueldo(double sueldo) { this.sueldo = sueldo; } public int getAntiguedad() { return antiguedad; } public void setAntiguedad(int antiguedad) { this.antiguedad = antiguedad; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + antiguedad; long temp; temp = Double.doubleToLongBits(sueldo); result = prime * result + (int) (temp ^ (temp >>> 32)); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Empleado other = (Empleado) obj; if (antiguedad != other.antiguedad) return false; if (Double.doubleToLongBits(sueldo) != Double .doubleToLongBits(other.sueldo)) return false; return true; } @Override public String toString() { // TODO Auto-generated method stub return super.toString() + " Sueldo: " + sueldo + " Antieguedad : " + antiguedad ; } } |
Ahora veamos como crear y utilizar los objetos en una clase main:
|
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 |
public class Principal { public static void main(String[] args) { Persona p = new Persona("Juan", 37, 'V', 1.78F); System.out.println(p); Empleado e = new Empleado("Maria", 32, 'H', 1.67F, 2500, 5); System.out.println(e); Empleado e2 = new Empleado("Maria", 32, 'H', 1.67F, 2500, 5); System.out.println(e2); // comprobar si son iguales if(e == e2){ System.out.println("Es el mismo objeto porque las direcciones de memeoria son la misma"); } if(e.equals(e2)){ System.out.println("Los obetos son iguales."); } } |