我最近正在处理很多接口(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/