[JAVA] °­ÁÂ 15



|15ÀÏÂ÷|



Instance º¯¼ö¿Í »ó¼Ó
instanceÂüÁ¶º¯¼ö¿¡ ´ëÇÏ¿© ÀÚ¼¼È÷ ¾Ë¾Æº¸µµ·Ï ÇÏÀÚ.

class A {
 int i=1;
 public static void main(String[] args) {
    A a;
    a = new A();
    System.out.println("aÀÇ i´Â " + a.i);
    A aCopy;
    aCopy = a;
    System.out.println("aCopyÀÇ i´Â " + aCopy.i);
 }
} i´Â " + a.i);
    System.out.println("a.i=2 ÀÌÈÄ¿¡ aCopyÀÇ i´Â " + aCopy.i);
 }
}
½ÇÇà°á°ú´Â ¾Æ·¡¿Í °°´Ù.

C:\Java ¿¬½À\OOP>java A
aÀÇ i´Â 1
aCopyÀÇ i´Â 1
a.i=2 ÀÌÈÄÀÇ aÀÇ i´Â 2
a.i=2 ÀÌÈÄ¿¡ aCopyÀÇ i´Â 2

¹Ù²Ù¾î ÁØ°ÍÀº a.iÀ̾ú´Ù.  ¸í½ÃÀûÀ¸·Î´Â aCopy.i´Â ¹Ù²Ù¾î ÁÖÁö ¾Ê¾Ò´Ù. ±×·¯³ª °á°ú¸¦ º¸¸é aCopy.i ¶ÇÇÑ 2·Î ¹Ù²î¾î Á³À½À» ¾Ë ¼ö ÀÖ´Ù.
ÀÌ·¯ÇÑ Çö»óÀº instanceÂüÁ¶º¯¼ö aCopy°¡ ¸Þ¸ð¸®»óÀÇ instance a¸¦ °¡¸®Å°°í ÀÖ´Â instance ÂüÁ¶º¯¼ö aÀÇ °ªÀ» Áï ÁÖ¼Ò¸¦ aCopy¿¡ ´ëÀÔÇÏ¿´±â ¶§¹®ÀÌ´Ù.
°á°úÀûÀ¸·Î instanceÂüÁ¶º¯¼ö a¿Í instanceÂüÁ¶º¯¼ö aCopy´Â ½ÇÁ¦ÀûÀ¸·Î °°Àº instance¸¦ °¡¸®Å°°í ÀÖ´Ù´Â °ÍÀ» ³ªÅ¸³½´Ù.

´ÙÀ½ ÄÚµå´Â a.i¸¦ ¹Ù²ÙÁö ¾Ê°í aCopy.i¸¦ ¹Ù²Ù¾úÀ» ¶§ ³ªÅ¸³ª´Â °á°ú¸¦ º¸¿©ÁØ´Ù.


class A {
 int i=1;
 public static void main(String[] args) {
    A a;
    a = new A();
    System.out.println("aÀÇ i´Â " + a.i);
    A aCopy;
    aCopy = a;      
    System.out.println("aCopyÀÇ i´Â " + aCopy.i);
    aCopy.i=2;
    System.out.println("aCopy.i=2 ÀÌÈÄ¿¡ aCopyÀÇ i´Â " + aCopy.i);
    System.out.println("aCopy.i=2 ÀÌÈÄ¿¡ aÀÇ i´Â " + a.i);
 }
}

C:\Java ¿¬½À\OOP>java A
aÀÇ i´Â 1
aCopyÀÇ i´Â 1
aCopy.i=2 ÀÌÈÄ¿¡ aCopyÀÇ i´Â 2
aCopy.i=2 ÀÌÈÄ¿¡ aÀÇ i´Â 2

a.i=2·Î ¹Ù²Ù¾îÁÖ¾úÀ» °æ¿ì¿Í ¿ÏÀüÈ÷ °°Àº °á°ú¸¦ ÁØ´Ù. instanceÂüÁ¶º¯¼ö a¿Í aCopy´Â ½ÇÁ¦·Î °°Àº instance¸¦ °¡¸®Å°°í ÀÖ´Ù.

======================================================

»ó¼ÓµÈ instance¿¡¼­ instanceÂüÁ¶º¯¼ö°¡ ÂüÁ¶ÇÒ ¼ö ÀÖ´Â Á¦ÇÑ»çÇ×À» ¾Ë¾Æº¸µµ·Ï ÇÏ°Ú½À´Ï´Ù.

ÀÏ´Ü ´ÙÀ½ Äڵ带 º¸µµ·Ï ÇÏÀÚ.

class A {
 int iofA=1;
}
class B extends A {
 int iofB=2;
}
class C extends B {
 int iofC=3;
 public static void main(String[] args) {
    C instC;
    instC = new C();
    System.out.println("instCÀÇ iofA´Â " + instC.iofA);
    System.out.println("instCÀÇ iofB´Â " + instC.iofB);
    System.out.println("instCÀÇ iofC´Â " + instC.iofC);
 }
}
½ÇÇà°á°ú¸¦ º¸¸é »ó¼Ó¿¡ ÀÇÇؼ­ º¯¼ö°¡ ´©ÀûµÇ¾úÀ½À» ¾Ë ¼ö ÀÖ´Ù.

C:\Java ¿¬½À\OOP>java C
instCÀÇ iofA´Â 1
instCÀÇ iofB´Â 2
instCÀÇ iofC´Â 3

compile error°¡ ¹ß»ýÇÑ ´ÙÀ½ Äڵ带 ÁÖÀÇ ±í°Ô »ìÆ캸ÀÚ. instanceÂüÁ¶º¯¼ö´Â class A¿¡¼­ ³ª¿Â instanceÂüÁ¶º¯¼öÀÌÁö¸¸ ½ÇÁ¦·Î »ý¼º½ÃŲ instance´Â class C¿¡¼­ »ý¼ºµÇ¾ú´Ù.

class A {
 int iofA=1;
}
class B extends A {
 int iofB=2;
}
class C extends B {
 int iofC=3;
 public static void main(String[] args) {
    A instA;
    instA = new C();
    System.out.println("instAÀÇ iofA´Â " + instA.iofA);
    System.out.println("instAÀÇ iofB´Â " + instA.iofB);
    System.out.println("instAÀÇ iofC´Â " + instA.iofC);
 }
}
compile error°¡ ³ª¸ç ´ÙÀ½ÀÇ ³»¿ëÀÌ´Ù.

C:\Java ¿¬½À\OOP>javac C.java
C.java:8: No variable iofB defined in class A.
    System.out.println("instAÀÇ iofB´Â " + instA.iofB);
                                              ^
C.java:9: No variable iofC defined in class A.
    System.out.println("instAÀÇ iofC´Â " + instA.iofC);
                                              ^
2 errors

class AÀÇ instanceÂüÁ¶º¯¼ö´Â class CÀÇ instance¸¦ °¡¸®Å°°í ÀÖ´Ù. ½ÇÁ¦·Î ¸Þ¸ð¸®°ø°£¿¡ »ý¼ºµÈ instance´Â class CÀÇ instanceÀÌÁö¸¸ ±×·¯?al-instanceÀÇ iofA " + refC.iofA);
 }
}

C:\Java ¿¬½À\OOP>javac C.java
C.java:6: Incompatible type for =. Explicit cast needed to convert A to C.
    refC = new A();
         ^
1 error

class C¿¡¼­ Á¤ÀÇµÈ instanceÂüÁ¶º¯¼ö refC´Â class AÀÇ iofAÀÇ Á¤º¸¿Í class BÀÇ iofB, class CÀÇ iofC¿¡ ´ëÇØ Ã£¾Æ°¡´Â Áöµµ(map) Á¤º¸¸¦ °¡Áö°í ÀÖ´Ù. class AÀÇ instance¸¦ »ý¼º½ÃÅ°°í class CÀÇ instanceÂüÁ¶º¯¼öÀÎ refC¿¡ ÁÖ¼Ò¸¦ ±â¾ï½ÃÄ×´Ù. ÀÌ·¯ÇÑ °æ¿ì¿¡ °ªÀ» ã¾Æ°¡´Â Á¤º¸¸¦ ¸ðµÎ °¡Áö°í ÀÖ´Â refC´Â ½ÇÁ¦·Î »ý¼ºµÈ class AÀÇ instance¸¦ ÁöÁ¤ÇÏ·Á°í ÇÏ´Ï Ã£¾Æ°¡´Â ¹æ¹ýÀº ¾Ë°í À־
±×·¯ÇÑ Á¤º¸°¡ ¾ø´Ù´Â °ÍÀ» ¾Ë°ÔµÈ´Ù.

=======================================================================

cast(Çüº¯È¯) ¿¹Á¦ 1   : ¼º°ø°æ¿ì

instanceÂüÁ¶º¯¼ö´Â top classÀÎ class A¿¡¼­, ÂüÁ¶º¯¼ö¿¡ µî·ÏÇÏ´Â instance´Â bottom class C¿¡¼­ ¸¸µé¾î Á³À» ¶§  top classÀÇ instanceº¯¼ö int iofA=1 ÀÇ »ç¿ë¿¡¼­ÀÇ cast

            »ý¹° : ¿¬·É=1
              |
     ----------------------
     |                    |
    ½Ä¹°                 µ¿¹° : ¼Óµµ=2
                          |
                   -------------------
                   |                 |


°ÍÀÌÁö¸¸ new C()·ÎºÎÅÍ »ý¼ºµÈ instance¸¦ (A) new C(); ¸¦ ÀÌ¿ëÇÏ¿© new A()·Î ºÎÅÍ ¹ß»ýµÈ
°Íó·³ °¡Àå(cast)ÇÔÀ¸·Î½á ½ÇÇàÀÌ µË´Ï´Ù.

===================================================================

cast(Çüº¯È¯) ¿¹Á¦ 2   : ½ÇÆаæ¿ì

instanceÂüÁ¶º¯¼ö´Â top classÀÎ class A¿¡¼­, ÂüÁ¶º¯¼ö¿¡ µî·ÏÇÏ´Â instance´Â bottom class C¿¡¼­ ¸¸µé¾î Á³À» ¶§  bottom class CÀÇ instanceº¯¼ö int iofC=3 ÀÇ »ç¿ë¿¡¼­ÀÇ cast

´ÙÀ½ Äڵ带 º¸µµ·Ï ÇսôÙ.

class A {
 int iofA=1;
}
class B extends A {
 int iofB=2;
}
class C extends B {
 int iofC=3;
 public static void main(String[] args) {
    A refA;
    refA = (A) new C();
    System.out.println("refA.iofC  " + refA.iofC);
 }
}

C:?ö¿? µî·ÏÇÏ´Â instance´Â top class A¿¡¼­ ¸¸µé¾î Á³À» ¶§
bottom class CÀÇ instanceº¯¼ö int iofC=3 ÀÇ »ç¿ë¿¡¼­ÀÇ cast


class A {
 int iofA=1;
}
class B extends A {
 int iofB=2;
}
class C extends B {
 int iofC=3;
 public static void main(String[] args) {
    C refC;
    refC = (C) new A();
    System.out.println("refC.iofC " + refC.iofC);
 }
}

C:\Java ¿¬½À\OOP>java C
java.lang.ClassCastException:
      at C.main(C.java:6)



´ÙÀ½¿¡´Â »ó¼Ó¿¡¼­ super this Å°¿öµå ¿¡ ´ëÇÑ ³»¿ëÀÌ ¿Ã¶ó¿Ã ¿¹Á¤ÀÔ´Ï´Ù.