ToString实现的共享多个类



我有多个使用以下代码的pojo类。

public class ToStringImpl {
public String toString(){
StringBuilder result = new StringBuilder();
String newLine = "n";

result.append( this.getClass().getName() );
result.append( " Data {" );
result.append(newLine);
//determine fields declared in this class only (no fields of superclass)
Field[] fields = this.getClass().getDeclaredFields();
//print field names paired with their values
for ( Field field : fields  ) {
result.append("  ");
try {
result.append( field.getName() );
result.append(": ");
//requires access to private field:
result.append( field.get(this) );
} catch ( IllegalAccessException ex ) {
System.out.println(ex);
}
result.append(newLine);
}
result.append("}");
return result.toString();
}
}

如何从不同的班级中调用上述班级?假设我有一个名为客户、商店、库存的pojo类

public class Customer {
private String name;
private String address;
...getter...setter...
public String toString(){
ToStringImpl log = new ToStringImpl();
//how do I pass different classes here?
return log.toString();
}
}
public class Store {
private String logo;
private String type;
....getter...setter...
}
public class Inventory {
private boolean isAvailable;
private long index;
...getter...setter
}

对于每节课,我如何通过不同的课程?或者是否有更好的方法?还是最好创建toString作为接口,在每个类中实现它,并将其作为构造函数传递?

您可以做的是使ToStringImpl类中的toString()方法静态。不过,我不会称之为toString(),而是将其改为类似getClassString()的东西

示例

public static String getClassString(Object o)
{
StringBuilder result = new StringBuilder();
String newLine = "n";
result.append(o.getClass().getName());
result.append(" Data {");
result.append(newLine);
// determine fields declared in this class only (no fields of
// superclass)
Field[] fields = o.getClass().getDeclaredFields();
// print field names paired with their values
for (Field field : fields)
{
result.append("  ");
try
{
result.append(field.getName());
result.append(": ");
// requires access to private field:
result.append(field.get(o));
}
catch (IllegalAccessException ex)
{
System.out.println(ex);
}
result.append(newLine);
}
result.append("}");
return result.toString();
}

然后在你的POJO类中,用调用它

public String toString()
{
// how do I pass different classes here?
// by passing the 'this' reference
return ToStringImpl.getClassString(this);
}

已经有一个库可以做到这一点。在apachecommons库中查找ToStringBuilder,域对象的toString方法如下所示:

@Override public String toString() {
return ToStringBuilder.reflectionToString(this);
}

在我看来,最好的计划是删除本地代码并放入apachecommons,或者使用Project Lombok。如果你必须重新发明这个轮子,那么复制ToStringBuilder使用静态方法并将要打印的对象作为参数的例子是合理的。

ToStringBuilder包含一个功能,允许您限制打印哪些字段,为了您自己的理智,您自己的代码应该做类似的事情。toString方法用于打印用于调试和日志记录的信息。如果你只得到所有的字段,比如你发布的代码,它会在每次你在日志条目中调用toString时转储整个对象的内容,并且你会有一些不可读的东西,它会填满你的日志,并减缓你的应用程序编写所有这些信息的速度。

你是这里信息的消费者,让它成为有用的东西,而不是压倒性的。

当您重写一个方法时,例如Object#toString(),您只为该类重写它。您可以执行以下操作之一:

  • 将您的toString()添加到每个可能需要它的类中,然后从该对象所在的任何位置对其调用toString()。(不推荐)
  • 在每个想要的类上扩展ToStringImpl,并在对象上调用toString()
  • 使ToStringImpl#toString()为静态并传递一个对象作为参数(推荐),例如:

    public static void objectToString(Object ob){
    //your code here, just replace "this" with "ob"
    }
    

最新更新