ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 인터페이스 실습
    과거⚰️ 2020. 12. 30. 19:03
    package day069_2;
    
    // 표시(marker) 인터페이스
    interface Repairable {}
    
    // 모든 유닛의 최고 조상 클래스
    class Unit {
    	
    	// 상수는 반드시 선언과 동시에 초기화를 시켜야 한다.
    	// 상수를 초기화 시키는 방법은 선언시 =을 사용해서 초기화시키는 방법과
    	// 생성자에서 초기화 시키는 방법이 있다.
    	final int MAX_HP;			// 최대 HP
    	int hp;						// 현재 HP
    	
    	// 유닛 생성시 최대 HP를 넘겨받아 MAX_HP를 초기화 시키는 생성자.
    	public Unit(int hp) {
    		MAX_HP = hp;
    	}
    	
    	public void damaged(int damage) {
    		hp -= damage;
    		
    		if(hp < 0) {
    			hp = 0;
    		}
    	}
    }
    
    class GroundUnit extends Unit {
    	public GroundUnit(int hp) {
    		super(hp);
    	}
    }
    
    class AirUnit extends Unit {
    	public AirUnit(int hp) {
    		super(hp);
    	}
    }
    
    class Tank extends GroundUnit implements Repairable {
    	public Tank() {
    		super(150);
    		hp = MAX_HP;
    		
    		System.out.println("Tank의 현재 hp는 " + hp + " 입니다.");
    	}
    	
    	@Override
    	public String toString() {
    		return "Tank [hp=" + hp + "]";
    	}
    }
    
    class Marine extends GroundUnit {
    	public Marine() {
    		super(70);
    		hp = MAX_HP;
    		
    		System.out.println("Marine의 현재 hp는 " + hp + " 입니다.");
    	}
    	
    	@Override
    	public String toString() {
    		return "Marine [hp=" + hp + "]";
    	}
    }
    
    class DropShip extends AirUnit implements Repairable {
    	public DropShip() {
    		super(120);
    		hp = MAX_HP;
    		
    		System.out.println("DropShip의 현재 hp는 " + hp + " 입니다.");
    	}
    	
    	@Override
    	public String toString() {
    		return "DropShip [hp=" + hp + "]";
    	}
    }
    
    class SCV extends GroundUnit implements Repairable {
    	public SCV() {
    		super(100);
    		hp = MAX_HP;
    		
    		System.out.println("SCV의 현재 hp는 " + hp + " 입니다.");
    	}
    	
    	/*
    	 * 수리하는 메서드는 유닛별로 각각 만들지 않고,
    	 * 실제 수리기능을 담당하는 유닛인 이곳에만 만든다.
    	 * 
    	 * 수리할 객체를 넘겨받은 Repairable 인터페이스에는 아무런 내용이 없으므로
    	 * 인수로 넘겨받은 객체의 클래스로 형변환 시킨 후 사용해야 한다.
    	 */
    	public void repair(Repairable repairable) {
    		Unit unit = (Unit)repairable;
    		while(unit.hp != unit.MAX_HP) {
    			unit.hp++;
    			
    			System.out.println(unit.hp + " / " + unit.MAX_HP);
    		}
    		System.out.println(unit + " 수리 완료");
    	}
    	
    	@Override
    	public String toString() {
    		return "SCV [hp=" + hp + "]";
    	}
    	
    }
    
    public class Day069인터페이스실습 {
    	public static void main(String[] args) {
    		
    		Tank tank = new Tank();
    		tank.damaged(5);
    		
    		DropShip dropShip = new DropShip();
    		dropShip.damaged(3);
    		
    		Marine marine = new Marine();
    		marine.damaged(4);
    		
    		SCV scv = new SCV();
    		scv.repair(tank);
    		scv.repair(dropShip);
    		// scv.repair(marine);
    		
    	}
    }

    '과거⚰️' 카테고리의 다른 글

    js 1  (0) 2020.12.31
    라이브 코딩  (0) 2020.12.31
    라이브 코딩  (0) 2020.12.30
    instanceof 연산자  (0) 2020.12.29
    미니프로젝트 shop  (0) 2020.12.29
Designed by Tistory.