新建一个配置文件
通过反射机制可以只修改配置文件就可以做到不同对象的实例化
package denglianbin.java.reflect;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
public class ReflectTest03 {
public static void main(String[] args) {
try {
FileInputStream fis = new FileInputStream("module009/classinfo.properties");
Properties pro = new Properties();
pro.load(fis);
fis.close();
String s = pro.getProperty("className");
System.out.println(s);
Class c = Class.forName(s);
Object obj = c.newInstance();
System.out.println(obj);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
}
}
}