ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • ์ค‘์ฒฉํด๋ž˜์Šค(Nested class)๋ž€ ๋ฌด์—‡์ธ๊ฐ€?
    ๊ฐœ๋ฐœ/Java 2022. 4. 7. 00:54

     

     

     

    ์•ˆ๋…•ํ•˜์„ธ์š”, ๋‹ค๋ผ๋ฏธ๐Ÿฟ๏ธ์ž…๋‹ˆ๋‹ค :)

    ์˜ค๋Š˜์€ ์ค‘์ฒฉํด๋ž˜์Šค๋ผ๊ณ  ๋ถˆ๋ฆฌ๋Š” Nested class์— ๋Œ€ํ•œ ๋‚ด์šฉ์„ ์ •๋ฆฌํ•˜๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

     

    ํ•ด๋‹น ๋‚ด์šฉ์€ ์ž๋ฐ”์˜์‹  1๊ถŒ, chapter16์˜ ๋‚ด์šฉ์„ ๊ธฐ๋ฐ˜์œผ๋กœ ์ž‘์„ฑํ•œ ๊ธ€์ž…๋‹ˆ๋‹ค.

     

     

     

    Nested Class 


    ์ž๋ฐ”์—์„œ๋Š” ํด๋ž˜์Šค ์•ˆ์— ํด๋ž˜์Šค๊ฐ€ ๋“ค์–ด๊ฐˆ ์ˆ˜ ์žˆ๋Š”๋ฐ, ์ด๋Ÿฌํ•œ ํด๋ž˜์Šค๋ฅผ "Nested class"๋ผ๊ณ  ๋ถ€๋ฆ…๋‹ˆ๋‹ค. ํ•œ๊ตญ์–ด๋กœ๋Š” "์ค‘์ฒฉ ํด๋ž˜์Šค"๋ผ๊ณ ๋„ ๋ถˆ๋ฆฝ๋‹ˆ๋‹ค.

     

    Nested class์˜ ๊ตฌ๋ถ„

     

    Nested class๋Š” static์œผ๋กœ ์„ ์–ธ๋˜์—ˆ๋Š”์ง€ ์—ฌ๋ถ€์— ๋”ฐ๋ผ "Static nested ํด๋ž˜์Šค"์™€ "๋‚ด๋ถ€(inner) ํด๋ž˜์Šค"๋กœ ๊ตฌ๋ถ„๋ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ ํด๋ž˜์Šค๋ฅผ ์„ ์–ธํ•  ๋•Œ static์œผ๋กœ ์„ ์–ธ์ด ๋˜์—ˆ๋‹ค๋ฉด, ๊ทธ ํด๋ž˜์Šค๋Š” static nested ํด๋ž˜์Šค๊ฐ€ ๋˜๊ณ , static์ด ์—†์œผ๋ฉด ๊ทธ๋ƒฅ ๋‚ด๋ถ€ ํด๋ž˜์Šค๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

    ๋‚ด๋ถ€ ํด๋ž˜์Šค๋Š” ๋‹ค์‹œ ๋‘ ๊ฐ€์ง€๋กœ ๋‚˜๋‰˜๋Š”๋ฐ, ์ด๋ฆ„์ด ์žˆ๋Š” ๋‚ด๋ถ€ ํด๋ž˜์Šค๋Š” "๋กœ์ปฌ(ํ˜น์€ ์ง€์—ญ) ๋‚ด๋ถ€ ํด๋ž˜์Šค(local inner class)"๋ผ๊ณ  ํ•˜๊ณ , ์ด๋ฆ„์ด ์—†๋Š” ํด๋ž˜์Šค๋ฅผ "์ต๋ช… ๋‚ด๋ถ€ ํด๋ž˜์Šค(anonymous inner class)"๋ผ๊ณ  ๋ถ€๋ฆ…๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ, ์ผ๋ฐ˜์ ์œผ๋กœ๋Š” ๊ฐ„๋‹จํ•˜๊ฒŒ ์ค„์—ฌ์„œ ๊ฐ๊ฐ "๋‚ด๋ถ€ ํด๋ž˜์Šค"์™€ "์ต๋ช… ํด๋ž˜์Šค"๋กœ ๋ถ€๋ฆ…๋‹ˆ๋‹ค. ์•ž์œผ๋กœ ๋กœ์ปฌ ๋‚ด๋ถ€ ํด๋ž˜์Šค๋Š” ๊ทธ๋ƒฅ ๋‚ด๋ถ€ ํด๋ž˜์Šค๋กœ, ์ต๋ช… ๋‚ด๋ถ€ ํด๋ž˜์Šค๋Š” ์ต๋ช… ํด๋ž˜์Šค๋กœ ๋ถ€๋ฅด๊ฒ ์Šต๋‹ˆ๋‹ค.

     

     

     

    ํ‘œ๋กœ ํ™•์ธํ•˜๊ธฐ


    ๋Œ€์ถฉ Nested class์˜ ๊ตฌ๋ถ„์„ ์‚ดํŽด๋ณด์•˜์œผ๋‹ˆ ๋‹ค์Œ ๋‚ด์šฉ์„ ๋‹ค๋ฃจ๊ธฐ ์ „์— ์•ž์œผ๋กœ ๋ฐฐ์šธ ๋‚ด์šฉ๋“ค์„ ๊ฐ„๋‹จํ•˜๊ฒŒ ํ‘œ๋กœ ์š”์•ฝํ•ด๋ณด์•˜์Šต๋‹ˆ๋‹ค. ์ญ‰ ํ›‘์–ด๋ณด๋ฉด์„œ ์–ด๋–ค ๋‚ด์šฉ๋“ค์„ ๋‹ค๋ฃฐ์ง€ ์ง์ž‘ํ•ด๋ณด์‹œ๊ณ , ๋˜ ์ด ๊ธ€์„ ๋‹ค ์ฝ๊ณ  ๋‚˜์„œ ์ •๋ฆฌ์šฉ์œผ๋กœ ํ•œ๋ฒˆ ๋” ๋ณด๊ณ  ํ™•์ธํ•ด๋ณด์‹œ๋ฉด ๋„์›€์ด ๋  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.

     

      Nested class
    ๊ตฌ๋ถ„ Static nested class (static ์„ ์–ธ O) inner(๋‚ด๋ถ€) class (static ์„ ์–ธ X)
    Local inner class
    (๋‚ด๋ถ€ ํด๋ž˜์Šค)
    Anonymous inner class (์ต๋ช… ํด๋ž˜์Šค)
    ๊ฐ์ฒด ์ƒ์„ฑ ex)
    OuterOfStatic.StaticNested staticNested = new OuterOfStatic.StaticNested();
    staticNested.setValue(3);
    ex)
    OuterOfInner outer =
    new OuterOfInner();
    OuterOfInner.Inn
    er inner = outer.new Inner();
    inner.setValue(
    3);
    ex)
     EventListener listener 
     = new EventListener() {
      @Override
       public void onClick() {
          System.out.println("Magic
          Button Clicked !!!");
        }
      };
    ์ฐธ์กฐ ๊ฐ€๋Šฅ ๋ณ€์ˆ˜ - (static nested -> ๊ฐ์‹ธ๋Š” ํด๋ž˜์Šค)
    ๊ฐ์‹ธ๊ณ  ์žˆ๋Š” ํด๋ž˜์Šค์˜ static ๋ณ€์ˆ˜๋งŒ ์ฐธ๊ณ ํ•  ์ˆ˜ ์žˆ๋‹ค. Static nested ํด๋ž˜์Šค๊ฐ€ static์œผ๋กœ ์„ ์–ธ๋˜์–ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๋ถ€๋ชจ ํด๋ž˜์Šค์— static ํ•˜์ง€ ์•Š์€ ๋ณ€์ˆ˜๋ฅผ ์ฐธ์กฐํ•  ์ˆ˜๋Š” ์—†๋‹ค. ๋งŒ์•ฝ ์ฐธ์กฐํ•˜๋Š” ์ฝ”๋“œ๊ฐ€ ์žˆ๋‹ค๋ฉด, ์ปดํŒŒ์ผ์‹œ์— ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฏ€๋กœ ์–ด๋–ป๊ฒŒ๋ผ๋„ ์‚ฌ์šฉํ•  ์ˆ˜๋Š” ์—†๋‹ค. 

    - (๊ฐ์‹ธ๋Š” ํด๋ž˜์Šค -> static nested) ๊ฐ ํด๋ž˜์Šค์˜ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•œ ํ›„ ๊ทธ ๊ฐ’์„ ์ฐธ์กฐํ•˜๋Š” ๊ฒƒ์€ ๊ฐ€๋Šฅํ•˜๋‹ค. ๊ทธ ๊ฐ’์ด private๋ผ๊ณ  ํ• ์ง€๋ผ๋„ ๋ชจ๋‘ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค.
    - ๋ชจ๋‘ ๊ฐ€๋Šฅ
    ์‚ฌ์šฉ ๋ชฉ์  - ์†Œ์Šค์˜ ๊ฐ€๋…์„ฑ๊ณผ ์œ ์ง€๋ณด์ˆ˜์„ฑ์„ ๋†’์ด๊ณ  ์‹ถ์„ ๋•Œ
    - ํ•œ ๊ณณ์—์„œ๋งŒ ์‚ฌ์šฉ๋˜๋Š” ํด๋ž˜์Šค๋ฅผ ๋…ผ๋ฆฌ์ ์œผ๋กœ ๋ฌถ์–ด์„œ ์ฒ˜๋ฆฌํ•  ํ•„์š”๊ฐ€ ์žˆ์„ ๋•Œ
    - ์†Œ์Šค์˜ ๊ฐ€๋…์„ฑ๊ณผ ์œ ์ง€๋ณด์ˆ˜์„ฑ์„ ๋†’์ด๊ณ  ์‹ถ์„ ๋•Œ
    - ์บก์Šํ™”๊ฐ€ ํ•„์š”ํ•  ๋•Œ(๋‚ด๋ถ€ ๊ตฌํ˜„์„ ๊ฐ์ถ”๊ณ  ์‹ถ์„ ๋•Œ)

     

     

    ์ง€๊ธˆ๋ถ€ํ„ฐ๋Š”, ์œ„์—์„œ ์ •๋ฆฌํ•œ ๋‚ด์šฉ์„ ์˜ˆ์ œ ์ฝ”๋“œ๋ฅผ ํ†ตํ•ด ํ’€์–ด์„œ ์„ค๋ช…ํ•ด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

     

     

     

     

    Static nested ํด๋ž˜์Šค์˜ ํŠน์ง•


     

    ์•„๋ž˜์˜ ์˜ˆ์‹œ ์ฝ”๋“œ๋ฅผ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ด ์ฝ”๋“œ๋ฅผ ์ปดํŒŒ์ผํ•˜๋ฉด ์–ด๋–ค ํด๋ž˜์Šค๊ฐ€ ๋งŒ๋“ค์–ด์งˆ๊นŒ์š”?

    package nested;
    
    public class OuterOfStatic {
        static class StaticNested{
            private int value = 0;
            public int getValue(){
                return value;
            }
            public void setValue(int value){
                this.value = value;
            }
        }
    }

     

    ์ปดํŒŒ์ผํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋‘ ๊ฐœ์˜ ํด๋ž˜์Šค๊ฐ€ ๋งŒ๋“ค์–ด์ง‘๋‹ˆ๋‹ค.

    OutOfStatic.class
    OutOfStatic$StaticNested.calss

     

    Nested ํด๋ž˜์Šค๋Š” ์œ„์—์„œ ๋ณด์‹œ๋Š” ๊ฒƒ ์ฒ˜๋Ÿผ ๊ฐ์‹ธ๊ณ  ์žˆ๋Š” ํด๋ž˜์Šค ์ด๋ฆ„ $  Nested ํด๋ž˜์Šค์˜ ์ด๋ฆ„์œผ๋กœ ํ‘œ๊ธฐ๊ฐ€ ๋˜๊ณ , ๋ณ„๋„์˜ ํด๋ž˜์Šค ํŒŒ์ผ๋กœ ๋งŒ๋“ค์–ด์ง‘๋‹ˆ๋‹ค. 

     

     

    ๊ทธ๋Ÿฌ๋ฉด ์ด StaticNested ํด๋ž˜์Šค์˜ ๊ฐ์ฒด๋Š” ์–ด๋–ป๊ฒŒ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ์„๊นŒ์š”?

     

    package nested;
    
    public class NestedSample {
    
        public static void main(String[] args){
            NestedSample sample = new NestedSample();
            sample.makeStaticNestedObject();
        }
        public void makeStaticNestedObject(){
            OuterOfStatic.StaticNested staticNested = new OuterOfStatic.StaticNested();
            staticNested.setValue(3);
            System.out.println(staticNested.getValue());
        }
    }

     

    ์ด๋ ‡๊ฒŒ Static Nested ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค์—ˆ์„ ๋•Œ ๊ฐ์ฒด ์ƒ์„ฑ์€ ํด๋ž˜์Šค ํŒŒ์ผ ์ด๋ฆ„์ฒ˜๋Ÿผ ์ค‘๊ฐ„์— $๋ฅผ ์“ฐ๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ, ๊ฐ์‹ธ๊ณ  ์žˆ๋Š” ํด๋ž˜์Šค ์ด๋ฆ„ ๋’ค์— .(์ )์„ ์ฐ๊ณ  ์“ฐ๋ฉด ๋ฉ๋‹ˆ๋‹ค. ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•œ ์ดํ›„์— ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ์ผ๋ฐ˜ ํด๋ž˜์Šค์™€ ๋™์ผํ•ฉ๋‹ˆ๋‹ค.

     

    ๊ทธ๋ ‡๋‹ค๋ฉด Static nested class๋ฅผ ์™œ ์‚ฌ์šฉํ• ๊นŒ์š”?

     

    ์ž๋ฐ”์—์„œ Static nested class๋ฅผ ์“ฐ๋Š” ์ด์œ 


    ์ผ๋ฐ˜์ ์œผ๋กœ Static nested ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“œ๋Š” ์ด์œ ๋Š” ํด๋ž˜์Šค๋ฅผ ๋ฌถ๊ธฐ ์œ„ํ•ด์„œ์ž…๋‹ˆ๋‹ค.

     

    ๋งŒ์•ฝ ํ•™๊ต๋ฅผ ๊ด€๋ฆฌํ•˜๋Š” School์ด๋ผ๋Š” ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค๊ณ , ๋Œ€ํ•™์„ ๊ด€๋ฆฌํ•˜๋Š” University๋ผ๋Š” ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค์—ˆ์„ ๋•Œ๋ฅผ ์ƒ๊ฐํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ด๋•Œ Student๋ผ๋Š” ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค๋ฉด School์˜ ํ•™์ƒ์ธ์ง€ University์˜ ํ•™์ƒ์ธ์ง€๊ฐ€ ๋ถˆ๋ถ„๋ช…ํ•ด์ง‘๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ๋งŒ์•ฝ School ๋‚ด์— static nested ํด๋ž˜์Šค์ธ student๋ฅผ ๋งŒ๋“ ๋‹ค๋ฉด, ์ด ํด๋ž˜์Šค์˜ ์šฉ๋„๊ฐ€ ๋ณด๋‹ค ์ •ํ™•ํ•ด์ง‘๋‹ˆ๋‹ค. ๋ฌผ๋ก  ์ด ๊ฒฝ์šฐ์— SchoolStudent๋ผ๋Š” ์‹์˜ ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค์–ด๋„ ๋˜์ง€๋งŒ, ํ•„์š”ํ•˜๋‹ค๋ฉด ์ด๋ ‡๊ฒŒ ๋งŒ๋“ค ์ˆ˜๋„ ์žˆ๋‹ค๋Š” ๋ง์ž…๋‹ˆ๋‹ค. ๊ฒŒ๋‹ค๊ฐ€ School.Student ํด๋ž˜์Šค๋Š” School์— ๋งŒ๋“ค์—ˆ๊ธฐ ๋•Œ๋ฌธ์— University ํด๋ ˆ์Šค์—์„œ๋Š” ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.

     

    University 

    public class University{
       static class Student{
       }
    }

    School

    public class School{
        static class Student{
        }
    }

    ๊ทธ๋ž˜์„œ ๊ฒ‰์œผ๋กœ ๋ณด๊ธฐ์—๋Š” ์œ ์‚ฌํ•˜์ง€๋งŒ, ๋‚ด๋ถ€์ ์œผ๋กœ ๊ตฌํ˜„์ด ๋‹ฌ๋ผ์•ผ ํ•  ๋•Œ ์ด์™€ ๊ฐ™์ด static nested ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

     

     

    ๋‚ด๋ถ€ ํด๋ž˜์Šค์™€ ์ต๋ช… ํด๋ž˜์Šค


    ์ด๋ฒˆ์—๋Š” ๋‚ด๋ถ€ ํด๋ž˜์Šค์™€ ์ต๋ช…ํด๋ž˜์Šค์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

     

    package nested;
    
    public class OuterOfInner {
        class Inner {
            private int value = 0;
            public int getValue(){
                return value;
            }
            public void setValue(int value){
                this.value = value;
            }
        }
    }

     

    ์•ž์˜ ์˜ˆ์ œ์—์„œ ์„ ์–ธํ–‡๋˜ StaticNested์™€ Inner ํด๋ž˜์Šค์˜ ๋‚ด์šฉ์€ ๋™์ผํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ, Inner ํด๋ž˜์Šค์˜ ์„ ์–ธ๋ถ€์—๋Š” static ์„ ์–ธ์ด ์—†์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฏ€๋กœ, ์ด Inner ํด๋ž˜์Šค์˜ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๋ฐฉ๋ฒ•๋„ ๋‹ค๋ฆ…๋‹ˆ๋‹ค.

     

    package nested;
    
    public class InnerSample {
        public static void main(String[] args){
            InnerSample sample = new InnerSample();
            sample.makeInnerObject();
        }
        public void makeInnerObject(){
            OuterOfInner outer = new OuterOfInner();
            OuterOfInner.Inner inner = outer.new Inner();
            inner.setValue(3);
            System.out.println(inner.getValue());
        }
    }

     

    ์•„๋ž˜์˜ ๋‘ ์ค„์„ ๋ณด๋ฉด ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•œ ๋‹ค์Œ์— ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์—๋Š” ์ฐจ์ด๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ, ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๋ฐฉ๋ฒ•์— ์ฐจ์ด๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. Inner ํด๋ž˜์Šค์˜ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ธฐ ์ „์—๋Š” ๋จผ์ € Inner ํด๋ž˜์Šค๋ฅผ ๊ฐ์‹ธ๊ณ  ์žˆ๋Š” OuterOfInner๋ผ๋Š” ํด๋ž˜์Šค์˜ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” outer๋ผ๋Š” ๊ฐ์ฒด์ด๋‹ค. ๊ทธ๋ฆฌ๊ณ , ์ด outer๋ผ๋Š” ๊ฐ์ฒด๋ฅผ ํ†ตํ•ด์„œ Inner ํด๋ž˜์Šค์˜ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค์–ด ๋‚ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

     

    ๊ทธ๋Ÿฐ๋ฐ ์ด์™€ ๊ฐ™์ด Inner ํด๋ž˜์Šค์˜ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“œ๋Š” ๋ฐฉ๋ฒ•์ด ๋ณต์žกํ•œ๋ฐ๋„ ๋ถˆ๊ตฌํ•˜๊ณ  ์™œ ๋งŒ๋“ค์–ด์กŒ์„๊นŒ์š”?

     

    ์ž๋ฐ”์—์„œ Inner class๋ฅผ ์“ฐ๋Š” ์ด์œ 


    ์ด๋Ÿฌํ•œ ๋‚ด๋ถ€ ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“œ๋Š” ์ด์œ ๊ฐ€ ์บก์Šํ™” ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ํ•˜๋‚˜์˜ ํด๋ž˜์Šค์—์„œ ์–ด๋–ค ๊ณตํ†ต์ ์ธ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๋Š” ํด๋ž˜์Šค๊ฐ€ ํ•„์š”ํ•œ๋ฐ ๋‹ค๋ฅธ ํด๋ž˜์Šค์—์„œ๋Š” ๊ทธ ํด๋ž˜์Šค๊ฐ€ ์ „ํ˜€ ํ•„์š”๊ฐ€ ์—†์„ ๋•Œ ์ด๋Ÿฌํ•œ ๋‚ด๋ถ€ ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค์–ด ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๋‚ด๋ถ€ ํด๋ž˜์Šค๋Š” GUI ๊ด€๋ จ ํ”„๋กœ๊ทธ๋žจ์„ ๊ฐœ๋ฐœํ•  ๋•Œ ๊ฐ€์žฅ ๋งŽ์ด ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

    GUI์—์„œ ๋‚ด๋ถ€ ํด๋ž˜์Šค๋“ค์ด ๋งŽ์ด ์‚ฌ์šฉ๋˜๋Š” ๋ถ€๋ถ„์€ ๋ฆฌ์Šค๋„ˆ(Listener)๋ผ๋Š” ๊ฒƒ์„ ์ฒ˜๋ฆฌํ•  ๋•Œ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์‚ฌ์šฉ์ž๊ฐ€ ๋ฒ„ํŠผ์„ ํด๋ฆญํ•˜๊ฑฐ๋‚˜, ํ‚ค๋ณด๋“œ๋ฅผ ์ž…๋ ฅํ•  ๋•Œ์—๋Š” ๋ชจ๋‘ ์ด๋ฒคํŠธ(Event)๋ผ๋Š” ๊ฒƒ์ด ๋ฐœ์ƒํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์–ด๋–ค ๋ฒ„ํŠผ์ด ๋ˆŒ๋ ธ์„ ๋•Œ ํ•ด์•ผ ํ•˜๋Š” ์ž‘์—…์„ ์ •์˜ํ•˜๊ธฐ ์œ„ํ•ด์„œ ๋‚ด๋ถ€ ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค์–ด ์‚ฌ์šฉํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

     

    ๊ทธ๋Ÿฐ๋ฐ, ํ•˜๋‚˜์˜ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ ์–ด๋–ค ๋ฒ„ํŠผ์ด ๋ˆŒ๋ ธ์„ ๋•Œ ์ˆ˜ํ–‰ํ•ด์•ผ ํ•˜๋Š” ์ž‘์—…์€ ๋Œ€๋ถ€๋ถ„ ์ƒ์ดํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜, ํ•˜๋‚˜์˜ ๋ณ„๋„ ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค์–ด ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค๋Š” ๋‚ด๋ถ€ ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“œ๋Š” ๊ฒƒ์ด ํ›จ์”ฌ ํŽธํ•ฉ๋‹ˆ๋‹ค.

     

    ๊ทธ๋ฆฌ๊ณ , ๋‚ด๋ถ€ ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“œ๋Š” ๊ฒƒ๋ณด๋‹ค๋„ ๋” ๊ฐ„๋‹จํ•œ ๋ฐฉ๋ฒ•์€ "์ต๋ช… ํด๋ž˜์Šค"๋ฅผ ๋งŒ๋“œ๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

     

    ์ต๋ช…์ด๋ผ๋Š” ๊ฒƒ์€ ์˜์–ด๋กœ Anonymous๋ผ๊ณ  ํ•˜๊ณ , ์ž๋ฐ”์—์„œ ์ต๋ช… ํด๋ž˜์Šค๋Š” ๋ง ๊ทธ๋Œ€๋กœ ์ด๋ฆ„์ด ์—†๋Š” ํด๋ž˜์Šค์ž…๋‹ˆ๋‹ค.

     

     

    package nested;
    
    public class MagicButton {
        public MagicButton(){
    
        }
        private EventListener listener;
        public void setListener(EventListener listener){
            this.listener = listener;
        }
        public void onClickProcess(){
            if(listener!=null){
                listener.onClick();
            }
        }
    }
    package nested;
    
    public class AnonymousSample {
        public static void main(String[] args){
            AnonymousSample sample = new AnonymousSample();
            sample.setButtonListenerAnonymous();
        }
        public void setButtonListenerAnonymous(){
            MagicButton button = new MagicButton();
            button.setListener(new EventListener() {
                @Override
                public void onClick() {
                    System.out.println("Magic Button Clicked !!!");
                }
            });
        }
    }

    setListener() ๋ฉ”์†Œ๋“œ๋ฅผ ๋ณด๋ฉด new EventListener()๋กœ ์ƒ์„ฑ์ž๋ฅผ ํ˜ธ์ถœํ•œ ํ›„ ๋ฐ”๋กœ ์ค‘๊ด„ํ˜ธ๋ฅผ ์—ด์—ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ , ๊ทธ ์ค‘๊ด„ํ˜ธ ์•ˆ์—๋Š” onClick() ๋ฉ”์†Œ๋“œ๋ฅผ ๊ตฌํ˜„ํ•œ ํ›„ ์ค‘๊ด„ํ˜ธ๋ฅผ ๋‹ซ์•˜์Šต๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ๊ตฌํ˜„ํ•œ ๊ฒƒ์ด ๋ฐ”๋กœ "์ต๋ช… ํด๋ž˜์Šค"์ž…๋‹ˆ๋‹ค.

     

    ํด๋ž˜์Šค์—๋Š” ์ด๋ฆ„์ด ์—†์ง€๋งŒ, onClick()๊ณผ ๊ฐ™์€ ๋ฉ”์†Œ๋“œ๊ฐ€ ๊ตฌํ˜„๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ์ต๋ช… ํด๋ž˜์Šค๋ฅผ ๊ตฌํ˜„ํ•  ๋•Œ ์กฐ์‹ฌํ•ด์•ผ ํ•˜๋Š” ๊ฒƒ์€ ๊ด„ํ˜ธ๋ฅผ ๋‹ซ๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ค‘๊ด„ํ˜ธ๋ฅผ ๋‹ซ๊ณ , ์†Œ๊ด„ํ˜ธ๋ฅผ ๋‹ซ๊ณ  ์„ธ๋ฏธ์ฝœ๋ก ์„ ํ–ˆ์Šต๋‹ˆ๋‹ค. ๊ฒ‰์œผ๋กœ ๋ณด๊ธฐ์—๋Š” ํด๋ž˜์Šค๋ฅผ ์„ ์–ธํ•œ ๊ฒƒ์ด์ง€๋งŒ, ์‹ค์ œ๋กœ๋Š” setListener() ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๊ณผ์ • ๋‚ด์— ์ต๋ช… ํด๋ž˜์Šค๊ฐ€ ์žˆ๋Š” ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— ์ด์™€ ๊ฐ™์ด ์†Œ๊ด„ํ˜ธ๋ฅผ ๋‹ซ๊ณ  ์„ธ๋ฏธ์ฝœ๋ก ์„ ํ•ด ์ค˜์•ผ๋งŒ ํ•ฉ๋‹ˆ๋‹ค.

     

    setListener() ๋ฉ”์†Œ๋“œ๊ฐ€ ํ˜ธ์ถœ๋˜์–ด onClick() ๋ฉ”์†Œ๋“œ๊ฐ€ ํ˜ธ์ถœ๋  ํ•„์š”๊ฐ€ ์žˆ์„ ๋•Œ ๊ทธ ์•ˆ์— ๊ตฌํ˜„๋˜์–ด ์žˆ๋Š” ๋‚ด์šฉ๋“ค์ด ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ, ์ด๋ ‡๊ฒŒ ๊ตฌํ˜„ํ–ˆ์„ ๋•Œ์—๋Š” ํด๋ž˜์Šค ์ด๋ฆ„๋„ ์—†๊ณ , ๊ฐ์ฒด ์ด๋ฆ„๋„ ์—†๊ธฐ ๋•Œ๋ฌธ์— ๋‹ค๋ฅธ ํด๋ž˜์Šค๋‚˜ ๋ฉ”์†Œ๋“œ์—์„œ๋Š” ์ฐธ์กฐํ•  ์ˆ˜๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ, ๋งŒ์•ฝ ๊ฐ์ฒด๋ฅผ ํ•ด๋‹น ํด๋ž˜์Šค ๋‚ด์—์„œ ์žฌ์‚ฌ์šฉํ•˜๋ ค๋ฉด, ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•œ ํ›„ ์‚ฌ์šฉํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

     

    package nested;
    
    public class AnonymousSample2 {
        public static void main(String[] args){
            AnonymousSample2 sample = new AnonymousSample2();
            sample.setButtonListenerAnonymous();
        }
        public void setButtonListenerAnonymous(){
            MagicButton button = new MagicButton();
            EventListener listener = new EventListener() {
                @Override
                public void onClick() {
                    System.out.println("Magic Button Clicked !!!");
                }
            };
            button.setListener(listener);
            button.onClickProcess();
        }
    }

     

    ๊ทธ๋Ÿฐ๋ฐ, ๊ทธ๋ƒฅ ๋‚ด๋ถ€ ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค๋ฉด ํŽธํ•  ๊ฒƒ ๊ฐ™์€๋ฐ ์™œ ์ž๋ฐ”์—์„œ๋Š” ์ด๋ ‡๊ฒŒ ๋ณต์žกํ•˜๊ฒŒ ์ต๋ช… ํด๋ž˜์Šค๋ผ๋Š” ๊ฒƒ์„ ์ œ๊ณตํ•˜๋Š” ๊ฒƒ์ผ๊นŒ์š”?

     

    ์ต๋ช… ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค์—ˆ์„ ๋•Œ์˜ ์žฅ์ ์„ ์ƒ๊ฐํ•ด๋ณด๋ฉด ๋ฉ๋‹ˆ๋‹ค.

     

    ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค๊ณ , ๊ทธ ํด๋ž˜์Šค๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด ๊ทธ ์ •๋ณด๋Š” ๋ฉ”๋ชจ๋ฆฌ์— ์˜ฌ๋ผ๊ฐ‘๋‹ˆ๋‹ค. ์ฆ‰, ํด๋ž˜์Šค๋ฅผ ๋งŽ์ด ๋งŒ๋“ค๋ฉด ๋งŒ๋“ค์ˆ˜๋ก ๋ฉ”๋ชจ๋ฆฌ๋Š” ๋งŽ์ด ํ•„์š”ํ•ด์ง€๊ณ , ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ์‹œ์ž‘ํ•  ๋•Œ ๋” ๋งŽ์€ ์‹œ๊ฐ„์ด ์†Œ์š”๋ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ, ์ž๋ฐ”์—์„œ๋Š” ์ด๋ ‡๊ฒŒ ๊ฐ„๋‹จํ•œ ๋ฐฉ๋ฒ•์œผ๋กœ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ด๋†“์•˜์Šต๋‹ˆ๋‹ค.

     

     

    ์ฐธ์กฐ ๊ฐ€๋Šฅํ•œ ๋ณ€์ˆ˜


    Nested ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ์•Œ๊ณ  ์žˆ์–ด์•ผ ํ•˜๋Š” ์‚ฌํ•ญ์€ ์ฐธ์กฐ ๊ฐ€๋Šฅํ•œ ๋ณ€์ˆ˜๋“ค์ž…๋‹ˆ๋‹ค.

    package nested;
    
    public class NestedValueReference {
        public int publicInt = 0;
        protected int protectedInt = 1;
        int justInt = 2;
        private int privateInt = 3;
        static int staticInt = 4;
    
        static class StaticNested{
            public void setValue(){
                staticInt = 14;
            }
        }
    
        class Inner{
            public void setValue(){
                publicInt = 20;
                protectedInt = 21;
                justInt = 22;
                privateInt = 23;
                staticInt = 24;
            }
        }
    
        public void setValue(){
            EventListener listener = new EventListener() {
                @Override
                public void onClick() {
                    publicInt = 30;
                    protectedInt = 31;
                    justInt = 32;
                    privateInt = 33;
                    staticInt = 34;
                }
            };
        }
    }

     

    ์˜ˆ์ œ์— ์žˆ๋Š” ๊ฒƒ๊ณผ ๊ฐ™์ด Static Nested ํด๋ž˜์Šค์—์„œ๋Š” ๊ฐ์‹ธ๊ณ  ์žˆ๋Š” ํด๋ž˜์Šค์˜ static ๋ณ€์ˆ˜๋งŒ ์ฐธ๊ณ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. StaticNested ํด๋ž˜์Šค๊ฐ€ static์œผ๋กœ ์„ ์–ธ๋˜์–ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๋ถ€๋ชจ ํด๋ž˜์Šค์— static ํ•˜์ง€ ์•Š์€ ๋ณ€์ˆ˜๋ฅผ ์ฐธ์กฐํ•  ์ˆ˜๋Š” ์—†์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ ์ฐธ์กฐํ•˜๋Š” ์ฝ”๋“œ๊ฐ€ ์žˆ๋‹ค๋ฉด, ์ปดํŒŒ์ผ์‹œ์— ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฏ€๋กœ ์–ด๋–ป๊ฒŒ๋ผ๋„ ์‚ฌ์šฉํ•  ์ˆ˜๋Š” ์—†์Šต๋‹ˆ๋‹ค. ์ฆ‰, ์—ฌ๊ธฐ์„œ publicInt, protectedInt, justInt, privateInt๋ผ๋Š” ๋ณ€์ˆ˜๋Š” StaticNested์—์„œ ์ฐธ์กฐ๊ฐ€ ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค๋Š” ๋ง์ž…๋‹ˆ๋‹ค.

     

    ๋ฌผ๋ก , static nested ํด๋ž˜์Šค์™€๋Š” ๋‹ค๋ฅด๊ฒŒ ๋‚ด๋ถ€ ํด๋ž˜์Šค์™€ ์ต๋ช… ํด๋ž˜์Šค๋Š” ๊ฐ์‹ธ๊ณ  ์žˆ๋Š” ํด๋ž˜์Šค์˜ ์–ด๋–ค ๋ณ€์ˆ˜๋ผ๋„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

     

    ๊ทธ๋ ‡๋‹ค๋ฉด, ๋ฐ˜๋Œ€๋กœ ๊ฐ์‹ธ๊ณ  ์žˆ๋Š” ํด๋ž˜์Šค์—์„œ Static Nested ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜๋‚˜ ๋‚ด๋ถ€ ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜๋กœ์˜ ์ ‘๊ทผํ•˜๋Š” ๊ฒƒ์€ ๊ฐ€๋Šฅํ• ๊นŒ์š”?

     

    ๋ฐ˜๋Œ€๋กœ์˜ ์ฐธ์กฐ๋„ ๋ฌผ๋ก  ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

     

    package nested;
    
    public class ReferenceAtNested {
        static class StaticNested{
            private int staticNestedInt = 99;
        }
        class Inner{
            private int innerValue = 100;
        }
        public void setValue(int value){
            StaticNested nested = new StaticNested();
            nested.staticNestedInt = value;
            Inner inner = new Inner();
            inner.innerValue = value;
        }
    }

    StaticNested ํด๋ž˜์Šค์™€ Inner ํด๋ž˜์Šค์— ๊ฐ๊ฐ ๋ณ€์ˆ˜๊ฐ€ ๋˜๊ณ , ๊ฐ์‹ธ๊ณ  ์žˆ๋Š” ReferenceNested ํด๋ž˜์Šค์— intํƒ€์ž…์˜ value๋ฅผ ๋งค๊ฐœ ๋ณ€์ˆ˜๋กœ ๋ฐ›๋Š” setValue(int) ๋ฉ”์†Œ๋“œ๊ฐ€ ์ถ”๊ฐ€๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ๊ฐ ํด๋ž˜์Šค์˜ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•œ ํ›„ ๊ทธ ๊ฐ’์„ ์ฐธ์กฐํ•˜๋Š” ๊ฒƒ์€ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ๊ทธ ๊ฐ’์ด private๋ผ๊ณ  ํ• ์ง€๋ผ๋„ ๋ชจ๋‘ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

    .

    .

    .

     

    ์ด์ƒ์œผ๋กœ ๋‚ด์šฉ์„ ๋งˆ๋ฌด๋ฆฌํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. 

    ๊ณ ์ƒํ•˜์…จ์Šต๋‹ˆ๋‹ค. ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค :)

     

    ๋Œ“๊ธ€

Designed by Tistory.