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

在实践中,ToString方法应该返回对象的字符串表示形式。

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

像:

@Override

public String toString() {

.......

return null;

}

对于我来说,这种做法违反了ToString方法的主要建议,该方法应返回对象的字符串表示形式。

object.toString()的API文档显示:

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

当我们重写对象的toString()方法时,不应该返回空值?

我承认我很好奇,在.......后面藏着什么?

实际上,我不知道,我在一个要求o1.toString()==null的比较器对象中看到了,当我看到我对自己说这不正常时,这不是一个好的实践。

ToString主要用于表示访问群体是程序员的情况,例如调试和日志记录,在这些情况下我们需要看到对象的一些文本版本。使用来自对象的信息,当您在日志文件或调试器中看到它时,这些信息将对您有用。从有效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. 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.

API文档中有关object to字符串的建议是:

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.

返回空值永远不会算作信息量大,而且很容易产生误导。好像有人用绳子做了一件从来没有打算做的事。

toString()的使用不限于调试。作为一个反例,考虑Integer.toString().,您是否打算暗示返回空值违反了jls?

@安迪:操作人员说返回空值可能会违反JLS。在您的示例中,ToString是使用适当格式化程序的快捷方式。因此,与用户友好的表示方式不同,它看起来的确是一种最适合于调试、日志记录、在repl中查看等的表示方式。

作为另一个反例,考虑Enum.toString()。重点是toString()经常用于调试以外的目的。在日志文件或调试器中随意更改实用程序的现有代码可能会破坏其他用途。

@安迪:我从调试扩展了使用;注意,对于enum.toString,apidoc意味着它用于提供"程序员友好"的表示。

Java语言规范不禁止从EDCOX1 OR 0中返回null。相反,它明确地承认可以返回空值。

第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)

您引用的文本来自Object.toString()的API文档。和您一样,我把它理解为强烈暗示返回了非空值。

返回非空值将是最有用的。返回空值在调试器中可能会混淆,这使得很难用ToString()的空返回值区分空引用和非常好的非空引用。

只要应用程序可行,就应该重写toString()方法。

至于返回空值,我发现这是完全无用的,因为toString()主要在调试期间使用。因此,不应该从ToString()方法返回空值。

你不能解决愚蠢的问题,但你可以从中学习。

在我看来,您不应该在toString()方法中返回空值。

它既不能提供信息,也不能使用。相反,使用toString()方法为数据模型类实例返回正确且信息丰富的字符串将帮助您和您的团队使用类,并将它们合并到更复杂的数据结构中。

toString()是对象的"人工"表示,像Nathan一样用于调试,也像应用程序中表示的"速度方法"。

有时,Pople将ToString()与序列化/反序列化(必须包括所有重要(非暂时)字段)混合在一起,并且由于读取值的算法而精确。

这看起来像是toString()的自定义用法。此重写方法纯粹基于用户根据需要尝试实现的实现。只要用户希望返回该值,就不会对返回空值造成损害,因为可以将空值存储在字符串数据类型中。

但是,是的,除非你有正当理由这样做,否则凌驾于字符串之上不是一个好的实践。

覆盖toString()没有任何错误,而且它经常是有用的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值