캡슐화란
특징
- 모든 멤버 변수의 접근제어자를 private으로 한다.
- 감춰진 멤버변수 값을 확인하기 위해서는 메서드를 이용해야한다.
- 감춰진 멤버변수 값을 받기 위해서는 getter메서드가 필요하다.
- 감춰진 멤버변수에 값을 부여하고 싶다면 setter()메서드가 필요하다.
- 메서드를 이용함으로서 로그를 남기고 그걸로 데이터 조작을 방지한다.
예제 코드
class Drink {
private int num;
private String name;
private int price;
private int cnt;
public Drink(int num, String name, int price) {
this.num = num;
this.name = name;
this.price = price;
this.cnt = 5;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public int getCnt() {
return cnt;
}
public void setCnt(int cnt) {
this.cnt = cnt;
}
}