欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

java 匿名内部类,

来源: javaer 分享于  点击 25236 次 点评:164

java 匿名内部类,


1.匿名内部类的调用

匿名内部类会继承或者实现此类。作为一个子类

2. 匿名内部类的应用

public  class Student<T> {
    private String name;

    protected Student(String name) {
        this.name = name;
    }
}

3. 测试

public class TestStudent {
    public static void main(String[] args) {
        Student student = new Student("test"){};
    }
}

Student student = new Student(“test”){}; 会继承一个Student的匿名内部类,如下:

public class Anonymous extends TestStudent {
    Anonymous () {
        super();
    }
}

使用javap编译class文件,可以看到jvm新创建了一个类:
创建的匿名内部类TestStudent$1.class

相关文章

    暂无相关文章
相关栏目:

用户点评