为什么java编译器不允许在注解中使用接口(interface)?
我可以在注释中使用枚举:
public @interface SomeAnnotation {
SomeEnum[] value() default {};
}
我可以在枚举中实现接口(interface):
public enum SomeEnum implements SomeInterface {
NAME1, NAME2;
}
但我不能在注释中使用接口(interface):
public @interface SomeAnnotation {
SomeInterface[] value() default {};
}
很郁闷...
最佳答案
因为接口(interface)不是有效的返回类型,根据 JLS注解类型中声明的方法的返回类型必须是以下类型之一,否则会出现编译时错误:
A primitive type
String
Class or an invocation of Class (§4.5)
An enum type
An annotation type
An array type whose component type is one of the preceding types (§10.1).
关于Java接口(interface)中的注解值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27764775/