본문 바로가기
IT/Java

Java (this생성자 공통메소드_Date)

by hjshims 2021. 5. 24.
package p0524This;

public class Date {
	int year;
	String month;
	int day;
	
	/*공통 메소드*/
	Date(int year, String month, int day){
		this.year = year;
		this.month = month;
		this.day = day;
	}
	Date(int year){
		this(year, "1월", 1);
		//this(year, null, 0);
	}
	Date(){	
		this(2022);
	}
	
	public String toString() {
		return "[year=" + year +", " + "month=" + month +", " + "day=" + day +"]";
	}
}
package p0524This;

public class DateEx {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Date date1 = new Date(2021, "5월", 1);
		Date date2 = new Date(2021);
		Date date3 = new Date();
		
		System.out.println(date1);
		System.out.println(date2);
		System.out.println(date3);
	}

}

 

<출력결과>

'IT > Java' 카테고리의 다른 글

Java (메소드 호출_주사위)  (0) 2021.05.28
Java (this생성자_Phonebook)  (0) 2021.05.24
Java (객체지향메소드_Car)  (0) 2021.05.24
Java (객체지향_Car)  (0) 2021.05.21
Java (1차원 배열_끝말잇기 게임)  (0) 2021.05.21