69 effective java 10 | 明升ms88


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方法时,应该返回关于对象的所有有用信息。如果对象中信息量巨大,那么应该返回一个概要。比如上面电话号码的例子,可以返回类似于"(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() { ... }