package Properties_reflect;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.lang.reflect.Method;
import java.util.Properties;
public class Demo {
/*
* 使用反射技术,写一段能够创建任意类的对象,并调用其中的方法
* */
public static void main(String[] args) throws Exception {
//使用properties对象读取props文件
Properties prop = new Properties();
prop.load(new FileReader("Review/props.txt"));
//得到prop文件中存储的类名和方法名
String className = prop.getProperty("className");
String methodName = prop.getProperty("methodName");
//使用反射技术得到class文件对象
Class<?> clazz = Class.forName(className);
Object o = clazz.newInstance();
Method m = clazz.getMethod(methodName);
//执行方法
m.invoke(o);//只有method对象才能调用invoke方法
}
}
反射技术之实现创建任意类的对象并能够实现指定的方法的功能(使用properties属性集读取txt文件中的对象)
最新推荐文章于 2020-12-14 16:05:30 发布