本帖最后由 杨兴庭 于 2013-7-17 21:13 编辑
- package cn.itcast.reflect1;
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import java.lang.reflect.Constructor;
- import java.lang.reflect.Method;
- import java.util.Properties;
- public class RefectDemo4 {
- public static void main(String[] args) throws Exception {
- Properties pt = new Properties();
- FileReader fr = new FileReader("c:\\config.properties");
- pt.load(fr);
- for (int i = 0; i < pt.size()/2; i++) {
- String className = pt.getProperty("className"+i);
- String methodName = pt.getProperty("methodName"+i);
- Class cls = Class.forName(className);
-
- Constructor con = cls.getConstructor();
-
- Method md =cls.getMethod(methodName);
- Object obj = cls.newInstance();
- md.invoke(obj);
-
- }
- }
- }
复制代码 我想问的是那个for (int i = 0; i < pt.size()/2; i++) {....}//这里问什么要除以2?老师今天说要我们自己想。我实在想不出来,不除以不行么? |