본문 바로가기
국비/스터디

스터디1_인터페이스 설계도

by haheaven 2021. 9. 5.

* 2021.09.04 자바의 정석 ex7_26  설계도 그려보기

* 2021.09.05 피드백

 

자바의 정석 ex7_26 

  public static void main(String[] args) {
	Tank tank = new Tank();
	Dropship dropship = new Dropship();
		
	Marine marine = new Marine();
	SCV scv = new SCV();
		
	scv.repair(tank);
	scv.repair(dropship);
 }
}

interface Repairable{}

class Unit{
	int hitPoint;
	final int MAX_HP;
	Unit(int hp){
		MAX_HP = hp;
	}
}

class GroundUnit extends Unit{
	GroundUnit(int hp){
		super(hp);
	}
}
class AirUnit extends Unit{
	AirUnit(int hp){
		super(hp);
	}
}

class Tank extends GroundUnit implements Repairable{
	Tank(){
		super(150);
		hitPoint = MAX_HP;
	}
	public String toString() {
		return "tank";
		
	}	
}

class Dropship extends AirUnit implements Repairable{
	Dropship (){
		super(125);
		hitPoint = MAX_HP;
	}
	public String toString() {
		return "Dropship ";
		
	}
}

class Marine extends GroundUnit{
	Marine(){
		super(40);
		hitPoint = MAX_HP;
	}
	public String toString() {
		return "Marine ";
		
	}
}
class SCV extends GroundUnit implements Repairable{
	SCV(){
		super(60);
		hitPoint = MAX_HP;
	}
	void repair(Repairable r) {
		if(r instanceof Unit) {
			Unit u = (Unit)r;
			while( u.hitPoint != MAX_HP) {
				u.hitPoint++;
			}
			System.out.println(u.toString() + "의 수리가 끝났습니다. ");
		}
	}

클래스 관계도 

 

 

 

과정 

 

'국비 > 스터디' 카테고리의 다른 글

스터디2_연습문제  (0) 2021.09.12

댓글