I think the format produced by Guava's MoreObjects.toStringHelper() is pretty nice, but it's mainly just good to have some consistent format that you use:
There's also the possibility to add an identifier with @, for example the default style for the commons-langToStringBuilder does that (using its own example):
Is there any standard, or simply just a best practice for a style?
No. The "best" output for a toString() method is determined by what you want to use it for. Is it for serializing the object state in a form that allows it to be deserialized? Is it for creating debug messages? Is it for rendering the object for display to end-users?
(Note that in Java, the toString() method can be used for either purpose. Using toString() for / in end-user messages has problems ... but people do it anyway.)
If you want to develop an in-house style for your debug/logging toString() methods, that's fine. But unless there was a requirement for this, I wouldn't bother. IMO, it is effort that could better be spent elsewhere.
Where the id is whatever makes sense for that object to be an identifier - the name for the canonical Person object, a primary key for an object from a database, etc.
check out phps print_r($obj, true) or also serialize() could work, dont know exactly for what its needed for. jsons is also a clean solution, especially if u want to import the data in javascript environbments
发布评论
评论(8)
我认为 Guava 的 MoreObjects.toStringHelper() 非常好,但主要是最好使用一些一致的格式:
I think the format produced by Guava's MoreObjects.toStringHelper() is pretty nice, but it's mainly just good to have some consistent format that you use:
就我个人而言,我发现
[]
和{}
的混合不太容易立即查看层次结构。我喜欢这种格式(并且我已经在很多地方看到过它的使用):
还可以使用
@
添加标识符,例如 < 的默认样式 a href="http://commons.apache.org/lang/" rel="nofollow noreferrer">commons-langToStringBuilder
这样做(使用它自己的示例):Personally, I find the mix of
[]
and{}
not so easy to get an immediate view of the hierarchy.I like this format (and I've seen it being used in a number of places):
There's also the possibility to add an identifier with
@
, for example the default style for the commons-langToStringBuilder
does that (using its own example):json 语法似乎非常适合,因为它是专门为将复杂对象表示为字符串而设计的
json syntax seems to fit pretty well since it was designed specifically to represent complex objects as strings
不是问题的直接答案,但是以下内容可以在初始开发过程中节省时间:
免责声明:使用 Apache Commons 库。
Java > 中添加一个名为
;将以下内容添加到其模式文本区域中:xreflect
的新 Eclipse 模板。编辑>模板OK
,OK
xreflect
并按 Ctrl + Space 自动填充 equals()、toString() 和 hashCode() 方法。Not a direct answer to the question, however below would be a time saver during initial development:
Disclaimer: Apache Commons library is used.
xreflect
inJava > Editor > Templates
; Add below into its pattern textarea:OK
,OK
xreflect
and press Ctrl + Space to autofill equals(), toString() and hashCode() methods automatically.不会。
toString()
方法的“最佳”输出取决于您想要使用它的用途。是为了以允许反序列化的形式序列化对象状态吗?它是用于创建调试消息吗?是为了渲染对象以显示给最终用户吗?(请注意,在 Java 中,
toString()
方法可用于任一目的。在最终用户消息中使用toString()
for / 会出现问题...但是人们无论如何都要做。)如果您想为调试/日志记录
toString()
方法开发内部样式,那没问题。但除非有这样的要求,否则我不会打扰。在我看来,这种努力最好用在其他地方。No. The "best" output for a
toString()
method is determined by what you want to use it for. Is it for serializing the object state in a form that allows it to be deserialized? Is it for creating debug messages? Is it for rendering the object for display to end-users?(Note that in Java, the
toString()
method can be used for either purpose. UsingtoString()
for / in end-user messages has problems ... but people do it anyway.)If you want to develop an in-house style for your debug/logging
toString()
methods, that's fine. But unless there was a requirement for this, I wouldn't bother. IMO, it is effort that could better be spent elsewhere.如果您的对象具有可能用作标识符的内容,我会实现类似于您的第二个示例的内容:
其中
id
是对该对象作为标识符有意义的任何内容 - 规范的名称Person
对象、数据库中对象的主键等。If your objects have something that might be useful as an identifier, I'd implement something like your second example:
Where the
id
is whatever makes sense for that object to be an identifier - the name for the canonicalPerson
object, a primary key for an object from a database, etc.既然您询问了其他开源项目,那么 jEdit 是如何实现的,这与 Wouter 的类似:
Since you asked about what other open source projects to, here's how jEdit does it, which is similar to Wouter's:
查看 phps print_r($obj, true)
或者serialize()也可以工作,但不知道它到底需要什么。
jsons 也是一个干净的解决方案,特别是如果你想在 javascript 环境中导入数据
check out phps print_r($obj, true)
or also serialize() could work, dont know exactly for what its needed for.
jsons is also a clean solution, especially if u want to import the data in javascript environbments