package ch06_quiz;
/*
* Military class 를 상속받는 Army, Navy, AirForce class 를 정의하세요
* - MilitaryTest class 에서 Miliraty class 변수를 사용해서 다형성으로 구현한 코드를 작성하세요
*/
public abstract class Military {
private String name;
public Military(String name) {
this.name = name;
}
public String getName() { return name; }
public void setName(String name) {
this.name =name;
}
public abstract void attack();
public abstract void move();
public void MilitaryInfo() {
System.out.println("--- Military classes ---");
}
}
class Army extends Military{
public Army(String name) {
super(name);
}
public void attack() {
System.out.println(getName() + " 공격");
}
public void move() {
System.out.println(getName() + " 무빙");
}
}
class Navy extends Military{
public Navy(String name) {
super(name);
}
public void attack() {
System.out.println(getName() + " 공격");
}
public void move() {
System.out.println(getName() + " 무빙");
}
}
class AirForce extends Military{
public AirForce(String name) {
super(name);
}
public void attack() {
System.out.println(getName() + " 공격");
}
public void move() {
System.out.println(getName() + " 무빙");
}
}
--------------------------------------------------------------------------------
//MilitaryTest
package ch06_quiz;
import java.util.Scanner;
public class MilitaryTest {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Military run = null;
String name = sc.next();
switch(name) {
case "육군":
run = new Army("육군"); break;
case "공군":
run = new AirForce("공군"); break;
case "해군":
run = new Navy("해군"); break;
}
if(run != null) {
run.attack();
run.move();
}
sc.close();
}
}