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

µÚ37ÕÂ

VB2008´ÓÈëÃŵ½¾«Í¨(PDF¸ñʽӢÎÄ°æ)-µÚ37ÕÂ

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

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






¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡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£»¡¡_¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡

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

Äã¿ÉÄÜϲ»¶µÄ