Effective Java ѧϰ±Ê¼Ç (11)
CloneÕâ¸ö·½·¨ºÜ¼¦Àߣ¬ÔÒòÊÇÒòΪËüÊÇÊܱ£»¤µÄ(protected)£¬Æä¶þCloneÕâ¸ö·½·¨ÊÇ¿ÉÑ¡µÄ£¬¼´ÓпÉÄÜÀàûÓÐÌṩÕâ¸ö·½·¨¡£
cloneûÓе÷Óù¹Ô캯Êý£¬¶øÇÒÈç¹û¸¸ÀàûÓÐʵÏÖcloneµÄ»°£¬×ÓÀàÓÀÔ¶Ò²²»¿ÉÄÜÕýȷʵÏÖclone.
×ÜÖ®£¬cloneÊǸöºÜÍ·Í´µÄº¯Êý£¬Èç¹ûÒªÕýȷʵÏÖ£¬±ØÐ뿼Âǵ½É±´µÄÇé¿ö£¬·ñÔò±»¿Ë¡³öÀ´µÄ¶ÔÏó¾Í²»ÊǶÀÁ¢µÄ£¬È磬Ҫдһ¸ö¶ÑÕ»ÀàµÄ¿Ë¡º¯Êý£¬ÕýÈ·µÄʵÏÖ·½·¨ÊÇ£º
@Override public Stack clone() {
try {
Stack result = (Stack) super.clone();
result.elements = elements.clone();
return result;
} catch (CloneNotSupportedException e) {
throw new AssertionError();
}
}
ҪעÒâ¶ÔÓÚfinal×ֶβ»ÄÜÕý³£¹¤×÷£¬clone½á¹¹Óë¿É±ä¶ÔÏóµÄfinal×Ö¶ÎÊDz»¼æÈݵġ£
¶ÔÓÚÁÐÉ¢Êý×飬½ö½öµÝ¹éµ÷ÓõÄclone·½·¨»¹²»¹»£¬¿´ÊéÖÐÕýÈ·µÄ×ö·¨£º
public class HashTable implements Cloneable {
private Entry[] buckets = ...;
private static class Entry {
final Object key;
Ïà¹ØÎĵµ£º
charÔÚjavaÖгÆΪ“×Ö·ûÐÍ” Õ¼2¸ö×Ö½Ú
×Ö·û³£Á¿ÊÇÓõ¥ÒýºÅÀ¨ÆðµÄÒ»¸ö×Ö·û ÇÒ×Ö·û³£Á¿ÔÚÄÚ´æÖд洢µÄÊǸÃ×Ö·ûÔÚUnicode×Ö·û¼¯ÖеÄÅÅÐòλÖ㬼´ÕûÊý
eg£ºchar x='a'
ÄÚ´æxÖд洢µÄÊÇ×Ö·ûaÔÚUnicode×Ö·û¼¯ÖеÄÅÅÐòλÖÃ97 Òò´ËÔÊÐí½«ÉÏÃæµÄÓï¾äд³Échar x=97;
ÓÉ´ËÀý¿ÉÒԵóöÒÔϽáÂÛ£º
1.Òª ......
AWTÖÐûÓÐÌṩ״̬À¸¹¤¾ß£¬¿ÉÒÔÔÚFrameÖÐÌí¼ÓÒ»¸öPanelʵÏÖÀàËƵŦÄÜ¡£»ù±¾¹¦ÄÜÓд´½¨×´Ì¬À¸£¬Ìí¼ÓָʾÆ÷£¬ÒƳýָʾÆ÷£¬¸Ä±äָʾÆ÷ÄÚÈÝ£¬»ñµÃָʾÆ÷ÄÚÈÝ£¬Ìí¼ÓָʾÆ÷Êó±êÏûÏ¢ÏìÓ¦£¬Ìí¼ÓָʾÆ÷ÓÒ¼üµ¯³ö²Ëµ¥£¬»ñµÃ¶ÔÒ»¸öָʾÆ÷µÄÒýÓõȡ£
import java.awt.Color;
import java.awt.Componen ......
×¢:²»ÊʺÏweb¹¤³Ì
1¡¢ÀûÓÃSystem.getProperty()º¯Êý»ñÈ¡µ±Ç°Â·¾¶£º
System.out.println(System.getProperty("user.dir"));//user.dirÖ¸¶¨Á˵±Ç°µÄ·¾¶
2¡¢Ê¹ÓÃFileÌṩµÄº¯Êý»ñÈ¡µ±Ç°Â·¾¶£º
File directory = new File("");//É趨Ϊµ±Ç°Îļþ¼Ð
try{
System.out.println(directory. ......
1. µ±Ä³¸öÀàÖеÄij¸ö·½·¨Öеľֲ¿±äÁ¿µÄÃû×ÖÓë¸ÃÀàÖеÄÈ«¾Ö±äÁ¿µÄÃû×ÖÏàͬʱ ¸ÃÈ«¾Ö±äÁ¿½«±»Òþ²Ø£»´Ëʱ Èç¹ûÏëÔڸ÷½·¨ÖÐʹÓÃÈ«¾Ö
±äÁ¿ ±ØÐëʹÓùؼü×Ö£¨this.È«¾Ö±äÁ¿Ãû£©
2. µ±this¹Ø¼ü×Ö³öÏÖÔÚÀàµÄʵÀý·½·¨ÖÐʱ£¬´ú±íÕýÔÚµ÷Óø÷½·¨µÄµ±Ç°¶ÔÏó
3.  ......
ʹÓÃJava²Ù×÷Îı¾ÎļþµÄ·½·¨Ïê½â
ÕªÒª: ×î³õjavaÊDz»Ö§³Ö¶ÔÎı¾ÎļþµÄ´¦ÀíµÄ£¬ÎªÁËÃÖ²¹Õâ¸öȱº¶¶øÒýÈëÁËReaderºÍWriterÁ½¸öÀà
×î³õjavaÊDz»Ö§³Ö¶ÔÎı¾ÎļþµÄ´¦ÀíµÄ£¬ÎªÁËÃÖ²¹Õâ¸öȱº¶¶øÒýÈëÁËReaderºÍWriterÁ½¸öÀ࣬ÕâÁ½¸öÀ඼ÊdzéÏóÀ࣬WriterÖÐ write(char[] ch,int off,int
length)£¬flush()ºÍclose()·½·¨Îª³éÏó·½· ......