Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.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 - Fatal编程技术网

Java自定义注释接受另一个注释

Java自定义注释接受另一个注释,java,Java,如何编写接受另一个注释和值的自定义注释 @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) public @interface TestAnnotation{ Class<? extends TestAnnotationChild> annotation(); } 我想做一些类似的事情 @TestAnnotation(@TestAnnotationChild={values}) 我怎样才能做那样的事情呢?

如何编写接受另一个注释和值的自定义注释

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface TestAnnotation{
  Class<? extends TestAnnotationChild> annotation();
}
我想做一些类似的事情

@TestAnnotation(@TestAnnotationChild={values})

我怎样才能做那样的事情呢?

你可以在你的
TestAnnotation
中有一个类型为
TestAnnotationChild
的属性,就像它是一个字符串,或者其他任何东西一样

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface TestAnnotation {
   TestAnnotationChild child();

   // Or for an array
   TestAnnotationChild[] children();
}
用法

然而,这是你陈述的一部分

价值观呢

让我觉得你想做一件不寻常的事吗
您能澄清一下吗?

您应该只使用
TestAnnotationChild value()而不是
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface TestAnnotation {
   TestAnnotationChild child();

   // Or for an array
   TestAnnotationChild[] children();
}
@TestAnnotation(
        @TestAnnotationChild(
               value = "42",
               anotherValue = 42
        )
)