달력

102025  이전 다음

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31


// 캡슐화( Encapsulation )   접근제한자에 따른 멤버 접근

class Encap
{

 private int a = 10;
 int b = 20;      // default
 protected int c = 30;
 public int d = 40;
 public int getA()
 {
  return a;
 }
}

class EncapEx
{
 public int aa = 1;
 int bb = 2;      //default
 protected int cc = 3;
 public int dd = 4;

 public static void main(String[] args)
 {
  EncapEx ee = new EncapEx();
  System.out.println( "Encap aa : " + ee.aa );
  System.out.println( "Encap bb : " + ee.bb );
  System.out.println( "Encap cc : " + ee.cc );
  System.out.println( "Encap dd : " + ee.dd );

  Encap ec = new Encap();
  // System.out.println( "Encap a : " + ec.a );    // 같은 멤버만 접근
  System.out.println( "Encap a : " + ec.getA() );
  System.out.println( "Encap b : " + ec.b );    // 같은 폴더만 접근
  System.out.println( "Encap c : " + ec.c );    // 같은 폴더이거나 상속받았을 때 접근
  System.out.println( "Encap d : " + ec.d );    // 아무나 접근
 }
}

Posted by cdprkr2077
|