[JAVA] 47. 오버로딩의 한계

문정준's avatar
Feb 17, 2025
[JAVA] 47. 오버로딩의 한계
package ex05.ch03; class Protoss { } class River { int hp; int power; public River() { this.hp = 100; this.power = 50; } public void attack(River unit) { unit.hp = unit.hp - this.power; } public void attack(Zealot unit) { unit.hp = unit.hp - this.power; } public void attack(Dragoon unit) { unit.hp = unit.hp - this.power; } } class Dragoon { int hp; int power; public Dragoon() { this.hp = 100; this.power = 10; } // 기존 오브젝트 수정 public void attack(River unit) { unit.hp = unit.hp - this.power; } public void attack(Zealot unit) { unit.hp = unit.hp - this.power; } public void attack(Dragoon unit) { unit.hp = unit.hp - this.power; } } class Zealot { int hp; int power; public Zealot() { this.hp = 100; this.power = 20; } // 기존 오브젝트 수정 public void attack(River unit) { unit.hp = unit.hp - this.power; } public void attack(Dragoon unit) { unit.hp = unit.hp - this.power; } public void attack(Zealot unit) { unit.hp = unit.hp - this.power; } } public class StarGame { public static void main(String[] args) { Zealot z1 = new Zealot(); Zealot z2 = new Zealot(); Dragoon d1 = new Dragoon(); z1.attack(d1); System.out.println("드라군 d1의 hp : " + d1.hp); z1.attack(z2); System.out.println("질럿 z2의 hp : " + z2.hp); } }
Share article

sxias