자바의 정석 - 추상클래스의 작성(2)

송용준·2023년 3월 10일

추상클래스의 작성

  • 추상화 <--> 구체화
  • 추상화된 코드는 구체화된 코드보다 유연하다. 변경에 유리
public class ex {
	public static void main(String[] args) {
//		Unit[] group = { new Marine(), new Tank(), new Dropship() };  	객체배열 : 참조변수를 묶은것
		Unit[] group = new Unit[3];
		group[0] = new Marine();
		group[1] = new Tank();
		group[2] = new Dropship();
		
		for (int i = 0; i < group.length; i++)
			group[i].move(100, 200);
	}
}

abstract class Unit{
	int x, y;
	abstract void move(int x, int y);
	void stop() {}
}

class Marine extends Unit{
	void move(int x, int y) {
		System.out.println("Marine[x=" + x + ", y=" + y + "]");
	}
	void stimPack() {}
}

class Tank extends Unit{
	void move(int x, int y) {
		System.out.println("Tank[x=" + x + ", y=" + y + "]");
	}
	void changeMode() {}
}

class Dropship extends Unit{
	void move(int x, int y) {
		System.out.println("Dropship[x=" + x + ", y=" + y + "]");
	}
	void load() {}
}
profile
용용

0개의 댓글