Loading

反射基本应用

反射机制的应用

创建一个CLass对象,它是类的原型。有时候我们不能直接操作类,可以通过获取类的原型Class对象,间接的操作或实例化类。

反射机制调用方法

与反射调用普通方法不同的是:

调用类的普通方法需要使用getMehotd等方法获得方法对象,再使用invoke(目标对象)调用。

调用类的构造方法时需要使用getDeclaredConstructor方法获得构造方法,然候再newInstance()实例化构造方法对象。

  • 构造方法
public class Vip {
    int no;
    String name;
    String birth;
    boolean sex;

    public Vip() {
    }

    public Vip(int no, String name, String birth, boolean sex) {
        this.no = no;
        this.name = name;
        this.birth = birth;
        this.sex = sex;
    }

}
import java.lang.reflect.Constructor;

public class ReflectTest12 {
    public static void main(String[] args) throws Exception {
        //不使用反射机制创建对象
        Vip v1 = new Vip();
        Vip v2 = new Vip(110,"zhangsan","2001-10-11",true);

        //使用反射机制创建对象
        Class c = Class.forName("com.cc.Vip");
        //这种方式对象实例化会调用无参构造方法
        Object obj = c.newInstance();
        System.out.println(obj);

        
        //调用有参构造
        //第一步:先获取到这个有参数的构造方法
        Constructor con = c.getDeclaredConstructor(int.class, String.class, String.class, boolean.class);//这里为4个参数的构造方法
        //第二步:调用构造方法new对象
        Object newObj = con.newInstance(110, "jackson", "1990-10-11", true);
        System.out.println(newObj);

        
        //获取无参构造方法
        Constructor con2 = c.getDeclaredConstructor();
        Object newObj2 = con2.newInstance();
        System.out.println(newObj2);
    }
}

从上面可以看到有2种调用无参构造方法的方式,一种是通过Class类实例调用,一种是反射获取构造方法,构造方法实例化调用。

而调用有参构造方法的方式只有一种,反射获取构造方法,构造方法实例化调用。

  • 普通方法
public class ReflectTest12 {
    public static void main(String[] args) throws Exception {
      //反射调用普通公有方法
        Class cat = Class.forName("com.cc.cat");
        Object o1 = cat.newInstance();
        Method hiMethod = cat.getMethod("cat");
        catMethod.invoke(o1);
        
      //反射调用普通私有方法
        Class cat = Class.forName("com.cc.cat");
        Object 01 = cat.newInstance();
        Method hiMethod = cat.getDeclaredMethod("hi");
        hiMethod.setAccessible(true);
        hiMethod.invoke(01);
    }

反射修改类的属性

  • public属性
package com.cc.reflection;

import java.lang.reflect.Field;

public class reflection_3 {
    public static void main(String[] args) throws Exception {
        //反射修改公有属性

        Class cls = Class.forName("com.cc.reflection.cat");
        Object o1 = cls.newInstance();

        //public:name   private:age

        Field name = cls.getField("name");
        System.out.println(name.get(o1));   //获得get(o1)
        name.set(o1,"姓名");      //改值set(01,value)
        System.out.println(name.get(o1));
    }
}
  • pricate属性
public class reflection_4 {
    public static void main(String[] args) throws Exception {
        //反射修改私有属性
        Class cls = Class.forName("com.cc.reflection.cat");
        Object o1 = cls.newInstance();

        Field age = cls.getDeclaredField("age");
        System.out.println(age);
//        System.out.println(age.get(o1));	提示错误,没有权限。
        age.setAccessible(true);
        System.out.println(age.get(o1));
        age.set(o1,2);
        System.out.println(age.get(o1));

    }
}
posted @ 2023-01-10 17:53  _rainyday  阅读(37)  评论(0)    收藏  举报