java匿名类

JAVA学习网 2020-05-06 08:33:02

 

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. 要点

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

 

阅读(2558) 评论(0)