Notice
Recent Posts
Recent Comments
Link
«   2024/10   »
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
Archives
Today
Total
관리 메뉴

Super Coding Addict

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

JAVA 문법

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

밍응애 2021. 3. 2. 22:44

 

# 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());
	}

}