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
- 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);
}
- 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的更多相关文章
- Java之所有对象的公用方法>10.Always override toString
providing a good toString implementation makes your class much more pleasant to use. It is recommend ...
- 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 ...
- 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 ...
- 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 ...
- 《Effective Java》读书笔记 - 3.对于所有对象都通用的方法
Chapter 3 Methods Common to All Objects Item 8: Obey the general contract when overriding equals 以下几 ...
- Effective Java 第三版——10. 重写equals方法时遵守通用约定
Tips <Effective Java, Third Edition>一书英文版已经出版,这本书的第二版想必很多人都读过,号称Java四大名著之一,不过第二版2009年出版,到现在已经将 ...
- Effective Java 第三版——12. 始终重写 toString 方法
Tips <Effective Java, Third Edition>一书英文版已经出版,这本书的第二版想必很多人都读过,号称Java四大名著之一,不过第二版2009年出版,到现在已经将 ...
- Effective Java 第三版——40. 始终使用Override注解
Tips <Effective Java, Third Edition>一书英文版已经出版,这本书的第二版想必很多人都读过,号称Java四大名著之一,不过第二版2009年出版,到现在已经将 ...
- Effective Java 36 Consistently use the Override annotation
Principle Use the Override annotation on every method declaration that you believe to override a sup ...
随机推荐
- css实现高度不固定的div元素模块在页面中水平垂直居中
<!DOCTYPE html><html> <head> <title>Laravel</title> <link ...
- Velocity魔法堂系列三:模板与宿主环境通信
一.前言 Velocity作为历史悠久的模板引擎不单单可以替代JSP作为Java Web的服务端网页模板引擎,而且可以作为普通文本的模板引擎来增强服务端程序文本处理能力.而且Velocity被移植到不 ...
- MVC,布局页面
一>>> 在_ViewStart.cshtml文件中,加入: @{ Layout = "~/Views/Shared/_Layout.cshtml"; PageD ...
- .Net 自定义应用程序配置
.Net 自定义应用程序配置 引言 几乎所有的应用程序都离不开配置,有时候我们会将配置信息存在数据库中(例如大家可能常会见到名为Config这样的表):更多时候,我们会将配置写在Web.config或 ...
- 【JavaScript回顾】对象创建的几种模式
组合使用构造函数模式和原型模式 创建自定义类型的常见方式,就是组合使用构造函数模式与原型模式.构造函数模式用于定义实 例属性,而原型模式用于定义方法和共享的属性.结果,每个实例都会有自己的一份实例属性 ...
- [水]用vb写了个PCB
这学期我们学操作系统,所以得写个PCB. 于是我借鉴了一下windows的PCB,写了这个 Imports System.Runtime.InteropServices ''' <summary ...
- WPF listbox UI虚拟化
ListBox 默认是UI虚拟化的. 1. 原生使用 <ListBox VirtualizingPanel.IsVirtualizing="True" Virtualiz ...
- jQuery: jquery.json.js
http://api.jquery.com/jQuery.parseJSON/ http://www.json.org/json-zh.html http://fineui.codeplex.com/ ...
- 【洛谷 p3382】模板-三分法(算法效率)
题目:给出一个N次函数,保证在范围[l,r]内存在一点x,使得[l,x]上单调增,[x,r]上单调减.试求出x的值. 解法:与二分法枚举中点使区间分成2份不一样,三分法是枚举三分点,再根据题目的情况修 ...
- (旧)子数涵数·C语言——条件语句
首先,我们讲一下理论知识,在编程中有三种结构,分别是顺序结构.条件结构.循环结构,如果用流程图来表示的话就是: 那么在C语言中,如何灵活运用这三种结构呢?这就需要用到控制语句了. 而条件语句便是控制语 ...