2018-07-03 Spring基础知识之反射机制基础

准备材料,一个类, 里面有1个全局变量,2个构造方法,一个普通方法, 接着我要利用反射机制来分别调用这两个构造方法生成新的对象,并调用那个普通方法。

package SpringBasic.Reflection;

/**
 * Created by vickyy on 7/3/2018.
 */
public class Car {
    private String brand;

    public Car(){}

    public Car(String brand){
        this.brand = brand;
    }

    public void setBrand(String brand){
        this.brand = brand;
    }

    public void introduce(){
        System.out.println("brand:"+brand);
    }
}

利用反射调用构造函数生成新的对象,并调用它的方法:
总结: 虽然说利用反射机制可以摆脱对类的直接调用,但是并不完全摆脱这个类,至少我们要知道这个类在哪里,类名是啥,有什么样的构造函数,有没有类变量,访问权限又是什么。
我觉得应该比较广的应该是用于写通用的调用方法,比如两个类的相同名字的字段值的拷贝, 这个时候就可以利用反射机制,动态生成目标类对象,然后利用反射机制取得对应字段类型,名字等,然后拷贝。所以说反射机制主要用到的就是4个类:ClassLoader, Class, Constructor, Method. 通过它们可以调用Class的各项功能。

package SpringBasic.Reflection;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * Created by vickyy on 7/3/2018.
 */
public class ReflectTest {
//1. 通过调用带参数的构造方法来实例化类
    public static Car initByDefaultConst() throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
        ClassLoader loader = Thread.currentThread().getContextClassLoader();
        Class clazz = loader.loadClass("SpringBasic.Reflection.Car");
        Constructor cons = clazz.getDeclaredConstructor(String.class);
        Car car = (Car) cons.newInstance("test");
        return car;
    }
//2. 通过调用无参数的构造方法来实例化类
    public static Car initByParamConst() throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException{
        ClassLoader loader = Thread.currentThread().getContextClassLoader();
        Class clazz = loader.loadClass("SpringBasic.Reflection.Car");
        Constructor cons = clazz.getDeclaredConstructor((Class[]) null);
        Car car = (Car) cons.newInstance();
        Method method = clazz.getMethod("setBrand", String.class);
        method.invoke(car, "test2");
        return car;
    }
//3 . 操作private类型的类变量
    public static Car updateField() throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException, NoSuchFieldException {
        ClassLoader loader = Thread.currentThread().getContextClassLoader();
        Class clazz = loader.loadClass("SpringBasic.Reflection.Car");
        Constructor cons = clazz.getDeclaredConstructor((Class[]) null);
        Car car = (Car) cons.newInstance();
        Field brand = clazz.getDeclaredField("brand");
        brand.setAccessible(true);
        brand.set(car, "HONDA");
        return car;
    }

    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException, NoSuchFieldException {
        Car car = initByDefaultConst();
        car.introduce();

        Car car2 =  initByParamConst();
        car2.introduce();

        Car car3 = updateField();
        car3.introduce();
    }
}

你可能感兴趣的:(2018-07-03 Spring基础知识之反射机制基础)