try {
String attr = "occPeoples";
Method[] methods = askContPlan.getClass().getDeclaredMethods();
// 截取第一个字母后面的内容
String ttr = attr.substring(1);
// 保证是方法是顺序调用的
for (int i = 0; i <= occtypes.size(); i++) {
for (int j = 0; j < methods.length; j++) {
Method method = methods[j];
if (method.getName().startsWith("get" + attr.substring(0, 1).toUpperCase() + ttr + (occtypes.size()+1))) {
String peoples = String.valueOf(method.invoke(askContPlan));
occtypes.add(returnNumber(peoples)+"");
}
}
}
} catch (InvocationTargetException | IllegalAccessException e2) {
log.error(e2.toString());
}
实战反射获取对象属性和值
最新推荐文章于 2021-04-01 11:25:05 发布