본문 바로가기

개발/Java

[Head First Java] Chapter4 GoodDog 클래스 캡슐화

GoodDog 클래스
class GoodDog {
	private int size;
	public int getSize(){
		return size;
	}
	
	public void setSize(int s){
		size = s;
	}
	
	void bark(){
		if(size > 60){
			System.out.println("Wooof! Wooof!");
		}
		else if(size > 14){
			System.out.println("Ruff! Ruff!)");
		}
		else{
			System.out.println("Yip! Yip!");
		}
	}
}
GoodDogTestDrive 클래스
class GoodDogTestDrive {
	public static void main(String[] args) {
		GoodDog one = new GoodDog();
		one.setSize(70);
		GoodDog two = new GoodDog();
		two.setSize(8);
		System.out.println("Dog one: " + one.getSize());
		System.out.println("Dog two: " + two.getSize());
		one.bark();
		two.bark();
	}
}
출력 결과
Dog one: 70
Dog two: 8
Wooof! Wooof!
Yip! Yip!