Java反射

一、获取字节码信息

  1. 对象名.getClass():Class c3 = Class.forName("com.test01.Person");
  2. 类名.class:Class c2 = Person.class;
  3. Class.forName(“类的全限定名”):Class c3 = Class.forName("com.test01.Person");
  4. 类加载器.load(“类的全限定名”):Class c4 = MyTest.class.getClassLoader().loadClass("com.test01.Person");

二、Class类的实例类型

  • demo

    @Test
    public void testForClassInstanceType() {
        /*可以作为Class类的实例的种类
        (1) 类:外部类、内部类
        (2) 接口
        (3) 注解
        (4) 数组
        (5) 基本数据类型
        (6) void
         */
        Class personClass = Person.class;
    
        Class comparableClass = Comparable.class;
    
        Class overrideClass = Override.class;
    
        int[] arr1 = {1, 2, 3};
        Class c1 = arr1.getClass();
        int[] arr2 = {1, 2, 3};
        Class c2 = arr2.getClass();
        System.out.println(c1 == c2);
    
        Class<Integer> i1 = int.class;
        Class<Integer> i2 = Integer.class;
        System.out.println(i1 == i2);
    
        Class<Void> voidClass = void.class;
    
    }
    

三、获取类的接口

  • 代码

    @Test
    public void testForGetClassInterface() {
        Class<Student> studentClass = Student.class;
        //获取实现的接口
        Class<?>[] interfaces = studentClass.getInterfaces();
        for (Class<?> anInterface : interfaces) {
            System.out.println(anInterface);
        }
    
        //获取父类
        Class<? super Student> superclass = studentClass.getSuperclass();
        System.out.println(superclass);
    }
    

四、获取类的构造器

  • 代码

    @Test
    public void testForGetConstructor() throws Exception {
        Class<Student> studentClass = Student.class;
    
        //获取全部修饰符的构造器
        Constructor<?>[] declaredConstructors = studentClass.getDeclaredConstructors();
        for (Constructor<?> declaredConstructor : declaredConstructors) {
            System.out.println(declaredConstructor);
        }
    
        System.out.println("==============================");
    
        //获取指定的构造器
        Constructor<Student> constructor = studentClass.getConstructor();
        System.out.println(constructor);
        Constructor<Student> constructor1 = studentClass.getConstructor(double.class, double.class);
        System.out.println(constructor1);
    
        //根据构造器创建对象
        Student student = constructor.newInstance();
        System.out.println(student);
        Student student1 = constructor1.newInstance(22, 56);
        System.out.println(student1);
    }
    

五、获取类的属性

  • 代码

    @Test
    public void testForGetFild() throws Exception {
        Class<Student> studentClass = Student.class;
        //获取属性
        Field sno = studentClass.getDeclaredField("name");
        //根据注解类型获取属性注解
        MyAnnotation annotation = sno.getAnnotation(MyAnnotation.class);
        Student student = studentClass.newInstance();
        System.out.println(annotation.value());
        //给属性赋值
        sno.set(student, annotation.value());
        System.out.println(student);
    }
    

六、获取类的方法

  • 代码

    @Test
    public void testForGetMethod() throws Exception {
        Class<Student> studentClass = Student.class;
        //获取方法
        Method showInfo = studentClass.getMethod("showInfo");
        Student student = studentClass.newInstance();
        //调用方法
        showInfo.invoke(student);
    }
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值