Contents
1. 오버로딩으로 게임 만들기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 Dragoon {
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(Archon unit) {
unit.hp = unit.hp - this.power;
}
public void attack(DarkTemplar 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(Archon unit) {
unit.hp = unit.hp - this.power;
}
public void attack(DarkTemplar 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(Archon unit) {
unit.hp = unit.hp - this.power;
}
public void attack(DarkTemplar unit) {
unit.hp = unit.hp - this.power;
}
}
class Archon {
int hp;
int power;
public Archon() {
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(Archon unit) {
unit.hp = unit.hp - this.power;
}
public void attack(DarkTemplar unit) {
unit.hp = unit.hp - this.power;
}
}
class DarkTemplar {
int hp;
int power;
public DarkTemplar() {
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(Archon unit) {
unit.hp = unit.hp - this.power;
}
public void attack(DarkTemplar 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();
DarkTemplar d3 = new DarkTemplar();
DarkTemplar d4 = new DarkTemplar();
River r1 = new River();
River r2 = new River();
Archon a1 = new Archon();
Archon a2 = new Archon();
z1.attack(d1);
z1.attack(d3);
r1.attack(a1);
a1.attack(r1);
d1.attack(d4);
r1.attack(r2);
System.out.println("드라군의 hp : " + d1.hp);
System.out.println("다크템플러의 hp : " + d3.hp);
System.out.println("아칸의 hp : " + a1.hp);
System.out.println("리버의 hp : " + r1.hp);
System.out.println("다크템플러의 hp : " + d4.hp);
System.out.println("리버의 hp : " + r2.hp);
}
}
Share article