Super Coding Addict
Ch10. 자바 기본 클래스 (5) - 코딩해보기 본문

# MyDateTest 클래스
package ch10.classEx;
class MyDate{
int day;
int month;
int year;
public MyDate(int day, int month, int year) {
this.day = day;
this.month = month;
this.year = year;
}
@Override
public int hashCode() {
return day * 11 + month * 101 + year * 1001;
}
@Override
public boolean equals(Object obj) {
if(obj instanceof MyDate) {
MyDate date = (MyDate)obj;
return (this.month == date.month && this.day == date.day && this.year == date.year);
}else {
return false;
}
}
}
public class MyDateTest {
public static void main(String[] args) {
MyDate date1 = new MyDate(3, 16, 2021);
MyDate date2 = new MyDate(3, 16, 2021);
System.out.println(date1.equals(date2));
System.out.println(date1.hashCode());
System.out.println(date2.hashCode());
}
}
'JAVA 문법' 카테고리의 다른 글
Ch11. 컬렉션 프레임 워크 - 제네릭 프로그래밍 (0) | 2021.03.04 |
---|---|
Ch10. 자바 기본 클래스 (6) - String, Wrapper 클래스 (0) | 2021.03.03 |
Ch10. 자바 기본 클래스 (4) - Class 클래스 (0) | 2021.03.01 |
Ch10. 자바 기본 클래스 (3) - Object 클래스 (0) | 2021.03.01 |
Ch10. 자바 기본 클래스 (2) - Object 클래스 (0) | 2021.03.01 |