注解Annotation

基本概念

  1. 注解(Annotation)也叫做元数据,用于修饰包、类、方法、属性、构造器、局部变量等数据信息。
  2. 和注释一样,注解不影响程序逻辑,但是注解可以被编译或运行,等同于嵌套在代码中的补充信息。
  3. 在javaSE中,注解用于标记过时的功能,忽略警告等,在JavaEE中注解用于配置应用程序的任何切面,代替JavaEE旧版本遗留的繁冗代码和XML配置等。

使用Annotation时要在其前面增加@符号,并把该Annotation当做一个修饰符使用。同于修饰它支持的程序元素。

@Override注解

  1. @Override:限定某个方法,时重写父类方法,该注解只能用于方法;不能修饰其他类、包、属性等。
  2.  @Override 注解放在say方法上,表示子类重写了父类的该方法;即使没有写也构成重写。
  3. 写了@Override注解,编译器就会检查是否重写对应的父类方法,是则通过,否则编译错误。
@Override
public class Override_ {
    public static void main(String[] args) {
        System.out.println("这是一个@Override 注解的演示解释");
    }
}
class Father{
    public void say(){
        System.out.println("Father.say()");
    }
}
class Son extends Father{
    
    @Override       // 说明
    public void say() {
        System.out.println("Son.say()");
    }
}

选中@Override按下【Ctrl】+【b】可以进入Override.java可以查看到@interface(表示一个注解类)查看源码。

@Target是修饰注解的注解,称为元注解

@Target(ElementType.METHOD)        // 说明只能修饰方法
@Retention(RetentionPolicy.SOURCE)
public @interface Override {
}

 @Deprecated注解

  1. @Deprecated 修饰某个元素,表示该元素已经过时不推荐使用
  2. 可以修饰方法、类、字段、包、参数等。
  3.  @Deprecated版本升级过渡使用
@Deprecated
 public class Deprecated_ {
    public static void main(String[] args) {
        A a = new A();
        a.say();

    }
}
@Deprecated
class A{
    public int num=9;
    public void say(){
        System.out.println("这是一个过时不推荐使用的类或方法,但是还可以使用");
    }
}

同样地查看源码

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(value={CONSTRUCTOR, FIELD, LOCAL_VARIABLE, METHOD, PACKAGE, MODULE, PARAMETER, TYPE})
public @interface Deprecated {

兼容过渡:例如在jdk8中有A8类;升级到jdk11之后为A11类;那么A8就可以用@Deprecated来修饰

@SuppressWarnings注解

  1.  当我们不希望看到警告时(不影响程序正常运行的警告),可以使用@SuppressWarnings来抑制。
  2. 作用范围取决于放置位置,可以放在具体的语句,方法,类上。
@SuppressWarnings
public class SuperWarnings_ {
    // 抑制与使用raw类型相关的警告
    // 抑制与未检查的作业相关的警告
    // 抑制与未用的程式码及停用的程式码相关的警告
    @SuppressWarnings({"rawtypes","unchecked","unused"})
    public static void main(String[] args) {

        List list=new ArrayList();
        list.add("mike");
        list.add("jack");
        list.add("rick");
        int i;
        System.out.println(list.get(1));
    }
}

@SuppressWarnings源码

@Retention(RetentionPolicy.SOURCE)
public @interface SuppressWarnings {
 
    String[] value();
}

注解中存在的数组String[] value();设置一个数组可以传入。
  @SuppressWarning 中的属性介绍以及属性说明

@Retention注解

 只能用于修饰一个Annotation定义,用于指定该类Annotation可以保留多长时间,@Retention包含一个RetentionPolicy类型的成员变量,使用@Retention时value成员变量指定值:

@Retention的三种值:

  1. RetentionPolicy.SOURCE:编译器使用后丢弃这种策略
  2. RetentionPolicy.CLASS:编译器把注解记录在class文件中,当Java程序运行时,JVM不会保留注解。(这是默认值)
  3. RetentionPolicy.RUNTIME:编译器把注解记录在class文件中,当Java程序运行时,JVM会保留注解,程序可以通过反射获取该注解。

编译与运行示意图

@Target元注解

基本说明:用于修饰Annotation定义,用于指定被修饰的Annotation能修饰哪些程序元素。@target包含一个value的成员变量。

@target源码:

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Target {
    /**
     * Returns an array of the kinds of elements an annotation interface
     * can be applied to.
     * @return an array of the kinds of elements an annotation interface
     * can be applied to
     */
    ElementType[] value();
}

步入ElementType

public enum ElementType {
    /** Class, interface (including annotation interface), enum, or record
     * declaration */
    TYPE,

    /** Field declaration (includes enum constants) */
    FIELD,

    /** Method declaration */
    METHOD,

    /** Formal parameter declaration */
    PARAMETER,

    /** Constructor declaration */
    CONSTRUCTOR,

    /** Local variable declaration */
    LOCAL_VARIABLE,

    /** Annotation interface declaration (Formerly known as an annotation type.) */
    ANNOTATION_TYPE,

    /** Package declaration */
    PACKAGE,

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

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

    /**
     * Module declaration.
     *
     * @since 9
     */
    MODULE,

    /**
     * Record component
     *
     * @jls 8.10.3 Record Members
     * @jls 9.7.4 Where Annotations May Appear
     *
     * @since 16
     */
    RECORD_COMPONENT;
}

从各个注解的源码上可以看到注解作用的对象不一样;例如@Override的元注解中说明了@Override只能作用于方法。

@Documented注解

基本说明:用于指定该元Annotation修饰的Annotation类将被javadoc工具提取到文档,即生成文档时,可以看到该注解。

通过查阅jdk的API文档中过时的方法可以看到方法前都存在@Deprecated,就是通过@Documented声明javadoc工具提取到文档把注解保留下来的。

源码:

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

 @Inherited注解
被他修饰的Annotation将具有继承性;如果某个类是用了它修饰的Annotation,则子类会自动具有该注解。

总结

元注解:本身作用不大,通过查看源码只要知道它的作用即可。

 

posted @ 2024-07-05 16:27  Dr丶云幕  阅读(13)  评论(0编辑  收藏  举报