[java]代码库
package cn.itcast_06;
import java.io.FileReader;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.Properties;
public class Test {
public static void main(String[] args)throws Exception {
//加载键值对数据(读取配置文件)
Properties prop = new Properties();
FileReader fr = new FileReader("class.txt");
prop.load(fr);
fr.close();
//获取数据
String className = prop.getProperty("className");
String methodName = prop.getProperty("methodName");
//反射
Class c = Class.forName(className);
Constructor con = c.getConstructor();
Object obj = con.newInstance();
//调用方法
Method m1 = c.getMethod(methodName);
m1.invoke(obj);
}
}
/*
*class.txt
*className=cn.itcast_06.Teacher
*methodName=love
*
*/
by: 发表于:2017-05-25 13:48:32 顶(0) | 踩(0) 回复
??
回复评论