[Java] 54. 오버라이딩의 한계

백하림's avatar
Feb 14, 2025
[Java] 54. 오버라이딩의 한계
package ex05.ch03; class Protoss { public int attack() { return 0; } } class River extends Protoss { int hp; int power; public River() { this.hp = 100; this.power = 50; } public void attack(Zealot unit) { unit.hp = unit.hp - this.power; } public void attack(Dragoon unit) { unit.hp = unit.hp - this.power; } public void attack(River unit) { unit.hp = unit.hp - this.power; } public void attack(DarkTempler unit) { unit.hp = unit.hp - this.power; } public void attack(Arkan unit) { unit.hp = unit.hp - this.power; } public int getHp() { return hp; } public void setHp(int hp) { this.hp = hp; } } class Dragoon extends Protoss { int hp; int power; public Dragoon() { this.hp = 100; this.power = 10; } public void attack(Zealot unit) { unit.hp = unit.hp - this.power; } public void attack(Dragoon unit) { unit.hp = unit.hp - this.power; } public void attack(River unit) { unit.hp = unit.hp - this.power; } public void attack(DarkTempler unit) { unit.hp = unit.hp - this.power; } public void attack(Arkan unit) { unit.hp = unit.hp - this.power; } public int getHp() { return hp; } public void setHp(int hp) { this.hp = hp; } } class Zealot extends Protoss { int hp; int power; public Zealot() { this.hp = 100; this.power = 20; } public void attack(Dragoon unit) { unit.hp = unit.hp - this.power; } public void attack(Zealot unit) { unit.hp = unit.hp - this.power; } public void attack(River unit) { unit.hp = unit.hp - this.power; } public void attack(DarkTempler unit) { unit.hp = unit.hp - this.power; } public void attack(Arkan unit) { unit.hp = unit.hp - this.power; } public int getHp() { return hp; } public void setHp(int hp) { this.hp = hp; } } class DarkTempler extends Protoss { int hp; int power; public DarkTempler() { this.hp = 100; this.power = 70; } public void attack(Dragoon unit) { unit.hp = unit.hp - this.power; } public void attack(Zealot unit) { unit.hp = unit.hp - this.power; } public void attack(River unit) { unit.hp = unit.hp - this.power; } public void attack(DarkTempler unit) { unit.hp = unit.hp - this.power; } public void attack(Arkan unit) { unit.hp = unit.hp - this.power; } public int getHp() { return hp; } public void setHp(int hp) { this.hp = hp; } } class Arkan extends Protoss { int hp; int power; public Arkan() { this.hp = 100; this.power = 70; } public void attack(Dragoon unit) { unit.hp = unit.hp - this.power; } public void attack(Zealot unit) { unit.hp = unit.hp - this.power; } public void attack(River unit) { unit.hp = unit.hp - this.power; } public void attack(DarkTempler unit) { unit.hp = unit.hp - this.power; } public void attack(Arkan unit) { unit.hp = unit.hp - this.power; } public int getHp() { return hp; } public void setHp(int hp) { this.hp = hp; } } public class StarGame { public static void main(String[] args) { Zealot z1 = new Zealot(); Zealot z2 = new Zealot(); Dragoon d1 = new Dragoon(); Dragoon d2 = new Dragoon(); River r1 = new River(); River r2 = new River(); DarkTempler dt1 = new DarkTempler(); DarkTempler dt2 = new DarkTempler(); Arkan ar1 = new Arkan(); Arkan ar2 = new Arkan(); // z1.attack(d1); // System.out.println("드라군d1의 hp : " + d1.hp); // // z1.attack(z2); // System.out.println("질럿z2 hp : " + z2.hp); //공격 //→ 질럿이 드라군 공격 hp 확인 z1.attack(d1); System.out.println("드라군d1 hp : " + d1.hp); //→ 질럿이 다크템플러 공격 hp 확인 z1.attack(dt1); System.out.println("다크템플러dt1 hp : " + dt1.hp); //→ 리버가 아칸 공격 hp 확인 r1.attack(ar1); System.out.println("아칸ar1 hp : " + ar1.hp); //→ 아칸 리버 공격 hp 확인 ar1.attack(r1); System.out.println("리버r1 hp : " + r1.hp); //→ 드라군이 다크템플러 공격 hp 확인 d1.attack(dt1); System.out.println("다크템플러dt1 hp : " + dt1.hp); //→ 리버가 리버 공격 hp 확인 r1.attack(r2); System.out.println("리버r2 hp : " + r2.hp); } }
notion image
Share article

harimmon