zoukankan      html  css  js  c++  java
  • Effective Java 10 Always override toString() method

    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 .
    作者:小郝
    出处:http://www.cnblogs.com/haokaibo/
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    python的编码判断_unicode_gbk/gb2312_utf8(附函数)
    stat文件状态信息结构体
    内核配置中 ramdisk 大小修改
    mount命令详解
    dirent和DIR 结构体 表示文件夹中目录内容信息
    nandwrite 参数
    mke2fs 制作ext2文件系统image
    ext2文件系统错误
    照度/感光度(Lux)
    摄像机的几个重要的技术指标
  • 原文地址:https://www.cnblogs.com/haokaibo/p/always-override-toString-method.html
Copyright © 2011-2022 走看看