本帖最后由 罗海清 于 2013-4-13 10:34 编辑
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class Test {
public static void main(String[] args) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
List list=new ArrayList();
Bean b1 = new Bean();
Bean b2 = new Bean();
list.add(b1);
list.add(b2);
Class clazz = list.getClass();
Method getMethod = clazz.getMethod("iterator");
Iterator it = (Iterator) getMethod.invoke(list, null);
while(it.hasNext()){
Bean bean = (Bean) it.next();
Class beanClass = bean.getClass();
Field[] fields = beanClass.getDeclaredFields();
for(Field field:fields){
field.setAccessible(true);
System.out.println(field.getName()+":"+field.get(b1));
}
}
}
}结果是:name:nullname:null
sex:null
sex:null
|