通用类toString操作

时间:2018-04-27 22:50:09

标签: java generics tostring

我正在寻找仅使用utils打印泛型类的最有效方法。至于现在我得到StackOverflowException

public class Message<E, B, C> {
    private final int requestNumber;
    private final E data;
    private final B from;
    private final C to;
    private E_MessageStatus status;
    private int replyToRequestNumber;
}

这是我试图打印的toString()

public  String toString() {
    return "Message [requestNumber=" +  requestNumber + ", data=" + data+ ", from=" + from+ ", to=" + to
            + ", status=" + status + ", replyToRequestNumber=" + replyToRequestNumber + "]";
}

2 个答案:

答案 0 :(得分:0)

您可以在大多数主要IDE(例如Eclipse,IntelliJ)中自动生成通用toString()方法

这是IntelliJ生成的:

public String toString()
{
        return "Message{" + "requestNumber=" + requestNumber + ", data=" + data + ", from=" + from + ", to=" + to + ", status=" + status + ", replyToRequestNumber=" + replyToRequestNumber + '}';
}

答案 1 :(得分:0)

在IntelliJ中: print + print + Ctrl并写&#34;生成toString&#34;。

在Eclipse中: 来源 - &gt;生成toString