ÓÑÇéÌáʾ£ºÈç¹û±¾ÍøÒ³´ò¿ªÌ«Âý»òÏÔʾ²»ÍêÕû£¬Çë³¢ÊÔÊó±êÓÒ¼ü¡°Ë¢Ð¡±±¾ÍøÒ³£¡
VB2008´ÓÈëÃŵ½¾«Í¨(PDF¸ñʽӢÎÄ°æ)-µÚ27²¿·Ö
¿ì½Ý²Ù×÷: °´¼üÅÌÉÏ·½Ïò¼ü ¡û »ò ¡ú ¿É¿ìËÙÉÏÏ·ҳ °´¼üÅÌÉ쵀 Enter ¼ü¿É»Øµ½±¾ÊéĿ¼ҳ °´¼üÅÌÉÏ·½Ïò¼ü ¡ü ¿É»Øµ½±¾Ò³¶¥²¿! Èç¹û±¾ÊéûÓÐÔĶÁÍ꣬ÏëÏ´μÌÐø½Ó×ÅÔĶÁ£¬¿ÉʹÓÃÉÏ·½ "Êղص½ÎÒµÄä¯ÀÀÆ÷" ¹¦ÄÜ ºÍ "¼ÓÈëÊéÇ©" ¹¦ÄÜ£¡
¡¡¡¡¡¡¡¡¡¡¡¡The¡¡change¡¡is¡¡a¡¡one¡liner¡£¡¡After¡¡the¡¡switch£»¡¡if¡¡we¡¡executed¡¡the¡¡same¡¡assignment¡¡code¡¡as¡¡in¡¡¡¡
the¡¡previous¡¡section¡¡when¡¡Node¡¡was¡¡a¡¡value¡¡type£»¡¡the¡¡data¡¡structure¡¡shown¡¡in¡¡Figure¡¡4¡12¡¡would¡¡¡¡
be¡¡created¡£¡¡
Figure¡¡4¡12¡£¡¡A¡¡valid¡¡state¡¡for¡¡the¡¡New¡¡York¡¡Node¡¡instance¡¡
¡¡¡¡¡¡¡¡¡¡¡¡Looking¡¡at¡¡the¡¡node¡¡structure¡¡in¡¡Figure¡¡4¡12£»¡¡you¡¡can¡¡see¡¡that¡¡New¡¡York¡¡points¡¡to¡¡Montreal¡¡¡¡
and¡¡back¡¡again¡£¡¡The¡¡infinite¡¡connection¡¡does¡¡not¡¡mean¡¡that¡¡you¡¡are¡¡using¡¡infinite¡¡resources¡£¡¡¡¡
Instead£»¡¡it¡¡means¡¡one¡¡reference¡¡is¡¡being¡¡set¡¡to¡¡another£»¡¡as¡¡illustrated¡¡in¡¡Figure¡¡4¡13¡£¡¡
¡¡¡¡¡¡¡¡Variable¡¡montreal¡¡points¡¡to¡¡heap¡¡
¡¡¡¡¡¡¡¡memory¡¡that¡¡contains¡¡the¡¡data¡¡for¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡There¡¡is¡¡a¡¡reference¡¡in¡¡newyork¡¡to¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡the¡¡city¡¡Montreal¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡montreal¡¡and¡¡vice¡¡versa¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡montreal¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡newyork¡¡
¡¡¡¡Public¡¡CityName¡¡As¡¡String¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Public¡¡CityName¡¡As¡¡String¡¡
¡¡¡¡Public¡¡X¡¡As¡¡Double¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Public¡¡X¡¡As¡¡Double¡¡
¡¡¡¡Public¡¡Y¡¡As¡¡Double¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Public¡¡Y¡¡As¡¡Double¡¡
¡¡¡¡Public¡¡Connections¡¡As¡¡Node£¨£©¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Public¡¡Connections¡¡As¡¡Node£¨£©¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡newyork¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡montreal¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Array¡¡element¡¡is¡¡a¡¡reference¡¡to¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡the¡¡other¡¡heap¡¡memory¡¡
Figure¡¡4¡13¡£¡¡Recursive¡¡assignment¡¡that¡¡seems¡¡like¡¡infinite¡¡resources¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Page¡¡118¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡
96¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡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¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡apparent¡¡infinite¡¡resources¡¡are¡¡the¡¡cross¡reference¡¡recursive¡¡assignment¡¡of¡¡two¡¡pieces¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡of¡¡heap¡¡memory¡£¡¡It¡¡is¡¡fine¡¡to¡¡do¡¡this£»¡¡and¡¡this¡¡ability¡¡is¡¡one¡¡of¡¡the¡¡reasons¡¡why¡¡people¡¡prefer¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡using¡¡reference¡¡types¡¡to¡¡value¡¡types¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Understanding¡¡Static¡¡Data¡¡Members¡¡and¡¡Methods¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡You¡¯ve¡¡seen¡¡how¡¡a¡¡constructor¡¡can¡¡be¡¡used¡¡to¡¡initialize¡¡the¡¡state¡¡of¡¡a¡¡particular¡¡instance¡¡of¡¡a¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡type¡£¡¡Now¡¡we¡¡need¡¡to¡¡define¡¡a¡¡constructor¡¡for¡¡the¡¡tree¡¡structure¡¡shown¡¡in¡¡Figure¡¡4¡2¡£¡¡A¡¡tree¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡implies¡¡a¡¡starting¡¡point£»¡¡but¡¡the¡¡code¡¡for¡¡the¡¡flight¡¡connections¡¡does¡¡not¡¡imply¡¡a¡¡single¡¡starting¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡point¡£¡¡Instead£»¡¡we¡¡have¡¡the¡¡declaration¡¡of¡¡a¡¡number¡¡of¡¡variables¡¡where¡¡the¡¡identifier¡¡of¡¡each¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡variable¡¡is¡¡a¡¡city¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡problem¡¡with¡¡such¡¡a¡¡declaration¡¡is¡¡that£»¡¡if¡¡you¡¡want¡¡to¡¡navigate¡¡a¡¡tree¡¡structure£»¡¡you¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡need¡¡to¡¡know¡¡the¡¡individual¡¡names¡¡of¡¡the¡¡variables¡¡and¡¡navigate¡¡the¡¡tree¡¡structure¡¡of¡¡each¡¡vari
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡able¡£¡¡This¡¡is¡¡not¡¡a¡¡plausible¡¡solution¡£¡¡You¡¡want¡¡to¡¡create¡¡a¡¡single¡¡overall¡¡point¡¡from¡¡where¡¡all¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡other¡¡cities¡¡can¡¡be¡¡referenced¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡solution¡¡lies¡¡in¡¡using¡¡an¡¡array¡¡like¡¡that¡¡used¡¡for¡¡the¡¡data¡¡member¡¡Connections¡£¡¡To¡¡address¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡the¡¡problem¡¡of¡¡providing¡¡a¡¡single¡¡access¡¡point£»¡¡we¡¡declare¡¡a¡¡shared¡¡data¡¡member£»¡¡as¡¡follows£º¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Public¡¡Class¡¡Node¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Public¡¡Shared¡¡RootNodes¡¡As¡¡Node£¨£©¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Public¡¡CityName¡¡As¡¡String¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Public¡¡X¡¡As¡¡Double¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Public¡¡Y¡¡As¡¡Double¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Public¡¡Connections¡¡As¡¡Node£¨£©¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Public¡¡Sub¡¡New£¨string¡¡city£»¡¡double¡¡x£»¡¡double¡¡y£©¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Me¡£CityName¡¡=¡¡city¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Me¡£X¡¡=¡¡x¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Me¡£Y¡¡=¡¡y¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Me¡£Connections¡¡=¡¡Nothing¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡Sub¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡Class¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡bold¡¡code¡¡shows¡¡the¡¡declaration¡¡of¡¡a¡¡data¡¡member¡¡with¡¡the¡¡modifier¡¡Shared¡¡to¡¡indi
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡cate¡¡that¡¡the¡¡data¡¡member¡¡is¡¡shared¡£¡¡So£»¡¡just¡¡what¡¡does¡¡shared¡¡mean¡¡in¡¡this¡¡context£¿¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Let¡¯s¡¡say¡¡that¡¡your¡¡family¡¡consists¡¡of¡¡your¡¡significant¡¡other¡¡and¡¡two¡¡kids¡£¡¡One¡¡day£»¡¡you¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡wander¡¡into¡¡the¡¡cell¡¡phone¡¡store¡¡and¡¡decide¡¡to¡¡buy¡¡four¡¡identical¡¡cell¡¡phones£»¡¡as¡¡the¡¡store¡¡is¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡having¡¡a¡¡family¡¡deal¡£¡¡When¡¡each¡¡phone¡¡is¡¡activated£»¡¡the¡¡state¡¡of¡¡each¡¡cell¡¡phone¡¡will¡¡be¡¡unique¡£¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Each¡¡person¡¡will¡¡have¡¡an¡¡individual¡¡number£»¡¡address¡¡book£»¡¡and¡¡so¡¡on¡£¡¡In¡¡the¡¡analogy¡¡to¡¡objects£»¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡the¡¡cell¡¡phone¡¡represents¡¡a¡¡type£»¡¡each¡¡person¡¡is¡¡an¡¡instance¡¡of¡¡the¡¡same¡¡type£»¡¡but¡¡with¡¡a¡¡unique¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡state¡¡that¡¡represents¡¡an¡¡instantiation¡¡of¡¡the¡¡type¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Some¡¡cell¡¡phones¡¡have¡¡a¡¡feature¡¡called¡¡push¡to¡talk¡£¡¡Essentially£»¡¡you¡¡convert¡¡a¡¡cell¡¡phone¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡into¡¡a¡¡walkie¡talkie¡£¡¡After¡¡buying¡¡individual¡¡cell¡¡phones¡¡for¡¡the¡¡family£»¡¡the¡¡push¡to¡talk¡¡feature¡¡is¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡activated¡¡for¡¡everyone¡¡in¡¡the¡¡family¡£¡¡This¡¡means¡¡if¡¡one¡¡person¡¡is¡¡using¡¡the¡¡push¡to¡talk¡¡feature£»¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡all¡¡family¡¡members¡¡will¡¡hear¡¡the¡¡conversation¡£¡¡The¡¡push¡to¡talk¡¡feature¡¡does¡¡not¡¡distinguish¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡between¡¡the¡¡cell¡¡phones£»¡¡so¡¡if¡¡multiple¡¡people¡¡talk¡¡at¡¡the¡¡same¡¡time£»¡¡so¡¡be¡¡it£»¡¡you¡¯ll¡¡hear¡¡quite¡¡a¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡bit¡¡of¡¡noise¡£¡¡Push¡to¡talk¡¡is¡¡a¡¡shared¡¡resource£»¡¡not¡¡associated¡¡with¡¡a¡¡particular¡¡cell¡¡phone¡£¡¡In¡¡the¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡same¡¡way£»¡¡Shared¡¡refers¡¡to¡¡a¡¡shared¡¡resource¡¡that¡¡is¡¡not¡¡associated¡¡with¡¡a¡¡particular¡¡type¡¡instance¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Page¡¡119¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡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¡¡97¡¡
¡¡¡¡¡¡¡¡¡¡¡¡When¡¡you¡¡associate¡¡Shared¡¡with¡¡a¡¡data¡¡member£»¡¡as¡¡in¡¡the¡¡code¡¡example¡¡you¡¡just¡¡saw£»¡¡you¡¡¡¡
are¡¡saying¡¡that£»¡¡regardless¡¡of¡¡how¡¡many¡¡times¡¡you¡¡instantiate¡¡Node£»¡¡there¡¡is¡¡always¡¡a¡¡single¡¡instance¡¡¡¡
of¡¡the¡¡data¡¡member¡¡RootNodes¡£¡¡You¡¡don¡¯t¡¡even¡¡need¡¡to¡¡instantiate¡¡Node¡¡to¡¡access¡¡RootNodes¡£¡¡Shared¡¡¡¡
methods¡¡are¡¡like¡¡shared¡¡data¡¡members£»¡¡in¡¡that¡¡they¡¡are¡¡a¡¡shared¡¡resource¡¡and¡¡are¡¡not¡¡associated¡¡¡¡
with¡¡a¡¡particular¡¡object¡¡£¨as¡¡illustrated¡¡by¡¡the¡¡Main£¨£©¡¡method¡¡used¡¡to¡¡start¡¡an¡¡application£©¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡Figure¡¡4¡14¡¡illustrates¡¡what¡¡you¡¡can¡¡and¡¡cannot¡¡do¡¡with¡¡shared¡¡and¡¡object¡¡instance¡¡data¡¡¡¡
members¡£¡¡
¡¡Class¡¡MySharedAndNonSharedClass¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Assignment¡¡allowed¡¡because¡¡shared¡¡
¡¡¡¡¡¡¡¡¡¡Public¡¡Shared¡¡value¡¡As¡¡Integer¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡can¡¡reference¡¡shared¡¡
¡¡¡¡¡¡¡¡¡¡Public¡¡instanceValue¡¡As¡¡Integer¡¡
¡¡¡¡¡¡¡¡¡¡Public¡¡Shared¡¡Sub¡¡MyMethod£¨£©¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Assignment¡¡not¡¡allowed¡¡because¡¡shared¡¡cannot¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡value¡¡=¡¡10¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡instanceValue¡¡=¡¡20¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡reference¡¡information¡¡that¡¡is¡¡not¡¡shared£»¡¡unless¡¡the¡¡
¡¡¡¡¡¡¡¡¡¡End¡¡Sub¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡containing¡¡type¡¡is¡¡instantiated¡¡with¡¡the¡¡New¡¡keyword¡¡
¡¡¡¡¡¡¡¡¡¡Public¡¡Sub¡¡MyInstanceMethod£¨£©¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡value¡¡=¡¡10¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Assignment¡¡allowed¡¡because¡¡instance¡¡can¡¡reference¡¡shared¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡instanceValue¡¡=¡¡20¡¡
¡¡¡¡¡¡¡¡¡¡End¡¡Sub¡¡
¡¡End¡¡Class¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Assignment¡¡allowed¡¡because¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡instance¡¡can¡¡reference¡¡instance¡¡
¡¡Class¡¡TestSharedVsNonShared¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Method¡¡call¡¡allowed¡¡because¡¡shared¡¡
¡¡¡¡¡¡¡¡¡¡Public¡¡Sub¡¡TestSimple£¨£©¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡means¡¡you¡¡do¡¡not¡¡need¡¡to¡¡instantiate¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡MySharedAndNonSharedClass¡£MyMethod£¨£©¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡the¡¡type¡¡using¡¡New¡¡keyword¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡MySharedAndNonSharedClass¡£MyInstanceMethod£¨£©¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Dim¡¡cls¡¡As¡¡MySharedAndNonSharedClass¡¡=¡¡New¡¡MySharedAndNonSharedClass£¨£©¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡cls¡£MyInstanceMethod£¨£©¡¡
¡¡¡¡¡¡¡¡¡¡End¡¡Sub¡¡
¡¡End¡¡Class¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Method¡¡call¡¡not¡¡allowed¡¡because¡¡method¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡can¡¡be¡¡called¡¡only¡¡when¡¡the¡¡type¡¡is¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡instantiated¡¡using¡¡New¡¡keyword¡¡
Figure¡¡4¡14¡£¡¡Examples¡¡of¡¡shared¡¡and¡¡object¡¡instance¡¡data¡¡members¡¡
¡¡¡¡¡¡¡¡¡¡¡¡The¡¡general¡¡rule¡¡of¡¡thumb¡¡is¡¡that¡¡shared¡¡data¡¡members¡¡or¡¡methods¡¡can¡¡be¡¡accessed¡¡without¡¡¡¡
having¡¡to¡¡instantiate¡¡the¡¡type¡£¡¡Also£»¡¡don¡¯t¡¡attempt¡¡to¡¡reference¡¡object¡¡instance¡¡data¡¡members¡¡¡¡
or¡¡methods¡¡in¡¡a¡¡shared¡¡method¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡Getting¡¡back¡¡to¡¡the¡¡Node¡¡declaration£»¡¡the¡¡shared¡¡data¡¡member¡¡RootNodes¡¡is¡¡used¡¡to¡¡define¡¡¡¡
a¡¡root¡¡for¡¡the¡¡search¡¡tree¡£¡¡As¡¡when¡¡instantiating¡¡a¡¡type£»¡¡there¡¡is¡¡a¡¡constructor¡¡for¡¡the¡¡shared¡¡type¡¡¡¡
that¡¡is¡¡called¡¡before¡¡the¡¡first¡¡time¡¡it¡¡is¡¡needed¡£¡¡The¡¡shared¡¡constructor¡¡is¡¡like¡¡the¡¡previously¡¡¡¡
defined¡¡constructor£»¡¡except¡¡the¡¡Public¡¡keyword¡¡is¡¡replaced¡¡with¡¡Shared¡£¡¡For¡¡the¡¡search¡¡tree¡¡¡¡
case£»¡¡it¡¡is¡¡used¡¡to¡¡initialize¡¡the¡¡tree¡¡and¡¡state¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡We¡¡now¡¡have¡¡a¡¡plete¡¡definition¡¡of¡¡the¡¡Node¡¡class£»¡¡with¡¡the¡¡following¡¡source¡¡code¡£¡¡Take¡¡¡¡
a¡¡moment¡¡to¡¡look¡¡it¡¡over¡¡and¡¡fit¡¡the¡¡pieces¡¡together¡£¡¡
Public¡¡Class¡¡Node¡¡
¡¡¡¡¡¡¡¡Shared¡¡Sub¡¡New£¨£©¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Dim¡¡montreal¡¡As¡¡Node¡¡=¡¡New¡¡Node£¨¡¨Montreal¡¨£»¡¡0£»¡¡0£©¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Dim¡¡newyork¡¡As¡¡Node¡¡=¡¡New¡¡Node£¨¡¨New¡¡York¡¨£»¡¡0£»¡¡¡3£©¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Dim¡¡miami¡¡As¡¡Node¡¡=¡¡New¡¡Node£¨¡¨Miami¡¨£»¡¡¡1£»¡¡¡11£©¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Dim¡¡toronto¡¡As¡¡Node¡¡=¡¡New¡¡Node£¨¡¨Toronto¡¨£»¡¡¡4£»¡¡¡1£©¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Dim¡¡houston¡¡As¡¡Node¡¡=¡¡New¡¡Node£¨¡¨Houston¡¨£»¡¡¡10£»¡¡¡9£©¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Dim¡¡losangeles¡¡As¡¡Node¡¡=¡¡New¡¡Node£¨¡¨Los¡¡Angeles¡¨£»¡¡¡17£»¡¡¡6£©¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Page¡¡120¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡
98¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡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¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Dim¡¡seattle¡¡As¡¡Node¡¡=¡¡New¡¡Node£¨¡¨Seattle¡¨£»¡¡¡16£»¡¡¡1£©¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡montreal¡£Connections¡¡=¡¡New¡¡Node£¨£©¡¡£ûnewyork£»¡¡toronto£»¡¡losangeles£ý¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡newyork¡£Connections¡¡=¡¡New¡¡Node£¨£©¡¡£ûmontreal£»¡¡houston£»¡¡miami£ý¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡miami¡£Connections¡¡=¡¡New¡¡Node£¨£©¡¡£ûtoronto£»¡¡houston£»¡¡newyork£ý¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡toronto¡£Connections¡¡=¡¡New¡¡Node£¨£©¡¡£ûmiami£»¡¡seattle£»¡¡montreal£ý¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡houston¡£Connections¡¡=¡¡New¡¡Node£¨£©¡¡£ûmiami£»¡¡seattle£»¡¡newyork£ý¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡seattle¡£Connections¡¡=¡¡New¡¡Node£¨£©¡¡£ûtoronto£»¡¡houston£»¡¡losangeles£ý¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡losangeles¡£Connections¡¡=¡¡New¡¡Node£¨£©¡¡£ûmontreal£»¡¡seattle£»¡¡houston£ý¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Node¡£RootNodes¡¡=¡¡New¡¡Node£¨£©¡¡£ûmontreal£»¡¡newyork£»¡¡miami£»¡¡toronto£»¡¡houston£»¡¡_¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡losangeles£»¡¡seattle£ý¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡Sub¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Public¡¡Sub¡¡New£¨ByVal¡¡city¡¡As¡¡String£»¡¡ByVal¡¡X¡¡As¡¡Double£»¡¡ByVal¡¡Y¡¡As¡¡Double£©¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Me¡£CityName¡¡=¡¡city¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Me¡£X¡¡=¡¡X¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Me¡£Y¡¡=¡¡Y¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Me¡£Connections¡¡=¡¡Nothing¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡Sub¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Public¡¡CityName¡¡As¡¡String¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Public¡¡Connections¡¡As¡¡Node£¨£©¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Public¡¡Shared¡¡RootNodes¡¡As¡¡Node£¨£©¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Public¡¡X¡¡As¡¡Double¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Public¡¡Y¡¡As¡¡Double¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡Class¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Defining¡¡the¡¡Algorithm¡¡Test¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡Node¡¡type¡¡is¡¡a¡¡self¡contained¡¡type£»¡¡meaning¡¡that¡¡the¡¡algorithm¡¡does¡¡not¡¡need¡¡to¡¡instantiate¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡the¡¡tree¡¡structure¡¡£¨the¡¡Node¡¡constructor¡¡does¡¡it¡¡for¡¡you£©¡£¡¡This¡¡is¡¡an¡¡example¡¡of¡¡good¡¡design£»¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡because¡¡if¡¡you¡¡had¡¡to¡¡add¡¡more¡¡cities£»¡¡the¡¡only¡¡changes¡¡required¡¡would¡¡be¡¡to¡¡Node¡¡itself¡£¡¡Any¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡search¡¡algorithm¡¡that¡¡uses¡¡the¡¡Node¡¡typ
¿ì½Ý²Ù×÷: °´¼üÅÌÉÏ·½Ïò¼ü ¡û »ò ¡ú ¿É¿ìËÙÉÏÏ·ҳ °´¼üÅÌÉ쵀 Enter ¼ü¿É»Øµ½±¾ÊéĿ¼ҳ °´¼üÅÌÉÏ·½Ïò¼ü ¡ü ¿É»Øµ½±¾Ò³¶¥²¿!
ÎÂÜ°Ìáʾ£º ο´Ð¡ËµµÄͬʱ·¢±íÆÀÂÛ£¬Ëµ³ö×Ô¼ºµÄ¿´·¨ºÍÆäËüС»ï°éÃÇ·ÖÏíÒ²²»´íŶ£¡·¢±íÊéÆÀ»¹¿ÉÒÔ»ñµÃ»ý·ÖºÍ¾Ñé½±Àø£¬ÈÏÕæдԴ´ÊéÆÀ ±»²ÉÄÉΪ¾«ÆÀ¿ÉÒÔ»ñµÃ´óÁ¿½ð±Ò¡¢»ý·ÖºÍ¾Ñé½±ÀøŶ£¡