java匿名类

1. 匿名子类

class A {...}           // 父类
class B extends A{...}  // 非匿名子类
class E {
    public static void main(String args[]) {
        B b = new B();
        A b = new A() { // 匿名子类
            ...
        };
    }
}

2. 匿名接口

interface Com {...}           // 接口
class A implements Com {...}  // 接口实现
class E {
    public static void main(String args[]) {
        Com com = new A();
        Com com = new Com() { // 匿名接口
            ...
        };
    }
}

3. 要点

  • 匿名类中不能定义静态初始化块
  • 匿名类中不能定义构造方法
  • 匿名类中不能定义接口

原文地址:https://www.cnblogs.com/bpf-1024/p/12831314.html