IT/Java

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

hjshims 2021. 5. 24. 15:52
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);
	}

}

 

<출력결과>