소프트웨어 공학에서 결과의 변경 업시 코드의 구조를 재조정하는 것
가독성을 높이고 유지보수를 편하게 할 수 있도록 함
import weapon.무기;
public class Player {
public String 이름;
private int hp;
private int power;
private int mp;
private String imgFile1;
private String imgFile2;
public 무기 전용무기;
public Player(String 이름, int hp, int power, int mp, String imgFile1, String imgFile2, 무기 전용무기) {
setHP(hp);
setPower(power);
setImgFile1(imgFile1);
setImgFile2(imgFile2);
setMP(mp);
this.이름 = 이름;
this.전용무기 = 전용무기;
}
// ...get/set 중략...
public void 공격(Player target) {
target.hp -= this.power;
}
public void 무기로공격(Player target) {
}
public Player() {}
}

import weapon.무기;
public class Player {
public String 이름;
private int hp;
private int power;
private int mp;
private String imgFile1;
private String imgFile2;
public 무기 전용무기;
public Player(String 이름, int hp, int power, int mp, String imgFile1, String imgFile2, 무기 전용무기) {
setHP(hp);
setPower(power);
setImgFile1(imgFile1);
setImgFile2(imgFile2);
setMP(mp);
this.이름 = 이름;
this.전용무기 = 전용무기;
}
//...get/set 중략...
public void 공격(Player target) {
target.hp -= this.power;
}
public void 무기로공격(Player target) {
int mymp = this.getMP();
int targethp = target.getHP();
mymp -= 전용무기.get필요mp();
targethp -= 전용무기.get데미지();
this.setMP(mymp);
target.setHP(targethp);
}
public Player() {}
}

→ 상속을 사용하면 각각의 자식 클래스에서 공통적으로 사용되는 필드나 메소드를 부모 클래스에서 한 번만 정의하면 코드를 간결하게 만들 수 있다!
JLabel 플레이어초상 = new JLabel("New label");
// 플레이어 생성
if (Math.random() * 10 < 3) {
플레이어초상.setIcon(new ImageIcon(Win.class.getResource("/image/" + 운장.getImgFile1())));
플레이어생성 = 0;
플레이어 = 운장;
} else if (Math.random() * 10 >= 8) {
플레이어초상.setIcon(new ImageIcon(Win.class.getResource("/image/" + 현덕.getImgFile1())));
플레이어생성 = 1;
플레이어 = 현덕;
} else if (Math.random() * 10 >= 3 && Math.random() <= 4) {
플레이어초상.setIcon(new ImageIcon(Win.class.getResource("/image/" + 맹덕.getImgFile1())));
플레이어생성 = 2;
플레이어 = 맹덕;
} else {
플레이어초상.setIcon(new ImageIcon(Win.class.getResource("/image/" + 익덕.getImgFile1())));
플레이어생성 = 3;
플레이어 = 익덕;
}
플레이어초상.setBounds(413, 435, 400, 400);
contentPane.add(플레이어초상);
JLabel 상대캐릭터초상 = new JLabel("New label");
// 상대 생성
if (Math.random() * 10 < 2 && 플레이어생성 != 0) {
상대캐릭터초상.setIcon(new ImageIcon(Win.class.getResource("/image/" + 운장.getImgFile1())));
상대 = 운장;
} else if (Math.random() * 10 >= 8 && 플레이어생성 != 1) {
상대캐릭터초상.setIcon(new ImageIcon(Win.class.getResource("/image/" + 현덕.getImgFile1())));
상대 = 현덕;
} else if (Math.random() * 10 >= 2 && Math.random() <= 4 && 플레이어생성 != 2) {
상대캐릭터초상.setIcon(new ImageIcon(Win.class.getResource("/image/" + 맹덕.getImgFile1())));
상대 = 맹덕;
} else if (플레이어생성 != 3) {
상대캐릭터초상.setIcon(new ImageIcon(Win.class.getResource("/image/" + 익덕.getImgFile1())));
상대 = 익덕;
}
상대캐릭터초상.setBounds(345, 10, 400, 400);
contentPane.add(상대캐릭터초상);
JLabel 플레이어초상 = new JLabel("New label");
// 플레이어 생성
double random1 = Math.random()*10;
if (random1 < 3) {
플레이어 = 운장;
} else if (random1 >= 8) {
플레이어 = 현덕;
} else if (random1 >= 3 && random1 <= 4) {
플레이어 = 맹덕;
} else 플레이어 = 익덕;
플레이어초상.setIcon(new ImageIcon(Win.class.getResource("/image/") + 플레이어.getImgFile1()));
플레이어초상.setBounds(413, 435, 400, 400);
contentPane.add(플레이어초상);
JLabel 상대캐릭터초상 = new JLabel("New label");
// 상대 생성
do {
double random2 = Math.random()*10;
if (random2 < 2) {
상대 = 운장;
} else if (random2 >= 8) {
상대 = 현덕;
} else if (random2 >= 2 && random2 <= 4) {
상대 = 맹덕;
} else
상대 = 익덕;
} while (플레이어 == 상대);
상대캐릭터초상.setIcon(new ImageIcon(Win.class.getResource("/image/") + 상대.getImgFile1()));
상대캐릭터초상.setBounds(345, 10, 400, 400);
contentPane.add(상대캐릭터초상);
→ 하나의 난수를 기준으로 캐릭터를 설정하고 싶다면 변수에 Math.random() 값을 저장해서 사용하면 된다는 것을 알게 되었다! 그리고 특정 조건일 때만 계속 반복하게 하고 싶으면 do-while을 사용하면 된다!