[英]Convert Class Object to Human Readable String
Is there any way in which I can automatically convert a Custom Class Object into a human readable string? 有没有什么方法可以自动将自定义类对象转换为人类可读的字符串?
eg consider the following class: 例如考虑以下课程:
class Person {
String Name;
int Salary;
...
}
Person p = new Person();
p.setName("Tony");
p.setSalary(1000);
I need to get something like: 我需要得到类似的东西:
Person: Name="Tony", Salary=1000
Importing Commons Lang you could use ToStringBuilder 导入Commons Lang你可以使用ToStringBuilder
Check method reflectionToString(java.lang.Object)
, this will create automatically the representation you are expecting. 检查方法
reflectionToString(java.lang.Object)
,这将自动创建您期望的表示。
This code: 这段代码:
Person p = new Person();
p.setName("Tony");
p.setSalary(1000);
System.out.println(ToStringBuilder.reflectionToString(p));
results this string: 结果这个字符串:
Person@64578ceb[Name=Tony,Salary=1000]
This is basically what toString
is for. 这基本上就是
toString
的用途。 But given you want this done automatically, you can create some general service that can do it. 但鉴于您希望自动完成此操作,您可以创建一些可以执行此操作的常规服务。 Use reflection to iterate all fields, and then print each one's name and value.
使用反射迭代所有字段,然后打印每个字段的名称和值。 Simplest way to print their values would be by using their
toString
, but you can also pass them into that printing service recursively on some cases (you'll have to find the halt condition, of course). 打印它们的值的最简单方法是使用它们的
toString
,但是你也可以在某些情况下以递归方式将它们传递给该打印服务(当然,你必须找到暂停条件)。
For example, on some class PrintUtils have: 例如,在某些类上,PrintUtils具有:
public static void printFields(Object o) {
System.out.print(o.getClass.getSimpleName() + ": ");
for (Field field : o.getClass().getDeclaredFields()) {
field.setAccessible(true); // you also get non-public fields
System.out.print(field.getName() + " = " + field.get(o) + ", ");
}
}
You'll have to handle exceptions etc. and possibly better format the output, of course. 当然,您必须处理异常等,并可能更好地格式化输出。 Also, this only print fields declared in the current class.
此外,这只是在当前类中声明的打印字段。 If you want fields declared higher in the inheritance hierarchy, you'll have to work a bit more.
如果您希望在继承层次结构中声明更高的字段,则必须多做一些工作。 Lastly, using reflection is much slower than just having a regular
toString
. 最后,使用反射比仅使用常规
toString
要慢得多。 If using toString
is possible, it is preferable. 如果可以使用
toString
, toString
可取。
sure you can override the toString
method of class. 确定你可以覆盖类的
toString
方法。
as follow: 如下:
class Person {
String name;
int salary;
...
@Override public String toString() {
return "Person: Name='" + name + "', Salary=" + salary;
}
}
refer for more details https://blogs.oracle.com/CoreJavaTechTips/entry/writing_tostring_methods_tech_days 请参阅更多详细信息https://blogs.oracle.com/CoreJavaTechTips/entry/writing_tostring_methods_tech_days
我认为你可以使用ToStringBuilder ,它是commons-lang
一部分。
class Person {
String Name;
int Salary;
...
@Override
public String toString() {
return "Person: Name = " + Name + "," +
"Salary="+Salary;
}
}
Person p = new Person();
p.setName("Tony");
p.setSalary(1000);
System.out.println(p.toString());
One way to do it is to rely on Apache Commons BeanUtils.describe . 一种方法是依靠Apache Commons BeanUtils.describe 。 This will produce a
Map
of bean's properties, which converts to a string nicely via Map.toString
. 这将生成一个bean的属性
Map
,它可以通过Map.toString
很好地转换为字符串。 If you want something more custom, you'll need to dig into the reflection API. 如果你想要更自定义的东西,你需要深入研究反射API。
你可以使用java中的消息格式: https ://docs.oracle.com/javase/tutorial/i18n//message.html seperate variable by - 你的类中有一个可读的字符串!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.