Annotation可以实现接口吗?

Kuc*_*.CZ 21 java annotations interface

注释中是否有可能实现接口?就像是:

public @interface NotNull implements LevelInterface  {
    ValidationLevel level();
};
Run Code Online (Sandbox Code Playgroud)

Thi*_*ilo 15

不,编译器说:

注释类型声明不能具有显式的超接口

你不能延长:

注释类型声明不能具有显式超类


Sea*_*oyd 13

不,注释不能有超级接口*(虽然接口可以从注释扩展,类可以实现注释,这两者都是一个可怕的实践imho)

[*]有趣的是:我找不到任何明确说明的文档,除了java编译器输出(既不是Sun Java教程,也不是Java 1.5 Annotations规范)


sfu*_*ger 6

不,你不能(正如我的评论中所说)。不过,您可以使用委托(正如 AlexR 所说)。但是,您必须使用枚举:

public enum LevelEnum implements LevelInterface {
  DEFAULT {
    public ValidationLevel level() {
      // SNIP (your code)
    }
  };
}

public @interface NotNull {
  LevelEnum level() default LevelEnum.DEFAULT;
}
Run Code Online (Sandbox Code Playgroud)