内部类与匿名内部类

内部类定义位置:1.成员位置 2.局部位置

class Out {
    static int i = 111;

    public void method() {
        final int i = 222;
        class Iit {
            int i = 333;

            public void run() {
                int i = 444;
                System.out.println(i);
                System.out.println(Iit.this.i);
                System.out.println(Out.i);

            }
        }
        Iit j = new Iit();
        j.run();
    }
}

class Demo {
    public static void main(String[] args) {
        Out o = new Out();
        o.method();
    }
}
abstract class Father {
    public abstract void show();
}

class Outter {
    public void method() {
        class Son extends Father {
            public void show() {
                System.out.println("哈哈");
            }
        }
        Son s = new Son();
        s.show();
        Father f = new Son() {
            public void show() {
                System.out.println("呵呵,长见识了吧");
            }
        };
        f.show();
    }
}

class Demo {
    public static void main(String[] args) {
        Outter o = new Outter();
        o.method();

    }
}

你可能感兴趣的:(内部类与匿名内部类)