Java 使用默认值在另一个自定义批注中定义自定义批注
我试图以以下方式声明自定义批注:Java 使用默认值在另一个自定义批注中定义自定义批注,java,annotations,Java,Annotations,我试图以以下方式声明自定义批注: Retention(RetentionPolicy.RUNTIME) @Target({ElementType.TYPE, ElementType.METHOD}) public @interface InnerAnnotation { } Retention(RetentionPolicy.RUNTIME) @Target({ElementType.TYPE, ElementType.METHOD}) public @interface OuterAnno
Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD})
public @interface InnerAnnotation {
}
Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD})
public @interface OuterAnnotation {
public String default "";
public InnerAnnotation innerAnnotation(); //here I wanted to do "public InnerAnnotation innerAnnotation() default {some default value}"
}
I wanted to use it in a way:
class first{
@OuterAnnotation(value = "new") //wanted to declare something like this without need to define innerAnnotation
public void func(){
}
}
我想为内部注释用法指定一些默认值(这样在使用时就不必提供任何强制值),但有些问题是我无法做到的,因为编译器要求为此提供编译时常量。是否可以建议如何使用具有任何默认值的内部注释?您需要的语法如下所示:
@Retention(RetentionPolicy.RUNTIME)
@目标({ElementType.TYPE,ElementType.METHOD})
公共@接口外部表示法{
公共字符串默认为“”;
public InnerAnnotation()default@InnerAnnotation();//这就可以了;
}
让我知道场景或如何使用此注释?