Java接口(interface)中的注解值

标签 java enums annotations

为什么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/

相关文章:

Java 8。泛型。使用原始类型。意外的类型转换

java - digital ocean 上如何学习java spring

java - 在 Maven 中存储程序集文件的正确位置是什么?

python - 为什么 Python 枚举中的可变值是同一个对象?

java - Java 中可配置的枚举

java - 如何查找带注释的函数参数的类型

java - 以树形菜单作为导航的 Primefaces 页面布局

C# 8 - 枚举上的 CS8605 "Unboxing possibly null value"

json - 根据用户角色控制字段可见性

Android javax.annotation.processing 包丢失