获取对象中的所有属性
private static Field[] getAllFields(Object object) {
Class clazz = object.getClass();
List<Field> fieldList = new ArrayList<>();
while (clazz != null) {
fieldList.addAll(new ArrayList<>(Arrays.asList(clazz.getDeclaredFields())));
clazz = clazz.getSuperclass();
}
Field[] fields = new Field[fieldList.size()];
fieldList.toArray(fields);
return fields;
}
比较对象中的内容是否一致
private StringBuilder compareContract(Object obj, Object obj2) {
StringBuilder stringBuilder = new StringBuilder();
try {
Field[] fields = getAllFields(obj);
for (int j = 0; j < fields.length; j++) {
fields[j].setAccessible(true);
// 字段值
if (!fields[j].get(obj).equals(fields[j].get(obj2))) {
stringBuilder.append(obj2.get字段());
stringBuilder.append(fields.getName()).append("、"));
}
}
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return stringBuilder;
}