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的更多相关文章

  1. Java之所有对象的公用方法>10.Always override toString

    providing a good toString implementation makes your class much more pleasant to use. It is recommend ...

  2. Effective Java 09 Always override hashCode when you override equals

    Failure to do so will result in a violation of the general contract for Object.hashCode, which will ...

  3. Effective Java Index

    Hi guys, I am happy to tell you that I am moving to the open source world. And Java is the 1st langu ...

  4. Part 57 to 58 Why should you override ToString and Equal Method

    Part 57 Why should you override ToString Method sometimes you can override ToString method like that ...

  5. 《Effective Java》读书笔记 - 3.对于所有对象都通用的方法

    Chapter 3 Methods Common to All Objects Item 8: Obey the general contract when overriding equals 以下几 ...

  6. Effective Java 第三版——10. 重写equals方法时遵守通用约定

    Tips <Effective Java, Third Edition>一书英文版已经出版,这本书的第二版想必很多人都读过,号称Java四大名著之一,不过第二版2009年出版,到现在已经将 ...

  7. Effective Java 第三版——12. 始终重写 toString 方法

    Tips <Effective Java, Third Edition>一书英文版已经出版,这本书的第二版想必很多人都读过,号称Java四大名著之一,不过第二版2009年出版,到现在已经将 ...

  8. Effective Java 第三版——40. 始终使用Override注解

    Tips <Effective Java, Third Edition>一书英文版已经出版,这本书的第二版想必很多人都读过,号称Java四大名著之一,不过第二版2009年出版,到现在已经将 ...

  9. Effective Java 36 Consistently use the Override annotation

    Principle Use the Override annotation on every method declaration that you believe to override a sup ...

随机推荐

  1. HTTP状态码大全(转自wiki)

    1xx消息 这一类型的状态码,代表请求已被接受,需要继续处理.这类响应是临时响应,只包含状态行和某些可选的响应头信息,并以空行结束.由于HTTP/1.0协议中没有定义任何1xx状态码,所以除非在某些试 ...

  2. 高级四则运算器—结对项目总结(193 &105)

    高级四则运算器—结对项目总结 为了将感想与项目经验体会分割一下,特在此新开一篇博文. 界面设计 啥都不说,先上图震慑一下... 上面的三个界面是我们本次结对项目的主界面,恩,我也觉得挺漂亮的!你问我界 ...

  3. java版复利计算器升级

    github地址:https://github.com/iamcarson/Carson 伙伴:彭宏亮 学号:201406114148 与伙伴工作帅照: 本次升级的地方: 1.改善了界面显示,让界面整 ...

  4. Scrum团队

    5.Scrum团队成立 5.1 团队名称,团队目标.团队口号.团队照: 团队名称:@four! 团队目标:做出像“数学口袋精灵”那么棒的软件 团队口号:多劳多得 团队照: 5.2 角色分配 产品负责人 ...

  5. ADO.NET学习系列(二)

    这次我使用ADO.NET来插入一条数据,到数据库中.主用到存储过程.我不想每次都是用SQL文本的形式了,那样始终没有进步--- 下面首先,我把我这次练习要用到的数据库脚本,贴出来: USE maste ...

  6. vs2015 Android SDK

    It was not possible to complete an automatic installation. This might be due to a problem with your ...

  7. mysql数据库入门

    在很多地方都有人提到MySQL这个数据,之前没有接触过的mysql数据库的童鞋们可以跟我一起走进mysql的世界. http://hovertree.com/menu/mysql/ 安装我就不多说了, ...

  8. Python入门笔记(22):Python函数(5):变量作用域与闭包

    一.全局变量与局部变量 一个模块中,最高级别的变量有全局作用域. 全局变量一个特征就是:除非被删除,否则他们存活到脚本运行结束,且对于所有的函数都可访问. 当搜索一个标识符(也称变量.名字等),Pyt ...

  9. Dynamics AX 中重点数据源方法

     数据源方法 描述   Active  当用户刚选中一行数据时执行该方法.若选中的是主表的数据,也用该方法来触发加载从表符合条件的数据.主要覆盖该方法来根据条件设置记录及其字段是否可见或是否可被编辑. ...

  10. C#怎样处理xml文件的大于号和小于号等常用符号(xml符号引发的程序错误)

    在程序中由xml配置而成的sql语句要转换为C#支持的sql语句 <settings> <select> a.*</select> <from> (se ...