Java – converts a class object to a human readable string
•
Java
Is there any way to automatically convert custom class objects to human readable strings?
For example, consider the following courses:
class Person { String Name; int Salary; ... } Person p = new Person(); p.setName("Tony"); p.setSalary(1000);
I need something similar:
Person: Name="Tony",Salary=1000
Solution
Tostringbuilder can be used to import commons Lang
Check the method reflectiontostring (Java. Lang. object), which will automatically create the representation you want
This Code:
Person p = new Person(); p.setName("Tony"); p.setSalary(1000); System.out.println(ToStringBuilder.reflectionToString(p));
The result is this string:
Person@64578ceb[Name=Tony,Salary=1000]
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码