3 Concept of "OOP"(Object-Oriented Program)
1. Encapsulation
-캡슐화. 은닉화.
적절한 접근 지정자 설정과 'method내에서의 적절한 유효성검사'로 구현할 수 있다.
*Access Modifier(접근 지정자) : private < (default) < protected < public
형식 상으로는 class 앞의 접근 지정자는 default, public이 가능하고,
data와 method 앞에는 4가지가 모두 가능하다.
그러나 캡슐화를 구현하기 위해서는 아래의 규칙을 따르는 것이 좋다.
data : 가능한한 항상 private으로 설정
method : public, protected, private, (default)
class MyProfile{
public static void main(){
MyDate birthday=new MyDate();
birthday.year=-2000;
}
}
public class MyDate{
private int year=-1985;
private int month=12;
private int day=25;
public void setYear(int newYear){
if(newYear>0){
year=newYear;
}else{
System.out.println("0보다 큰 값만 들어올 수 있습니다.");
}
}
}
2. Inheritance
3. Polymorphism
댓글
댓글 쓰기