Java

๋‚ด๋ถ€ ํด๋ž˜์Šคinner class

voider 2020. 9. 9. 10:42

๐Ÿ“š ์ž๋ฐ”์˜ ์ •์„์„ ์ •๋ฆฌํ•œ ๋‚ด์šฉ์ž…๋‹ˆ๋‹ค.

๋‚ด๋ถ€ํด๋ž˜์Šค์˜ ์ข…๋ฅ˜

๋‚ด๋ถ€ ํด๋ž˜์Šค๋Š” ๋‹จ์ˆœํžˆ ํด๋ž˜์Šค ๋‚ด์— ์„ ์–ธ๋œ ํด๋ž˜์Šค๋‹ค.

  • ๋‚ด๋ถ€ ํด๋ž˜์Šค์˜ ์žฅ์ 
    1.๋‚ด๋ถ€ ํด๋ž˜์Šค์—์„œ ์™ธ๋ธŒ ํด๋ž˜์Šค ๋ฉค๋ฒ„์— ์‰ฝ๊ฒŒ ์ ‘๊ทผ ๊ฐ€๋Šฅ
    2.์ฝ”๋“œ ๋ณต์žก์„ฑ์„ ์ค„์ธ๋‹ค(์บก์Šํ™”)


์œ„ ๋‹ค์ด์–ด๊ทธ๋žจ์—์„œ ๋ณด๋‹ค์‹œํ”ผ ๋‚ด๋ถ€ ํด๋ž˜์Šค๋Š” ํด๋ž˜์Šค์˜ ๋ฉค๋ฒ„์ฒ˜๋Ÿผ ๋‹ค๋ฃฌ๋‹ค. ๋‹ค๋ฅธ ์ ์€ ์ต๋ช… ํด๋ž˜์ŠคAnonymous class๊ฐ€ ์žˆ๋‹ค.

๋‚ด๋ถ€ํด๋ž˜์Šค์˜ ์ œ์–ด์ž, ์ ‘๊ทผ์„ฑ

์ž๋ฐ”์˜ ์ •์„ ์˜ˆ์ œ์— ๊ฐ„๋‹จํžˆ ์ฃผ์„์„ ๋‹ฌ์•˜์Šต๋‹ˆ๋‹ค.

class Outer {
    class InstanceInner {
        int iv = 100;
    }

    static class StaticInner {
        int iv = 200;
        static int cv = 300;
    }

    void myMethod() {
        class LocalInner {
            int iv = 400;
        }
    }
}

public class InnerEx4 {
    public static void main(String[] args) {
        /*
        * ์ธ์Šคํ„ด์Šค๋‚ด๋ถ€ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•˜๋ ค๋ฉด
        * ์™ธ๋ถ€ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค๊ฐ€ ๋จผ์ € ํ•„์š”ํ•˜๋‹ค.
        * */
        Outer outer = new Outer();
        Outer.InstanceInner instanceInner = outer.new InstanceInner();

        System.out.println("instanceInner.iv : " + instanceInner.iv);
        System.out.println("Outer.StaticInner.cv : " + Outer.StaticInner.cv);

        /*static ๋‚ด๋ถ€ ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค๋Š” ์™ธ๋ถ€ ํด๋ž˜์Šค๋ฅผ ๋จผ์ € ์ƒ์„ฑํ•˜์ง€ ์•Š์•„๋„ ๋œ๋‹ค.*/
        Outer.StaticInner staticInner = new Outer.StaticInner();
        System.out.println("staticInner.iv : " + staticInner.iv);
        System.out.println("staticInner.iv : " + staticInner);
    }
}

์ต๋ช…ํด๋ž˜์Šคanonymous class

์ต๋ช…ํด๋ž˜์Šค๋Š” ์ด๋ฆ„ ์—†๋Š” ๋‚ด๋ถ€ ํด๋ž˜์Šค๋‹ค. ์ด๋ฆ„์ด ์—†์œผ๋ฏ€๋กœ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์—†๊ณ , ์žฌ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์—†๋‹ค.

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Anonymous {
    public static void main(String[] args) {
        Button button = new Button("Start");
    }
}

class EventHandler implements ActionListener {

    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("ActionEvent Occured!");
    }
}

์ด๋Ÿฐ ์ฝ”๋“œ๋ฅผ

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Anonymous {
    public static void main(String[] args) {
        Button button = new Button("Start");
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("ActionEvent occurred!!!");
            }
        });
    }
}

์ต๋ช…ํด๋ž˜์Šค๋ฅผ ์ด์šฉํ•ด์„œ ์ด๋ ‡๊ฒŒ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋‹ค.

'Java' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

Stack & Queue  (0) 2020.09.09
Collections Framework - ํ•ต์‹ฌ ์ธํ„ฐํŽ˜์ด์Šค  (0) 2020.09.09
์ถ”์ƒํด๋ž˜์Šค & ์ธํ„ฐํŽ˜์ด์Šค  (0) 2020.09.09
๋‹คํ˜•์„ฑPolymorphism  (0) 2020.09.06
์ƒ์†Inheritance  (0) 2020.09.06