2.Sprng-IoC-Java反射例子

时间:2021-05-27 22:59:34

1.Car.java

 package com.jike.spring.chapter03.reflect;

 public class Car {
private String brand; private String color; private int maxSpeed; //1.默认构造函数
public Car(){
System.out.println("init car!!");
} //2.带参构造函数
public Car(String brand,String color,int maxSpeed){
this.brand = brand;
this.color = color;
this.maxSpeed = maxSpeed;
} //3.未带参的方法
public void introduce() {
System.out.println("brand:"+brand+";color:"+color+";maxSpeed:"+maxSpeed);
} public String getBrand() {
return brand;
} public void setBrand(String brand) {
this.brand = brand;
} public String getColor() {
return color;
} public void setColor(String color) {
this.color = color;
} public int getMaxSpeed() {
return maxSpeed;
} public void setMaxSpeed(int maxSpeed) {
this.maxSpeed = maxSpeed;
}
}

2.ReflectTest.java

 package com.jike.spring.chapter03.reflect;

 import java.lang.reflect.Constructor;
import java.lang.reflect.Method; public class ReflectTest { public static Car initByDefaultConst() throws Throwable
{
//1.通过类装载器获取Car类对象
ClassLoader loader = Thread.currentThread().getContextClassLoader();
Class clazz = loader.loadClass("com.jike.spring.chapter03.reflect.Car"); //2.获取类的默认构造器对象并实例化Car
Constructor cons = clazz.getDeclaredConstructor((Class[])null);
Car car = (Car)cons.newInstance(); //3.通过反射方法设置属性
Method setBrand = clazz.getMethod("setBrand",String.class);
setBrand.invoke(car,"奔驰");
Method setColor = clazz.getMethod("setColor",String.class);
setColor.invoke(car,"黑色");
Method setMaxSpeed = clazz.getMethod("setMaxSpeed",int.class);
setMaxSpeed.invoke(car,200);
return car;
} public static Car initByParamConst() throws Throwable{
//1.通过类装载器获取Car类对象
ClassLoader loader = Thread.currentThread().getContextClassLoader();
Class clazz = loader.loadClass("com.jike.spring.chapter03.reflect.Car"); //2.获取类的带有参数的构造器对象
Constructor cons = clazz.getDeclaredConstructor(new Class[]{String.class,String.class,int.class}); //3.使参数的构造器对象实例化Car
Car car = (Car)cons.newInstance(new Object[]{"宝马","红色",180});
return car;
} public static void main(String[] args) throws Throwable {
Car car1 = initByDefaultConst();
Car car2 = initByParamConst();
car1.introduce();
car2.introduce();
}
}