๐ 10์ฅ ์บก์ํ(Encapsulation)
๋ฉค๋ฒ ๋ณ์ ์ก์ธ์ค ์ ์ด
์ ๊ทผ ์ง์ ์ (access modifier)

HP๋ฅผ private๋ก ์ง์
๐ก
์์ฑ์, ๋ฉ์๋๋ ์ผ๋ฐ์ ์ผ๋ก ๋ค๋ฅธ ์ฝ๋์์ ํ์ฉํ๊ธฐ ์ํด ์ฃผ๋ก public์ผ๋ก ์ง์ ํ๋ค.

๐ก
+ : public
- : private
~ : default
๋ฉค๋ฒ ์ก์ธ์ค ์ง์ ์ ์
๐ก
ํ๋ ์์ฑ์ ์ฃผ๋ก private
๋ฉ์๋๋ ์ฃผ๋ก public
* static ๋ณ์๋ public์ ์ฌ์ฉ
getter์ setter
- ๋ชจ๋ ํ๋๋ฅผ private๋ก ์ง์ ํด์ ๋ค๋ฅธ ํด๋์ค๋ก๋ถํฐ ์ ๊ทผ์ด ์ ๋๋๋ก ๋ง๋๋ค.
- ๋ฐ๋ผ์, ๋ฉ์๋๋ฅผ ํตํด์ ๋ฉค๋ฒ ๋ณ์์ ์ ๊ทผ ํ๋๋ก ํด๋์ค๋ฅผ ์ค๊ณํ๋ ๊ฒ์ด ๊ธฐ๋ณธ์ด๋ค!

King ํด๋์ค์ talk() ๋ฉ์๋๋ฅผ ์์


์บก์ํ ์ ๊ณผ ํ ๋น๊ต


getter / setter ํ์ฉ
๐ก
1. Read Only, Write Only์ ํ๋์ ์คํ
2. ํ๋์ ์ด๋ฆ ๋ฑ, ํด๋์ค์ ๋ด๋ถ ์ค๊ณ๋ฅผ ์์ ๋กญ๊ฒ ๋ณ๊ฒฝ์ด ๊ฐ๋ฅํ๋ค.
3. ํ๋๋ก์ ์ก์ธ์ค๋ฅผ ๊ฒ์ฌ ๊ฐ๋ฅ
๐ก
ํด๋์ค์ ๋ํ ์ก์ธ์ค ์ ์ด

๐ก
ํด๋์ค ๋ํ ๋ค๋ฅธ ์ฝ๋์์ ํ์ฉํ๊ธฐ ์ํด ์ฃผ๋ก ์ฌ์ฉํ๋ฏ๋ก, public์ ์ฌ์ฉํ๋ค.
๐ก
์๊ณ ๋ฆฌ์ฆ ๋ฌธ์ ํ ๋, class๋ฅผ ๊ฐ์ ํ์ผ ๋ด ์ฌ๋ฌ๊ฐ ๋ง๋ค๊ธฐ ์ํด package private๋ฅผ ์ฌ์ฉํ ์๋ ์๋ค. (๊ทธ ์ธ์๋ ์ ์์ฐ์)
๋น public ํด๋์ค์ ํน์ง
1. ํด๋์ค๋ช
์ ์์ค ํ์ผ๋ช
๊ณผ ๋ฌ๋ผ๋ ๋๋ค.
2. ํ ์์ค ํ์ผ์ ์ฌ๋ฌ ํด๋์ค ์ ์ธ ๊ฐ๋ฅ
๐์ฐ์ต ๋ฌธ์
10-1

package Exam;
public class Wizard {
private int hp;
private int mp;
private String name;
private Wand wand;
public void heal(Hero hero) {
int basePoint = 10; // ๊ธฐ๋ณธ ํ๋ณต ํฌ์ธํธ
int recovPoint = (int) (basePoint * this.wand.power); // ์งํก์ด์ ์ํ ์ฆํญ
hero.setHp(hero.getHp() + recovPoint); // ์ฉ์ฌ์ HP๋ฅผ ํ๋ณต
}
}
package Exam;
public class Wand {
private String name; // ์งํก์ด ์ด๋ฆ
private double power; // ์งํก์ด ๋ง๋ ฅ
}
10-2

package Exam;
public class Wizard {
private int hp;
private int mp;
private String name;
private Wand wand;
public int getHp() {
return hp;
}
public int getMp() {
return mp;
}
public String getName() {
return name;
}
public Wand getWand() {
return wand;
}
public void setHp(int hp) {
this.hp = hp;
}
public void setMp(int mp) {
this.mp = mp;
}
public void setName(String name) {
this.name = name;
}
public void setWand(Wand wand) {
this.wand = wand;
}
public void heal(Hero hero) {
int basePoint = 10; // ๊ธฐ๋ณธ ํ๋ณต ํฌ์ธํธ
int recovPoint = (int) (basePoint * this.wand.getPower()); // ์งํก์ด์ ์ํ ์ฆํญ
hero.setHp(hero.getHp() + recovPoint); // ์ฉ์ฌ์ HP๋ฅผ ํ๋ณต
}
}
package Exam;
public class Wand {
private String name; // ์งํก์ด ์ด๋ฆ
double power; // ์งํก์ด ๋ง๋ ฅ
public double getPower() {
return power;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void setPower(double power) {
this.power = power;
}
}
10-3

package Exam;
public class Wizard {
private int hp;
private int mp;
private String name;
private Wand wand;
public int getHp() {
return hp;
}
public int getMp() {
return mp;
}
public String getName() {
return name;
}
public Wand getWand() {
return wand;
}
public void setHp(int hp) {
if (hp < 0) {
this.hp = 0;
}
this.hp = hp;
}
public void setMp(int mp) {
if (mp < 0) {
throw new IllegalArgumentException("๋ง๋ฒ์ฌ์ MP๋ 0 ์ด์์ด์ด์ผ ํฉ๋๋ค.");
}
this.mp = mp;
}
public void setName(String name) {
if (name == null || name.length() < 3) {
throw new IllegalArgumentException("์ด๋ฆ์ null ๋ถ๊ฐ๋ฅ, 3๊ธ์ ์ด์");
} else {
this.name = name;
}
}
public void setWand(Wand wand) {
if (wand == null) {
throw new IllegalArgumentException("์๋๋ null ๋ถ๊ฐ๋ฅ");
}
this.wand = wand;
}
public void heal(Hero hero) {
int basePoint = 10; // ๊ธฐ๋ณธ ํ๋ณต ํฌ์ธํธ
int recovPoint = (int) (basePoint * this.wand.getPower()); // ์งํก์ด์ ์ํ ์ฆํญ
hero.setHp(hero.getHp() + recovPoint); // ์ฉ์ฌ์ HP๋ฅผ ํ๋ณต
}
}
package Exam;
public class Wand {
private String name; // ์งํก์ด ์ด๋ฆ
private double power; // ์งํก์ด ๋ง๋ ฅ
public double getPower() {
return power;
}
public String getName() {
return name;
}
public void setName(String name) {
if (name == null || name.length() < 3) {
throw new IllegalArgumentException("์ด๋ฆ์ null ๋ถ๊ฐ๋ฅ, 3๊ธ์ ์ด์");
} else {
this.name = name;
}
}
public void setPower(double power) {
if (0.5 <= power && power <= 100.0) {
this.power = power;
} else {
throw new IllegalArgumentException("๋ง๋ ฅ์ 0.5 ์ด์ 100.0 ์ดํ๋ง ๊ฐ๋ฅ");
}
}
}
package Exam;
public class Main {
public static void main(String[] args) {
Wizard wizard = new Wizard();
Wand wand = new Wand();
wand.setName("๋๋ฌด ์งํก์ด");
wand.setPower(10.5);
wizard.setHp(50);
wizard.setMp(10);
wizard.setName("ํด๋ฆฌํฌํฐ");
wizard.setWand(wand);
System.out.println("์บ๋ฆญํฐ์ด๋ฆ : " + wizard.getName() + ", HP: " + wizard.getHp() + ", MP: " + wizard.getMp());
System.out.println("์ฅ์ฐฉํ ์๋: " + wizard.getWand().getName() + ", ํ์ : " + wizard.getWand().getPower());
}
}
Uploaded by N2T