
根据配置文件 re.properties 指定信息,创建 Cat 对象并调用方法 hi()
classfullpath=reflection_.Cat method=hi快速入门
package reflection_;
import java.io.FileInputStream;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Properties;
public class ReflectionQuestion {
public static void main(String[] args) throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
// 根据配置文件 re.properties 创建 Cat 对象并调用 hi() 方法
// 传统方式:
// 1. 使用 Properties 对象读取配置文件
Properties properties = new Properties();
properties.load(new FileInputStream("src\re.properties"));
String classfullpath = properties.getProperty("classfullpath").toString(); // 字符串
String methodName = properties.getProperty("method").toString();
System.out.println("classfullpath = " + classfullpath);
System.out.println("method = " + methodName);
// 2. 创建对象
// new classfullpath(); 编译不通过,无法通过配置文件方式 new 对象
// new reflection_.Cat(); // 可以实现
// 3. 使用反射机制解决
// (1) 加载类,返回 Class 类型的对象 aClass
Class aClass = Class.forName(classfullpath);
// (2) 通过 aClass.newInstance() 得到你加载的类 reflection_.Cat 的对象实例
Object o = aClass.newInstance();
System.out.println(o.getClass()); // o 的运行类型:class reflection_.Cat
// (3) 通过 aClass.getMethod() 得到你加载的类 reflection_.Cat 的 methodName"hi" 的方法对象
// 即:在反射中,可以把方法视为对象(万物皆对象)
Method method = aClass.getMethod(methodName);
// (4) 通过 method 调用方法,即通过方法对象来实现调用方法
System.out.println("===================");
method.invoke(o); // 传统方法:对象.方法() 反射:方法.invoke(对象)
}
}
反射机制
package reflection_;
import java.io.FileInputStream;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Properties;
public class Reflection01 {
public static void main(String[] args) throws Exception {
// 根据配置文件 re.properties 创建 Cat 对象并调用 hi() 方法
// 传统方式:
// 1. 使用 Properties 对象读取配置文件
Properties properties = new Properties();
properties.load(new FileInputStream("src\re.properties"));
String classfullpath = properties.getProperty("classfullpath").toString(); // 字符串
String methodName = properties.getProperty("method").toString();
// 3. 使用反射机制解决
// (1) 加载类,返回 Class 类型的对象 aClass
Class aClass = Class.forName(classfullpath);
// (2) 通过 aClass.newInstance() 得到你加载的类 reflection_.Cat 的对象实例
Object o = aClass.newInstance();
System.out.println(o.getClass()); // o 的运行类型:class reflection_.Cat
// (3) 通过 aClass.getMethod() 得到你加载的类 reflection_.Cat 的 methodName"hi" 的方法对象
// 即:在反射中,可以把方法视为对象(万物皆对象)
Method method = aClass.getMethod(methodName);
// (4) 通过 method 调用方法,即通过方法对象来实现调用方法
System.out.println("===================");
method.invoke(o); // 传统方法:对象.方法() 反射:方法.invoke(对象)
// java.lang.reflect.Field:代表类的成员变量,Field 对象表示某个类的成员变量
// 得到 name 字段,getField 不能获取私有属性
Field nameField = aClass.getField("age");
System.out.println(nameField.get(o)); // 传统方法:对象.成员变量 反射:成员变量.get(对象)
// java.lang.reflect.Constructor:代表类的构造方法 Constructor
// () 中可以指定构造器参数类型,返回无参构造器
Constructor constructor = aClass.getConstructor();
System.out.println(constructor);
// () 中传入 String.class,就是 String 类的 Class 对象
Constructor constructor1 = aClass.getConstructor(String.class);
System.out.println(constructor1); // Cat(String name)
}
}