直接上代码
public void copy(Object source, Object dest) throws Exception {
// 获取属性
Field[] sourceProperty = source.getClass().getDeclaredFields();
Field[] destProperty = dest.getClass().getDeclaredFields();
try {
for (int i = 0; i < sourceProperty.length; i++) {
for (int j = 0; j < destProperty.length; j++) {
if (sourceProperty[i].getName().equals(destProperty[j].getName())) {
destProperty[j].setAccessible(true);
sourceProperty[i].setAccessible(true);
destProperty[j].set(dest,sourceProperty[i].get(source));
break;
}
}
}
} catch (Exception e) {
throw new Exception("属性复制出错:" + e.getMessage());
}
}
网友评论