• 一、annotation


    java.lang.annotation,接口 Annotation。对于Annotation,是Java5的新特性,JDK5引入了Metadata(元数据)很容易的就能够调用Annotations。Annotations提供一些本来不属于程序的数据,比如:一段代码的作者或者告诉编译器禁止一些特殊的错误。An annotation 对代码的执行没有什么影响。Annotations使用@annotation的形式应用于代码:类(class),属性(attribute),方法(method)等等。一个Annotation出现在上面提到的开始位置,而且一般只有一行,也可以包含有任意的参数。

    对于Annotation,是Java5的新特性,下面是Sun的Tutorial的描述,因为是英文,这里我翻译下,希望能够比较清晰的描述一下Annotation的语法以及思想。Annotation:Release 5.0 of the JDK introduced a metadata facility called annotations. Annotations provide data about a program that is not part of the program,such as naming the author of a piece of code or instructing the compiler to suppress specific errors. An annotation has no effect on how the code performs. Annotations use the form @annotation and may be applied to a program’s declarations: its classes,fields,methods,and so on. The annotation appears first and often (by convention) on its own line,and may include optional arguments: JDK5引入了Metadata(元数据)很容易的就能够调用Annotations.Annotations提供一些本来不属于程序的数据,比如:一段代码的作者或者告诉编译器禁止一些特殊的错误。An annotation 对代码的执行没有什么影响。Annotations使用@annotation的形式应用于代码:类(class),属性(field),方法(method)等等。一个Annotation出现在上面提到的开始位置,而且一般只有一行,也可以包含有任意的参数。@Author(“MyName”)class myClass() { }
    or @SuppressWarnings(“unchecked”)void MyMethod() { }
    Defining your own annotation is an advanced technique that won’t be described here,but there are three built-in annotations that every Java programmer should know: @Deprecated,@Override,and @SuppressWarnings. The following example illustrates all three annotation types,applied to methods:
    定义自己的Annotation是一个比较高级的技巧,这里我们不做讨论,这里我们仅仅讨论每一个Java programer都应该知道的内置的annotations:@Deprecated,@Override,and @SuppressWarnings。下面的程序阐述了这三种annotation如何应用于methods。import java.util.List;

    二、基本用法

    //声明Test注解
    @Target(ElementType.METHOD)
    @Retention(RetentionPolicy.RUNTIME)
    public @interface Test {
    
    } 
    

       我们使用了@interface声明了Test注解,并使用@Target注解传入ElementType.METHOD参数来标明@Test只能用于方法上,@Retention(RetentionPolicy.RUNTIME)则用来表示该注解生存期是运行时,从代码上看注解的定义很像接口的定义,确实如此,毕竟在编译后也会生成Test.class文件。对于@Target和@Retention是由Java提供的元注解,所谓元注解就是标记其他注解的注解,下面分别介绍

    public enum ElementType {
        /**标明该注解可以用于类、接口(包括注解类型)或enum声明*/
        TYPE,
    
        /** 标明该注解可以用于字段(域)声明,包括enum实例 */
        FIELD,
    
        /** 标明该注解可以用于方法声明 */
        METHOD,
    
        /** 标明该注解可以用于参数声明 */
        PARAMETER,
    
        /** 标明注解可以用于构造函数声明 */
        CONSTRUCTOR,
    
        /** 标明注解可以用于局部变量声明 */
        LOCAL_VARIABLE,
    
        /** 标明注解可以用于注解声明(应用于另一个注解上)*/
        ANNOTATION_TYPE,
    
        /** 标明注解可以用于包声明 */
        PACKAGE,
    
        /**
         * 标明注解可以用于类型参数声明(1.8新加入)
         * @since 1.8
         */
        TYPE_PARAMETER,
    
        /**
         * 类型使用声明(1.8新加入)
         * @since 1.8
         */
        TYPE_USE
    }
  • 相关阅读:
    nginx学习二:快速入门
    nginx学习一:http协议
    使用itext生成pdf的,各种布局
    关于java poi itext生成pdf文件的例子以及方法
    半透明全屏蒙层+全屏屏蔽+内容居中+css
    通过html文件生成PDF文件
    mybatis中文官网
    经典sql语句
    关于Cell中的各种值的类型判断
    bootstrap表格参数说明
  • 原文地址:https://www.cnblogs.com/wpj593780933/p/10549899.html
Copyright © 2020-2023  润新知