我正在尝试将对象的属性作为列表获取。例如:
class Sample {
private String type;
private String name;
private int value;
// getter-setter
}
期望输出:{"type", "name", "value"}
有没有编程方式可以做到这一点?
我正在尝试将对象的属性作为列表获取。例如:
class Sample {
private String type;
private String name;
private int value;
// getter-setter
}
期望输出:{"type", "name", "value"}
有没有编程方式可以做到这一点?
public static String getClassProperties(Class<?> clazz) {
return Arrays.stream(clazz.getDeclaredFields())
.map(field -> "\"" + field.getName() + "\"")
.collect(Collectors.joining(", ", "{", "}"));
}
用法示例:
public static void main(String[] args) {
System.out.println(getClassProperties(Sample.class));
}
输出:
{"类型", "名称", "值"}