public final class BeanUtils { /** * 将srcObject的字段值赋值到destObject对象中去 * * @param srcObject * @param destObject * @param <S> * @param <T> * @return */ public static <S, T> T initDestObject(S srcObject, T destObject) { Map<String,Object> srcMap = new HashMap<>(); Class srcClass = srcObject.getClass(); Field[] declaredFields = srcClass.getDeclaredFields(); for (int i = 0; i < declaredFields.length; i++) { try { Field field = declaredFields[i]; field.setAccessible(true); String fieldName = field.getName(); Object value = field.get(srcObject); srcMap.put(fieldName, value); } catch (IllegalAccessException e) { e.printStackTrace(); } } try { destObject = initProperty(destObject, srcMap); } catch (IllegalAccessException e) { e.printStackTrace(); } System.out.println(srcMap); return destObject; } private static <T> T initProperty(T obj, Map<String,Object> map) throws IllegalAccessException { Class<?> clazz = obj.getClass(); Field[] declaredFields = clazz.getDeclaredFields(); for (int i = 0; i < declaredFields.length; i++) { declaredFields[i].setAccessible(true); String fieldName = declaredFields[i].getName(); Set<String> keySet = map.keySet(); for (String s : keySet) { if (fieldName.equals(s)) { declaredFields[i].set(obj, map.get(s)); } } } return obj; } }