1、定义一个Student类,在配置文件中定义类名,属性值,都用键值对的形式进行定义
然后读取配置文件,创建指定的对象,并为该对象的属性值进行赋值
@Test //junit 测试
public void show() throws Exception {
Properties p =new Properties();
InputStream is = this.getClass().getClassLoader().getResourceAsStream("com/aura/day17/student.properties");
p.load(is);
Class c = Class.forName(p.getProperty("classname"));
Students stu = (Students) c.newInstance();
Field age = c.getDeclaredField("age");
age.setAccessible(true);
age.set(stu, Integer.parseInt(p.getProperty("age")));
stu.id = Integer.parseInt(p.getProperty("id"));
stu.name= p.getProperty("name");
System.out.println(stu);
}
class Students{
public int id ;
public String name;
private int age;
@Override
public String toString() {
return "Students [id=" + id + ", name=" + name + ", age=" + age + "]";
}
}