-
案例需求
-
通过反射运行配置文件中指定类的指定方法
-
-
代码实现
public class ReflectTest02 {
public static void main(String[] args) throws Exception {
//加载数据
Properties prop = new Properties();
FileReader fr = new FileReader("myReflect\\class.txt");
prop.load(fr);
fr.close();
/*
className=com.leon_06.Student
methodName=study
*/
String className = prop.getProperty("className");
String methodName = prop.getProperty("methodName");
//通过反射来使用
Class<?> c = Class.forName(className);//com.leon_06.Student
Constructor<?> con = c.getConstructor();
Object obj = con.newInstance();
Method m = c.getMethod(methodName);//study
m.invoke(obj);
}
}
版权声明:本文为Leon_Jinhai_Sun原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。