[자바 (JAVA)] 37. 오버 로딩의 한계

귤's avatar
Feb 14, 2025
[자바 (JAVA)] 37. 오버 로딩의 한계
package test; /* DarkTempler (hp=100, power=70) Arkan (hp=100, power=70) 5가지 유닛이 서로 다 공격할 수 있게 attack() 구현하기 생성 → 질럿2 → 드라군2 → 다크템플러2 → 리버2 → 아칸2 공격 → 질럿이 드라군 공격 hp 확인 → 질럿이 다크템플러 공격 hp 확인 → 리버가 아칸 공격 hp 확인 → 아칸 리버 공격 hp 확인 → 드라군이 다크템플러 공격 hp 확인 → 리버가 리버 공격 hp 확인 */ class Protoss { } // 1. 5가지 유닛이 서로 다 공격할 수 있게 attack() 구현하기 (아칸 -> 다크템플러 -> 리버 -> 드라군 -> 질럿 순서로 작성함) class Arkan { int hp; int power; public Arkan() { this.hp = 100; this.power = 70; } 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; } public void attack(DarkTempler unit) { unit.hp = unit.hp - this.power; } public void attack(Arkan unit) { unit.hp = unit.hp - this.power; } } class DarkTempler { int hp; int power; public DarkTempler() { this.hp = 100; this.power = 70; } 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; } public void attack(DarkTempler unit) { unit.hp = unit.hp - this.power; } public void attack(Arkan unit) { unit.hp = unit.hp - this.power; } } 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; } public void attack(DarkTempler unit) { unit.hp = unit.hp - this.power; } public void attack(Arkan unit) { unit.hp = unit.hp - this.power; } } class Dragoon { int hp; int power; public Dragoon() { this.hp = 100; this.power = 10; } // 기존 오브젝트 수정 public void attack(DarkTempler unit) { unit.hp = unit.hp - this.power; } public void attack(Arkan unit) { unit.hp = unit.hp - this.power; } 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(DarkTempler unit) { unit.hp = unit.hp - this.power; } public void attack(Arkan unit) { unit.hp = unit.hp - this.power; } 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 Test { public static void main(String[] args) { // 생성 ======================================== // 질럿2 Zealot z1 = new Zealot(); Zealot z2 = new Zealot(); // 드라군2 Dragoon d1 = new Dragoon(); Dragoon d2 = new Dragoon(); // 다크템플러2 DarkTempler dt1 = new DarkTempler(); DarkTempler dt2 = new DarkTempler(); // 리버2 River r1 = new River(); River r2 = new River(); // 아칸2 Arkan a1 = new Arkan(); Arkan a2 = new Arkan(); // 공격 ======================================== // 1. 질럿이 드라군 공격 hp 확인 z1.attack(d1); System.out.println("드라군 d1의 hp : " + d1.hp); // 2. 질럿이 다크템플러 공격 hp 확인 z1.attack(dt1); System.out.println("다크템플러 dt1의 hp : " + dt1.hp); // 3. 리버가 아칸 공격 hp 확인 r1.attack(a1); System.out.println("아칸 a1의 hp : " + a1.hp); // 4. 아칸 리버 공격 hp 확인 a1.attack(r1); System.out.println("리버 r1의 hp : " + r1.hp); // 5. 드라군이 다크템플러 공격 hp 확인 d1.attack(dt1); System.out.println("다크템플러 dt1의 hp : " + dt1.hp); // 6. 리버가 리버 공격 hp 확인 r1.attack(r2); System.out.println("리버 r2의 hp : " + r2.hp); } }
notion image
Share article

gyul