java - @inherited 也使 @Overrided 方法值继承了注释?

标签 java android inheritance interface annotations

我最近正在处理很多接口(interface)继承和注释,我对 @Inherited 注释有疑问。我知道默认情况下,子类和方法不会继承java注释。使用@Inherited注解我们可以让该注解被子类继承。

但它也适用于方法构造函数中的带注释的值吗?

例如:

@Inherited
@StringDef({ID_REAR, ID_FRONT})
@Retention(RetentionPolicy.SOURCE)
public @interface CustomId {
}

public interface Setting {
   void update(@CustomId String myCustomId);
}   

public class CustomSetting implements Setting{

   @Override public void update(String myCustomId) {

   }
}

在这种情况下,CustomSetting 中的方法 update 是否会为其构造函数 (String myCustomId) 实现注解 @CustomId

最佳答案

正如 Jesper 上面的回答,API docs非常清楚:@Inherit 注释不适用于除类之外的任何内容:

Note that this meta-annotation type has no effect if the annotated type is used to annotate anything other than a class. Note also that this meta-annotation only causes annotations to be inherited from superclasses; annotations on implemented interfaces have no effect.

关于java - @inherited 也使 @Overrided 方法值继承了注释?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41740474/

相关文章:

java - 用 Java 截取指定小程序或程序的屏幕截图?

android - 无法卸载Android应用:收到崩溃消息

java - 将位图设置为 TextView 的背景 - Android

objective-c - 如果我想添加类型化属性,子类化 NSNotification 是正确的途径吗?

c++ - 从多态指针检查类成员是否存在?

c++ - 构造函数C++的 super 与子类继承

java - 如何将 JPA 与随机数据库(即用户提交的)一起使用

java - android ClassNotFoundException : com. sun.msv.datatype.xsd.XSDatatype

java - 使用父类(super class) java 中的方法显示 arrayList 中的元素

android - 在 GridView 上准确显示 n 行