Effective Java Item10
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 numb
相关文档:
tomcat启动web服务的时候总是会有8小时的时差,
琢磨了好久,最简单的办法就是
找到文件夹jdk1.6.0_07\jre\lib\zi\Etc
这里边都是一些时区文件GTM+1,GTM+2,GTM+3....
备份一份Etc
拷贝N份GTM+8文件(中国的时区是GTM+8)
把GMT+8文件名改成GTM+1,GTM+2,GTM+3....
覆盖掉原来的Etc里边的文件
重启tomcat问题解决 ......
举例子说明一切。
JAVA_HOME: C:\j2sdk1.4.2_13
Path: C:\j2sdk1.4.2_13\bin;
Classpath:.;JAVA_HOME\lib\tools.jar ; JAVA_HOME\lib\dt.jar;
&n ......
目录
1. 简介
2. 安装
3. log4j基本概念
3.1. Logger
3.2. Appender
3.2.1. 使用ConsoleAppender
3.2.2. 使用FileAppender
3.2.3. 使用WriterAppender
3.3. Layout
3.4. 基本示例
3.4.1. SimpleLayout和FileAppender
3.4.2. HTMLLayout和WriterAppender
3.4.3. PatternLayout和ConsoleAppender
4. 使用外部 ......