当我们覆盖toString()方法时,我们应该总是返回对象的字符串表示? [英] When we override the toString() method we should always return a string representation of the object?

查看:148
本文介绍了当我们覆盖toString()方法时,我们应该总是返回对象的字符串表示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

实际上,toString方法应返回对象的String表示形式。

In practice, the toString method should return a String representation of the object.

在一个项目中,我发现一些类重写了toString方法,允许返回null。

In one project, I found some classes that override the toString method allowing return null.

喜欢:

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

对我来说,这种做法违反了toString方法的主要建议应该返回对象的String表示。

For me, this practice is a violation to the principal propose of toString method that should return a String representation of the object.

Object.toString()的API文档说:

The API documentation for Object.toString() says:


public String toString()

返回对象的字符串表示形式。通常,
toString方法返回一个文本表示此
对象的字符串。结果应该是一个简洁但信息丰富的表示
,一个人很容易阅读。建议所有
子类重写此方法。

Returns a string representation of the object. In general, the toString method returns a string that "textually represents" this object. 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.

当我们覆盖Object的toString()方法时,你应该永远不会返回null?

When we override the toString() method of Object, you should never return null?

推荐答案

toString主要用于表示受众是程序员的事情,比如调试和记录,我们在哪里需要看一些对象的文本版本。当您在日志文件或调试器中看到对象时,使用对您有用的信息。从Effective Java,第3章,第10项:

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. 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:


虽然它不如遵守equals和hashCode合同那么重要(第8项,第9项,提供了良好的toString实现,使您的课程使用起来更加愉快。当对象传递给println,printf,字符串连接运算符或断言,或由调试器打印时,会自动调用toString方法。

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. 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.

Object#toString的API文档中的建议是:

The advice in the API documentation for Object#toString is:


通常,toString方法返回一个文本表示的字符串这个对象。结果应该是简洁但信息丰富的表示,便于人们阅读。建议所有子类都覆盖此方法。

In general, the toString method returns a string that "textually represents" this object. 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.

返回null永远不会算作提供信息,很容易产生误导。似乎有人使用toString来做一些它从未打算过的事情。

Returning null would never count as informative, and could easily be misleading. It seems like someone was using toString to do something it was never intended for.

这篇关于当我们覆盖toString()方法时,我们应该总是返回对象的字符串表示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆