• JavaSE之注解


    注解的属性类型

    注解只能存放简单的数据
    如:

    基本数据类型
    String
    Class
    注解
    枚举
    以上类型的一维数组

    格式:

    public @interface 注解名称 {

    public 属性类型 属性名();
    }

    public @interface MyAn1 {
        public String name();//String类型
        int age();//基本数据类型
        Class aaa();//Class类型
        Enum1 bbb();//枚举类型
        //注解类型
        MyAn2 ddd();
        //以上类型的一维数组
        //不能用自定义类型
        String[] ccc();
        String value();//名字为value时,在用注解时可以省略
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    public @interface MyAn2 {//空注解
    }
    
    • 1
    • 2
    public @interface MyAn3 {
        String name();
        int age();
    }
    
    • 1
    • 2
    • 3
    • 4
    @MyAn3(name = "李四",age = 99)
    public class Demo {
        public static void main(String[] args) {
    
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    元注解

    什么是元注解?

    修饰注解的注解

    元注解@Target

    正常情况注解可以放在类的任何一个元素上面

    作用:

    用来标识注解使用的位置,如果没有使用该注解标识,则自定义的注解可以使用在任意位置。

    可使用的值定义在ElementType枚举类中,常用值如下
    TYPE,类,接口

    FIELD, 成员变量

    METHOD, 成员方法

    PARAMETER, 方法参数

    CONSTRUCTOR, 构造方法

    LOCAL_VARIABLE, 局部变量

    元注解@Retention

    作用
    用来标识注解的生命周期(有效范围)

    可使用的值定义在RetentionPolicy枚举类中,常用值如下

    默认是CLASS

    SOURCE:注解只作用在源码阶段,生成的字节码文件中不存在

    CLASS:注解作用在源码阶段,字节码文件阶段,运行阶段不存在,默认值

    RUNTIME:注解作用在源码阶段,字节码文件阶段,运行阶段

    注解解析

    注解解析就是得到注解中的数据

    AnnotatedElement接口

    Annotation[] getAnnotations() 获取所有注解

    < Annotation > T getAnnotation(Class< T > annotationClass) 获取一个指定的注解

    boolean isAnnotationPresent(Class< Annotation > annotationClass) 判断是否有指定的注解

    如何解析注解?

    通过反射来解析注解,原则注解在谁头上就用谁来解析

    如果注解在构造方法上,使用Constructor来获取

    如果注解在成员方法上,使用Method来获取

    如果注解在成员变量上,使用Field来获取

    定义这样一个注解,必须是RUNTIME时期,因为getAnnotations只能拿到RUNTIME时期的注解

    @Retention(RetentionPolicy.RUNTIME)
    public @interface StudentAnno {
        String name();
        int age();
        String[] parent();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    将注解用于此类方法

    public class Student {
    
        @StudentAnno(name = "elephant",age = 11,parent = {"dog", "cat"})
        public void study(){
            System.out.println("认真学习");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    解析注解

    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException {
            Class<Student> cls = Student.class;
            Method study = cls.getMethod("study");
            boolean b = study.isAnnotationPresent(StudentAnno.class);
            if (b) {
                StudentAnno annotation = study.getAnnotation(StudentAnno.class);
                System.out.println("我的名字是" + annotation.name() + "\r\n" + "今年" + annotation.age() + "岁\r\n" + "父母是" + Arrays.toString(annotation.parent()));
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    在这里插入图片描述

    模拟junit

    首先创建一个注解

    @Retention(RetentionPolicy.RUNTIME)
    public @interface MyTest {
    }
    
    • 1
    • 2
    • 3

    接着在方法上添加注解

    public class Teacher {
    
        /*
        所以junit里面的方法是public 无返回值 ,无参数
        方便反射
         */
        public void study(){
            System.out.println("学习");
        }
    
        @MyTest
        public void eat(){
            System.out.println("吃饭");
        }
    
        @MyTest
        public void sleep(){
            System.out.println("睡觉");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    最后解析注解,如果有此注解则运行方法

    public static void main(String[] args) throws Exception {
            Class<Teacher> cls = Teacher.class;
            Method[] methods = cls.getMethods();
            Teacher teacher = cls.getConstructor().newInstance();
            for(Method m : methods){
                boolean b = m.isAnnotationPresent(MyTest.class);
                if(b){
                    m.invoke(teacher);
                }
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    最后

    如果你对本文有疑问,你可以在文章下方对我留言,敬请指正,对于每个留言我都会认真查看。

  • 相关阅读:
    Mysql 18式
    用Java写一个贪吃蛇游戏
    【kali-信息收集】(1.6)服务的指纹识别:Nmap、Amap
    程序员的护城河
    文心一言 vs GPT-4 —— 全面横向比较
    『力扣刷题本』:合并两个有序链表(递归解法)
    laravel高校毕业实习管理系统
    GoLang连接mysql数据库
    STM32物联网项目-RS485通信(Modbus协议)
    安全、可靠、合规,华为云守护企业网站安全
  • 原文地址:https://blog.csdn.net/weixin_47543906/article/details/127905007