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.·½·¨Ò» ÓÃTimerTask
ÀûÓÃjava.util.TimerºÍjava.util.TimerTaskÀ´×ö¶¯Ì¬¸üУ¬±Ï¾¹Ã¿´Î¸üпÉÒÔ¿´×÷ÊǼÆÊ±1Ãë·¢ÉúÒ»´Î¡£
´úÂëÈçÏ£º
import java.awt.Dimension;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util ......
Java±à³ÌÄÇЩʶù52—·½·¨ÉùÃ÷
µÚÆßÕÂ ·½·¨
·½·¨µÄÀ´Ô´
·½·¨µÄ¸ÅÄîÀ´Ô´ÓÚÊýѧÉϵĺ¯Êý£¬ÔÚÊýѧÖУ¬µ±Êý¾Ý¾ßÓÐÒ»¶¨µÄ¹æÂÉʱ£¬¾ÍÊÇÓÃÒ»¸öº¯ÊýÀ´´úÂë¸ÃÊý×ֵĹæÂÉ£¬ÀýÈçf£¨n£©=nÔò´ú±í1¡¢2¡¢3¡¢……ÕâÑùµÄÒ»¸öÊýÁС£ÔÚÊýѧÉÏnÊDzÎÊý£¬¶ÔÓÚÈ·¶¨µÄnÖµÖ»ÓÐÒ»¸öf£¨n£©µÄÖµºÍËü¶ÔÓ¦
·½·¨ÊÇÒ»×éΪÁËʵÏÖÌØ¶¨¹¦Ä ......