一、利用反射实现深度拷贝

    public static void copyDeeply(Class<?> type, Object sourceBean, Object destBean) {
        Class<?> parent = type;
        while (parent != null) {
            Field[] fields = parent.getDeclaredFields();
            for (Field field : fields) {
                if (!field.isAccessible()) {
                    try {
                        field.setAccessible(true);
                        field.set(destBean, field.get(sourceBean));
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    }
                }
            }
            parent = parent.getSuperclass();
        }
    }

你可能感兴趣的:(反射)