Last active
April 17, 2021 18:37
-
-
Save Apolofx/d78e8779164fd58026513b0db6ca509f to your computer and use it in GitHub Desktop.
equals(Object o) toString() overwrite java's equals and toString methods #Java #equals #toString
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class Fecha { | |
| private int dia = 1; | |
| private int mes = 1; | |
| private int año = 2006; | |
| //El metodo equals por defecto es una Shallow comparison. Devuelve si 2 referencias a objetos refieren al mismo objeto. | |
| public boolean equals(Object o){ | |
| boolean result=false; | |
| // me aseguro de que no sea null y que ademas sea una instancia de Fecha, para poder hacer el downcasting de Object a Fecha | |
| if ((o!=null) && (o instanceof Fecha)){ | |
| // Hago un casting de Object a Fecha para poder acceder a los metodos de Fecha | |
| Fecha f=(Fecha)o; | |
| if ((f.getDia()==this.getDia()) | |
| &&(f.getMes() ==this.getMes())&& | |
| (f.getAño() ==this.getAño()) ) result=true; | |
| } | |
| return result; } | |
| @Override | |
| public String toString(){ | |
| return "Fecha :" + getDia() + "-" + getMes() + "-" + getAño(); | |
| } | |
| // |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment