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
Ïà¹ØÎĵµ£º
ʱ¼ä¶¯Ì¬ÏÔʾ
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 ......
1.¼ÆËãijһÔ·ݵÄ×î´óÌìÊý
Calendar time=Calendar.getInstance();
time.clear();
time.set(Calendar.YEAR,year);
time.set(Calendar.MONTH,i-1);//×¢Òâ,Calendar¶ÔÏóĬÈÏÒ»ÔÂΪ0
int day=time.getActualMaximum(Calendar.DAY_OF_MONTH);//±¾Ô·ݵÄÌìÊý ......
Java±à³ÌÄÇЩʶù52—·½·¨ÉùÃ÷
µÚÆßÕÂ ·½·¨
·½·¨µÄÀ´Ô´
·½·¨µÄ¸ÅÄîÀ´Ô´ÓÚÊýѧÉϵĺ¯Êý£¬ÔÚÊýѧÖУ¬µ±Êý¾Ý¾ßÓÐÒ»¶¨µÄ¹æÂÉʱ£¬¾ÍÊÇÓÃÒ»¸öº¯ÊýÀ´´úÂë¸ÃÊý×ֵĹæÂÉ£¬ÀýÈçf£¨n£©=nÔò´ú±í1¡¢2¡¢3¡¢……ÕâÑùµÄÒ»¸öÊýÁС£ÔÚÊýѧÉÏnÊDzÎÊý£¬¶ÔÓÚÈ·¶¨µÄnÖµÖ»ÓÐÒ»¸öf£¨n£©µÄÖµºÍËü¶ÔÓ¦
·½·¨ÊÇÒ»×éΪÁËʵÏÖÌØ¶¨¹¦Ä ......
JAVA³ÌÐòÔ±ÃæÊÔÖ®¿û»¨±¦µä
1¡¢ÃæÏò¶ÔÏóµÄÌØÕ÷ÓÐÄÄЩ·½Ãæ
1.³éÏ󣺳éÏó¾ÍÊǺöÂÔÒ»¸öÖ÷ÌâÖÐÓ뵱ǰĿ±êÎ޹صÄÄÇЩ·½Ã棬ÒÔ±ã¸ü³ä·ÖµØ×¢ÒâÓ뵱ǰĿ±êÓйصķ½Ãæ¡£³éÏó²¢²»´òËãÁ˽âÈ«²¿ÎÊÌ⣬¶øÖ»ÊÇÑ¡ÔñÆäÖеÄÒ»²¿·Ö£¬ÔÝʱ²»Óò¿·Öϸ½Ú¡£³éÏó°üÀ¨Á½¸ö·½Ã棬һÊǹý³Ì³éÏ󣬶þÊÇÊý¾Ý³éÏó¡£
2.¼Ì³Ð£º¼Ì³ÐÊÇÒ»ÖÖÁª½áÀàµÄ²ã´ÎÄ£ ......
package org.bupt.test;
import java.util.ArrayList;
class MyResource {
ArrayList<Integer> arrList= new ArrayList<Integer>();
public MyResource(ArrayList<Integer> arrList) {
this.arrList = arrList;
&nbs ......