---
title: "合并类里所有属性值"
author: "e_e"
date: "2018-08-22T17:43:03Z"
tags: []
categories: ["编程语言"]
slug: "untitled"
draft: false
lastmod: "2018-08-27T14:18:51Z"
id: 2429160
url: "https://e-e.iteye.com/blog/2429160"
---

```
public String combineAllFields(String split) {
    final Field[] fields = this.getClass().getDeclaredFields();
    if (fields.length < 1) {
        return null;
}
    StringBuffer result = new StringBuffer();
    for (int i = 0; i < fields.length; i++) {
        char[] cs = fields[i].getName().toCharArray();
cs[0] -= 32;
        try {
            Method method = this.getClass().getMethod("get" + String.valueOf(cs));
String fieldValue = (String) method.invoke(this, new Object[]{});
result.append(fieldValue).append(split);
} catch (Exception e) {
            e.printStackTrace();
}
    }
    return result.toString().endsWith(split) ? result.substring(0, result.length() - 1) : result.toString();
}
```