ÓÑÇéÌáʾ£ºÈç¹û±¾ÍøÒ³´ò¿ªÌ«Âý»òÏÔʾ²»ÍêÕû£¬Çë³¢ÊÔÊó±êÓÒ¼ü¡°Ë¢Ð¡±±¾ÍøÒ³£¡
µÚÈýµç×ÓÊé ·µ»Ø±¾ÊéĿ¼ ¼ÓÈëÊéÇ© ÎÒµÄÊé¼Ü ÎÒµÄÊéÇ© TXTÈ«±¾ÏÂÔØ ¡ºÊղص½ÎÒµÄä¯ÀÀÆ÷¡»

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

¿ì½Ý²Ù×÷: °´¼üÅÌÉÏ·½Ïò¼ü ¡û »ò ¡ú ¿É¿ìËÙÉÏÏ·­Ò³ °´¼üÅÌÉ쵀 Enter ¼ü¿É»Øµ½±¾ÊéĿ¼ҳ °´¼üÅÌÉÏ·½Ïò¼ü ¡ü ¿É»Øµ½±¾Ò³¶¥²¿! Èç¹û±¾ÊéûÓÐÔĶÁÍ꣬ÏëÏ´μÌÐø½Ó×ÅÔĶÁ£¬¿ÉʹÓÃÉÏ·½ "Êղص½ÎÒµÄä¯ÀÀÆ÷" ¹¦ÄÜ ºÍ "¼ÓÈëÊéÇ©" ¹¦ÄÜ£¡


instance¡£¡¡It¡¡is¡¡fine¡¡to¡¡return¡¡a¡¡LightingController¡¡instance£»¡¡because¡¡it¡¡is¡¡a¡¡globally¡¡defined¡¡type¡¡¡¡

and¡¡can¡¡serve¡¡as¡¡a¡¡basis¡¡for¡¡the¡¡builder¡¡method¡£¡¡In¡¡the¡¡implementation¡¡of¡¡the¡¡builder¡¡method£»¡¡¡¡

the¡¡room¡¡groupings¡¡and¡¡rooms¡¡are¡¡instantiated¡¡and¡¡added¡¡to¡¡the¡¡LightingController¡¡instance¡£¡¡¡¡

This¡¡is¡¡the¡¡work¡¡that¡¡the¡¡builder¡¡should¡¡save¡¡the¡¡end¡¡user¡£¡¡Additionally£»¡¡by¡¡providing¡¡a¡¡builder¡¡¡¡

method£»¡¡you¡¡avoid¡¡creating¡¡museum¡¡structures¡¡that¡¡have¡¡glaring¡¡errors¡¡in¡¡their¡¡structures¡£¡¡¡¡



¡öNote¡¡¡¡Factory¡¡types¡¡serve¡¡to¡¡instantiate¡¡types¡¡and¡¡define¡¡builder¡¡methods£»¡¡but¡¡can¡¡also¡¡be¡¡used¡¡to¡¡perform¡¡¡¡

generic¡¡structural¡¡operations¡£¡¡Let¡¯s¡¡say¡¡that¡¡in¡¡your¡¡museum¡¡there¡¡is¡¡a¡¡wing£»¡¡which¡¡contains¡¡three¡¡public¡¡¡¡

rooms¡¡and¡¡a¡¡private¡¡room¡£¡¡You¡¡could¡¡define¡¡a¡¡builder¡¡method¡¡that¡¡creates¡¡a¡¡wing£»¡¡and¡¡the¡¡wing¡¡is¡¡added¡¡to¡¡¡¡

an¡¡already¡¡created¡¡building¡£¡¡The¡¡general¡¡idea¡¡behind¡¡the¡¡factory¡¡type¡¡is¡¡to¡¡avoid¡¡errors¡¡and¡¡centralize¡¡repeated¡¡¡¡

instantiations¡£¡¡



Learning¡¡More¡¡About¡¡Private¡¡Classes¡¡and¡¡¡¡

Object¡¡Initialization¡¡



In¡¡this¡¡chapter£»¡¡you¡¡learned¡¡how¡¡to¡¡apply¡¡interfaces£»¡¡implementations£»¡¡and¡¡ponents¡¡in¡¡a¡¡¡¡

kernel¡­type¡¡situation¡£¡¡This¡¡is¡¡very¡¡much¡¡the¡¡type¡¡of¡¡programming¡¡that¡¡you¡¡will¡¡encounter¡¡as¡¡¡¡

you¡¡continue¡¡using¡¡Visual¡¡Basic¡£¡¡Here£»¡¡I¡¡will¡¡provide¡¡a¡¡few¡¡more¡¡details¡¡about¡¡using¡¡private¡¡¡¡

classes¡¡and¡¡initializing¡¡objects¡¡with¡¡nested¡¡data¡¡types¡£¡¡



Private¡¡Classes¡¡



The¡¡RoomGrouping¡¡and¡¡Room¡¡classes¡¡are¡¡defined¡¡in¡¡the¡¡¡¡LibLightingController¡¡project¡¡and¡¡are¡¡¡¡

private¡¡to¡¡the¡¡library¡£¡¡This¡¡is¡¡because¡¡RoomGrouping¡¡and¡¡Room¡¡are¡¡classes¡¡only¡¡¡¡LightingController¡¡¡¡

needs¡¡to¡¡support¡¡its¡¡functionality¡£¡¡The¡¡declaration¡¡of¡¡each¡¡class¡¡is¡¡internal¡¡to¡¡the¡¡assembly£»¡¡¡¡

which¡¡is¡¡good£»¡¡but¡¡it¡¡still¡¡means¡¡that¡¡some¡¡developers¡¡could¡¡use¡¡the¡¡classes¡¡within¡¡the¡¡kernel¡¡¡¡

assembly¡¡for¡¡their¡¡own¡¡purposes¡£¡¡Sometimes¡¡that¡¡is¡¡a¡¡desirable¡¡feature£»¡¡sometimes¡¡it¡¡is¡¡not¡£¡¡¡¡


¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­Page¡¡248¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­

226¡¡¡¡¡¡¡¡¡¡¡¡¡¡CH¡¡AP¡¡T¡¡E¡¡R¡¡¡¡¡¡8¡¡¡¡¡¡¡ö¡¡¡¡¡¡¡¡L¡¡E¡¡A¡¡R¡¡N¡¡IN¡¡G¡¡¡¡¡¡AB¡¡OU¡¡T¡¡¡¡¡¡CO¡¡M¡¡P¡¡O¡¡N¡¡E¡¡N¡¡TO¡¡R¡¡IE¡¡N¡¡T¡¡E¡¡D¡¡¡¡AR¡¡C¡¡HI¡¡TE¡¡CT¡¡U¡¡R¡¡E¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡In¡¡the¡¡case¡¡of¡¡¡¡LightingController£»¡¡another¡¡approach¡¡is¡¡to¡¡declare¡¡the¡¡classes¡¡in¡¡the¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡context¡¡of¡¡LightingController£»¡¡as¡¡follows£º¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Public¡¡Class¡¡LightingController¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Private¡¡Class¡¡RoomGrouping¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡Class¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Private¡¡Class¡¡Room¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡Class¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡Class¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡Room¡¡and¡¡RoomGrouping¡¡classes¡¡are¡¡declared¡¡within¡¡the¡¡class£»¡¡and¡¡their¡¡declarations¡¡are¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Private¡£¡¡This¡¡means¡¡that¡¡only¡¡LightingController¡¡can¡¡instantiate¡¡and¡¡use¡¡the¡¡classes£»¡¡and¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡there¡¡can¡¡never¡¡be¡¡the¡¡situation¡¡where¡¡another¡¡class¡¡will¡¡instantiate¡¡the¡¡types¡£¡¡In¡¡the¡¡case¡¡of¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡LightingController£»¡¡this¡¡would¡¡have¡¡been¡¡a¡¡better¡¡solution¡£¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Private¡¡classes¡¡are¡¡also¡¡used¡¡in¡¡the¡¡factory¡¡context¡£¡¡For¡¡example£»¡¡imagine¡¡the¡¡situation¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡where¡¡you¡¡don¡¯t¡¡ever¡¡want¡¡anyone¡¡but¡¡the¡¡factory¡¡to¡¡instantiate¡¡a¡¡room¡£¡¡A¡¡possible¡¡¡¡IRoom¡¡decla

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ration¡¡and¡¡factory¡¡could¡¡be¡¡as¡¡follows£º¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Public¡¡Module¡¡Factory¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Private¡¡Class¡¡MyRoom¡¡£º¡¡Implements¡¡IRoom¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡Class¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Public¡¡Function¡¡CreateMyRoom£¨£©¡¡As¡¡IRoom¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Return¡¡New¡¡MyRoom£¨£©¡¡

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

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡Module¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡In¡¡this¡¡implementation¡¡of¡¡MyRoom£»¡¡you¡¡can¡¡be¡¡sure¡¡that¡¡only¡¡Factory¡¡can¡¡ever¡¡instantiate¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡MyRoom£»¡¡and¡¡you¡¡can¡¡always¡¡be¡¡sure¡¡that¡¡the¡¡only¡¡way¡¡to¡¡manipulate¡¡MyRoom¡¡is¡¡through¡¡the¡¡IRoom¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡interface¡£¡¡All¡¡too¡¡often£»¡¡developers¡¡bee¡¡lazy¡¡and¡¡instantiate¡¡types¡¡within¡¡the¡¡assembly£»¡¡and¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡switch¡¡to¡¡the¡¡implementation¡¡type¡¡whenever¡¡the¡¡interface¡¡does¡¡not¡¡have¡¡the¡¡methods¡¡or¡¡prop

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡erties¡¡that¡¡they¡¡want¡£¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Object¡¡Initialization¡¡with¡¡Nested¡¡Data¡¡Types¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡In¡¡this¡¡chapter£»¡¡you¡¡saw¡¡how¡¡to¡¡use¡¡object¡¡initialization¡¡to¡¡assign¡¡data¡¡members¡¡in¡¡lieu¡¡of¡¡a¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡constructor¡£¡¡Object¡¡initialization¡¡also¡¡works¡¡using¡¡nested¡¡data¡¡types¡£¡¡Consider¡¡the¡¡situation¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡where¡¡a¡¡type¡¡references¡¡another¡¡type¡£¡¡Using¡¡object¡¡initialization£»¡¡you¡¡can¡¡instantiate¡¡and¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡assign¡¡multiple¡¡levels¡¡of¡¡objects¡£¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Suppose¡¡you¡¡had¡¡this¡¡source¡¡code£º¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Class¡¡MyType¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Private¡¡_dataMember¡¡As¡¡Integer¡¡

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

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Public¡¡Property¡¡DataMember£¨£©¡¡As¡¡Integer¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Get¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Return¡¡_dataMember¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡Get¡¡


¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­Page¡¡249¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡C¡¡H¡¡AP¡¡TE¡¡R¡¡¡¡¡¡8¡¡¡¡¡¡¡ö¡¡¡¡¡¡¡¡L¡¡E¡¡AR¡¡N¡¡IN¡¡G¡¡¡¡¡¡AB¡¡O¡¡U¡¡T¡¡¡¡¡¡CO¡¡M¡¡P¡¡O¡¡N¡¡E¡¡N¡¡T¡¡O¡¡R¡¡IE¡¡N¡¡TE¡¡D¡¡¡¡¡¡A¡¡R¡¡CH¡¡I¡¡TE¡¡C¡¡TU¡¡R¡¡E¡¡227¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Set¡¡£¨ByVal¡¡value¡¡as¡¡Integer£©¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡_dataMember¡¡=¡¡value¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡Set¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡Property¡¡

¡¡¡¡¡¡¡¡End¡¡Class¡¡

¡¡¡¡¡¡¡¡¡¡

¡¡¡¡¡¡¡¡Class¡¡EmbeddedMyType¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Private¡¡_embedded¡¡As¡¡MyType¡¡

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

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Public¡¡Property¡¡MyTypeProperty£¨£©¡¡As¡¡MyType¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Get¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Return¡¡_embedded¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡Get¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Set¡¡£¨ByVal¡¡value¡¡As¡¡MyType£©¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡_embedded¡¡=¡¡value¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡Get¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡Property¡¡

¡¡¡¡¡¡¡¡End¡¡Class¡¡



¡¡¡¡¡¡¡¡¡¡The¡¡type¡¡¡¡EmbeddedMyType¡¡has¡¡a¡¡property¡¡that¡¡references¡¡MyType¡£¡¡If¡¡you¡¡were¡¡to¡¡instantiate¡¡¡¡

EmbeddedMyType£»¡¡you¡¡would¡¡probably¡¡also¡¡want¡¡to¡¡instantiate¡¡and¡¡assign¡¡the¡¡property¡¡MyType¡£¡¡¡¡

You¡¡can¡¡do¡¡that¡¡with¡¡object¡¡initialization£»¡¡like¡¡this£º¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Dim¡¡cls¡¡As¡¡EmbeddedMyType¡¡=¡¡_¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡New¡¡EmbeddedMyType£¨£©¡¡With¡¡£û¡¡_¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡£MyTypeProperty¡¡=¡¡_¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡New¡¡MyType£¨£©¡¡With¡¡£û¡£DataMember¡¡=¡¡10£ý£ý¡¡



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



In¡¡this¡¡chapter£»¡¡you¡¡learned¡¡about¡¡writing¡¡a¡¡kernel£»¡¡using¡¡Visual¡¡Basic¡¡default¡¡properties£»¡¡and¡¡¡¡

the¡¡implementing¡¡enumeration¡¡functionality¡£¡¡The¡¡main¡¡items¡¡to¡¡remember¡¡are¡¡as¡¡follows£º¡¡



¡¡¡¡¡¡¡¡¡¡o¡¡A¡¡kernel¡¡is¡¡a¡¡ponent¡­oriented¡¡architecture¡¡where¡¡you¡¡are¡¡not¡¡in¡¡control¡¡of¡¡certain¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡implementations¡£¡¡ponents¡¡make¡¡it¡¡possible¡¡to¡¡modularize¡¡a¡¡development¡¡process¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡so¡¡that¡¡separate¡¡teams¡¡have¡¡their¡¡own¡¡tasks¡£¡¡



¡¡¡¡¡¡¡¡¡¡o¡¡Interfaces¡¡are¡¡contracts¡¡between¡¡modules£»¡¡and¡¡you¡¡test¡¡against¡¡the¡¡interface£»¡¡not¡¡the¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡implementation¡£¡¡



¡¡¡¡¡¡¡¡¡¡o¡¡Placeholder¡¡interfaces¡¡are¡¡used¡¡to¡¡make¡¡it¡¡simpler¡¡to¡¡group¡¡object¡¡instances¡£¡¡



¡¡¡¡¡¡¡¡¡¡o¡¡Default¡¡properties¡¡are¡¡structural£»¡¡and¡¡thus¡¡they¡¡serve¡¡as¡¡a¡¡tool¡¡to¡¡help¡¡you¡¡acplish¡¡a¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡task¡¡quicker¡£¡¡



¡¡¡¡¡¡¡¡¡¡o¡¡Default¡¡properties¡¡make¡¡it¡¡possible¡¡for¡¡your¡¡type¡¡to¡¡behave¡¡like¡¡an¡¡array¡£¡¡



¡¡¡¡¡¡¡¡¡¡o¡¡To¡¡iterate¡¡something¡¡that¡¡does¡¡not¡¡support¡¡iteration£»¡¡you¡¡add¡¡enumeration¡¡function

¡¡¡¡¡¡¡¡¡¡¡¡¡¡ality£»¡¡which¡¡implies¡¡implementing¡¡the¡¡IEnumerable¡¡and¡¡¡¡IEnumerator¡¡interfaces¡£¡¡


¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­Page¡¡250¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­

228¡¡¡¡¡¡¡¡¡¡¡¡¡¡CH¡¡AP¡¡T¡¡E¡¡R¡¡¡¡¡¡8¡¡¡¡¡¡¡ö¡¡¡¡¡¡¡¡L¡¡E¡¡A¡¡R¡¡N¡¡IN¡¡G¡¡¡¡¡¡AB¡¡OU¡¡T¡¡¡¡¡¡CO¡¡M¡¡P¡¡O¡¡N¡¡E¡¡N¡¡TO¡¡R¡¡IE¡¡N¡¡T¡¡E¡¡D¡¡¡¡AR¡¡C¡¡HI¡¡TE¡¡CT¡¡U¡¡R¡¡E¡¡



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



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡following¡¡are¡¡some¡¡exercises¡¡that¡¡will¡¡help¡¡you¡¡apply¡¡the¡¡concepts¡¡you¡¡learned¡¡in¡¡this¡¡chapter¡£¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡1¡£¡¡¡¡The¡¡LightingController¡£AddRoomGrouping£¨£©¡¡method¡¡has¡¡a¡¡mistake¡£¡¡Write¡¡some¡¡tests¡¡to¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡find¡¡the¡¡error£»¡¡and¡¡then¡¡fix¡¡the¡¡code¡¡and¡¡rerun¡¡your¡¡tests¡¡to¡¡verify¡¡that¡¡the¡¡error¡¡has¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡been¡¡fixed¡£¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡2¡£¡¡¡¡The¡¡TestInsert£¨£©¡¡test¡¡method¡¡is¡¡one¡¡example¡¡of¡¡an¡¡insertion¡¡test£»¡¡but¡¡not¡¡all¡¡variations¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡have¡¡been¡¡tested¡£¡¡Write¡¡another¡¡test¡¡method¡¡that¡¡implements¡¡the¡¡remaining¡¡variation£¨s£©¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡that¡¡need¡¡to¡¡be¡¡tested¡£¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡3¡£¡¡¡¡The¡¡declarations¡¡of¡¡RoomGrouping¡¡and¡¡Room¡¡are¡¡not¡¡optimal¡£¡¡Fix¡¡the¡¡declarations¡£¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡4¡£¡¡¡¡Implement¡¡a¡¡general¡¡collection¡¡class¡¡based¡¡on¡¡the¡¡experience¡¡of¡¡using¡¡the¡¡class¡¡in¡¡the¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡class¡¡LightingController¡£¡¡Hint£º¡¡look¡¡at¡¡how¡¡the¡¡linked¡¡list¡¡for¡¡Room¡¡is¡¡declared¡¡and¡¡figure¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡out¡¡a¡¡way¡¡to¡¡abstract¡¡that¡¡into¡¡some¡¡general¡¡collection¡¡class¡£¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡5¡£¡¡¡¡When¡¡the¡¡method¡¡¡¡LightingController¡£AddRoom£¨£©¡¡is¡¡called£»¡¡the¡¡method¡¡is¡¡tested¡¡internally¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡to¡¡see¡¡if¡¡the¡¡handle¡¡is¡¡of¡¡type¡¡RoomGrouping¡£¡¡Can¡¡you¡¡think¡¡of¡¡a¡¡more¡¡defensive¡¡pro

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡gramming¡¡technique¡¡to¡¡make¡¡sure¡¡that¡¡the¡¡code¡¡someone¡¡else¡¡passes¡¡to¡¡the¡¡kernel¡¡will¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡not¡¡cause¡¡the¡¡kernel¡¡to¡¡fail£¿¡¡Hint£º¡¡think¡¡about¡¡the¡¡methods¡¡to¡¡turn¡¡on¡¡or¡¡off¡¡the¡¡lights¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡and¡¡think¡¡of¡¡what¡¡could¡¡go¡¡wrong¡£¡¡


¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­Page¡¡251¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­

C¡¡¡¡H¡¡¡¡A¡¡¡¡P¡¡¡¡T¡¡¡¡E¡¡¡¡R¡¡¡¡¡¡¡¡¡¡9¡¡



¡ö¡¡¡ö¡¡¡ö¡¡



Learning¡¡About¡¡Lists£»¡¡Delegates£»¡¡¡¡

and¡¡Lambda¡¡Expressions¡¡¡¡



One¡¡of¡¡the¡¡most¡¡mon¡¡pieces¡¡of¡¡code¡¡that¡¡you¡¡will¡¡write¡¡is¡¡code¡¡that¡¡manages¡¡many¡¡object¡¡¡¡

instances¡£¡¡In¡¡the¡¡previous¡¡examples£»¡¡many¡¡object¡¡instances¡¡were¡¡managed¡¡using¡¡an¡¡array¡£¡¡In¡¡¡¡

Chapter¡¡8£»¡¡you¡¡learned¡¡that¡¡a¡¡linked¡¡list¡¡used¡¡in¡¡conjunction¡¡with¡¡a¡¡default¡¡property¡¡could¡¡make¡¡a¡¡¡¡

plain¡­vanilla¡¡object¡¡look¡¡like¡¡a¡¡collection¡£¡¡This¡¡chapter¡¡introduces¡¡the¡¡¡¡collection¡¡classes£»¡¡¡¡

which¡¡provide¡¡an¡¡easy¡¡way¡¡to¡¡manage¡¡a¡¡set¡¡of¡¡object¡¡instances¡£¡¡Think¡¡of¡¡a¡¡collection¡¡object¡¡as¡¡¡¡

an¡¡infinite¡¡sack¡¡where¡¡things¡¡can¡¡be¡¡added£»¡¡iterated¡¡through£»¡¡and¡¡retrieved¡£¡¡¡¡

¡¡¡¡¡¡¡¡¡¡The¡¡chapter¡¡begins¡¡with¡¡a¡¡discussion¡¡of¡¡how¡¡to¡¡manage¡¡collections¡£¡¡Then¡¡we¡¡will¡¡look¡¡at¡¡¡¡

different¡¡ways¡¡of¡¡iterating¡¡the¡¡data¡¡using¡¡a¡¡delegate¡£¡¡Finally£»¡¡we¡¡will¡¡look¡¡at¡¡lambda¡¡expressions¡¡¡¡

and¡¡closures¡¡to¡¡illustrate¡¡another¡¡way¡¡of¡¡processing¡¡data¡£¡¡

¡¡¡¡¡¡¡¡¡¡The¡¡project¡¡structure¡¡used¡¡in¡¡this¡¡chapter¡¡is¡¡a¡¡single¡¡console¡¡application¡£¡¡As¡¡we¡¡will¡¡not¡¡be¡¡¡¡

building¡¡an¡¡overall¡¡application£»¡¡but¡¡rather¡¡a¡¡set¡¡of¡¡sample¡¡code¡¡snippets£»¡¡no¡¡tests¡¡or¡¡libraries¡¡¡¡

are¡¡involved¡£¡¡



Managing¡¡Collections¡¡



When¡¡you¡¡have¡¡a¡¡collection£»¡¡what¡¡you¡¡actually¡¡have¡¡is¡¡an¡¡object¡¡that¡¡happens¡¡to¡¡point¡¡to¡¡many¡¡¡¡

other¡¡objects¡£¡¡¡¡

¡¡¡¡¡¡¡¡¡¡Visual¡¡Basic¡¡provides¡¡collection¡¡classes¡¡for¡¡managing¡¡collections¡£¡¡Visual¡¡Basic¡¡2005¡¡intro

duced¡¡a¡¡different¡¡approach¡¡to¡¡collections£»¡¡which¡¡solved¡¡many¡¡of¡¡the¡¡problems¡¡that¡¡came¡¡up¡¡¡¡

in¡¡earlier¡¡Visual¡¡Basic¡¡versions¡£¡¡Here£»¡¡we¡¯ll¡¡look¡¡at¡¡managing¡¡collections¡¡both¡¡before¡¡and¡¡after¡¡¡¡

Visual¡¡Basic¡¡2005£»¡¡which¡¡will¡¡help¡¡you¡¡to¡¡understand¡¡how¡¡collections¡¡are¡¡used¡£¡¡¡¡



Managing¡¡a¡¡Collection¡¡Before¡¡Visual¡¡Basic¡¡2005¡¡



Before¡¡Visual¡¡Basic¡¡2005£»¡¡the¡¡main¡¡collection¡¡classes¡¡were¡¡stored¡¡in¡¡the¡¡namespace¡¡System¡£¡¡

Collections¡¡£¨and£»¡¡of¡¡course£»¡¡these¡¡classes¡¡are¡¡still¡¡available¡¡for¡¡you¡¡to¡¡use£©¡£¡¡The¡¡following¡¡are¡¡¡¡

some¡¡of¡¡the¡¡classes¡¡and¡¡interfaces¡¡in¡¡that¡¡namespace£º¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡229¡¡


¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­Page¡¡252¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­

230¡¡¡¡¡¡¡¡¡¡¡¡¡¡CH¡¡AP¡¡T¡¡E¡¡R¡¡¡¡¡¡9¡¡¡¡¡¡¡ö¡¡¡¡¡¡¡¡L¡¡E¡¡A¡¡R¡¡N¡¡IN¡¡G¡¡¡¡¡¡AB¡¡OU¡¡T¡¡¡¡¡¡L¡¡I¡¡ST¡¡S£»¡¡¡¡¡¡D¡¡E¡¡L¡¡E¡¡G¡¡A¡¡T¡¡E¡¡S£»¡¡¡¡¡¡A¡¡N¡¡D¡¡¡¡¡¡L¡¡A¡¡M¡¡B¡¡DA¡¡¡¡¡¡E¡¡X¡¡P¡¡R¡¡E¡¡S¡¡SI¡¡ON¡¡S¡¡¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡o¡¡¡¡ArrayList£º¡¡A¡¡general¡¡collection¡¡that¡¡manages¡¡all¡¡of¡¡the¡¡referenced¡¡objects¡¡using¡¡an¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡internal¡¡
·µ»ØĿ¼ ÉÏÒ»Ò³ ÏÂÒ»Ò³ »Øµ½¶¥²¿ ÔÞ£¨0£© ²È£¨0£©
¿ì½Ý²Ù×÷: °´¼üÅÌÉÏ·½Ïò¼ü ¡û »ò ¡ú ¿É¿ìËÙÉÏÏ·­Ò³ °´¼üÅÌÉ쵀 Enter ¼ü¿É»Øµ½±¾ÊéĿ¼ҳ °´¼üÅÌÉÏ·½Ïò¼ü ¡ü ¿É»Øµ½±¾Ò³¶¥²¿!
ÎÂÜ°Ìáʾ£º ο´Ð¡ËµµÄͬʱ·¢±íÆÀÂÛ£¬Ëµ³ö×Ô¼ºµÄ¿´·¨ºÍÆäËüС»ï°éÃÇ·ÖÏíÒ²²»´íŶ£¡·¢±íÊéÆÀ»¹¿ÉÒÔ»ñµÃ»ý·ÖºÍ¾­Ñé½±Àø£¬ÈÏÕæдԭ´´ÊéÆÀ ±»²ÉÄÉΪ¾«ÆÀ¿ÉÒÔ»ñµÃ´óÁ¿½ð±Ò¡¢»ý·ÖºÍ¾­Ñé½±ÀøŶ£¡