# 접근제한자란?
클래스(객체) 멤버에 대한 접근을 제한하는 키워드
public : 접근 제한 없음
protected : 상속 + default(잘 안씀)
default : 선언 안하면 default, 동일한 패키지 범위까지 접근가능
private : 클래스 내부에서만 접근가능
멤버에는 직접 접근 불가 --> 그럼 값을 수정 할 수 없나?
이때 필요한 것이 getter (값 설정하는 메서드), setter (값 반환하는 메서드)
public class Point {
private int x = -1;
private int y = 0;
public void setX(int n) {
x = n;
}
public void setY(int n) {
y = n;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
{
System.out.println("초기화 블록");
x=1;
for(int i=0;i<10;i++) {
y+=x;
}
}
Point(){
System.out.println("디폴트 생성자 ");
x=0;
y=0;
}
Point(int n1, int n2) {
System.out.println("오버라이딩 된 생성자(int, int)");
x = n1;
y = n2;
}
String pointToString() {
return "(x=" + x + ",y=" + y + ")";
}
}
public class Point_main {
public static void main(String[] args) {
Point po1 = new Point();
Point po2 = new Point(10,20);
System.out.println(po1.pointToString());
System.out.println(po2.pointToString());
po1.setX(11);
po1.setY(21);
System.out.println(po1.getX());
System.out.println(po1.getY());
[최종정리] 객체 초기화 하는 방법
1) 직접 초기화 x=1, y=1
2) 블록 초기화 {} 안에서 초기화
3) 생성자 초기화(중요!!)
우리는 이미 객체를 생성을 하면서 생성자를 만들지 않았지만 자동으로 호출하고 있었음
= new Point(); --> 디폴트 생성
= new Point(10, 20) --> 오버로딩하여 값을 받을 수도 있음