定义配置文件run.properties内容如下:
className=com.zgjy.reflect.Person
methodName=getSum
通过读取配置文件内容,使用反射运行对应Person类中的对应方法getSum
分析:
1.有配置文件—>读取配置文件内容—>提取出有消息,反射需要的信息1) 完整包名的类名 2) getSum方法
2.读取配置文件,使用Hashtable子类,Properties---->load—>将配置文件内容读取到Properties中
3.通过Properties中方法getProperty(key)---->value—>获取到类名和方法名
4.通过类名和方法名使用反射进行Person中的getSum方法运行
代码:
import java.io.FileReader;
import java.lang.reflect.Method;
import java.util.Properties;
public class PropertiesReflect {
public static void main(String[] args) throws Exception {
//1.使用IO流进行文件的内容读取
FileReader fr = new FileReader("D:\\eclipse-workspace\\HelloWorld\\src\\homework\\day25\\run.properties");
//2.将使用IO流读取到的配置文件内容放置到Properties键值对的集合中去
Properties p = new Properties();
//3.使用Properties类中的load方法,将IO流中读取到的内容放置到集合中
p