@inherited还使@Overrided方法值继承了注释?

时间:2017-01-19 11:16:40

标签: java android inheritance interface annotations

我最近使用了很多接口继承和注释,我对@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) {

   }
}

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

1 个答案:

答案 0 :(得分:0)

正如Jesper在上面回答的那样,API docs非常明确:@Inherit注释不适用于除类之外的任何其他内容:

  

请注意,如果使用带注释的类型来注释除类之外的任何内容,则此元注释类型无效。另请注意,此元注释仅导致注释从超类继承;已实现的接口上的注释无效。

相关问题