一、annotation

本文介绍了Java 5的新特性Annotation。它是JDK 5引入元数据后可调用的功能,能提供非程序本身的数据,对代码执行无影响,以@annotation形式应用于类、属性、方法等。还提到三种内置注解,以及使用@interface声明注解等基本用法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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
}

转载于:https://www.cnblogs.com/wpj593780933/p/10549899.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值