lunes, 7 de abril de 2014

Explicando herencia 2

Definiendo un objeto sofá que herede propiedades de un mueble.




package ejerciciomuebles;

/**
*
* @author juan
*/
public class EjercicioMuebles {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Mueble miMueble = new Mueble();
 

      
        Sofa miSofa = new Sofa(1,2,3,4,Situacion.SALON,"madera, tela y metal");
        Mueble otroMueble;
        System.out.println("material sofá "+ miSofa.getMaterial());
        otroMueble=miSofa;
        System.out.println("material otroMueble" + otroMueble.getMaterial());
        System.out.println(miSofa.equals(miMueble));
    }
}


package ejerciciomuebles;

/**
*
* @author juan
*/

public class Mueble {

    private double pvp;
    private double largo;
    private double ancho;
    private double alto;
    private Situacion lugar;
    private String material;

    public Mueble() {
    }

    public Mueble(double pvp, double largo, double ancho, double alto, Situacion lugar, String material) {
        this.pvp = pvp;
        this.largo = largo;
        this.ancho = ancho;
        this.alto = alto;
        this.lugar = lugar;
        this.material = material;
       
    }

    public double getAlto() {
        return alto;
    }

    public void setAlto(double alto) {
        this.alto = alto;
    }

    public double getAncho() {
        return ancho;
    }

    public void setAncho(double ancho) {
        this.ancho = ancho;
    }

    public double getLargo() {
        return largo;
    }

    public void setLargo(double largo) {
        this.largo = largo;
    }

    public Situacion getLugar() {
        return lugar;
    }

    public void setLugar(Situacion lugar) {
        this.lugar = lugar;
    }

    public String getMaterial() {
        return material;
    }

    public void setMaterial(String material) {
        this.material = material;
    }

    public double getPvp() {
        return pvp;
    }

    public void setPvp(double pvp) {
        this.pvp = pvp;
    }
   
}


package ejerciciomuebles;

/**
*
* @author juan
*/
enum Situacion {COCINA,SALON,DORMITORIO};



/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package ejerciciomuebles;

/**
*
* @author juan
*/
public class Sofa extends Mueble{
    int tipo;
    int plazas;

    public Sofa() {
    }

    public Sofa(double pvp, double largo, double ancho, double alto, Situacion lugar, String material) {
        super(pvp, largo, ancho, alto, lugar, material);
    }

    public int getPlazas() {
        return plazas;
    }

    public void setPlazas(int plazas) {
        this.plazas = plazas;
    }
  @Override
    public boolean equals(Object other) {
        if (other == null) return false;
        if (other == this) return true;
        if (!(other instanceof Sofa))return false;
        Sofa elOtro = (Sofa)other;
        return (elOtro.getLargo()==this.getLargo() && elOtro.getMaterial().equalsIgnoreCase(this.getMaterial()));
    }
   
   
   
   
}

No hay comentarios: