JAVA/코드

2022_10_14 연습 01 Quiz 14 Abstract클래스 연습

0304호 2022. 10. 14.
package quiz14;

public class MainClass {
	
	public static void main(String[] args) {
		
		Marine m = new Marine();
		m.location();
		m.move(3, 3);
		m.location();
		m.stop();
		System.out.println("Marine의 armor : " + Marine.armor);
		System.out.println("Marine의 attack : " + Marine.attack);
		
		System.out.println();
		System.out.println();
		
		Tank t = new Tank();
		t.location();
		t.move(0, 3);
		t.location();
		t.changeMode();
		
		System.out.println();
		System.out.println();

		t.stop();
		
		Unit d = new DropShip();
		d.location();
		d.move(4, 0);
		d.location();
		d.stop();
	}

}
package quiz14;

public class Marine extends Unit{

	public static int attack = 6;
	public static int armor = 0;

	public Marine() {
		super(40);
	}
	@Override
	public void location() {
		System.out.println("x는 : " + this.x + ", y는 : " + this.y);
	}

	@Override
	public void move(int x, int y) {
		this.x +=x;
		this.y +=y;
	}
	
	
	
	

}
package quiz14;

public class Tank extends Unit{
	
	private boolean mode;
	
	public Tank() {
		super(100);
	}
	
	@Override
	public void location() {
		System.out.println("x는 : " + this.x + ", y는 : " + this.y);
	}

	@Override
	public void move(int x, int y) {
		this.x +=x;
		this.y +=y;
	}
	public void changeMode() {
		if (mode) {
			mode = false;
		}else {
			mode = true;
		}
		System.out.println("공격모드를 변경합니다");
	}

}​
package quiz14;

public abstract class Unit {
	
	public int  x=0;
	public int  y=0;
	public int  hp;
	public Unit( int hp) {
		this.hp = hp;
		System.out.println("hp : " + hp);
	}
	
	public  abstract void location();
	public  abstract void move(int x, int y);
	public static void stop() {
		System.out.println("현재 위치에 정지");
	}
}
package quiz14;

public class DropShip extends Unit{

	public DropShip() {
		super(60);
	}
	
	@Override
	public void location() {
		System.out.println("x는 : " + this.x + ", y는 : " + this.y);
	}

	@Override
	public void move(int x, int y) {
		this.x +=x;
		this.y +=y;
	}
}

댓글