<aside> 💡 학습 목표
- 상속을 활용해서 코드를 작성해 보자.
- 오버로딩 문법을 적용해 보자.
- 다형성을 활용한 attack 메소드 작성해보기
</aside>
ver 0.0.3
일반적으로 버전 번호는 메이저.마이너.패치 형식으로 구성되며, 각 부분은 다음과 같은 의미를 가집니다
- 메이저 번호: 큰 변화나 새로운 기능 추가, 기존 버전과 호환되지 않을 수 있는 주요 업데이트.
- 마이너 번호: 새로운 기능 추가나 개선이 있지만, 기존 버전과 호환되는 업데이트.
- 패치 번호: 버그 수정, 성능 개선, 보안 패치 등 작은 규모의 업데이트.
package com.starcraft_v03;
public class Unit {
protected String name;
protected int power;
protected int hp;
public String getName() {
return name;
}
public int getPower() {
return power;
}
public int getHp() {
return hp;
}
public void attack(Unit unit) {
unit.beAttacked(this.power);
System.out.println(name + " 이 " + unit.getName() + "을 공격합니다.");
}
public void beAttacked(int power) {
if (this.hp <= 0) {
System.out.println(name + "이(가) 이미 사망하였습니다.");
//실행 제어권을 반납하고 싶으면 돌아가 코드!!
return;
}
this.hp -= power;
System.out.println(name + "(이)가 공격을 당합니다");
}
public void showInfo() {
System.out.println("-------✨상태창✨------");
System.out.println(" 이름 : " + name);
System.out.println(" 공겨력 : " + power);
System.out.println(" 체력 : " + hp);
}
}
// for (int i = 0; i < units.length; i++) {
// if (units[i] != null) {
// if (units[i] instanceof Marine) {
// ((Marine) units[i]).attack(units[i + 1]);
// ((Marine) units[i]).showInfo();
// } else if (units[i] instanceof Zealot) {
// ((Zealot) units[i]).attack(units[i + 1]);
// ((Zealot) units[i]).showInfo();
// } else if (units[i] instanceof Zergling) {
// if (i + 1 > units.length) {
// ((Zergling) units[i]).attack(units[i + 1]);
// } else {
// ((Zergling) units[i]).attack(units[i - 1]);
//
// }
// ((Zergling) units[i]).showInfo();
// }
// }
// }
//Unit[] units = new Unit[100];
//units[0] = new Zealot("지러1");
//units[1] = new Marine("마리1");
//units[2] = new Zergling("저그리1");
//units[3] = new Zergling("지러2");
//units[4] = new Zergling("마리2");
//units[5] = new Zergling("저그리2");
//units[6] = new Zergling("지러3");
//units[7] = new Zergling("마리3");
//units[8] = new Zergling("저그리3");
//units[9] = new Zergling("지러4");
package com.starcraft_v03;
public class Marine extends Unit{
// 생성자 1
public Marine(String name) {
this.name = name;
this.power = 3;
this.hp = 50;
}
}
package com.starcraft_v03;
/**
* @jang
*/
public class Zealot extends Unit {
public Zealot(String name) {
super.name = name;
this.power = 5;
this.hp = 80;
}
}
package com.starcraft_v03;
public class Zergling extends Unit {
// 생성자 1
public Zergling(String name) {
this.name = name;
this.power = 3;
this.hp = 50;
}
}
'JAVA 객체 지향 핵심' 카테고리의 다른 글
(JAVA)인터페이스(interface) - 19 (0) | 2025.04.23 |
---|---|
(JAVA) 추상 클래스(abstract class) - 18 (0) | 2025.04.23 |
(JAVA)다형성(Polymorphism) - 17 (0) | 2025.04.22 |
(JAVA)연관관계(Association) - 16 (1) | 2025.04.22 |
(JAVA)Composition(포함 관계) - 15 (2) | 2025.04.21 |