gpt4 book ai didi

Java - 注释引用本身有什么作用?

转载 作者:太空宇宙 更新时间:2023-11-04 06:09:31 26 4
gpt4 key购买 nike

我最近偶然发现了“文档化”Java 注释的源代码。它看起来像这样:

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Documented {
}

Documented 本身中“@Documented”的用途是什么?

最佳答案

来自documentation :

Indicates that annotations with a type are to be documented by javadoc and similar tools by default. This type should be used to annotate the declarations of types whose annotations affect the use of annotated elements by their clients. If a type declaration is annotated with Documented, its annotations become part of the public API of the annotated elements.

@Documented 是一个元注释 - 它注释另一种注释类型。当注释类型由 @Documented 注释时,javadoc 等工具在代码中使用时应包含该注释。

例如,@Deprecated注释类型用@Documented注释。了解某些内容是否已弃用非常重要,因此 @Deprecated 被视为公共(public) API 的一部分,应包含在文档中。

另一方面,@SuppressWarnings只是对编译器的一个提示,对API来说并不重要,所以没有用@Documented注释。

@Documented 注释类型也会注释自身。基本上,这仅意味着您将在文档中看到 @Documented 的任何用法。这样做是为了让您可以查看是否会记录任何注释类型。

关于Java - 注释引用本身有什么作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28906143/

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