Java/chapter2

ch13 Unit

낭구리 2021. 8. 30. 18:48
package ch13;

public class Zealot extends Unit {

	public Zealot(String name) {
		this.name = name;
		this.power = 10;
		this.hp = 100;

	}

}

 

 

package ch13;

public  class Zergling extends Unit {

	public Zergling(String name) {
		this.name = name;
		this.power = 5;
		this.hp = 100;
	}


}

 

 

package ch13;

public class Marine extends Unit {

	public Marine(String name) {
		this.name = name;
		this.power = 5;
		this.hp = 100;
	}


}

 

 

package ch13;

public abstract class Unit {

	protected String name;
	protected int power;
	protected int hp;



	public String getName() {
		return name;
	}

	public int getPower() {
		return power;
	}

	public int getHp() {
		return hp;
	}
//	public abstract void showInfo(); //추상메서드를 사용해서 강제성을 가지게된다
	//통신 모듈은 추상메서드로 만들게된다. API ATTP 구현하는 클래스를 여러개만들기때문
	
	public void showInfo() {
		System.out.println("====정보창(" + this.name + ")====");
		System.out.println("이름 : " + this.name);
		System.out.println("공격력 : " + this.power);
		System.out.println("체력 : " + this.hp);
		System.out.println("===========");
	}
	
	public void attack(Unit unit) {
		System.out.println(this.name + " 이 " + unit.getName() + " 을 공격 합니다.");
		unit.beAttacked(this.power);
	}

	public void beAttacked(int power) {
		if (this.hp == 0) {
			System.out.println("이미 사망하였습니다.");
		}else {
			this.hp -= power;
		}
	}
	
	
}

 

package ch13;

public abstract class MainTest1 extends Unit {
	public static void main(String[] args) {
		Zealot zealot1 = new Zealot("질럿1");
		
		Zergling zergling1 = new Zergling("저글링1");
	
		Marine marine1 = new Marine("마린1");
		
		zealot1.attack(marine1);
		zealot1.attack(marine1);
		zealot1.attack(marine1);
		
		marine1.showInfo();
		
		marine1.attack(zergling1);
		zergling1.showInfo();
		

	}
}

'Java > chapter2' 카테고리의 다른 글

ch13 GateWay(Unit연계)  (0) 2021.08.30
ch12 ArrayListMainTest  (0) 2021.08.27
ch12 Book(Array)  (0) 2021.08.27
ch11 MainTest2  (0) 2021.08.26
ch11 ArrayMainTest  (0) 2021.08.26