°´¼üÅÌÉÏ·½Ïò¼ü ¡û »ò ¡ú ¿É¿ìËÙÉÏÏ·ҳ£¬°´¼üÅÌÉ쵀 Enter ¼ü¿É»Øµ½±¾ÊéĿ¼ҳ£¬°´¼üÅÌÉÏ·½Ïò¼ü ¡ü ¿É»Øµ½±¾Ò³¶¥²¿£¡
¡ª¡ª¡ª¡ªÎ´ÔĶÁÍꣿ¼ÓÈëÊéÇ©ÒѱãÏ´μÌÐøÔĶÁ£¡
Éß¡ª¡ª¼´½øÐС°Éî²ã¸´ÖÆ¡±¡ª¡ª±ØÐëÔÚ±»¸²¸ÇµÄclone£¨£©Àï²ÉÈ¡¸½¼ÓµÄ²Ù×÷¡£¡¡¡¡
ͨ³£¿ÉÔÚ´ÓÒ»¸öÄÜ¿Ë¡µÄÀàÀïµ÷Óá¡super¡£clone£¨£©£¬ÒÔÈ·±£ËùÓлù´¡ÀàÐж¯£¨°üÀ¨Object¡£clone£¨£©£©Äܹ»½ø¡¡
ÐС£Ëæ×ÅÊÇΪ¶ÔÏóÄÚÿ¸ö¾ä±ú¶¼Ã÷È·µ÷ÓÃÒ»¸ö¡¡clone£¨£©£»·ñÔòÄÇЩ¾ä±ú»á±ðÃû±ä³ÉÔʼ¶ÔÏóµÄ¾ä±ú¡£¹¹½¨Æ÷¡¡
µÄµ÷ÓÃÒ²´óÖÂÏàͬ¡ª¡ªÊ×Ïȹ¹Ôì»ù´¡À࣬ȻºóÊÇÏÂÒ»¸öÑÜÉúµÄ¹¹½¨Æ÷¡¡ÒÔ´ËÀàÍÆ£¬Ö±µ½Î»ÓÚ×îÉî²ãµÄÑÜÉú¡¡
¹¹½¨Æ÷¡£Çø±ðÔÚÓÚ¡¡clone£¨£©²¢²»ÊǸö¹¹½¨Æ÷£¬ËùÒÔûÓа취ʵÏÖ×Ô¶¯¿Ë¡¡£ÎªÁË¿Ë¡£¬±ØÐëÓÉ×Ô¼ºÃ÷È·½ø¡¡
ÐС£¡¡¡¡
12¡£2¡£6¡¡¿Ë¡ºÏ³É¶ÔÏó¡¡¡¡
ÊÔͼÉî²ã¸´ÖƺϳɶÔÏóʱ»áÓöµ½Ò»¸öÎÊÌâ¡£±ØÐë¼Ù¶¨³ÉÔ±¶ÔÏóÖеÄclone£¨£©·½·¨Ò²ÄÜÒÀ´Î¶Ô×Ô¼ºµÄ¾ä±ú½øÐС¡
Éî²ã¸´ÖÆ£¬ÒÔ´ËÀàÍÆ¡£ÕâʹÎÒÃǵIJÙ×÷±äµÃ¸´ÔÓ¡£ÎªÁËÄÜÕý³£ÊµÏÖÉî²ã¸´ÖÆ£¬±ØÐë¶ÔËùÓÐÀàÖеĴúÂë½øÐпء¡
ÖÆ£¬»òÕßÖÁÉÙÈ«ÃæÕÆÎÕÉî²ã¸´ÖÆÖÐÐèÒªÉæ¼°µÄÀ࣬ȷ±£ËüÃÇ×Ô¼ºµÄÉî²ã¸´ÖÆÄÜÕýÈ·½øÐС£¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡356¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Page¡¡358¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡
ÏÂÃæÕâ¸öÀý×Ó×ܽáÁËÃæ¶ÔÒ»¸öºÏ³É¶ÔÏó½øÐÐÉî²ã¸´ÖÆʱÐèÒª×öÄÄЩÊÂÇ飺¡¡¡¡
¡¡¡¡
//£º¡¡DeepCopy¡£java¡¡¡¡
//¡¡Cloning¡¡a¡¡posed¡¡object¡¡¡¡
¡¡¡¡
class¡¡DepthReading¡¡implements¡¡Cloneable¡¡£û¡¡¡¡
¡¡¡¡private¡¡double¡¡depth£»¡¡¡¡
¡¡¡¡public¡¡DepthReading£¨double¡¡depth£©¡¡£û¡¡¡¡¡¡
¡¡¡¡¡¡¡¡this¡£depth¡¡=¡¡depth£»¡¡¡¡
¡¡¡¡£ý¡¡¡¡
¡¡¡¡public¡¡Object¡¡clone£¨£©¡¡£û¡¡¡¡
¡¡¡¡¡¡¡¡Object¡¡o¡¡=¡¡null£»¡¡¡¡
¡¡¡¡¡¡¡¡try¡¡£û¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡o¡¡=¡¡super¡£clone£¨£©£»¡¡¡¡
¡¡¡¡¡¡¡¡£ý¡¡catch¡¡£¨CloneNotSupportedException¡¡e£©¡¡£û¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡e¡£printStackTrace£¨£©£»¡¡¡¡
¡¡¡¡¡¡¡¡£ý¡¡¡¡
¡¡¡¡¡¡¡¡return¡¡o£»¡¡¡¡
¡¡¡¡£ý¡¡¡¡
£ý¡¡¡¡
¡¡¡¡
class¡¡TemperatureReading¡¡implements¡¡Cloneable¡¡£û¡¡¡¡
¡¡¡¡private¡¡long¡¡time£»¡¡¡¡
¡¡¡¡private¡¡double¡¡temperature£»¡¡¡¡
¡¡¡¡public¡¡TemperatureReading£¨double¡¡temperature£©¡¡£û¡¡¡¡
¡¡¡¡¡¡¡¡time¡¡=¡¡System¡£currentTimeMillis£¨£©£»¡¡¡¡
¡¡¡¡¡¡¡¡this¡£temperature¡¡=¡¡temperature£»¡¡¡¡
¡¡¡¡£ý¡¡¡¡
¡¡¡¡public¡¡Object¡¡clone£¨£©¡¡£û¡¡¡¡
¡¡¡¡¡¡¡¡Object¡¡o¡¡=¡¡null£»¡¡¡¡
¡¡¡¡¡¡¡¡try¡¡£û¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡o¡¡=¡¡super¡£clone£¨£©£»¡¡¡¡
¡¡¡¡¡¡¡¡£ý¡¡catch¡¡£¨CloneNotSupportedException¡¡e£©¡¡£û¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡e¡£printStackTrace£¨£©£»¡¡¡¡
¡¡¡¡¡¡¡¡£ý¡¡¡¡
¡¡¡¡¡¡¡¡return¡¡o£»¡¡¡¡
¡¡¡¡£ý¡¡¡¡
£ý¡¡¡¡
¡¡¡¡
class¡¡OceanReading¡¡implements¡¡Cloneable¡¡£û¡¡¡¡
¡¡¡¡private¡¡DepthReading¡¡depth£»¡¡¡¡
¡¡¡¡private¡¡TemperatureReading¡¡temperature£»¡¡¡¡
¡¡¡¡public¡¡OceanReading£¨double¡¡tdata£»¡¡double¡¡ddata£©£û¡¡¡¡
¡¡¡¡¡¡¡¡temperature¡¡=¡¡new¡¡TemperatureReading£¨tdata£©£»¡¡¡¡
¡¡¡¡¡¡¡¡depth¡¡=¡¡new¡¡DepthReading£¨ddata£©£»¡¡¡¡
¡¡¡¡£ý¡¡¡¡
¡¡¡¡public¡¡Object¡¡clone£¨£©¡¡£û¡¡¡¡
¡¡¡¡¡¡¡¡OceanReading¡¡o¡¡=¡¡null£»¡¡¡¡
¡¡¡¡¡¡¡¡try¡¡£û¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡o¡¡=¡¡£¨OceanReading£©super¡£clone£¨£©£»¡¡¡¡
¡¡¡¡¡¡¡¡£ý¡¡catch¡¡£¨CloneNotSupportedException¡¡e£©¡¡£û¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡e¡£printStackTrace£¨£©£»¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡357¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Page¡¡359¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡
¡¡¡¡¡¡¡¡£ý¡¡¡¡
¡¡¡¡¡¡¡¡//¡¡Must¡¡clone¡¡handles£º¡¡¡¡
¡¡¡¡¡¡¡¡o¡£depth¡¡=¡¡£¨DepthReading£©o¡£depth¡£clone£¨£©£»¡¡¡¡
¡¡¡¡¡¡¡¡o¡£temperature¡¡=¡¡¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡£¨TemperatureReading£©o¡£temperature¡£clone£¨£©£»¡¡¡¡
¡¡¡¡¡¡¡¡return¡¡o£»¡¡//¡¡Upcasts¡¡back¡¡to¡¡Object¡¡¡¡
¡¡¡¡£ý¡¡¡¡
£ý¡¡¡¡
¡¡¡¡
public¡¡class¡¡DeepCopy¡¡£û¡¡¡¡
¡¡¡¡public¡¡static¡¡void¡¡main£¨String£§£§¡¡args£©¡¡£û¡¡¡¡
¡¡¡¡¡¡¡¡OceanReading¡¡reading¡¡=¡¡¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡new¡¡OceanReading£¨33¡£9£»¡¡100¡£5£©£»¡¡¡¡
¡¡¡¡¡¡¡¡//¡¡Now¡¡clone¡¡it£º¡¡¡¡
¡¡¡¡¡¡¡¡OceanReading¡¡r¡¡=¡¡¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡£¨OceanReading£©reading¡£clone£¨£©£»¡¡¡¡
¡¡¡¡£ý¡¡¡¡
£ý¡¡///£º~¡¡¡¡
¡¡¡¡
DepthReading¡¡ºÍ¡¡TemperatureReading¡¡·Ç³£ÏàËÆ£»ËüÃǶ¼Ö»°üº¬ÁË»ù±¾Êý¾ÝÀàÐÍ¡£ËùÒÔclone£¨£©·½·¨Äܹ»·Ç³£¡¡
¼òµ¥£ºµ÷Óá¡super¡£clone£¨£©²¢·µ»Ø½á¹û¼´¿É¡£×¢ÒâÁ½¸öÀàʹÓõÄclone£¨£©´úÂëÊÇÍêÈ«Ò»Öµġ£¡¡¡¡
OceanReading¡¡ÊÇÓÉ¡¡DepthReading¡¡ºÍ¡¡TemperatureReading¡¡¶ÔÏóºÏ²¢¶ø³ÉµÄ¡£ÎªÁ˶ÔÆä½øÐÐÉî²ã¸´ÖÆ£¬clone£¨£©¡¡
±ØÐëͬʱ¿Ë¡OceanReading¡¡Äڵľä±ú¡£Îª´ïµ½Õâ¸öÄ¿±ê£¬super¡£clone£¨£©µÄ½á¹û±ØÐëÔìÐͳÉÒ»¸ö¡¡
OceanReading¡¡¶ÔÏó£¨ÒÔ±ã·ÃÎÊdepth¡¡ºÍ¡¡temperature¡¡¾ä±ú£©¡£¡¡¡¡
12¡£2¡£7¡¡Óá¡Vector¡¡½øÐÐÉî²ã¸´ÖÆ¡¡¡¡
ÏÂÃæÈÃÎÒÃǸ´Ï°Ò»Ï±¾ÕÂÔçЩʱºòÌá³öµÄVector¡¡Àý×Ó¡£ÕâÒ»´Î¡¡Int2¡¡ÀàÊÇ¿ÉÒÔ¿Ë¡µÄ£¬ËùÒÔÄܶÔVector¡¡½øÐС¡
Éî²ã¸´ÖÆ£º¡¡¡¡
¡¡¡¡
//£º¡¡AddingClone¡£java¡¡¡¡
//¡¡You¡¡must¡¡go¡¡through¡¡a¡¡few¡¡gyrations¡¡to¡¡¡¡
//¡¡add¡¡cloning¡¡to¡¡your¡¡own¡¡class¡£¡¡¡¡
import¡¡java¡£util¡£*£»¡¡¡¡
¡¡¡¡
class¡¡Int2¡¡implements¡¡Cloneable¡¡£û¡¡¡¡
¡¡¡¡private¡¡int¡¡i£»¡¡¡¡
¡¡¡¡public¡¡Int2£¨int¡¡ii£©¡¡£û¡¡i¡¡=¡¡ii£»¡¡£ý¡¡¡¡
¡¡¡¡public¡¡void¡¡increment£¨£©¡¡£û¡¡i£«£«£»¡¡£ý¡¡¡¡
¡¡¡¡public¡¡String¡¡toString£¨£©¡¡£û¡¡¡¡
¡¡¡¡¡¡¡¡return¡¡Integer¡£toString£¨i£©£»¡¡¡¡
¡¡¡¡£ý¡¡¡¡
¡¡¡¡public¡¡Object¡¡clone£¨£©¡¡£û¡¡¡¡
¡¡¡¡¡¡¡¡Object¡¡o¡¡=¡¡null£»¡¡¡¡
¡¡¡¡¡¡¡¡try¡¡£û¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡o¡¡=¡¡super¡£clone£¨£©£»¡¡¡¡
¡¡¡¡¡¡¡¡£ý¡¡catch¡¡£¨CloneNotSupportedException¡¡e£©¡¡£û¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡System¡£out¡£println£¨¡¨Int2¡¡can't¡¡clone¡¨£©£»¡¡¡¡
¡¡¡¡¡¡¡¡£ý¡¡¡¡
¡¡¡¡¡¡¡¡return¡¡o£»¡¡¡¡
¡¡¡¡£ý¡¡¡¡
£ý¡¡¡¡
¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡358¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Page¡¡360¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡
//¡¡Once¡¡it's¡¡cloneable£»¡¡inheritance¡¡¡¡
//¡¡doesn't¡¡remove¡¡cloneability£º¡¡¡¡
class¡¡Int3¡¡extends¡¡Int2¡¡£û¡¡¡¡
¡¡¡¡private¡¡int¡¡j£»¡¡//¡¡Automatically¡¡duplicated¡¡¡¡
¡¡¡¡public¡¡Int3£¨int¡¡i£©¡¡£û¡¡super£¨i£©£»¡¡£ý¡¡¡¡
£ý¡¡¡¡
¡¡¡¡
public¡¡class¡¡AddingClone¡¡£û¡¡¡¡
¡¡¡¡public¡¡static¡¡void¡¡main£¨String£§£§¡¡args£©¡¡£û¡¡¡¡
¡¡¡¡¡¡¡¡Int2¡¡x¡¡=¡¡new¡¡Int2£¨10£©£»¡¡¡¡
¡¡¡¡¡¡¡¡Int2¡¡x2¡¡=¡¡£¨Int2£©x¡£clone£¨£©£»¡¡¡¡
¡¡¡¡¡¡¡¡x2¡£increment£¨£©£»¡¡¡¡
¡¡¡¡¡¡¡¡System¡£out¡£println£¨¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¨x¡¡=¡¡¡¨¡¡£«¡¡x¡¡£«¡¡¡¨£»¡¡x2¡¡=¡¡¡¨¡¡£«¡¡x2£©£»¡¡¡¡
¡¡¡¡¡¡¡¡//¡¡Anything¡¡inherited¡¡is¡¡also¡¡cloneable£º¡¡¡¡
¡¡¡¡¡¡¡¡Int3¡¡x3¡¡=¡¡new¡¡Int3£¨7£©£»¡¡¡¡
¡¡¡¡¡¡¡¡x3¡¡=¡¡£¨Int3£©x3¡£clone£¨£©£»¡¡¡¡
¡¡¡¡
¡¡¡¡¡¡¡¡Vector¡¡v¡¡=¡¡new¡¡Vector£¨£©£»¡¡¡¡
¡¡¡¡¡¡¡¡for£¨int¡¡i¡¡=¡¡0£»¡¡i¡¡¡¶¡¡10£»¡¡i£«£«¡¡£©¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡v¡£addElement£¨new¡¡Int2£¨i£©£©£»¡¡¡¡
¡¡¡¡¡¡¡¡System¡£out¡£println£¨¡¨v£º¡¡¡¨¡¡£«¡¡v£©£»¡¡¡¡
¡¡¡¡¡¡¡¡Vector¡¡v2¡¡=¡¡£¨Vector£©v¡£clone£¨£©£»¡¡¡¡
¡¡¡¡¡¡¡¡//¡¡Now¡¡clone¡¡each¡¡element£º¡¡¡¡
¡¡¡¡¡¡¡¡for£¨int¡¡i¡¡=¡¡0£»¡¡i¡¡¡¶¡¡v¡£size£¨£©£»¡¡i£«£«£©¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡v2¡£setElementAt£¨¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡£¨£¨Int2£©v2¡£elementAt£¨i£©£©¡£clone£¨£©£»¡¡i£©£»¡¡¡¡
¡¡¡¡¡¡¡¡//¡¡Increment¡¡all¡¡v2's¡¡elements£º¡¡¡¡
¡¡¡¡¡¡¡¡for£¨Enumeration¡¡e¡¡=¡¡v2¡£elements£¨£©£»¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡e¡£hasMoreElements£¨£©£»¡¡£©¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡£¨£¨Int2£©e¡£nextElement£¨£©£©¡£increment£¨£©£»¡¡¡¡
¡¡¡¡¡¡¡¡//¡¡See¡¡if¡¡it¡¡changed¡¡v's¡¡elements£º¡¡¡¡
¡¡¡¡¡¡¡¡System¡£out¡£println£¨¡¨v£º¡¡¡¨¡¡£«¡¡v£©£»¡¡¡¡
¡¡¡¡¡¡¡¡System¡£out¡£println£¨¡¨v2£º¡¡¡¨¡¡£«¡¡v2£©£»¡¡¡¡
¡¡¡¡£ý¡¡¡¡
£ý¡¡///£º~¡¡¡¡
¡¡¡¡
Int3¡¡×ÔInt2¡¡¼Ì³Ð¶øÀ´£¬²¢Ìí¼ÓÁËÒ»¸öеĻù±¾ÀàÐͳÉÔ±¡¡int¡¡j¡£´ó¼ÒÒ²ÐíÈÏΪ×Ô¼ºÐèÒªÔٴθ²¸Ç¡¡clone£¨£©£¬¡¡
ÒÔÈ·±£j¡¡µÃµ½¸´ÖÆ£¬µ«ÊµÇé²¢·ÇÈç´Ë¡£½«¡¡Int2¡¡µÄ¡¡clone£¨£©µ±×÷Int3¡¡µÄ¡¡clone£¨£©µ÷ÓÃʱ£¬Ëü»áµ÷Óá¡
Object¡£clone£¨£©£¬Åжϳöµ±Ç°²Ù×÷µÄÊÇ¡¡Int3£¬²¢¸´ÖÆInt3¡¡ÄÚµÄËùÓжþ½øÖÆλ¡£Ö»ÒªÃ»ÓÐÐÂÔöÐèÒª¿Ë¡µÄ¾ä¡¡
±ú£¬¶Ô¡¡Object¡£clone£¨£©µÄÒ»¸öµ÷ÓþÍÄÜÍê³ÉËùÓбØÒªµÄ¸´ÖÆ¡ª¡ªÎÞÂÛclone£¨£©ÊÇÔÚ²ã´Î½á¹¹¶àÉîµÄÒ»¼¶¶¨Òå¡¡
µÄ¡£¡¡¡¡
ÖÁ´Ë£¬´ó¼Ò¿ÉÒÔ×ܽá³ö¶ÔVector¡¡½øÐÐÉî²ã¸´ÖƵÄÏȾöÌõ¼þ£