public static List<Field> getAllDeclaredFields(Class<?> clazz) { List<Field> allFields = new ArrayList<>(); Class<?> superClass = clazz.getSuperclass(); // 獲取當前類宣告的所有欄位 Field[] declaredFields = clazz.getDeclaredFields(); allFields.addAll(Arrays.asList(declaredFields)); // 如果存在父類,則遞迴獲取父類的欄位 if (superClass != null) { allFields.addAll(getAllDeclaredFields(superClass)); } return allFields; }