반응형
□ 접근 지정자(Access Identifier)의 종류
◇ private
◇ public
◇ protected
□ private 멤버의 접근
◇ private 멤버에 직접 접근할 수 없다.
◇ private 멤버에 접근하기 위해서 public 메서드를 이용한다.
□ private 멤버 변수를 포함한 클래스
1
2
3
4
5 |
public class Person { public int age; //public 멤버 변수 선언 public float height; //public 멤버 변수 선언 private float weight; //private 멤버 변수 선언 } |
□
private 에 직접 접근하기 때문에 에러가 발생하는 예
1
2
3
4
5
6
7
8
9
10
11 |
public class PrivateAccessMain{ public static void main(String[] args) { Person brother = new Person(); //객체 생성 brother.age = 100 ; //public 멤버 접근 brother.height = 170 .0F; //public 멤버 접근 brother.weight = 67 .0F; //private 멤버 접근 - 에러 System.out.println( "age:" + brother.age); //public 멤버 접근 System.out.println( "height:" + brother.height); //public 멤버 접근 System.out.println( "weight:" + brother.weight); //private 멤버 접근 - 에러 } } |
반응형
'JAVA > 소설같은자바' 카테고리의 다른 글
private의 사용 이유 (0) | 2011.08.08 |
---|---|
private에 접근하는 방법 (0) | 2011.08.08 |
접근제어란? (0) | 2011.08.08 |
클래스와 메서드 결론 (0) | 2011.08.08 |
변수와 메서드의 분리와 결합 (0) | 2011.08.08 |