3초기억력

게임) 상속, 오버라이딩 본문

자바_기초

게임) 상속, 오버라이딩

잠수콩 2019. 5. 27. 13:54

실행 결과물

//유닛 배열 생성
//변수: hp, mp | method : attack
//하늘 - 비행기, 드래곤 각 10개
//땅 - 탱크, 사람 각 10개

class Unit{
	int hp;
	int mp;
	
	void attack() {
		System.out.println("기본 공격 ~~~~~~~~~~~~~~~~~");
	}
}

//하늘
class Sky extends Unit{
}

//땅
class Ground extends Unit{	
}

//비행기
class Airplane extends Sky{
	@Override
	void attack() {
		System.out.println("미사일 공격!!");
	}
}
//드래곤
class Dragon extends Sky{
	@Override
	void attack() {
		System.out.println("불 공격!!");
	}
}

//탱크
class Tank extends Ground{
	@Override
	void attack() {
		System.out.println("포탄 공격!!");
	}
}
//마린
class Marine extends Ground{
	@Override
	void attack() {
		super.attack();
		System.out.println("총 공격!!");
	}
}

public class exam_Game {

	public static void main(String[] args) {

		Sky[] s1 = new Sky[10];

		s1[0] = new Airplane();
		s1[0].attack();
		
		System.out.println("================");
		
		s1[1] = new Dragon();
		s1[1].attack();
		
		System.out.println("================");
		
		Ground[] g1 = new Ground[10];
		g1[0] = new Marine();
		g1[0].attack();
		
		System.out.println("================");
		
		g1[1] = new Tank();
		g1[1].attack();

	}

}
Comments