有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Java注释教程

我参与了一个使用注释的Java项目。是否可以开发自定义注释,或者仅限于Java框架已经设置的注释

如果web上有任何优秀的Java注释教程可以展示如何构建自定义注释,请有人向我提供该链接

我这样问是因为我相信这是我充分理解注释的最好方式


共 (2) 个答案

  1. # 1 楼答案

    可以自己构建注释,它们基本上是特殊的接口。查看甲骨文网站上的Lesson: Annotations,了解如何自己构建它们

    提示:找到这个链接就像在谷歌上搜索“java注释教程”一样简单。你可以用同样的方法找到其他的

  2. # 2 楼答案

    您可以创建自己的注释以满足需要

    这里有一个不错的教程:

    http://www.aviyehuda.com/blog/2009/12/29/create-your-own-java-customized-annotations/

    以下是您可能创建的新创建注释的一些元注释:

    元注释实际上是注释使用的注释

    -@Target–指定此注释附加到的元素类型

    ElementType.TYPE-can be applied to any element of a class
    ElementType.FIELD-can be applied to a field or property
    ElementType.METHOD-can be applied to a method level annotation
    ElementType.PARAMETER-can be applied to the parameters of a method
    ElementType.CONSTRUCTOR-can be applied to constructors
    ElementType.LOCAL_VARIABLE-can be applied to local variables
    ElementType.ANNOTATION_TYPE-indicates that the declared type itself is an annotation type
    

    -@Retention–指定此批注的保留级别

    RetentionPolicy.SOURCE—Retained only at the source level and will be ignored by the compiler
    RetentionPolicy.CLASS—Retained by the compiler at compile time, but will be ignored by the VM
    RetentionPolicy.RUNTIME—Retained by the VM so they can be read only at run-time
    

    -@Documented–默认情况下,java文档中会提到注释,此元注释将使此注释被提及

    -@Inherited–表示注释将自动被继承