Java
자바 연습 (equals, hashCode)
이무쿤
2019. 10. 29. 14:44
반응형

public class Date {
public int month;
public int day;
public int year;
public Date(int a, int b, int c)
{
this.year = a;
this.month = b;
this.day = c;
}
@Override
public boolean equals(Object obj)
{
Date temp = (Date)obj;
if(this.day == temp.day && this.year == temp.year && this.month == temp.month)
return true;
return false;}
@Override
public int hashCode()
{
return day+month*year;
}
}
class M{
public static void main(String[] args)
{
Date d1 = new Date(2019,10,30);
Date d2 = new Date(2019,10,30);
if(d1.equals(d2)&&d1.hashCode() == d2.hashCode())
{
System.out.println("같은 객체입니다");
}
}
}
반응형