12.【Java核心.注解】JDK内置注解,元注解,自定义注解

注解

1.注解的概念

注解: 说明程序的,给计算机看的

注释: 用文字描述程序,给程序员看的

/**
*
*/
//

定义:

​ 注解(Annotation),也叫元数据。一种代码级别的说明。它是JDK1.5及以后版本引入的一个特性,与类、接口、枚举是在同一个层次。它可以声明在包、类、字段、方法、局部变量、方法参数等的前面,用来对这些元素进行说明,注释。

作用分类:

1.编写文档:通过代码里标识的注解生成文档【生成文档doc文档】

2.代码分析:通过代码里标识的注解对代码进行分析【使用反射】

3.编译检查:通过代码里标识的注解让编译器能够实现基本的编译检查【Override】

2.JDK预定义的注解

@Override:检查被该注解标注的方式是否是继承自父类【接口】

@Deprecated: 该注解表示注释的内容过时

@SuppressWarnings: 压制警告

all to suppress all warnings (抑制所有警告)
boxing to suppress warnings relative to boxing/unboxing operations(抑制装箱、拆箱操作时候的警告)
cast to suppress warnings relative to cast operations (抑制映射相关的警告)
dep-ann to suppress warnings relative to deprecated annotation(抑制启用注释的警告)
deprecation to suppress warnings relative to deprecation(抑制过期方法警告)
fallthrough to suppress warnings relative to missing breaks in switch statements(抑制确在switch中缺失breaks的警告)
finally to suppress warnings relative to finally block that don’t return (抑制finally模块没有返回的警告)
hiding to suppress warnings relative to locals that hide variable()
incomplete-switch to suppress warnings relative to missing entries in a switch statement (enum case)(忽略没有完整的switch语句)
nls to suppress warnings relative to non-nls string literals(忽略非nls格式的字符)
null to suppress warnings relative to null analysis(忽略对null的操作)
rawtypes to suppress warnings relative to un-specific types when using generics on class params(使用generics时忽略没有指定相应的类型)
restriction to suppress warnings relative to usage of discouraged or forbidden references
serial to suppress warnings relative to missing serialVersionUID field for a serializable class(忽略在serializable类中没有声明serialVersionUID变量)
static-access to suppress warnings relative to incorrect static access(抑制不正确的静态访问方式警告)
synthetic-access to suppress warnings relative to unoptimized access from inner classes(抑制子类没有按最优方法访问内部类的警告)
unchecked to suppress warnings relative to unchecked operations(抑制没有进行类型检查操作的警告)
unqualified-field-access to suppress warnings relative to field access unqualified (抑制没有权限访问的域的警告)
unused to suppress warnings relative to unused code  (抑制没被使用过的代码的警告)
@SuppressWarnings("all")
public class AnnoDemo01 {

    @Override
    public String toString() {
        return "AnnoDemo01{}";
    }

    @Deprecated
    public void show1(){
        // 发现过时了,功能更不上需求了
    }

    public void show2(){
        // 功能更加强大的方法
    }

    public void demo(){
        show1(); // 不推荐使用,但是可以使用
        show2();
        Date date = new Date();
        date.getYear();
    }
}

3.自定义注解

格式

// 元注解
public @interface 注解名称{
    // 属性列表
}

自定义的注解反编译后的内容

public interface MyAnno extends java.lang.annotation.Annotation {

}

注解的本质其实就是一个接口,继承Annotation父接口

/**
 * 注解的本质就是接口
 */
public @interface MyAnno {
    public String show();
}

属性:在接口中定义的抽象方法

返回结果必须是如下类型

1.基本数据类型

2.String类型

3.枚举类型

4.注解

5.以上类型的数组

属性赋值注意点:

1.如果定义的属性时,使用default关键字给属性默认初始值,可以在使用注解是不赋值

2.如果只有一个属性需要赋值,而且该属性的名称是value,那么在赋值时 value可以省略

3.数组赋值的时候,值使用{}包裹,如果数组中只有一个值,那么{}可以省略

/**
 * 注解的本质就是接口
 */
public @interface MyAnno {

    String value();
    MyAnno2 show4();
    PersonEnum show5();
    String[] show3();
    //String name();
    //int age() default 18; // 指定默认值 在使用注解的时候没有给该属性赋值,那么就使用默认值

    /*String show1();*/
    /*int show2();
    String[] show3();
    MyAnno2 show4();
    PersonEnum show5();*/

}
@MyAnno(value="bobo",show4 = @MyAnno2,show5 = PersonEnum.P1,show3 = "a")
public void show2(){
    // 功能更加强大的方法
}

4.元注解

JDK中给我们提供的4个元注解

1.@Target:描述当前注解能够作用的位置

​ ElementType.TYPE:可以作用在类上

​ ElementType.METHOD:可以作用在方法上

​ ElementType.FIELD:可以作用在成员变量上

2.@Retention: 描述注解被保留到的阶段

​ SOURCE < CLASS < RUNTIME

​ SOURCE:表示当前注解只在代码阶段有效

​ CLASS:表示该注解会被保留到字节码阶段

​ RUNTIME:表示该注解会被保留到运行阶段 JVM

​ 自定义的注解:RetentionPolicy.RUNTIME

3.@Documented:描述注解是否被抽取到JavaDoc api中

4.@inherited:描述注解是否可以被子类继承

@Target({ElementType.TYPE,ElementType.METHOD,ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface MyAnno3 {
}

5.自定义注解的案例

/**
 * 自定义注解
 *     该注解表面要执行哪个类中的哪个方法
 */
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface InvokAnno {

    String className();
    String methodName();
}
public class Student1 {
    public void show(){
        System.out.println("student1 show ....");
    }
}
@InvokAnno(className = "com.gupao.edu.anno2.Student2",methodName = "show")
public class MyMain {

    public static void main(String[] args) throws Exception {
        // 获取类对象
        Class<MyMain> clazz = MyMain.class;
        // 获取类对象中的注解
        InvokAnno an = clazz.getAnnotation(InvokAnno.class);
        /**
         *  注解本质是 接口  获取到的其实是接口的实现
         *  public class MyInvokAnno implements InvokAnno{
         *
         *      String className(){
         *          return "com.gupao.edu.anno2.Student1";
         *      }
         *      String methodName(){
         *          return "show";
         *      }
         *  }
         */
        // 获取注解中对应的属性
        String className = an.className();
        String methodName = an.methodName();
        System.out.println(className);
        System.out.println(methodName);

        // 通过反射的方式实现接口的功能
        Class<?> aClass = Class.forName(className);
        Method show = aClass.getDeclaredMethod("show");
        // 方法的执行
        Object o = aClass.newInstance();
        show.invoke(o); // 执行对应的方法
    }
}
posted @ 2021-03-16 14:02  想~(●—●)肥~  阅读(115)  评论(0编辑  收藏  举报