配置文件
className = fanshe.Student1
showInfo = show1
类student1
public class Student1 {public Student1() {System.out.println("调用了Student1 无参构造函数");}public void show1(){System.out.println("调用了show1()方法");}
}
测试类
public class Test {public static void main(String[] args) throws Exception{ //1. 获取类Class clazz = Class.forName(getValue("className"));//2. 获取showinfo方法Method m_showInfo = clazz.getMethod(getValue("showInfo"));//3.调用showInfo()方法//3.1 实例化一个类对象,invoke中必须传入类对象的实例Object obj = clazz.getConstructor().newInstance();//3.2 用实例化好的类对象去调用方法m_showInfo.invoke(obj);} public static String getValue(String key) throws Exception{Properties pro = new Properties();//获取配置文件对象 FileReader file = new FileReader("proInfo.txt");//获取输入流对象pro.load(file);//加载file.close();//关闭流对象return pro.getProperty(key);//返回需要取的目标值}
}
输出
调用了Student1 无参构造函数
调用了show1()方法