java复习

2020-08-02运行配置文件指定的内容

2020-08-12  本文已影响0人  智障猿
public class Demo {
    public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, IOException, ClassNotFoundException, InstantiationException {
        Properties properties = new Properties();
        FileReader fileReader = new FileReader("test\\test.txt");
        properties.load(fileReader);
        fileReader.close();
        String className = properties.getProperty("className");
        String methodName = properties.getProperty("methodName");
        System.out.println(methodName);
        //通过反射使用
        Class<?> aClass = Class.forName(className);
        //通过反射获得构造方法对象
        Constructor<?> declaredConstructor = aClass.getDeclaredConstructor();
        //通过构造方法对象实例化
        Object o = declaredConstructor.newInstance();
        //通过反射获得方法对象
        Method method = aClass.getMethod(methodName);
        method.invoke(o);
    }
}
捕获.JPG
上一篇下一篇

猜你喜欢

热点阅读