注释匿名内部类 [英] Annotate anonymous inner class

查看:187
本文介绍了注释匿名内部类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法标注Java中的匿名内部类?

在这个例子中,你可以添加一个类级别的注释为Class2中?

 公共无效方法1(){
  添加(新等级2(){
    公共无效method3(){}
  });
}


解决方案

没有。你需要把它提升到一个适当类。它仍然可以在外部类中如果有必要范围,所以它并不需要是一个顶级类,或公开,或什么的。但它确实需要一个适当的类定义注释重视。

Is there a way to annotate an anonymous inner class in Java?

In this example could you add a class level annotation to Class2?

public void method1() {
  add(new Class2() {
    public void method3() {}
  });
}

解决方案

No. You'd need to promote it to a "proper" class. It can still be scoped within the outer class if necessary, so it doesn't need to be a top-level class, or public, or whatever. But it does need a proper class definition to attach the annotation to.

这篇关于注释匿名内部类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆