反射创建实例对象
第一步:Constructor 对象获取(更多请查看JDK文档,关键字:Class)
- Constructor<?>[] getConstructors() 获取当前Class类对象中,所有的公共的构造方法
- Constructor<?>[] getDeclaredConstructors() 获取当前Class类对象中,所有的构造方法(常用)
- Constructor<T> getConstructor(Class<?>... parameterTypes) 获取一个单个的公共的构造方法对象
- Constructor<T> getDeclaredConstructor(Class<?>... parameterTypes) 获取一个单个的,任意权限修饰符的构造方法对象(推荐使用)
// 学生类
public class Student { private String name; int age; protected String address; public String tel; // getter 和 setter 方法 public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getTel() { return tel; } public void setTel(String tel) { this.tel = tel; } // 成员方法 void eat(){ System.out.println("吃东西"); } protected void sleep(){ System.out.println("睡觉"); } private void study(String subject){ System.out.println("学" + subject); }
private void play(String sth, int num){ System.out.println("玩" + sth + num + "次"); } // 构造方法 public Student() {} // 构0
Student(String name, int age) { // 构2 this.name = name; this.age = age; }
protected Student(String name, int age, String address) { // 构3 this.name = name; this.age = age; this.address = address; }
private Student(String name, int age, String address, String tel) { // 构4 this.name = name; this.age = age; this.address = address; this.tel = tel; } @Override public String toString() { return "Student{" + "name='" + name + '\'' + ", age=" + age + ", address='" + address + '\'' + ", tel='" + tel + '\'' + '}'; } }
import java.io.FileInputStream; import java.io.IOException; import java.lang.reflect.Constructor; import java.util.Properties; public class Test { public static void main(String[] args) throws IOException, ClassNotFoundException, NoSuchMethodException { // 1.读取需要被加载进内存的类的路径 Properties properties = new Properties(); FileInputStream fis = new FileInputStream("day14\\class.properties"); // 读取配置文件 properties.load(fis); //2.从集合中,把className的信息获取到 String className = properties.getProperty("className"); //3.反射根据类的全路径(包名 + 类名),将当前的类加载进内存,形参Class类对象 Class<?> c = Class.forName(className); System.out.println("Constructor<?>[] getConstructors() 获取所有的公共的构造方法"); Constructor<?>[] constructors = c.getConstructors(); for (Constructor<?> constructor : constructors) { System.out.println(constructor); } System.out.println("Constructor<?>[] getDeclaredConstructors() 获取所有的构造方法"); Constructor<?>[] declaredConstructors = c.getDeclaredConstructors(); for (Constructor<?> declaredConstructor : declaredConstructors) { System.out.println(declaredConstructor); }
System.out.println("Constructor<T> getConstructor(Class<?>... parameterTypes) 获取单个的公共的构造方法对象"); Constructor<?> constructor = c.getConstructor(); // 获取了一个公共的,空参的构造方法(构0) System.out.println(constructor); System.out.println("Constructor<T> getDeclaredConstructor(Class<?>... parameterTypes) 获取单个的,任意权限修饰符的构造方法对象 (推荐使用)"); // 构0 Constructor<?> declaredConstructor0 = c.getDeclaredConstructor(); System.out.println(declaredConstructor0); // 构2 Constructor<?> declaredConstructor2 = c.getDeclaredConstructor(String.class, int.class); System.out.println(declaredConstructor2); // 构3 Constructor<?> declaredConstructor3 = c.getDeclaredConstructor(String.class, int.class, String.class, String.class); System.out.println(declaredConstructor3); fis.close(); // 关流 } }
测试结果如下:
Constructor<?>[] getConstructors() 获取所有的公共的构造方法
public cn.powernode.javase.reflect.constructor.Student()
Constructor<?>[] getDeclaredConstructors() 获取所有的构造方法
public cn.powernode.javase.reflect.constructor.Student()
protected cn.powernode.javase.reflect.constructor.Student(java.lang.String,int,java.lang.String)
private cn.powernode.javase.reflect.constructor.Student(java.lang.String,int,java.lang.String,java.lang.String)
cn.powernode.javase.reflect.constructor.Student(java.lang.String,int)
Constructor<T> getConstructor(Class<?>... parameterTypes) 获取单个的公共的构造方法对象
public cn.powernode.javase.reflect.constructor.Student()
Constructor<T> getDeclaredConstructor(Class<?>... parameterTypes) 获取单个的,任意权限修饰符的构造方法对象 (推荐使用)
public cn.powernode.javase.reflect.constructor.Student()
cn.powernode.javase.reflect.constructor.Student(java.lang.String,int)
private cn.powernode.javase.reflect.constructor.Student(java.lang.String,int,java.lang.String,java.lang.String)
第二步:根据获取到的 Constructor 对象,创建该类的实例对象(更多请查看JDK文档,关键字:Constructor)
- T newInstance(Object... initargs) 创建该构造方法对象的实例 (实参列表...)
import java.io.FileInputStream; import java.io.IOException; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; import java.util.Properties; /* 1.加载类进内存,拿到Class类对象 2.Class类对象中,获取Constructor对象 3.根据获取的这个Constructor对象,创建该类的实例对象 -> new 对象(); */ public class Test02 { public static void main(String[] args) throws IOException, ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException { // 1.读取需要被加载进内存的类的路径 Properties properties = new Properties(); FileInputStream fis = new FileInputStream("day14\\class.properties"); properties.load(fis); //2.从集合中,把 className 的信息获取到 String className = properties.getProperty("className"); //3.反射根据类的全路径(包名 + 类名),将当前的类加载进内存,形参Class类对象 Class<?> c = Class.forName(className); /* Constructor<?> declaredConstructor = c.getDeclaredConstructor(String.class, int.class, String.class, String.class); // 构4 //对于私有的成员需要取消访问检查,不会给我们进行非公共的检查了,所有的权限修饰符都可以操作了 -> 暴力反射 declaredConstructor.setAccessible(true); // 对于私有的成员需要取消访问检查 Object o = declaredConstructor.newInstance("zs", 25, "深圳宝安", "135xxxxxxxx"); if (o instanceof Student){ Student stu = (Student) o; System.out.println(stu); // Student{name='zs', age=25, address='深圳宝安', tel='135xxxxxxxx'} } */ // 读取配置文件的做法
//1.根据传递的参数不同,调用不同的构造方法 String constructorArgs = properties.getProperty("constructorArgs"); // String.class,int.class String[] split = constructorArgs.split(","); String constructorValues = properties.getProperty("constructorValues"); // zs,25 String[] splitValue = constructorValues.split(","); if (constructorArgs.equals("null")){ // 构0 Constructor<?> declaredConstructor = c.getDeclaredConstructor(); Object o = declaredConstructor.newInstance(); if (o instanceof Student){ Student stu = (Student) o; System.out.println(stu); } }else if (split.length == 2 && split[0].equals("String.class") && split[1].equals("int.class")){ // 构2 Constructor<?> declaredConstructor = c.getDeclaredConstructor(String.class, int.class); Object o = declaredConstructor.newInstance(splitValue[0],Integer.parseInt(splitValue[1])); if (o instanceof Student){ Student stu = (Student) o; System.out.println(stu); } } fis.close(); } }
// 执行结果:Student{name='zs', age=25, address='null', tel='null'}