[JAVA] 12. 상속 Ⅲ. 메서드 오버라이딩

최재원's avatar
Feb 14, 2025
[JAVA] 12. 상속 Ⅲ. 메서드 오버라이딩
부모의 메서드를 자식이 재정의 하면!!
  • 부모의 메서드를 호출하는 순간 부모의 메서드가 무효화(오버라이드) 되고
  • 자식의 메서드가 호출된다. (동적바인딩)
notion image

1. 오버로딩으로 게임 만들기

DarkTempler (hp=100, power=70)
Arkan (hp=100, power=70)
 
5가지 유닛이 서로 다 공격할 수 있게 attack() 구현하기
 
생성
→ 질럿2
→ 드라군2
→ 다크템플러2
→ 리버2
→ 아칸2 공격 → 질럿이 드라군 공격 hp 확인
→ 질럿이 다크템플러 공격 hp 확인
→ 리버가 아칸 공격 hp 확인
→ 아칸 리버 공격 hp 확인
→ 드라군이 다크템플러 공격 hp 확인
→ 리버가 리버 공격 hp 확인
아래의 예제를 사용해 코드를 완성하시오.
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); } }

2. 오버로딩의 한계

package ex05.ch03; class Protoss { } class Dragoon { int hp; int power; public Dragoon() { this.hp = 100; this.power = 10; } 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(Arkan unit) { unit.hp = unit.hp - this.power; } public void attack(DarkTempler unit) { unit.hp = unit.hp - this.power; } } class Zealot { 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(Arkan unit) { unit.hp = unit.hp - this.power; } public void attack(DarkTempler unit) { unit.hp = unit.hp - this.power; } } class River { int hp; int power; public River() { this.hp = 100; this.power = 50; } 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(Arkan unit) { unit.hp = unit.hp - this.power; } public void attack(DarkTempler unit) { unit.hp = unit.hp - this.power; } } class Arkan { 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(Arkan unit) { unit.hp = unit.hp - this.power; } public void attack(DarkTempler unit) { unit.hp = unit.hp - this.power; } }; class DarkTempler { 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(Arkan unit) { unit.hp = unit.hp - this.power; } public void attack(DarkTempler 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(); Dragoon d2 = new Dragoon(); River r1 = new River(); River r2 = new River(); Arkan ar1 = new Arkan(); Arkan ar2 = new Arkan(); DarkTempler dt1 = new DarkTempler(); DarkTempler dt2 = new DarkTempler(); z1.attack(d1); System.out.println("z1질럿->d1드라군 | z1질럿<hp>:" + z1.hp + " / d1드라군<hp>:" + d1.hp + "(↓" + z1.power + ")"); z2.attack(dt1); System.out.println("z2질럿->dt1다크템플러 | z2질럿<hp>:" + z2.hp + " / dt1다크템플러<hp>:" + dt1.hp + "(↓" + z2.power + ")"); r1.attack(ar1); System.out.println("r1리버->ar1아칸 | r1리버<hp>:" + r1.hp + " / ar1아칸<hp>:" + ar1.hp + "(↓" + r1.power + ")"); ar2.attack(r2); System.out.println("ar2아칸->r2리버 | ar2아칸<hp>: " + ar2.hp + " / r2리버<hp>:" + r2.hp + "(↓" + ar2.power + ")"); d2.attack(dt2); System.out.println("d2드라군->dt2다크템플러 | d2드라군<hp>: " + d2.hp + " / dt2다크템플러<hp>:" + dt2.hp + "(↓" + d2.power + ")"); r1.attack(r2); System.out.println("r1리버->r2리버 | r1리버<hp>: " + r1.hp + " / r2리버<hp>:" + r2.hp + "(↓" + r1.power + ")"); } }
notion image

3. 오버라이딩으로 해결

package ex05.ch04; abstract class Protoss { abstract int getHp(); abstract void setHp(int hp); abstract int getPower(); abstract void attack(Protoss protoss); } class Dragoon extends Protoss { private int hp; private int power; public Dragoon() { this.hp = 100; this.power = 10; } public int getHp() { return this.hp; } public void setHp(int hp) { this.hp = hp; } public int getPower() { return this.power; } public void attack(Protoss protoss) { protoss.setHp(protoss.getHp() - this.power); } } class Zealot extends Protoss { private int hp; private int power; public Zealot() { this.hp = 100; this.power = 20; } public int getHp() { return this.hp; } public void setHp(int hp) { this.hp = hp; } public int getPower() { return this.power; } public void attack(Protoss protoss) { protoss.setHp(protoss.getHp() - this.power); } } class River extends Protoss { private int hp; private int power; public River() { this.hp = 100; this.power = 50; } public int getHp() { return this.hp; } public void setHp(int hp) { this.hp = hp; } public int getPower() { return this.power; } public void attack(Protoss protoss) { protoss.setHp(protoss.getHp() - this.power); } } class Arkan extends Protoss { private int hp; private int power; public Arkan() { this.hp = 100; this.power = 70; } public int getHp() { return this.hp; } public void setHp(int hp) { this.hp = hp; } public int getPower() { return this.power; } public void attack(Protoss protoss) { protoss.setHp(protoss.getHp() - this.power); } }; class DarkTempler extends Protoss { private int hp; private int power; public DarkTempler() { this.hp = 100; this.power = 70; } public int getHp() { return this.hp; } public void setHp(int hp) { this.hp = hp; } public int getPower() { return this.power; } public void attack(Protoss protoss) { protoss.setHp(protoss.getHp() - this.power); } } public class StarGame { public static void main(String[] args) { Protoss z1 = new Zealot(); Protoss z2 = new Zealot(); Protoss d1 = new Dragoon(); Protoss d2 = new Dragoon(); Protoss r1 = new River(); Protoss r2 = new River(); Protoss ar1 = new Arkan(); Protoss ar2 = new Arkan(); Protoss dt1 = new DarkTempler(); Protoss dt2 = new DarkTempler(); z1.attack(d1); System.out.println("z1질럿->d1드라군 | z1질럿<hp>:" + z1.getHp() + " / d1드라군<hp>:" + d1.getHp() + "(↓" + z1.getPower() + ")"); z2.attack(dt1); System.out.println("z2질럿->dt1다크템플러 | z2질럿<hp>:" + z2.getHp() + " / dt1다크템플러<hp>:" + dt1.getHp() + "(↓" + z2.getPower() + ")"); r1.attack(ar1); System.out.println("r1리버->ar1아칸 | r1리버<hp>:" + r1.getHp() + " / ar1아칸<hp>:" + ar1.getHp() + "(↓" + r1.getPower() + ")"); ar2.attack(r2); System.out.println("ar2아칸->r2리버 | ar2아칸<hp>: " + ar2.getHp() + " / r2리버<hp>:" + r2.getHp() + "(↓" + ar2.getPower() + ")"); d2.attack(dt2); System.out.println("d2드라군->dt2다크템플러 | d2드라군<hp>: " + d2.getHp() + " / dt2다크템플러<hp>:" + dt2.getHp() + "(↓" + d2.getPower() + ")"); r1.attack(r2); System.out.println("r1리버->r2리버 | r1리버<hp>: " + r1.getHp() + " / r2리버<hp>:" + r2.getHp() + "(↓" + r1.getPower() + ")"); }
notion image

Share article

jjack1