item12 · 始终要覆盖 toString
函数原型
/**
* Returns a string representation of the object. In general, the
* {@code 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.
* <p>
* The {@code toString} method for class {@code Object}
* returns a string consisting of the name of the class of which the
* object is an instance, the at-sign character `{@code @}', and
* the unsigned hexadecimal representation of the hash code of the
* object. In other words, this method returns a string equal to the
* value of:
* <blockquote>
* <pre>
* getClass().getName() + '@' + Integer.toHexString(hashCode())
* </pre></blockquote>
*
* @return a string representation of the object.
*/
public String toString() {
return getClass().getName() + "@" + Integer.toHexString(hashCode());
}
- 可见
Object
类默认提供的toString
方法的实现包含了类的名称、一个 ”@“ 符号以及散列码的无符号十六进制表示法。通常这种提示都没有什么用 - 正如
toString
约定中指出It is recommended that all subclasses override this method.
最好去覆盖toString
方法 - 在实际应用中,
toString
方法应该返回对象中包含的所有值的关注的信息 - 无论是否指定输出的格式,都应该在文档中明确地表明你的意图
/**
* Converts this {@code Date} object to a {@code String}
* of the form:
* <blockquote><pre>
* dow mon dd hh:mm:ss zzz yyyy</pre></blockquote>
* where:<ul>
* <li>{@code dow} is the day of the week ({@code Sun, Mon, Tue, Wed,
* Thu, Fri, Sat}).
* <li>{@code mon} is the month ({@code Jan, Feb, Mar, Apr, May, Jun,
* Jul, Aug, Sep, Oct, Nov, Dec}).
* <li>{@code dd} is the day of the month ({@code 01} through
* {@code 31}), as two decimal digits.
* <li>{@code hh} is the hour of the day ({@code 00} through
* {@code 23}), as two decimal digits.
* <li>{@code mm} is the minute within the hour ({@code 00} through
* {@code 59}), as two decimal digits.
* <li>{@code ss} is the second within the minute ({@code 00} through
* {@code 61}, as two decimal digits.
* <li>{@code zzz} is the time zone (and may reflect daylight saving
* time). Standard time zone abbreviations include those
* recognized by the method {@code parse}. If time zone
* information is not available, then {@code zzz} is empty -
* that is, it consists of no characters at all.
* <li>{@code yyyy} is the year, as four decimal digits.
* </ul>
*
* @return a string representation of this date.
* @see java.util.Date#toLocaleString()
* @see java.util.Date#toGMTString()
*/
public String toString() {
// "EEE MMM dd HH:mm:ss zzz yyyy";
BaseCalendar.Date date = normalize();
StringBuilder sb = new StringBuilder(28);
int index = date.getDayOfWeek();
if (index == BaseCalendar.SUNDAY) {
index = 8;
}
convertToAbbr(sb, wtb[index]).append(' '); // EEE
convertToAbbr(sb, wtb[date.getMonth() - 1 + 2 + 7]).append(' '); // MMM
CalendarUtils.sprintf0d(sb, date.getDayOfMonth(), 2).append(' '); // dd
CalendarUtils.sprintf0d(sb, date.getHours(), 2).append(':'); // HH
CalendarUtils.sprintf0d(sb, date.getMinutes(), 2).append(':'); // mm
CalendarUtils.sprintf0d(sb, date.getSeconds(), 2).append(' '); // ss
TimeZone zi = date.getZone();
if (zi != null) {
sb.append(zi.getDisplayName(date.isDaylightTime(), TimeZone.SHORT, Locale.US)); // zzz
} else {
sb.append("GMT");
}
sb.append(' ').append(date.getYear()); // yyyy
return sb.toString();
}