?? testreflection.java
字號(hào):
import java.util.*;
import java.io.*;
import java.lang.reflect.*;
public class TestReflection {
public static void main(String[] args) throws Exception{
FileInputStream fi=new FileInputStream("exc2.txt");
Properties ps=new Properties();
ps.load(fi);
//構(gòu)造對(duì)象
String className=ps.getProperty("className");
Class c=Class.forName(className);
Object o=c.newInstance();
//設(shè)置對(duì)象屬性
Enumeration es=ps.propertyNames();
while(es.hasMoreElements()){
String s=(String)es.nextElement();
if (s.equals("className")) continue;
if (s.equals("method")) continue;
if (s.equals("parameter")) continue;
String value=ps.getProperty(s);
String methodName="set"+s;
Method m=c.getMethod(methodName,String.class);
m.invoke(o,value);
}
//調(diào)用方法
String mName=ps.getProperty("method");
String parameter=ps.getProperty("parameter");
Method m=c.getMethod(mName,String.class);
m.invoke(o,parameter);
}
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -