简体   繁体   English

当我们覆盖toString()方法时,我们应该总是返回对象的字符串表示?

[英]When we override the toString() method we should always return a string representation of the object?

In practice, the toString method should return a String representation of the object. 实际上,toString方法应返回对象的String表示形式。

In one project, I found some classes that override the toString method allowing return null. 在一个项目中,我发现了一些覆盖toString方法的类,允许返回null。

Like: 喜欢:

@Override
public String toString() {
    .......
    return null;
}

For me, this practice is a violation to the principal propose of toString method that should return a String representation of the object. 对我来说,这种做法违反了toString方法的主要建议,该方法应该返回对象的String表示。

The API documentation for Object.toString() says: Object.toString()的API文档说:

public String toString() public String toString()

Returns a string representation of the object. 返回对象的字符串表示形式。 In general, the toString method returns a string that "textually represents" this object. 通常,toString方法返回一个“文本表示”此对象的字符串。 The result should be a concise but informative representation that is easy for a person to read. 结果应该是一个简洁但信息丰富的表示,便于人们阅读。 It is recommended that all subclasses override this method. 建议所有子类都重写此方法。

When we override the toString() method of Object, you should never return null? 当我们覆盖Object的toString()方法时,你永远不应该返回null?

toString is mainly used for representing things where the audience is the programmer, like debugging and logging, where we need to see some text version of an object. toString主要用于表示受众是程序员的事情,比如调试和日志记录,我们需要查看对象的某些文本版本。 Use information from the object that will be useful to you when you see it in a log file or in the debugger. 当您在日志文件或调试器中看到对象时,使用对您有用的信息。 From Effective Java, Chapter 3, item 10: 从Effective Java,第3章,第10项:

While it isn't as important as obeying the equals and hashCode contracts (Item 8, Item 9), providing a good toString implementation makes your class much more pleasant to use. 虽然它不像遵守equals和hashCode合同那样重要(Item 8,Item 9),但是提供一个好的toString实现会让你的类更加愉快。 The toString method is automatically invoked when an object is passed to println, printf, the string concatenation operator, or assert, or printed by a debugger. 将对象传递给println,printf,字符串连接运算符或断言或由调试器打印时,会自动调用toString方法。

The advice in the API documentation for Object#toString is: Object#toString的API文档中的建议是:

In general, the toString method returns a string that "textually represents" this object. 通常,toString方法返回一个“文本表示”此对象的字符串。 The result should be a concise but informative representation that is easy for a person to read. 结果应该是一个简洁但信息丰富的表示,便于人们阅读。 It is recommended that all subclasses override this method. 建议所有子类都重写此方法。

Returning null would never count as informative, and could easily be misleading. 返回null将永远不会被视为提供信息,并且很容易产生误导。 It seems like someone was using toString to do something it was never intended for. 似乎有人使用toString来做一些它从未打算过的事情。

The Java Language Specification does not prohibit returning null from toString() . Java语言规范不禁止从toString()返回null。 Instead, it explicitly acknowledges that null may be returned. 相反,它明确承认可以返回null。

Section 4.3.1: Objects 第4.3.1节:对象

The string concatenation operator + (§15.18.1), which, when given a String operand and a reference, will convert the reference to a String by invoking the toString method of the referenced object (using "null" if either the reference or the result of toString is a null reference) 字符串连接运算符+(§15.18.1),当给定String操作数和引用时,将通过调用引用对象的toString方法将引用转换为String(如果引用或引用,则使用“null”) toString的结果是空引用)

The text you cite is from the API documentation for Object.toString() . 您引用的文本来自Object.toString()的API文档。 Like you, I read it to strongly imply that a non-null value is returned. 和你一样,我读它时强烈暗示返回一个非null值。

Returning a non-null value will be most useful. 返回非空值将是最有用的。 Returning null may be confusing in debuggers, making it difficult to distinguish between a null reference and a perfectly good non-null reference with an unfortunately null return value from toString(). 返回null可能会在调试器中造成混淆,因此难以区分空引用和非常好的非空引用,以及来自toString()的遗憾的null返回值。

You should override the toString() method whenever it is practical for the application to do so. 只要应用程序可行 ,就应该覆盖toString()方法。

As for returning null, I find that to be utterly useless as toString() is mostly used during debugging. 至于返回null,我发现完全没用,因为toString()主要在调试期间使用。 So no, you should never return null from a toString() method. 所以不,你永远不应该从toString()方法返回null。

You cannot fix stupid, but you can learn from it. 你不能修复愚蠢,但你可以从中学习。

toString() is "human" presentation of Object, used like Nathan for debugging and as "speed method" of presentation in applications. toString()是Object的“人类”表示,像Nathan一样用于调试,也可以作为应用程序中表示的“速度方法”。

Sometimes poeple are mixing toString() with serialization / deserialization (must include all important (non-transient) fields) and be precise because of algorithm for reading values. 有时候人们正在混合toString()与序列化/反序列化(必须包括所有重要的(非瞬态)字段)并且由于读取值的算法而准确。

From my point of view your should not return null in toString() methods. 从我的角度来看,你不应该在toString()方法中返回null。

It is not informative nor usable. 它没有提供信息也无法使用。 In contrast, using toString() method to return correct and informative strings for your data model class instances will help you and your teammates to use your classes and incorporate them to more complex data structures. 相反,使用toString()方法为数据模型类实例返回正确且信息丰富的字符串将帮助您和您的团队成员使用您的类并将它们合并到更复杂的数据结构中。

This looks like customized usage of ToString(). 这看起来像ToString()的自定义用法。 This override method is purely based upon the implementation which user tries to achieve as per his/her needs. 这种覆盖方法完全基于用户试图根据他/她的需要实现的实现。 As per me returning null is no harm as long as user expects this value to be returned, as null could be stored in string data type. 据我所知,只要用户期望返回此值,返回null就没有坏处,因为null可以存储在字符串数据类型中。

But yes overriding ToString is not a good practice unless you got proper justification to do this. 但是,除非你有充分的理由去做,否则覆盖ToString不是一个好习惯。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM