Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用默认值在另一个自定义批注中定义自定义批注_Java_Annotations - Fatal编程技术网

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();//这就可以了;
}

让我知道场景或如何使用此注释?