When is it desired to not implement toString() in Java?

后端 未结 24 2097
心在旅途
心在旅途 2020-12-10 00:45

A lead developer on my project has taken to referring to the project\'s toString() implementations as \"pure cruft\" and is looking to remove them from the code base.

<
24条回答
  •  不思量自难忘°
    2020-12-10 01:12

    I would only implement it for more complex objects where client code doesn't care about the fine grained details of the object state, but rather care about some more human understandable, sense making message, that summarizes what is going on, state wise...

    For everything else, like JavaBeans, I would expect client code to throw my object into a ToStringBuilder method or similar, if it needs to do low-level debugging.

    ToStringBuilder.reflectionToString(myObject);
    

    Or client code should just call standard property getters and log the way they like...

提交回复
热议问题