Effective Java Item10-总是覆盖toString方法
来源:互联网 发布:淘宝联盟有部分退款 编辑:程序博客网 时间:2024/05/16 23:39
Effective Java 2nd Edition Reading Notes
Item10: Always override toString
总是覆盖toString方法
java.lang.Object的toString方法的说明为:
Returns a string representation of the object. In general, the 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.
The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@', 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:
getClass().getName() + '@' + Integer.toHexString(hashCode())
java.lang.Object的toString方法返回的是类的名字,加上@符号,然后是一个无符号的16进制数(对象的hash code值),这样的toString方法显然传达的信息还不够。而toString方法的说明中包含了“建议所有的子类都覆盖这个方法”。
toString方法在println,printf,字符串连接,以及assert断言中,以及在调试器中,都被调用。
toString方法应该包含所有对象的重要信息。
另外一个问题是:是否需要对toString返回的值进行指定格式。
指定格式的好处在于它可以做为一个标准,不会带有歧义,并且可读性好。这个返回值可以用于输入或者输出,持久化的可读对象(xml文件)。如果指定格式的话,最好再提供一个工厂类用于根据String构造实例。
指定格式的缺点在于它是和接口一样,一旦指定了,就不能改变,因为可能有外部使用它,一旦它改变了,外部代码将无法使用。
一旦需要指定格式,那么要准确的定义并进行文档注释:
/**
* 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);
}
- Effective Java Item10-总是覆盖toString方法
- Effective C# Item5:总是提供ToString()方法
- Effective Java之总是要改写toString
- Effective Java(覆盖equals时总要覆盖hashcode方法、始终要覆盖toString)
- Effective Java (10) 始终要覆盖toString
- Effective Java读书笔记-始终要覆盖toString
- java反射覆盖toString()方法
- Effective Java 对于所用对象都通用的方法 10.始终要覆盖toString,玩转toString
- effective java(10) 之始终要覆盖toString
- 总是提供ToString()方法
- 使用Java反射机制覆盖toString()方法
- Effective C# Item10:理解GetHashCode()方法的缺陷
- Effective C#阅读笔记-4总是为你的类提供ToString()方法
- Java中覆盖toString()
- 十月读书笔记:Effective Java(五)--clone和toString方法
- 《Effective java》读书记录-第10条-始终要覆盖toString
- 覆盖toString()方法的坑
- 《问题总结》JAVA什么时候要覆盖toString()、equals()、hashCode()方法??为什么要覆盖呢? 《通俗回答》
- 迷恋中国风
- 开始→运行→输入的命令集锦(转载)
- Acegi + Spring + Hibernate + Struts 2搭建基于角色的权限控制系统
- quartz 从入门到进阶
- 使用quartz实现定时功能
- Effective Java Item10-总是覆盖toString方法
- 使用私匙加密文件
- Quartz + Spring 简单实现定时任务
- 走迷宫
- lucene笔记
- 进程间通讯方法总结参考(转)
- Effective Java Item12-考虑实现Comparable接口
- vivi启动过程------s3c2440
- Flex坐标系统及转换