°Ë±¦Êé¿â > ÎÄѧÆäËûµç×ÓÊé > VB2008´ÓÈëÃŵ½¾«Í¨(PDF¸ñʽӢÎÄ°æ) >

µÚ34²¿·Ö

VB2008´ÓÈëÃŵ½¾«Í¨(PDF¸ñʽӢÎÄ°æ)-µÚ34²¿·Ö

С˵£º VB2008´ÓÈëÃŵ½¾«Í¨(PDF¸ñʽӢÎÄ°æ) ×ÖÊý£º ÿҳ4000×Ö

°´¼üÅÌÉÏ·½Ïò¼ü ¡û »ò ¡ú ¿É¿ìËÙÉÏÏ·­Ò³£¬°´¼üÅÌÉ쵀 Enter ¼ü¿É»Øµ½±¾ÊéĿ¼ҳ£¬°´¼üÅÌÉÏ·½Ïò¼ü ¡ü ¿É»Øµ½±¾Ò³¶¥²¿£¡
¡ª¡ª¡ª¡ªÎ´ÔĶÁÍꣿ¼ÓÈëÊéÇ©ÒѱãÏ´μÌÐøÔĶÁ£¡




pointless¡£¡¡The¡¡AndAlso¡¡operator¡¡used¡¡in¡¡place¡¡of¡¡And¡¡says¡¡if¡¡a¡¡does¡¡not¡¡equal¡¡b£»¡¡then¡¡stop¡¡processing¡¡¡¡

and¡¡move¡¡to¡¡the¡¡next¡¡piece¡¡of¡¡code¡£¡¡

¡¡¡¡¡¡¡¡¡¡The¡¡real¡¡question¡¡is¡¡why¡¡make¡¡the¡¡difference¡¡between¡¡AndAlso¡¡and¡¡And£¿¡¡I¡¡can¡¡only¡¡postu

late£»¡¡but¡¡in¡¡the¡¡early£»¡¡early¡¡days¡¡of¡¡Visual¡¡Basic£»¡¡programmers¡¡used¡¡to¡¡write¡¡code¡¡like¡¡this£º¡¡



If¡¡a¡¡=¡¡b¡¡And¡¡b¡¡=¡¡code¡£NextStatement£¨£©¡¡Then¡¡



¡¡¡¡¡¡¡¡¡¡The¡¡method¡¡NextStatement£¨£©¡¡not¡¡only¡¡verifies¡¡state£»¡¡but¡¡also¡¡modifies¡¡state¡£¡¡If¡¡a¡¡did¡¡not¡¡¡¡

equal¡¡b£»¡¡then¡¡the¡¡state¡¡was¡¡not¡¡modified¡£¡¡And¡¡that¡¡was¡¡a¡¡problem£»¡¡since¡¡some¡¡programmers¡¡¡¡

relied¡¡on¡¡the¡¡fact¡¡that¡¡the¡¡other¡¡piece¡¡of¡¡code¡¡would¡¡be¡¡called¡¡regardless¡¡of¡¡how¡¡it¡¡was¡¡used¡£¡¡¡¡

Think¡¡about¡¡this¡¡logically£»¡¡and¡¡you¡¯ll¡¡see¡¡it¡¡is¡¡not¡¡incorrect¡£¡¡After¡¡all£»¡¡what¡¡is¡¡the¡¡difference¡¡¡¡

between¡¡the¡¡following¡¡two¡¡pieces¡¡of¡¡code£¿¡¡


¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­Page¡¡132¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­

110¡¡¡¡¡¡¡¡¡¡¡¡¡¡CH¡¡AP¡¡T¡¡E¡¡R¡¡¡¡¡¡4¡¡¡¡¡¡¡ö¡¡¡¡¡¡¡¡L¡¡E¡¡A¡¡R¡¡N¡¡IN¡¡G¡¡¡¡¡¡AB¡¡OU¡¡T¡¡¡¡¡¡D¡¡AT¡¡A¡¡¡¡S¡¡TR¡¡U¡¡CT¡¡U¡¡R¡¡E¡¡S£»¡¡¡¡¡¡DE¡¡CI¡¡SI¡¡ON¡¡S£»¡¡¡¡¡¡A¡¡N¡¡D¡¡¡¡¡¡L¡¡O¡¡OP¡¡S¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡If¡¡b¡¡=¡¡code¡£NextStatement£¨£©¡¡and¡¡a¡¡=¡¡b¡¡Then¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡and¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡If¡¡¡¡b¡¡=¡¡code¡£NextStatement£¨£©¡¡Then¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡answer¡¡is¡¡nothing£»¡¡but¡¡if¡¡And¡¡behaved¡¡like¡¡AndAlso¡¡by¡¡default£»¡¡then¡¡you¡¡would¡¡get¡¡an¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡inconsistent¡¡state¡£¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Preventing¡¡Repetition¡¡in¡¡the¡¡Route¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡¡¡FindNextLeg£¨£©¡¡method¡¡calls¡¡the¡¡CanContinue£¨£©¡¡method£»¡¡which¡¡is¡¡designed¡¡to¡¡halt¡¡the¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡search¡£¡¡In¡¡the¡¡case¡¡of¡¡our¡¡depth¡­first¡¡search¡¡algorithm£»¡¡the¡¡purpose¡¡is¡¡to¡¡not¡¡fly¡¡to¡¡the¡¡same¡¡city¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡twice¡£¡¡Contained¡¡within¡¡the¡¡function¡¡is¡¡code¡¡similar¡¡to¡¡FindNextLeg£¨£©£º¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Private¡¡Function¡¡CanContinueSearch£¨ByVal¡¡returnArray¡¡As¡¡Node£¨£©£»¡¡_¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ByVal¡¡city¡¡As¡¡Node£©¡¡As¡¡Boolean¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡For¡¡c1¡¡As¡¡Integer¡¡=¡¡0¡¡To¡¡¡¡returnArray¡£Length¡¡1¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡If¡¡returnArray£¨c1£©¡¡IsNot¡¡Nothing¡¡Then¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡If¡¡returnArray£¨c1£©¡£CityName¡£pareTo£¨city¡£CityName£©¡¡=¡¡0¡¡Then¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Return¡¡False¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡If¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡If¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Next¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Return¡¡True¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡Function¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡logic¡¡is¡¡that¡¡CanContinueSearch£¨£©¡¡will¡¡iterate¡¡through¡¡the¡¡returnArray¡¡and¡¡see¡¡if¡¡the¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡city¡¡being¡¡considered¡¡£¨variable¡¡city£©¡¡is¡¡already¡¡in¡¡the¡¡found¡¡path¡£¡¡If¡¡the¡¡city¡¡is¡¡in¡¡the¡¡path£»¡¡then¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡we¡¡stop¡¡searching¡¡that¡¡part¡¡of¡¡the¡¡tree£»¡¡otherwise£»¡¡we¡¡continue¡¡searching¡£¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Running¡¡the¡¡Depth¡­First¡¡Search¡¡Algorithm¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Everything¡¡has¡¡been¡¡implemented£»¡¡including¡¡tests£»¡¡so¡¡we¡¡are¡¡ready¡¡to¡¡run¡¡the¡¡test¡¡of¡¡finding¡¡the¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡flight¡¡between¡¡Montreal¡¡and¡¡Seattle¡£¡¡Looking¡¡at¡¡Figure¡¡4¡­2£»¡¡you¡¡can¡¡see¡¡two¡¡paths£º¡¡Montreal¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡to¡¡Los¡¡Angeles¡¡to¡¡Seattle£»¡¡or¡¡Montreal¡¡to¡¡Toronto¡¡to¡¡Seattle¡£¡¡However£»¡¡running¡¡the¡¡algorithm¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡generates¡¡the¡¡following¡¡peculiar¡¡result¡¡£¨you¡¡have¡¡not¡¡seen¡¡how¡¡to¡¡display¡¡the¡¡results£»¡¡but¡¡that¡¡is¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡done¡¡easily¡¡enough¡¡with¡¡a¡¡For¡¡loop¡¡that¡¡iterates¡¡over¡¡foundRoute£©£º¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡o¡¡¡¡Montreal¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡o¡¡¡¡New¡¡York¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡o¡¡¡¡Houston¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡o¡¡¡¡Miami¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡o¡¡¡¡Toronto¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡o¡¡¡¡Seattle¡¡


¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­Page¡¡133¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡CH¡¡AP¡¡T¡¡E¡¡R¡¡¡¡¡¡4¡¡¡¡¡¡¡ö¡¡¡¡¡¡¡¡L¡¡E¡¡A¡¡R¡¡N¡¡I¡¡N¡¡G¡¡¡¡¡¡A¡¡B¡¡OU¡¡T¡¡¡¡¡¡D¡¡AT¡¡A¡¡¡¡S¡¡TR¡¡U¡¡CT¡¡U¡¡R¡¡E¡¡S£»¡¡¡¡¡¡DE¡¡CI¡¡SI¡¡ON¡¡S£»¡¡¡¡¡¡A¡¡N¡¡D¡¡¡¡¡¡L¡¡O¡¡OP¡¡S¡¡111¡¡



¡¡¡¡¡¡¡¡¡¡Looking¡¡at¡¡the¡¡result£»¡¡you¡¡are¡¡probably¡¡thinking¡¡that¡¡the¡¡algorithm¡¡does¡¡not¡¡work£»¡¡because¡¡¡¡

the¡¡proposed¡¡flight¡¡includes¡¡every¡¡city¡¡except¡¡Los¡¡Angeles¡£¡¡If¡¡a¡¡travel¡¡agent¡¡were¡¡to¡¡propose¡¡¡¡

such¡¡a¡¡flight¡¡route¡¡to¡¡you£»¡¡you¡¡would¡¡probably¡¡have¡¡a¡¡panic¡¡attack¡£¡¡

¡¡¡¡¡¡¡¡¡¡The¡¡algorithm¡¡did¡¡not¡¡fail£»¡¡rather£»¡¡the¡¡CanContinueSearch£¨£©¡¡function¡¡did¡¡not¡¡include¡¡func

tionality¡¡to¡¡optimize¡¡the¡¡flight¡£¡¡Right¡¡now£»¡¡the¡¡algorithm¡¡says¡¡to¡¡perform¡¡a¡¡depth¡­first¡¡search£»¡¡¡¡

meaning¡¡to¡¡go¡¡down¡¡the¡¡tree¡¡before¡¡backtracking¡£¡¡So¡¡let¡¯s¡¡go¡¡through¡¡the¡¡structure¡¡in¡¡the¡¡Node¡¡¡¡

shared¡¡constructor¡¡again¡£¡¡

¡¡¡¡¡¡¡¡¡¡We¡¡started¡¡our¡¡route¡¡in¡¡Montreal£»¡¡which¡¡had¡¡the¡¡following¡¡¡¡Connections¡¡definitions£º¡¡



montreal¡£Connections¡¡=¡¡New¡¡Node£¨£©¡¡£û¡¡newyork£»¡¡toronto£»¡¡losangeles¡¡£ý¡¡



¡¡¡¡¡¡¡¡¡¡Applying¡¡our¡¡depth¡­first¡¡algorithm£»¡¡it¡¡means¡¡the¡¡first¡¡array¡¡element¡¡of¡¡the¡¡tree¡¡is¡¡consid

ered¡¡a¡¡connection£»¡¡and¡¡thus¡¡our¡¡route¡¡takes¡¡us¡¡to¡¡New¡¡York¡£¡¡New¡¡York¡¡has¡¡the¡¡following¡¡flight¡¡¡¡

connections£º¡¡



newyork¡£Connections¡¡=¡¡New¡¡Node£¨£©¡¡£û¡¡montreal£»¡¡houston£»¡¡miami¡¡£ý¡¡



¡¡¡¡¡¡¡¡¡¡The¡¡first¡¡connection¡¡from¡¡New¡¡York¡¡is¡¡Montreal£»¡¡which¡¡is¡¡already¡¡in¡¡the¡¡flight¡¡route¡£¡¡Thus£»¡¡¡¡

the¡¡second¡¡array¡¡element¡¡is¡¡searched£»¡¡which¡¡is¡¡Houston¡£¡¡Houston¡¡has¡¡the¡¡following¡¡flight¡¡¡¡

connections£º¡¡



houston¡£Connections¡¡=¡¡New¡¡Node£¨£©¡¡£û¡¡miami£»¡¡seattle£»¡¡newyork¡¡£ý¡¡



¡¡¡¡¡¡¡¡¡¡Following¡¡the¡¡flight¡¡route¡¡from¡¡Houston£»¡¡we¡¡travel¡¡to¡¡Miami£»¡¡which¡¡has¡¡the¡¡following¡¡¡¡

connections£º¡¡



miami¡£Connections¡¡=¡¡New¡¡Node£¨£©¡¡£û¡¡toronto£»¡¡houston£»¡¡newyork¡¡£ý¡¡



¡¡¡¡¡¡¡¡¡¡Following¡¡the¡¡flight¡¡route¡¡from¡¡Miami£»¡¡we¡¡travel¡¡to¡¡Toronto£»¡¡which¡¡has¡¡the¡¡following¡¡¡¡

connections£º¡¡



toronto¡£Connections¡¡=¡¡New¡¡Node£¨£©¡¡£û¡¡miami£»¡¡seattle£»¡¡montreal¡¡£ý¡¡



¡¡¡¡¡¡¡¡¡¡At¡¡Toronto£»¡¡the¡¡first¡¡connection¡¡is¡¡Miami£»¡¡where¡¡we¡¡have¡¡already¡¡been¡£¡¡The¡¡second¡¡¡¡

connection¡¡is¡¡Seattle£»¡¡and¡¡that¡¡is¡¡our¡¡end¡¡destination¡£¡¡

¡¡¡¡¡¡¡¡¡¡So¡¡from¡¡the¡¡perspective¡¡of¡¡the¡¡algorithm£»¡¡everything¡¡worked¡£¡¡From¡¡the¡¡perspective¡¡of¡¡the¡¡¡¡

traveler£»¡¡it¡¯s¡¡not¡¡ideal¡£¡¡This¡¡demonstrates¡¡yet¡¡again¡¡how¡¡important¡¡it¡¡is¡¡to¡¡write¡¡test¡¡routines£»¡¡as¡¡¡¡

algorithms¡¡might¡¡be¡¡correct£»¡¡but¡¡they¡¡will¡¡generate¡¡responses¡¡that¡¡you¡¡might¡¡not¡¡have¡¡antici

pated¡£¡¡Improving¡¡the¡¡example¡¡is¡¡one¡¡of¡¡the¡¡exercises¡¡at¡¡the¡¡end¡¡of¡¡the¡¡chapter¡£¡¡



The¡¡Important¡¡Stuff¡¡to¡¡Remember¡¡



In¡¡this¡¡chapter£»¡¡you¡¡learned¡¡about¡¡data¡¡structures¡¡and¡¡algorithms¡£¡¡Here¡¡are¡¡the¡¡key¡¡points¡¡to¡¡¡¡

keep¡¡in¡¡mind£º¡¡



¡¡¡¡¡¡¡¡¡¡o¡¡¡¡When¡¡developing¡¡a¡¡program£»¡¡you¡¡need¡¡to¡¡think¡¡of¡¡the¡¡data¡¡structures¡¡and¡¡algorithms¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡that¡¡are¡¡involved¡£¡¡



¡¡¡¡¡¡¡¡¡¡o¡¡¡¡A¡¡single¡¡best¡¡data¡¡structure¡¡and¡¡a¡¡single¡¡best¡¡algorithm¡¡do¡¡not¡¡exist¡£¡¡Every¡¡data¡¡structure¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡and¡¡algorithm¡¡has¡¡promises¡£¡¡You¡¡need¡¡to¡¡choose¡¡the¡¡data¡¡structure¡¡and¡¡algorithm¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡that¡¡best¡¡suits¡¡your¡¡needs¡¡with¡¡the¡¡least¡¡number¡¡of¡¡critical¡¡promises¡£¡¡


¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­Page¡¡134¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­

112¡¡¡¡¡¡¡¡¡¡¡¡¡¡CH¡¡AP¡¡T¡¡E¡¡R¡¡¡¡¡¡4¡¡¡¡¡¡¡ö¡¡¡¡¡¡¡¡L¡¡E¡¡A¡¡R¡¡N¡¡IN¡¡G¡¡¡¡¡¡AB¡¡OU¡¡T¡¡¡¡¡¡D¡¡AT¡¡A¡¡¡¡S¡¡TR¡¡U¡¡CT¡¡U¡¡R¡¡E¡¡S£»¡¡¡¡¡¡DE¡¡CI¡¡SI¡¡ON¡¡S£»¡¡¡¡¡¡A¡¡N¡¡D¡¡¡¡¡¡L¡¡O¡¡OP¡¡S¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡o¡¡¡¡Data¡¡structures¡¡and¡¡algorithms¡¡do¡¡not¡¡need¡¡to¡¡be¡¡the¡¡same¡¡class¡£¡¡They¡¡can¡¡be¡¡different¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡types¡¡and¡¡often¡¡are¡£¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡o¡¡¡¡Data¡¡structures¡¡can¡¡be¡¡implemented¡¡using¡¡value¡¡£¨Structure£©¡¡or¡¡reference¡¡£¨Class£©¡¡types¡£¡¡¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡o¡¡¡¡Value¡¡types¡¡when¡¡used¡¡as¡¡data¡¡structures¡¡have¡¡three¡¡constraints¡¡that¡¡you¡¡need¡¡to¡¡be¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡aware¡¡of¡¡that¡¡relate¡¡to¡¡the¡¡fact¡¡that¡¡data¡¡is¡¡copied£»¡¡what¡¡happens¡¡when¡¡you¡¡embed¡¡refer

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ence¡¡types¡¡in¡¡value¡¡types£»¡¡and¡¡what¡¡happens¡¡when¡¡you¡¡use¡¡value¡¡types¡¡as¡¡parameters¡¡to¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡methods¡£¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡o¡¡¡¡For¡¡the¡¡most¡¡part£»¡¡you¡¡will¡¡use¡¡reference¡¡types£»¡¡but¡¡you¡¡can¡¡also¡¡use¡¡value¡¡types¡£¡¡When¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡using¡¡value¡¡types£»¡¡you¡¡need¡¡to¡¡be¡¡aware¡¡of¡¡how¡¡a¡¡value¡¡behaves£»¡¡otherwise£»¡¡you¡¡might¡¡get¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡undesirable¡¡interactions¡£¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡o¡¡¡¡A¡¡constructor¡¡is¡¡a¡¡special¡¡type¡¡of¡¡method¡¡that¡¡is¡¡called¡¡when¡¡a¡¡type¡¡is¡¡being¡¡instantiated¡£¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡You¡¡would¡¡assign¡¡parameters¡¡to¡¡a¡¡constructor¡¡when¡¡you¡¡want¡¡to¡¡enforce¡¡a¡¡verifiably¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡correct¡¡state¡¡for¡¡the¡¡object¡£¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡o¡¡¡¡A¡¡rule¡¡of¡¡thumb¡¡when¡¡trying¡¡to¡¡decide¡¡whether¡¡to¡¡use¡¡value¡¡and¡¡reference¡¡types¡¡is¡¡to¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡consider¡¡the¡¡context¡£¡¡Are¡¡you¡¡creating¡¡a¡¡simple¡¡assign¡­once¡¡structure£»¡¡or¡¡are¡¡you¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡creating¡¡a¡¡plex¡¡navigable¡¡structure£¿¡¡If¡¡your¡¡structure¡¡is¡¡plex£»¡¡then¡¡use¡¡a¡¡refer

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ence¡¡type£»¡¡otherwise£»¡¡a¡¡value¡¡type¡¡is¡¡fine¡£¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡o¡¡¡¡When¡¡you¡¡instantiate¡¡a¡¡type£»¡¡each¡¡object¡¡has¡¡its¡¡own¡¡set¡¡of¡¡instances¡¡of¡¡methods¡¡and¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡data¡¡members¡£¡¡When¡¡a¡¡type¡¡has¡¡methods¡¡or¡¡data¡¡members¡¡declared¡¡with¡¡the¡¡Shared¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡keyword£»¡¡that¡¡type¡¡has¡¡a¡¡single¡¡instance¡¡of¡¡the¡¡shared¡¡method¡¡or¡¡data¡¡member¡¡and¡¡is¡¡not¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡associated¡¡with¡¡a¡¡type¡¡instance¡£¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡o¡¡¡¡Writing¡¡the¡¡test¡¡before¡¡the¡¡type¡¡implementation¡¡allows¡¡a¡¡developer¡¡to¡¡get¡¡a¡¡feeling¡¡of¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡how¡¡the¡¡type¡¡should¡¡look¡¡and¡¡behave£»¡¡and¡¡gives¡¡some¡¡guidance¡£¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡o¡¡¡¡When¡¡you¡¡write¡¡methods£»¡¡you¡¡don¡¯t¡¡want¡¡to¡¡rely¡¡too¡¡heavily¡¡on¡¡magic¡¡data¡¡making¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡everything¡¡work¡£¡¡When¡¡writing¡¡classes£»¡¡you¡¡need¡¡to¡¡think¡¡in¡¡terms¡¡of¡¡IKEA¡¡furniture¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡£¨assembly¡¡required£©£»¡¡as¡¡that¡¡will¡¡make¡¡your¡¡code¡¡more¡¡flexible¡¡and¡¡a¡¡candidate¡¡for¡¡reuse¡£¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡o¡¡¡¡When¡¡you¡¡write¡¡a¡¡For¡¡loop£»¡¡think¡¡of¡¡the¡¡statements¡¡in¡¡the¡¡brackets¡¡as¡¡being¡¡code¡¡that¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡generates¡¡an¡¡index¡¡that¡¡is¡¡used¡¡to¡¡retrieve¡¡the¡¡actual¡¡information¡¡being¡¡iterated¡¡over¡£¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡o¡¡¡¡Decisions¡¡are¡¡implemented¡¡using¡¡a¡¡bination¡¡of¡¡If£»¡¡ElseIf£»¡¡and¡¡Else¡¡statements¡£¡¡¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Some¡¡Things¡¡for¡¡You¡¡to¡¡Do¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡following¡¡are¡¡some¡¡exercises¡¡to¡¡practice¡¡what¡¡you¡¡learned¡¡in¡¡this¡¡chapter¡£¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡1¡£¡¡¡¡Node¡¡was¡¡declared¡¡to¡¡be¡¡a¡¡reference¡¡type¡£¡¡Can¡¡you¡¡think¡¡of¡¡where¡¡in¡¡the¡¡declaration¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡of¡¡Node¡¡it¡¡would¡¡be¡¡more¡¡appropriate¡¡to¡¡use¡¡a¡¡value¡¡type£¿¡¡And¡¡if¡¡you¡¡can¡¡think¡¡of¡¡it£»¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡rewrite¡¡Node¡£¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡2¡£¡¡¡¡The¡¡shared¡¡data¡¡member¡¡Node¡£RootNodes¡¡is¡¡exposed¡¡for¡¡every¡¡class¡¡to¡¡consume¡£¡¡Is¡¡there¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡a¡¡way¡¡to¡¡decouple¡¡RootNodes¡¡so¡¡that¡¡the¡¡user¡¡of¡¡Node¡¡is¡¡not¡¡aware¡¡of¡¡the¡¡location¡¡of¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡the¡¡tree£¿¡¡


¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­Page¡¡135¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡CH¡¡AP¡¡T¡¡E¡¡R¡¡¡¡¡¡4¡¡¡¡¡¡¡ö¡¡¡¡¡¡¡¡L¡¡E¡¡A¡¡R¡¡N¡¡I¡¡N¡¡G¡¡¡¡¡¡A¡¡B¡¡OU¡¡T¡¡¡¡¡¡D¡¡AT¡¡A¡¡¡¡S¡¡TR¡¡U¡¡CT¡¡U¡¡R¡¡E¡¡S£»¡¡¡¡¡¡DE¡¡CI¡¡SI¡¡ON¡¡S£»¡¡¡¡¡¡A¡¡N¡¡D¡¡¡¡¡¡L¡¡O¡¡OP¡¡S¡¡113¡¡



3¡£¡¡¡¡We¡¡discussed¡¡a¡¡keyhole¡¡problem¡¡regarding¡¡the¡¡allocation¡¡of¡¡an¡¡array¡£¡¡Yet¡¡there¡¡is¡¡also¡¡a¡¡¡¡

¡¡¡¡¡¡¡¡coupling¡¡problem¡¡between¡¡Node¡¡and¡¡DepthFirstSearch¡£¡¡Explain¡¡why¡¡there¡¡i

·µ»ØĿ¼ ÉÏÒ»Ò³ ÏÂÒ»Ò³ »Øµ½¶¥²¿ ÔÞ£¨0£© ²È£¨1£©

Äã¿ÉÄÜϲ»¶µÄ