카테고리 없음

[JAVA] 객체지향입문- 접근제어지시자(access modifier), 정보은닉

서연연연 2023. 1. 21. 17:56

Birthday

package ch10;

public class BirthDay {
	
	private int day;
	private int month;
	private int year;
	
	private boolean isValid;
	
	public int getDay() {
		//get메서드
		return day;
	}
	public void setDay(int day) {
		this.day=day;
				
	}
	public int getMonth() {
		return month;
	}
	public void setMonth(int month) {
		
		//set메서드에서 값을 제어
		if(month<1||month>12) {
			isValid=false;
		}
		else {
		isValid=true;
		}
		this.month = month;
	}
	public int getYear() {
		return year;
	}
	public void setYear(int year) {
		this.year = year;
	}
	
	public void showDate() {
		if(isValid) {
			System.out.println(year+"년"+month+"월"+day+"일");
		}
		else {
			System.out.println("유효하지 않습니다");
		}
	}
}

BirthdayTest

package ch10;

public class BirthdayTest {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		BirthDay date= new BirthDay();
		date.setYear(2019);
		date.setMonth(13);
		date.setDay(23);
		
		date.setYear(2019);
		date.setMonth(12);
		date.setDay(23);
		
		date.showDate();
	}

}