//通过反射加载类
import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Properties;
public class Deom {
public static void main(String[] args) throws IOException, ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
/**
* 读取配置文件信息
*/
Properties properties = new Properties();
FileReader fileReader = new FileReader("TWO\\src\\Day15\\Text06\\cofing.properties");
properties.load(fileReader);
String className = properties.getProperty("ClassName");
String functionName = properties.getProperty("FunctionName");
/**
* 反射读取的类和方法
*/
Class<?> c = Class.forName(className);
Object o = c.getConstructor().newInstance();
Method method = c.getMethod(functionName);
/**
* 调用方法
*/
method.invoke(o);
}
}
通过反射加载类
最新推荐文章于 2024-05-19 18:46:15 发布