注解

注解

什么是注解

Annotation是从JDK5.0开始引入的新技术

Annotation的作用 :

  1. 不是程序本身 , 可以对程序作出解释.(这一点和注释(comment)没什么区别)
  2. 可以被其他程序(比如:编译器等)读取

Annotation的格式 :
注解是以"@注释名"在代码中存在的 , 还可以添加一些参数值 , 例如:@SuppressWarnings(value="unchecked")

Annotation在哪里使用?
可以附加在package , class , method , field 等上面 , 相当于给他们添加了额外的辅助信息, 我们可以通过反射机制编程实现对这些元数据的访问

在之前的代码编写中,经常会用到重写,在我们重写的方法上会有一个特殊标记@Override ,这个标记代表重写,这个标记叫做注解,和我们之前用到的注释不同,注释只是给程序员和用户观看,方便理解代码的逻辑,对计算机本身而言并没有什么用处,而注解不但可以给程序员起到提示的作用,也会给计算机起到提示的作用

查看Override源码:

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.SOURCE)
public @interface Override {
}

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.SOURCE)

上面的两个注解为元注解,元注解是注解的注解,一共有四个元注解:

  1. @Target(value ={ElementType.METHOD,ElementType.TYPE,ElementType.FIELD})
    

上面的注解中的参数决定注解的作用域

  1. @Retention(RetentionPolicy.RUNTIME) 参数决定注解运行时级别
  2. @Documented 产生DOC文档时候使用
  3. @Inherited 子类可以继承父类的注解

元注解源码查看

Target

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)         //前三个都是元注解
public @interface Target {
    /**
     * Returns an array of the kinds of elements an annotation type
     * can be applied to.
     * @return an array of the kinds of elements an annotation type
     * can be applied to
     */
    ElementType[] value();      //参数是ElementType类的数组
    //参数类型        //参数名字
}

查看 ElementType[]

public enum ElementType {          //枚举类型
    /** Class, interface (including annotation type), or enum declaration */
    TYPE,    //表示作用域是类

    /** Field declaration (includes enum constants) */
    FIELD,      //成员变量

    /** Method declaration */
    METHOD,         //方法                      //注释的这三个也是最常用的

    /** Formal parameter declaration */
    PARAMETER,

    /** Constructor declaration */
    CONSTRUCTOR,

    /** Local variable declaration */
    LOCAL_VARIABLE,

    /** Annotation type declaration */
    ANNOTATION_TYPE,

    /** Package declaration */
    PACKAGE,

    /**
     * Type parameter declaration
     *
     * @since 1.8
     */
    TYPE_PARAMETER,

    /**
     * Use of a type
     *
     * @since 1.8
     */
    TYPE_USE
}

要注意,使用该元注解时如果有多个参数,value不能省略,参数使用大括号括起,中间使用逗号隔开

Retention

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Retention {
    /**
     * Returns the retention policy.
     * @return the retention policy
     */
    RetentionPolicy value();
}

查看RetentionPolicy

public enum RetentionPolicy {
    /**
     * Annotations are to be discarded by the compiler.
     */
    SOURCE,

    /**
     * Annotations are to be recorded in the class file by the compiler
     * but need not be retained by the VM at run time.  This is the default
     * behavior.
     */
    CLASS,

    /**
     * Annotations are to be recorded in the class file by the compiler and
     * retained by the VM at run time, so they may be read reflectively.
     *
     * @see java.lang.reflect.AnnotatedElement
     */
    RUNTIME        //最常用
}

Documented

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

该元注解无参数,直接使用

Inherited

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

内置注解

@Override : 定义在 java.lang.Override 中 , 此注释只适用于修辞方法 , 表示一个方法声明打算重写超类中的另一个方法声明.

@Deprecated : 定义在java.lang.Deprecated中 , 此注释可以用于修辞方法 , 属性 , 类 , 表示不鼓励程序员使用这样的元素 , 通常是因为它很危险或者存在更好的选择

@SuppressWarnings : 定义在java.lang.SuppressWarnings中,用来抑制编译时的警告信息. 与前两个注释有所不同,你需要添加一个参数才能正确使用,这些参数都是已经定义好了的, 我们选择性的使用就好了

  1. @SuppressWarnings("all")
  2. @SuppressWarnings("unchecked")
  3. @SuppressWarnings(value={"unchecked","deprecation"})
  4. 等等…

自定义注解

使用 @interface自定义注解时 , 自动继承了java.lang.annotation.Annotation接口

分析 :

  1. @ interface用来声明一个注解 , 格式 : public @ interface 注解名 { 方法 }
  2. 其中的每一个方法实际上是声明了一个配置参数
  3. 方法的名称就是参数的名称.
  4. 返回值类型就是参数的类型 ( 返回值只能是基本类型,Class , String , enum )
  5. 可以通过default来声明参数的默认值
  6. 如果只有一个参数成员 , 一般参数名为value
  7. 注解元素必须要有值,我们定义注解元素时,经常使用空字符串,0作为默认值.

自定义注解示例:

@Target(value = {ElementType.METHOD,ElementType.TYPE,ElementType.FIELD}) // 作用域
@Retention(RetentionPolicy.RUNTIME)  //运行时级别
@Documented  // 生产DOc文档时候使用
@Inherited //子类可以继承父类的注解
@interface MyAnnotation{

}

通过上述格式便可完成自定义注解,之后可以直接使用

查看Thread类:
在这里插入图片描述在这里可以看到个别方法上有横线 ,说明该方法已经废弃或者有更好的方法来代替,其实使用的是@Deprecated注解

代码示例:
在这里插入图片描述
在给stop方法加上@Deprecated注解后,可以看到stop方法在被调用时在上面自动加上了删除线,来提示用户改方法已经过时或者有更好的方法来替代

将注解注释掉
将注解注释掉可以看到删除线消失,这就是注解的作用,可以起到提示计算机和用户的功能,功能比较强大,在以后还会进行及时的比较详细的补充

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值