| (그림과함께)자바변수메소드접근제어, public, private, protected |
| 글쓴이 : 오라클자바 (61.36.106.242) |
[예제]
package onj;
class Circle
{ private double area = 0;
public void setRadius( double r )
{ area = Math.PI * r * r; }
//같은 패키지(디렉토리)나 하위클래스에서 접근가능
protected double getArea() { return area; }
public void display()
{ System.out.println("Areas of circle with radius " + Math.sqrt( area / Math.PI ) + " = " + getArea() ); } } class AccessTest { public static void main( String args[] ) { Circle circle1 = new Circle(), circle2 = new Circle(); circle1.setRadius( 2 ); circle2.setRadius( 3 ); circle1.display(); circle2.display(); double sumAreas = circle1.getArea() + circle2.getArea(); System.out.println( "circle1 + circle2 = " + sumAreas ); // Circle Class의 private 변수이므로 접근 불가 // System.out.println( circle1.area ); } }
[결과]
Areas of circle with radius 2.0 = 12.566370614359172
Areas of circle with radius 3.0 = 28.274333882308138 circle1 + circle2 = 40.840704496667314
오라클자바커뮤니티에서 설립한 개발자교육6년차 오엔제이프로그래밍 실무교육센터
(오라클SQL,튜닝,힌트,자바프레임워크,안드로이드,아이폰,닷넷 실무개발강의)
[개강확정강좌]오라클자바커뮤니티에서 운영하는 개발자 전문교육 ,개인80%환급(www.onjprogramming.co.kr)
[주말] [10/26]C#,ASP.NET마스터 [10/27]JAVA&WEB프레임워크실무과정 [평일야간] [10/17]SQL초보에서실전전문가까지 [10/22]자바초보에서안드로이드까지 [10/25]C#,ASP.NET마스터 [10/25]Spring3.X, MyBatis, Hibernate실무과정 [주간] [10/21]Spring3.X, MyBatis, Hibernate실무과정 |
댓글 없음:
댓글 쓰기