45. 内部クラス1

45. 内部クラス1

ネストトップクラス

L:\lesson114\test.java

class test {
    public static void main(String args[]) {
        test.Kitty obj = new test.Kitty();
        obj.write();

        Kitty obj2 = new Kitty();
        obj2.write();

        System.out.println(top.Kitty.str);

        top2 obj3 = new top2();
        obj3.run();
    }

    static class Kitty {
        void write() {
            System.out.println("test.Kitty");
        }
    }
}

class top {
    static class Kitty {
        public static final String str = "top.Kitty";
    }
}

class top2 {
    void run() {
        top2.Kitty obj = new top2.Kitty();
        obj.write();
    }
    private static class Kitty {
        void write() {
            System.out.println("top.2Kitty");
            top2.Kitty.Sakura obj = new top2.Kitty.Sakura();
            obj.write();
        }
        private static class Sakura {
            void write() {
                System.out.println("top2.Kitty.Sakura");
            }
        }
    }
}

実行結果

L:\lesson114>java test
test.Kitty
test.Kitty
top.Kitty
top.2Kitty
top2.Kitty.Sakura