단축키
생성자 설정 : Ctrl + Space + Enter
Getter Setter 설정 : Alt + Shift + S --> getter 누른다.
package starcraft.ver01;
public class Zealot {
// 멤버 변수
private String name;
private int power;
private int hp;
// 생성자3
public Zealot(String name) {
this.name = name;
power = 5;
hp = 80;
} // end of Zealot()
// getter , setter 설정
// 단축키 : alt + shif + s --> getter 누른다.
// getter 메소드 MainTest 에서 보여주기 위해
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 attackZergling(Zergling z) { // 객체와 객체간의 상호작용
System.out.println(this.name + " 이 " + z.getName() + " 을 공격합니다.");
z.beAttacked(this.power);
}
// 질럿이 마린을 공격합니다.
public void attackMarine(Marine m) { // 객체와 객체간의 상호작용, 참조값을 받아와야 한다.
System.out.println(this.name + " 이 " + m.getName() + " 을 공격합니다.");
m.beAttacked(this.power);
}
// 자신이 공격을 당합니다.
public void beAttacked(int power) {
// hp = hp - power;
// 방어적 코드 작성
// 80 -- 72 == 5
// 5 - 75 == -70
if (hp <= 0) {
System.out.println("[" + this.name + "]" + "이미 사망하였습니다.");
hp = 0; // 체력이 0 밑으로 출력되는 것을 막기위해 0 으로 지정함
return; // 함수 종료
}
hp -= power;
}
public void showInfo() {
System.out.println("==== 상태창 ====");
System.out.println("이름 : " + this.name);
System.out.println("공격력 : " + this.power);
System.out.println("생명력 : " + this.hp);
}
}
package starcraft.ver01;
public class Marine {
// 멤버 변수
private String name;
private int power;
private int hp;
// 생성자
public Marine(String name) {
this.power = 5; // 공격력 설정
this.hp = 80; // 체력 설정
} // end of Marine()
// getter , setter 설정
// 단축키 : alt + shif + s --> getter 누른다.
// getter 메소드 MainTest 에서 보여주기 위해
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;
}
// 마린이 질럿을 공격합니다. attackZealot
public void attackZealot(Zealot z) {
System.out.println(this.name + " 이 " + z.getName() + " 을 공격합니다.");
z.beAttacked(this.power);
}
// 마린이 저글링을 공격합니다. attackZergling
public void attackZergling(Zergling z) {
System.out.println(this.name + " 이 " + z.getName() + " 을 공격합니다.");
z.beAttacked(this.power);
}
// 자신이 공격을 당합니다. beAttacked
public void beAttacked(int power) {
// hp = hp - power;
// 방어적 코드 작성
// 80 -- 72 == 5
// 5 - 75 == -70
if (hp <= 0) {
System.out.println("[" + this.name + "]" + "이미 사망하였습니다.");
hp = 0; // 체력이 0 밑으로 출력되는 것을 막기위해 0 으로 지정함
return; // 함수 종료
}
hp -= power;
}
public void showInfo() {
System.out.println("==== 상태창 ====");
System.out.println("이름 : " + this.name);
System.out.println("공격력 : " + this.power);
System.out.println("생명력 : " + this.hp);
}
}
package starcraft.ver01;
public class Zergling {
// 멤버 변수
private String name;
private int power;
private int hp;
public Zergling(String name) {
this.power = 5; // 공격력 설정
this.hp = 80; // 체력 설정
} // end of Zergling()
// getter , setter 설정
// 단축키 : alt + shif + s --> getter 누른다.
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;
}
// 저글링이 질럿을 공격합니다. attackZealot
public void attackZealot(Zealot z) {
System.out.println(this.name + "이 " + z.getName() + "을 공격합니다.");
z.beAttacked(power);
}
// 저글링이 마린을 공격합니다. attackZealot
public void attackMarine(Marine m) {
System.out.println(this.name + "이" + m.getName() + "을 공격합니다.");
m.beAttacked(power);
}
// 자신이 공격을 당합니다.
public void beAttacked(int power) {
// hp = hp - power;
// 방어적 코드 작성
// 80 -- 72 == 5
// 5 - 75 == -70
if (hp <= 0) {
System.out.println("[" + this.name + "]" + "이미 사망하였습니다.");
hp = 0; // 체력이 0 밑으로 출력되는 것을 막기위해 0 으로 지정함
return; // 함수 종료
}
hp -= power;
}
public void showInfo() {
System.out.println("==== 상태창 ====");
System.out.println("이름 : " + this.name);
System.out.println("공격력 : " + this.power);
System.out.println("생명력 : " + this.hp);
}
}
---------------------------------------------------------------------------------------------
DO -WHILE WHIL 문
// for문 : 횟수가 있는 반복일 경우
// while문 : 횟수가 정해저 있지 않는 반복일 경우
// while 문에는 while 문과, do - while 문이 있다.
// while --> 조건식을 확인하고 코드를 수행하는 녀석이다.
// do while --> 무조건 한번은 수행을 하고 다시 조건을 확인하는 녀석
do {
// 반복수행 구문
}while(/*조건식*/);
while(/*조건식*/) {
// 반복 수행 구문
}
-------------------------------------------------------------------------------------------------
package starcraft.ver01;
public class GateWay {
private int gateWayNumber;
private int count;
// 생성자
public GateWay(int gateWayNumber) {
this.gateWayNumber = gateWayNumber;
count = 0;
}
public int getCount() {
return count;
}
public int getGateWayNumber() {
return gateWayNumber;
}
// 기능 - 질럿 생산하는 기능을 만들어 보세요
public Zealot createZealot(String name) {
count++;
return new Zealot(name);
}
} // end of class
package starcraft.ver01;
import java.util.Scanner;
public class StarCraftTest2 {
public static void main(String[] args) {
final int ZEALOT = 1;
final int MARINE = 2;
final int ZERGLING = 3;
final int GAME_END = 0;
GateWay gateWay1 = new GateWay(1);
GateWay gateWay2 = new GateWay(2);
Zealot zealot1 = gateWay1.createZealot("질럿1");
Zealot zealot2 = gateWay2.createZealot("질럿2");
System.out.println(gateWay1.getGateWayNumber() + " : " +gateWay1.getCount());
System.out.println(gateWay2.getGateWayNumber() + " : " +gateWay2.getCount());
System.out.println("---------------------------");
Marine marine1 = new Marine("마린1");
Zergling zergling1 = new Zergling("저글링1");
Scanner sc = new Scanner(System.in);
int unitChoice = -1;
do {
System.out.println("유닛을 선택하세요");
System.out.println("1.질럿\t 2.마린\t 3.저글링\t 0.게임종료");
unitChoice = sc.nextInt();
if (unitChoice == ZEALOT) {
} else if(unitChoice == MARINE) {
} else if(unitChoice == ZERGLING) {
} else {
System.out.println("프로그램을 종료 합니다.");
unitChoice = GAME_END;
}
} while (unitChoice != GAME_END);
} // end of main
} // end of class'Java' 카테고리의 다른 글
| 2024.04.19 static 으로 숫자를 중복사용 하지 않는 방법 (0) | 2024.04.19 |
|---|---|
| 2024.04.18 static 변수 (0) | 2024.04.18 |
| 2024.04.18 this 3가지 사용 방법 (0) | 2024.04.18 |
| 2024.04.17 접근 제어 지시자 (0) | 2024.04.17 |
| 2024.04.16 객체지향 패러다임이란 (0) | 2024.04.16 |