복습
https://shins99.tistory.com/32
[JAVA] 상속
복습 https://shins99.tistory.com/25 [자바 기초] 배열 복습 https://shins99.tistory.com/23 [자바 기초] static 복습 https://shins99.tistory.com/21 [자바 기초] C R U D 의 개념 복습 https://shins99.tistory.com/20 [자바 기초] this
shins99.tistory.com
💡 학습 목표
배웠던 내용 활용해보기
1. 상속 활용하기
2. 메서드 오버로딩에 활용
3. 오버라이드 활용
public class Unit {
protected String name;
protected int power;
protected int hp;
public Unit(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPower() {
return power;
}
public void setPower(int power) {
this.power = power;
}
public int getHp() {
return hp;
}
public void setHp(int hp) {
this.hp = hp;
}
public void attack(Zealot zealot) {
zealot.beAttacked(power);
System.out.println(name + "이 " + zealot.getName() + "을 공격 합니다");
}
public void attack(Marine marine) {
marine.beAttacked(power);
System.out.println(name + "이 " + marine.getName() + "을 공격 합니다");
}
public void attack(Zergling zergling) {
zergling.beAttacked(power);
System.out.println(name + "이 " + zergling.getName() + "을 공격 합니다");
}
public void beAttacked(int power) {
if (hp <= 0) {
System.out.println(name + "이 이미 사망 하였습니다.");
return;
}
hp -= power;
}
public void showInfo() {
System.out.println("=== 상태 창 ===");
System.out.println("이름 : " + name);
System.out.println("공격력 : " + power);
System.out.println("체력 : " + hp);
}
}
// 질럿 설계
public class Zealot extends Unit {
public Zealot(String name) {
super(name);
super.power = 5;
super.hp = 80;
}
}
// 저글링 설계
public class Zergling extends Unit {
public Zergling(String name) {
super(name);
super.power = 2;
super.hp = 45;
}
}
// 마린 설계
public class Marine extends Unit {
public Marine(String name) {
super(name);
super.power = 3;
super.hp = 50;
}
}
package starcraft;
import java.util.Scanner;
public class UnitMainTest {
public static void main(String[] args) {
int ZEALOT = 1;
int MARINE = 2;
int ZERGLING = 3;
int GAME_END = 4;
Zealot zealot1 = new Zealot("질럿");
Marine marine1 = new Marine("마린");
Zergling zergling1 = new Zergling("저글링");
Scanner sc = new Scanner(System.in);
zealot1.attack(marine1);
marine1.attack(zealot1);
zergling1.attack(zealot1);
zealot1.showInfo();
실행결과
질럿이 마린을 공격 합니다
마린이 질럿을 공격 합니다
저글링이 질럿을 공격 합니다
=== 상태 창 ===
이름 : 질럿
공격력 : 5
체력 : 75
'[JAVA] > [자바 기초]' 카테고리의 다른 글
[자바 기초] 추상 클래스 (0) | 2023.08.10 |
---|---|
[자바 기초] 다형성 (1) | 2023.08.09 |
[자바 기초] 상속 (0) | 2023.08.07 |
[자바 기초] 책 스토어 프로그램 만들어보기 C R U D (0) | 2023.08.04 |
[자바 기초] 배열 (0) | 2023.08.04 |