首页 > 其他 > 详细

Effective Java 10 Always override toString() method

时间:2014-03-05 21:21:55      阅读:377      评论:0      收藏:0      [点我收藏+]

Advantage

Provide meaningful of an object info to client.

Disadvantage

Constrain the ability of changing the toString() implementation since this will affect the client who use this formatted string as persistence.

   

Note

  1. Whether or not you decide to specify the format, you should clearly document your intentions.

    /**

    * 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);

    }

       

  2. Whether or not you specify the format, provide programmatic access to all of the information contained in the value returned by toString .

Effective Java 10 Always override toString() method,布布扣,bubuko.com

Effective Java 10 Always override toString() method

原文:http://www.cnblogs.com/haokaibo/p/3583105.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!