toString()
返回的字符串,应该符合某种特定的规范格式,而且 应该在文档中明确表明意图。格式化的数据,让客户端程序员可以更容易地解析返回的字符串,以得到某些信息。
public class PhoneNumber {
// remainder omitted
/**
* Returns the string representation of the phone number:
* The string consists of fourteen characters whose format is:
* "(XXX) YYY - ZZZZ"
* where, XXX is the area code;
* YYY is the prefix;
* ZZZZ is the line number;
* The exact details of the representation are unspecified and
* subject to change.
*/
public String toString() {
return "(" + areaCode + ") " + prefix + " - " + lineNumber;
}
}
但客户端程序员依赖于解析toString()
返回的字符串的副作用是:一旦以后更改这种格式,过去很多依赖解析数据的代码将无法工作。 这会增加系统维护的难度,因为一旦指定了某种格式,以后就很难更改。
因此无论是否指定格式,都应该为toString()
返回值中包含的所有信息,提供一种编程式的访问途径。比如说getter访问方法。
public class PhoneNumber {
private final short areaCode;
private final short prefix;
private final short lineNumber;
public String toString() {
return "(" + areaCode + ") " + prefix + " - " + lineNumber;
}
/**
* 提供toString()方法中返回的所有信息的getter访问方法。
*/
public int getAreaCode() {
return (int)areaCode;
}
public int getPrefix() {
return (int)prefix;
}
public int getLineNumber() {
return (int)lineNumber;
}
}