×

消息

EU e-Privacy Directive

This website uses cookies to manage authentication, navigation, and other functions. By using our website, you agree that we can place these types of cookies on your device.

View e-Privacy Directive Documents

You have declined cookies. This decision can be reversed.

任何情况下toString方法都应该被覆盖

java.lang.Object已经提供了一个默认的toString方法,它的返回值,是"[类名]@[对象的hash]",比如:"PhoneNumber@163b91"。这样的信息,显然不太有用。所以语言规范中建议开发者在所有Object的子类中都覆盖toString方法,以返回明确易读的关于对象的信息。

在覆盖toString方法时,应该返回关于对象的所有有用信息。如果对象中信息量巨大,那么应该返回一个概要。比如上面电话号码的例子,可以返回类似于"(707) 867-5309"的结果;而对于"曼哈顿黄页"这样的对象,可以返回"Manhattan white pages (1487536 listings)",而不是把所有的listing都列出来。

关于返回值的格式,可以在注释中以规范的形式公布,并且提供一个factory或者构造函数,让开发者可以方便地在对象和它的字符串表示之间转换。这样做的坏处是,一旦规范公布,并且类型被大量使用,之后就很难对其进行修改,因为那将破坏用户端代码。不管toString格式是否提供规范,都应该在注释中说明,比如下面是为item 9中PhoneNumber类定义的toString方法:

/**
* Returns the string representation of this phone number.
* The string consists of fourteen characters whose format
* is "(XXX) YYY-ZZZZ", where XXX is the area code, YYY is
* the prefix, and ZZZZ is the line number. (Each of the
* capital letters represents a single decimal digit.)
*
* If any of the three parts of this phone number is too small
* to fill up its field, the field is padded with leading zeros.
* For example, if the value of the line number is 123, the last
* four characters of the string representation will be "0123".
*
* Note that there is a single space separating the closing
* parenthesis after the area code from the first digit of the
* prefix.
*/
@Override public String toString() {
	return String.format("(%03d) %03d-%04d", areaCode, prefix, lineNumber);
}

下面是不提供格式规范的代码:

/**
* Returns a brief description of this potion. The exact details
* of the representation are unspecified and subject to change,
* but the following may be regarded as typical:
*
* "[Potion #9: type=love, smell=turpentine, look=india ink]"
*/
@Override public String toString() { ... }

还有一点需要注意,toString方法中提供的所有信息,都要有直接存取接口。否则,使用者很可能会被迫去解析toString的返回值,这使得它成为一个事实上的api,并且客户代码严重依赖其格式,之后很难再做改动。

提交评论


安全码
刷新