JAVA

221014 abstract 실습 유닛

주영재 2022. 10. 14. 11:55

부모

package quiz14;

public abstract class Unit {//부모클래스
	public int x;
	public int y;
	public int hp;
	

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

 

자식

package quiz14;

public class Marine extends Unit{
	
	public static int attack = 6;
	public static int armor = 0;
	
	
	public Marine(){
		super(60);
	}
	
	
	@Override
	public void location() {
		System.out.println("현재 x좌표:" +x+", 현재 y좌표:"+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좌표:" +x+", 현재 y좌표:"+y);
	}
	@Override
	public void move(int x, int y) {
		this.x=x;
		this.y=y;
	}
	
	
	public void changeMod() {
		if(mode) {
			mode=false;
		} else {
			mode=true;
		}
		System.out.println("공격모드 변경");
	}

}

 

package quiz14;

public class DropShip extends Unit{
	
	public DropShip(){
		super(60);
	}
	
	@Override
	public void location() {
		System.out.println("현재 x좌표:" +x+", 현재 y좌표:"+y);
	}
	@Override
	public void move(int x, int y) {
		this.x=x;
		this.y=y;
	}
	

}

 

 

main

package quiz14;

public class MainClass {
	public static void main(String[] args) {
		Marine m = new Marine();
		m.location();
		m.move(2, 3);
		m.location();
		m.stop();
		System.out.println(m.hp);
		System.out.println(Marine.attack);
		System.out.println(Marine.armor);
		
		
		Marine m2 = new Marine();
		System.out.println(m2.attack);
		System.out.println(m2.armor);
		
		Tank t = new Tank();
		System.out.println(t.hp);
		t.location();
		t.move(5, 5);
		t.location();
		t.stop();
		t.changeMod();
		
		
		DropShip d = new DropShip();
		System.out.println(d.hp);
		d.location();
		d.move(10, 21);
		d.location();
		d.stop();
		
		
	}

}
현재 x좌표:0, 현재 y좌표:0
현재 x좌표:2, 현재 y좌표:3
현재 위치에 정지
60
6
0
6
0
100
현재 x좌표:0, 현재 y좌표:0
현재 x좌표:5, 현재 y좌표:5
현재 위치에 정지
공격모드 변경
60
현재 x좌표:0, 현재 y좌표:0
현재 x좌표:10, 현재 y좌표:21
현재 위치에 정지

특별히 어렵지 않은 문제. static메서드를 클래스이름으로 호출할 수 있다는 것만 다시보기.