Java/Excercise

inheritance ch02 Hero

낭구리 2021. 8. 27. 17:52
package ch02;

public class Hero {

	String name;
	int hp;
	public Hero(String name, int hp) {
		this.name = name;
		this.hp = hp;
	} //생성자를 호출해주어야한다.
	
	public void attack() {
		System.out.println("기본공격합니다.");
	}
}

 

package ch02;

public class Warrior extends Hero {

	public Warrior(String name, int hp) {
		super(name, hp);//부모라는뜻 부모가 메모리에 있어야 호출이 가능하다.
	}

	public void comboAttack() {
		System.out.println("2단 공격입니다.");
	}
	
}

 

package ch02;

public class Wizard extends Hero {

	public Wizard(String name, int hp) {
		super(name, hp);
	}

	public void frezzing() {
		System.out.println("얼음 공격입니다.");
	}
}

 

package ch02;

public class Archer extends Hero {
	

	public Archer(String name, int hp) {
		super(name, hp);
	}

	public void fireArrow() {
		System.out.println("불화살공격입니다.");
	}

}

 

package ch02;

public class HeroMainTest {

	public static void main(String[] args) {

		Warrior warrior = new Warrior("전사1", 100);
		Archer archer = new Archer("궁수1", 100);
		Wizard wizard = new Wizard("마법사1", 100);
		
		warrior.attack();
		warrior.comboAttack();
		
		archer.attack();
		archer.fireArrow();
		
		wizard.attack();
		wizard.frezzing();
	}

}