gpt4 book ai didi

java - 注释怎么可能是对自身的注释?

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:25:57 25 4
gpt4 key购买 nike

我在浏览 JDK 7 的文档时注意到 java.lang.annotation 包中有一个名为 @Target 的注解。该类的标题是

@Documented
@Retention(value=RUNTIME)
@Target(value=ANNOTATION_TYPE)
public @interface Target

现在,@Target 被用作对自身的注解。这怎么可能? @Target 甚至在声明之前就在 header 中使用了。我用我写的注释试了一下,效果也不错。谁能解释这里发生了什么?

最佳答案

JLS 在 section 9.6 Annotation Types 中特别预料到这一点:

If an annotation a (§9.7) on an annotation type declaration corresponds to an annotation type T, and T has a (meta-)annotation m that corresponds to java.lang.annotation.Target, then m must have either an element whose value is java.lang.annotation.ElementType.ANNOTATION_TYPE, or an element whose value is java.lang.annotation.ElementType.TYPE, or a compile-time error occurs.

第 9.6 节或第 9.7 节中没有任何其他部分说明使用对正在声明的注释的引用来注释注释声明是非法的。

关于java - 注释怎么可能是对自身的注释?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17629873/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com