List list=new ArrayList();
Bean b1 = new Bean();
Bean b2 = new Bean();
list.add(b1);
list.add(b2);b1和b2中都分别有两个属性 name 和 sex。要通过反射得到获取list中b1和b2的name和sex 怎么获取?求救!!!
Bean b1 = new Bean();
Bean b2 = new Bean();
list.add(b1);
list.add(b2);b1和b2中都分别有两个属性 name 和 sex。要通过反射得到获取list中b1和b2的name和sex 怎么获取?求救!!!
(Bean)list.get(0).getSex();(Bean)list.get(1).getSex();
//Bean.java
package com.sort;public class Bean {
private String name; private String sex; public Bean() {
super();
// TODO Auto-generated constructor stub
} public Bean(String name, String sex) {
super();
this.name = name;
this.sex = sex;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getSex() {
return sex;
} public void setSex(String sex) {
this.sex = sex;
}
}//Test.java
package com.sort;import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;public class Test {
public static void main(String[] args) {
Method metd = null;
String fdname = null;
// 添加两个测试数据
List list = new ArrayList();
list.add(new Bean("张三", "女"));
list.add(new Bean("李四", "男"));
try {
// 遍历集合
for (Object object : list) {
Class clazz = object.getClass();// 获取集合中的对象类型
Field[] fds = clazz.getDeclaredFields();// 获取他的字段数组
for (Field field : fds) {// 遍历该数组
fdname = field.getName();// 得到字段名, metd = clazz.getMethod("get" + change(fdname), null);// 根据字段名找到对应的get方法,null表示无参数 if ("name".equals(fdname) && metd != null) {// 比较是否在字段数组中存在name字段,如果不存在短路,如果存在继续判断该字段的get方法是否存在,同时存在继续执行
Object name = metd.invoke(object, null);// 调用该字段的get方法
System.out.print("姓名:" + name);// 输出结果
}
if ("sex".equals(fdname) && metd != null) {// 同上
Object sex = metd.invoke(object, null);
System.out.println("\t性别:" + sex);
}
}
}
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} /**
* @param src
* 源字符串
* @return 字符串,将src的第一个字母转换为大写,src为空时返回null
*/
public static String change(String src) {
if (src != null) {
StringBuffer sb = new StringBuffer(src);
sb.setCharAt(0, Character.toUpperCase(sb.charAt(0)));
return sb.toString();
} else {
return null;
}
}
}
说明就不用了,请看注释.....
不知道能否让lz明白,如果不明白,留言即可...