• 反射和注解


    反射和注解

    1.反射

    Java反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法
    对于任意一个对象,都能够调用它的任意一个方法和属性
    这种动态获取的信息以及动态调用对象的方法的功能称为java语言的反射机制
    
    • 1
    • 2
    • 3

    2.获取字节码的三种方式

    2.1通过权限定名 获取字节码对象

    Class aClass = Class.forName("com.aaa.day01.test.People");
    
    • 1

    2.2通过类名获取

    Class peopleClass = People.class;
    
    • 1

    2.3通过对象名获取

    People people = new People();
    Class aClass1 = people.getClass();
    
    • 1
    • 2

    3.通过反射创建对象

    在这里插入图片描述

    Class aClass = Class.forName("com.aaa.day01.test.People");
    
    Constructor constructor = aClass.getConstructor();
    Object o = constructor.newInstance(); // People p = new People(); 调用无参构造
    
    Constructor constructor = aClass.getConstructor(int.class);
    Object o1 = constructor.newInstance(10); // People p = new People(10); 有参构造
    
    Constructor constructor = aClass.getConstructor(int.class,String.class);
    Object o2 = constructor.newInstance(20,"aaa"); // People p = new People(20,"aaa");
    
    //特殊形式 调用无参构造
    aClass.newInstance();
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    4.反射获取方法

    在这里插入图片描述

    Class peopleClass = People.class;
    
    //获取 自己和父类所有的公共函数
    Method[] methods = peopleClass.getMethods();
    
    //获取 当前类所有的函数
    Method[] declaredMethods = peopleClass.getDeclaredMethods();
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    4.1字节码对象获取函数信息并且调用

    @Test
    public void test02(0) throws Exception {
    
    // 1 创建对象
    People people = new PeopTe(:
    int a = people.eat("胡辣汤):
    Class peopleClass = People.class ;
                       
    // 自己和父类所有的公共函数
    Method[] methods = peop1eCTass .getMethods  ;
     System.out.printIn(Arrays .tostring( methods))!
    // 获取当前类所有的函数
    Method[] declaredMethods = peop1eClass .getDecTaredMethods ;
    System.out.printIn( Arrays .tostring( declaredMethods ));
                       
    Method eat = peopleClass .getDeclaredMethod("eat,string.class);
    object b = eat.invoke(people,"油馍头");
                                                
    Method haha = peopleClass .getDeclaredMethod("haha");
                                                
    haha.setAccessible(true);
                                                
    haha.invoke(peop1e);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    4.2字节码获取成员变量并赋值

    在这里插入图片描述

    People people = new People();	
    
    Class peopleClass = People.class;
    //获取成员变量
    Field[] fields = peopleClass.getDeclaredFields();
    //单独拿
    Field id = peopleClass.getDeclaredFields("id");
    Field name = peopleClass.getDeclaredFields("name");
    //打开权限
    id.setAccessible(true);
    name.setAccessible(true);
    
    id.set(people,666);
    id.set(people,"张three");
    System.out.println(people);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    5.注解

    注解就是代码里的特殊标志,可以在编译,类加载运行时被读取,并执行相应的处理

    5.1自定义注解

    
    @Target(ElementType.METHOD)
    @Retention(RetentionPolicy.RUNTIME)
    public @interface Role {
        String value();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
  • 相关阅读:
    前端使用 Konva 实现可视化设计器(10)- 对齐线
    C语言的基础概念
    .NET 线程 Thread 进程 Process、线程池 pool、Invoke、begininvoke、异步回调
    jvm中的类加载器
    淘宝店铺所有商品api、店铺商品列表、店铺列表接口
    基于Springboot+MySQL的个人健康监控管理系统
    深入思考JAVA虚拟机,实现从0到1的突破
    如何用mindspore高阶API实现互学习
    c语言实现base64编码和解码
    java mock单测
  • 原文地址:https://blog.csdn.net/g877835148/article/details/132732735