1,配置文件:
配置里面的内容基本上都是 键--值 成对成对存在的
我们写一个configure文件,里面写入类名与方法名
写一个Studnet类:
public class Student {
private int age;
private String name;
public Student(int age, String name) {
this.age = age;
this.name = name;
}
public Student() {
}
public void show(){
System.out.println("学生学习");
}
}
接下来我们结合class文件对象的知识,调用一下成员方法
import java.io.FileReader;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.Properties;
public class ReflexDemo {
public static void main(String[] args) throws Exception{
// Student student = new Student();
// student.show();
//java中提供了一个Properties类
Properties properties = new Properties();
//读取配置文件
FileReader fileReader = new FileReader("E:\code\idea\src\com\bigdata\Day27\configure.txt");
properties.load(fileReader);
fileReader.close();
//通过配置文件中的Key,返回Value
String className = properties.getProperty("className");
System.out.println(className);
String methodName = properties.getProperty("methodName");
System.out.println(methodName);
//通过类名获取class文件对象
Class> c =Class.forName(className);
//获取一个对象
Constructor> con = c.getConstructor();
Object o = con.newInstance();
Method method = c.getMethod(methodName);
//调用成员方法
method.invoke(o);
}
}



