全类名 com.Student
//加载数据
Properties prop = new Properties();
FileReader fr = new FileReader( fileName: "module\\class.txt");
prop.load(fr);
fr.close();
/*
className=com.Student
methodName=study
*/
String className = prop.getProperty("className");
String methodName = prop.getProperty("methodName");
//通过反射来使用
class<?> c =Class.forName(className); //classname = com.Student (等同于类Student)
Constructor<?> con = c.getConstructor();
Object obj = con.newInstance(); //获得对象
Method m = c.getMethod(methodName); //获得方法对象
m.invoke(obj);
}
class.txt
className = com.Student
methodName = study
Student对象
package com.a;
public class Student{
public void study(){
System.out.printIn("好好学习");
}
}
Teacher对象
package com.a;
public class Teacher{
public void teach(){
System.out.printIn("教书育人");
}
}