public class Test8 {
public static void main(String[] args) throws Exception {
loadFile();
} public static void loadFile() throws Exception {
Properties prop = new Properties();     // 创建Properties加载配置文件
FileInputStream fis = new FileInputStream(new File("Test8.properties"));
prop.load(fis);
String name = prop.getProperty("a");    //调用配置文件Test8.properties中的a
System.out.println(name);
Class<?> c = Class.forName(name);
DemoClass dc = (DemoClass) c.newInstance();
Method m = c.getMethod("run");
m.invoke(dc);
if (fis != null) {
fis.close();
}
}
}
class DemoClass {
public void run() {
System.out.println("welcome to heima!");
}
}

解决方案 »

  1.   


    public class Test8 {
    public static void main(String[] args) throws Exception {
    loadFile();
    }public static void loadFile() throws Exception {
    Properties prop = new Properties();     // 创建Properties加载配置文件
    FileInputStream fis = new FileInputStream(new File("Test8.properties"));
    prop.load(fis);
    String name = prop.getProperty("a");    //调用配置文件Test8.properties中的a
    System.out.println(name);
    Class<?> c = Class.forName(name);  //加载配置文件中类名称对应的类
    DemoClass dc = (DemoClass) c.newInstance();   //实例化对象
    Method m = c.getMethod("run");      //获取对象的run方法
    m.invoke(dc);               //执行run方法
    if (fis != null) {
    fis.close();
    }
    }
    }
    class DemoClass {
    public void run() {
    System.out.println("welcome to heima!");
    }
    }