* 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() + "의 수리가 끝났습니다. ");
}
}
댓글