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

µÚ50²¿·Ö

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

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

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





¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­Page¡¡189¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡CH¡¡AP¡¡T¡¡E¡¡R¡¡¡¡¡¡7¡¡¡¡¡¡¡ö¡¡¡¡¡¡¡¡L¡¡E¡¡AR¡¡N¡¡IN¡¡G¡¡¡¡¡¡AB¡¡O¡¡U¡¡T¡¡¡¡¡¡CO¡¡M¡¡P¡¡O¡¡N¡¡E¡¡N¡¡TS¡¡¡¡¡¡AN¡¡D¡¡¡¡¡¡C¡¡L¡¡AS¡¡S¡¡¡¡H¡¡I¡¡E¡¡R¡¡AR¡¡C¡¡HI¡¡E¡¡S¡¡167¡¡



Class¡¡ExampleImplementation¡¡¡¡

¡¡¡¡¡¡¡¡Implements¡¡IExample¡¡



¡¡¡¡¡¡¡¡Public¡¡Sub¡¡Method£¨£©¡¡Implements¡¡IExample¡£Method¡¡

¡¡¡¡¡¡¡¡End¡¡Sub¡¡



¡¡¡¡¡¡¡¡Public¡¡Property¡¡ExampleProperty£¨£©¡¡As¡¡Integer¡¡Implements¡¡IExample¡£ExampleProperty¡¡

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

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Return¡¡0¡¡

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

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

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

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

End¡¡Class¡¡



¡¡¡¡¡¡¡¡¡¡¡¡ExampleImplementation¡¡implements¡¡the¡¡interface¡¡IExample¡¡using¡¡an¡¡explicit¡¡cross¡­reference¡¡¡¡

of¡¡the¡¡method¡¡or¡¡property¡¡signature¡£¡¡The¡¡cross¡­reference¡¡of¡¡interface¡¡to¡¡implementation¡¡in¡¡the¡¡¡¡

sample¡¡method¡¡is¡¡prefixed¡¡with¡¡the¡¡keyword¡¡Implements£»¡¡and¡¡the¡¡appropriate¡¡interface¡¡and¡¡¡¡

method¡£¡¡The¡¡method¡¡name¡¡doesn¡¯t¡¡need¡¡to¡¡be¡¡the¡¡same¡¡as¡¡the¡¡interface¡¯s¡¡method¡¡name¡£¡¡All¡¡¡¡

that¡¡matters¡¡is¡¡the¡¡signature¡¡of¡¡the¡¡method£»¡¡or¡¡property¡£¡¡¡¡



¡öNote¡¡¡¡Visual¡¡Basic¡¡and¡¡the¡¡CLR¡¡are¡¡single¡­inheritance¡¡models¡¡in¡¡that¡¡a¡¡class¡¡can¡¡subclass¡¡only¡¡a¡¡single¡¡class¡£¡¡¡¡

But¡¡a¡¡Visual¡¡Basic¡¡class¡¡can¡¡implement¡¡as¡¡many¡¡interfaces¡¡as¡¡necessary¡£¡¡If¡¡a¡¡class¡¡subclasses¡¡another¡¡class£»¡¡it¡¡uses¡¡¡¡

Inherits£»¡¡if¡¡it¡¡implements¡¡interfaces£»¡¡it¡¡uses¡¡Implements¡£¡¡The¡¡class¡¡is¡¡the¡¡first¡¡identifier¡¡after¡¡the¡¡colon¡£¡¡¡¡



¡¡¡¡¡¡¡¡¡¡When¡¡implementing¡¡an¡¡interface£»¡¡all¡¡of¡¡the¡¡methods¡¡and¡¡properties¡¡of¡¡the¡¡interface¡¡must¡¡be¡¡¡¡

implemented¡¡in¡¡the¡¡class¡£¡¡If¡¡they¡¡are¡¡not£»¡¡then¡¡when¡¡the¡¡source¡¡code¡¡is¡¡piled£»¡¡the¡¡piler¡¡will¡¡¡¡

plain¡¡about¡¡missing¡¡methods¡¡or¡¡properties¡¡and¡¡consider¡¡the¡¡implementation¡¡as¡¡inplete¡£¡¡

¡¡¡¡¡¡¡¡¡¡As¡¡inheritance¡¡is¡¡used£»¡¡you¡¡can¡¡think¡¡of¡¡¡¡IExample¡¡as¡¡a¡¡base¡¡class¡¡that¡¡has¡¡no¡¡implementa

tion¡£¡¡The¡¡following¡¡code¡¡illustrates¡¡how¡¡to¡¡instantiate¡¡the¡¡implementation¡¡and¡¡assign¡¡the¡¡¡¡

instance¡¡to¡¡a¡¡variable¡¡that¡¡has¡¡the¡¡interface¡¡as¡¡its¡¡type¡£¡¡



Dim¡¡cls¡¡As¡¡IExample¡¡=¡¡New¡¡ExampleImplementation£¨£©¡¡

cls¡£Method£¨£©¡¡



¡¡¡¡¡¡¡¡¡¡In¡¡the¡¡example£»¡¡the¡¡class¡¡ExampleImplementation¡¡is¡¡instantiated¡¡and¡¡assigned¡¡to¡¡a¡¡variable¡¡¡¡

cls¡¡of¡¡type¡¡¡¡IExample¡£¡¡Think¡¡of¡¡it¡¡as¡¡saying£»¡¡¡°I¡¡am¡¡instantiating¡¡ExampleImplementation¡¡and¡¡¡¡

assigning¡¡the¡¡instance¡¡to¡¡the¡¡type¡¡IExample¡¡that¡¡is¡¡in¡¡my¡¡inheritance¡¡hierarchy¡£¡±¡¡When¡¡the¡¡¡¡

method¡¡cls¡£Method£¨£©¡¡is¡¡called£»¡¡the¡¡caller¡¡is¡¡really¡¡calling¡¡¡¡ExampleImplementation¡£Method£¨£©£»¡¡¡¡

although¡¡the¡¡caller¡¡would¡¡not¡¡know¡¡that£»¡¡as¡¡it¡¡is¡¡using¡¡the¡¡base¡¡type¡¡IExample¡£¡¡

¡¡¡¡¡¡¡¡¡¡The¡¡mechanics¡¡of¡¡defining¡¡an¡¡interface¡¡and¡¡its¡¡associated¡¡implementation¡¡are¡¡straightfor

ward£»¡¡but¡¡why¡¡would¡¡you¡¡do¡¡it£¿¡¡To¡¡explain£»¡¡I¡¯ll¡¡use¡¡a¡¡real¡­life¡¡analogy¡£¡¡

¡¡¡¡¡¡¡¡¡¡Let¡¯s¡¡say¡¡that¡¡you¡¡are¡¡going¡¡to¡¡a¡¡restaurant¡¡for¡¡an¡¡evening¡¡of¡¡fine¡¡dining¡£¡¡When¡¡you¡¡are¡¡¡¡

sitting¡¡at¡¡the¡¡table£»¡¡you¡¡expect¡¡a¡¡waiter¡¡to¡¡take¡¡your¡¡order£»¡¡serve¡¡your¡¡food£»¡¡remove¡¡the¡¡used¡¡¡¡

dishes£»¡¡and¡¡give¡¡you¡¡the¡¡bill¡£¡¡All¡¡of¡¡these¡¡actions¡¡are¡¡ideas¡¡used¡¡to¡¡run¡¡a¡¡restaurant¡¡that¡¡serves¡¡a¡¡¡¡


¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­Page¡¡190¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­

168¡¡¡¡¡¡¡¡¡¡¡¡¡¡CH¡¡AP¡¡T¡¡E¡¡R¡¡¡¡¡¡7¡¡¡¡¡¡¡ö¡¡¡¡¡¡¡¡L¡¡E¡¡A¡¡R¡¡N¡¡IN¡¡G¡¡¡¡¡¡AB¡¡OU¡¡T¡¡¡¡¡¡CO¡¡M¡¡P¡¡O¡¡N¡¡E¡¡N¡¡TS¡¡¡¡¡¡AN¡¡D¡¡¡¡C¡¡L¡¡AS¡¡S¡¡¡¡H¡¡I¡¡E¡¡R¡¡AR¡¡C¡¡H¡¡IE¡¡S¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡clientele¡£¡¡You¡¡make¡¡use¡¡of¡¡the¡¡idea£»¡¡as¡¡do¡¡millions¡¡of¡¡other¡¡clients¡£¡¡The¡¡idea¡¡is¡¡applied¡¡across¡¡all¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡restaurants¡£¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Think¡¡about¡¡this¡¡a¡¡bit¡£¡¡The¡¡idea¡¡is¡¡a¡¡waiter£»¡¡but¡¡the¡¡implementation¡¡is¡¡a¡¡human¡£¡¡When¡¡you¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡sit¡¡down£»¡¡your¡¡waiter¡¡may¡¡introduce¡¡himself¡¡by¡¡his¡¡name£»¡¡but¡¡do¡¡you¡¡ever¡¡use¡¡the¡¡name£¿¡¡Most¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡people¡¡don¡¯t£»¡¡because¡¡they¡¡think¡¡of¡¡the¡¡waiter¡¡as¡¡a¡¡human¡¡that¡¡takes¡¡their¡¡order£»¡¡serves¡¡their¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡food£»¡¡and¡¡so¡¡on¡£¡¡The¡¡point¡¡is¡¡that¡¡the¡¡human¡¡is¡¡the¡¡implementation£»¡¡but¡¡you¡¡really¡¡only¡¡care¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡about¡¡a¡¡waiter¡¡fulfilling¡¡the¡¡scope¡¡of¡¡the¡¡task¡£¡¡For¡¡example£»¡¡while¡¡you¡¡might¡¡be¡¡sympathetic¡¡that¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡your¡¡waiter¡¡is¡¡having¡¡a¡¡bad¡¡day£»¡¡you¡¡want¡¡him¡¡to¡¡be¡¡chipper¡¡and¡¡happy£»¡¡and¡¡to¡¡do¡¡his¡¡job¡¡of¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡taking¡¡your¡¡order£»¡¡serving¡¡your¡¡food£»¡¡and¡¡so¡¡on¡£¡¡Bluntly¡¡put£»¡¡you¡¡probably¡¡would¡¡not¡¡care¡¡if¡¡the¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡waiter¡¯s¡¡puppy¡¡had¡¡been¡¡run¡¡over¡¡by¡¡a¡¡truck¡£¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡This¡¡is¡¡exactly¡¡what¡¡interfaces¡¡and¡¡implementations¡¡are¡¡about¡£¡¡The¡¡interface¡¡defines¡¡a¡¡role¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡of¡¡tasks¡¡that¡¡an¡¡implementation¡¡is¡¡supposed¡¡to¡¡implement¡£¡¡You¡¡don¡¯t¡¡care¡¡if¡¡the¡¡implementa

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡tion¡¡is¡¡capable¡¡of¡¡doing¡¡more£»¡¡or¡¡if¡¡the¡¡implementation¡¡is¡¡¡°having¡¡a¡¡bad¡¡day¡£¡±¡¡All¡¡you¡¡care¡¡about¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡is¡¡that¡¡the¡¡implementation¡¡does¡¡what¡¡the¡¡interface¡¡says¡¡it¡¡should¡¡do¡£¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡What¡¡you¡¡have¡¡is¡¡a¡¡decoupling¡¡of¡¡the¡¡idea¡¡from¡¡the¡¡interface£»¡¡just¡¡like¡¡at¡¡the¡¡restaurant£»¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡where¡¡you¡¡don¡¯t¡¡care¡¡if¡¡Tom£»¡¡Dick£»¡¡Mary£»¡¡or¡¡Jane¡¡is¡¡your¡¡server¡£¡¡In¡¡fact£»¡¡would¡¡you¡¡care¡¡if¡¡the¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡human¡¡waiter¡¡were¡¡replaced¡¡by¡¡a¡¡robot£¿¡¡Probably¡¡not£»¡¡because¡¡what¡¡you¡¡really¡¡care¡¡about¡¡is¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡eating¡¡the¡¡food¡£¡¡This¡¡is¡¡an¡¡important¡¡aspect¡¡of¡¡interfaces¡¡and¡¡implementations£»¡¡in¡¡that¡¡imple

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡mentations¡¡are¡¡replaceable£»¡¡and¡¡you¡¡want¡¡to¡¡be¡¡able¡¡to¡¡swap¡¡one¡¡implementation¡¡for¡¡another¡£¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡When¡¡you¡¡use¡¡interfaces¡¡and¡¡types¡¡that¡¡implement¡¡interfaces£»¡¡you¡¡are¡¡writing¡¡ponent

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡oriented¡¡software¡£¡¡ponents¡¡and¡¡inheritance¡¡are¡¡two¡¡different¡¡object¡­oriented¡¡techniques¡£¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡You¡¡use¡¡inheritance¡¡to¡¡implement¡¡interfaces£»¡¡but¡¡ponents¡¡serve¡¡the¡¡purpose¡¡of¡¡making¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ideas¡¡happen¡£¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Understanding¡¡How¡¡Inheritance¡¡and¡¡ponents¡¡Work¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Inheritance¡¡is¡¡the¡¡act¡¡of¡¡defining¡¡base¡¡classes¡¡with¡¡functionality¡¡that¡¡may¡¡or¡¡may¡¡not¡¡be¡¡over

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ridden¡¡or¡¡overloaded£»¡¡as¡¡explained¡¡in¡¡the¡¡previous¡¡chapter¡£¡¡ponents¡¡define¡¡subsystems¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡that¡¡are¡¡put¡¡together¡¡like¡¡pieces¡¡of¡¡a¡¡puzzle¡£¡¡The¡¡idea¡¡behind¡¡ponents¡¡is¡¡to¡¡be¡¡able¡¡to¡¡asso

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ciate¡¡two¡¡interface¡¡instances¡¡and¡¡make¡¡them¡¡work¡¡with¡¡each¡¡other¡¡without¡¡knowing¡¡what¡¡the¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡other¡¡does¡£¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡To¡¡get¡¡a¡¡feeling¡¡of¡¡the¡¡difference¡¡between¡¡inheritance¡¡using¡¡classes¡¡and¡¡ponents¡¡that¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡use¡¡interfaces¡¡and¡¡classes£»¡¡we¡¡will¡¡look¡¡at¡¡a¡¡classic¡¡example¡¡of¡¡inheritance¡¡and¡¡how¡¡that¡¡example¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡translates¡¡to¡¡ponents¡£¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Illustrating¡¡Inheritance¡¡Using¡¡a¡¡Shape£»¡¡Rectangle£»¡¡and¡¡Square¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡One¡¡of¡¡the¡¡most¡¡popular¡¡examples¡¡of¡¡using¡¡inheritance¡¡involves¡¡shapes¡¡and¡¡how¡¡to¡¡calculate¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡the¡¡area¡¡of¡¡a¡¡shape¡£¡¡The¡¡starting¡¡point¡¡of¡¡this¡¡inheritance¡¡is¡¡a¡¡MustInherit¡¡base¡¡class¡¡that¡¡has¡¡a¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡single¡¡property¡¡and¡¡method¡¡to¡¡indicate¡¡a¡¡single¡¡dimension¡¡and¡¡its¡¡associated¡¡area¡£¡¡For¡¡example£»¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡the¡¡following¡¡would¡¡be¡¡an¡¡appropriate¡¡base¡¡class¡¡definition¡£¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡MustInherit¡¡Class¡¡Shape¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Public¡¡MustOverride¡¡Function¡¡CalculateArea£¨£©¡¡As¡¡Double¡¡

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



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡method¡¡CalculateArea£¨£©¡¡is¡¡used¡¡to¡¡calculate¡¡the¡¡area¡¡of¡¡the¡¡shape¡£¡¡It¡¡is¡¡declared¡¡as¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡MustOverride¡¡and¡¡must¡¡be¡¡implemented¡¡by¡¡a¡¡derived¡¡class¡£¡¡


¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­Page¡¡191¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡CH¡¡AP¡¡T¡¡E¡¡R¡¡¡¡¡¡7¡¡¡¡¡¡¡ö¡¡¡¡¡¡¡¡L¡¡E¡¡AR¡¡N¡¡IN¡¡G¡¡¡¡¡¡AB¡¡O¡¡U¡¡T¡¡¡¡¡¡CO¡¡M¡¡P¡¡O¡¡N¡¡E¡¡N¡¡TS¡¡¡¡¡¡AN¡¡D¡¡¡¡¡¡C¡¡L¡¡AS¡¡S¡¡¡¡H¡¡I¡¡E¡¡R¡¡AR¡¡C¡¡HI¡¡E¡¡S¡¡169¡¡



¡¡¡¡¡¡¡¡¡¡For¡¡starters£»¡¡let¡¯s¡¡define¡¡Square£»¡¡which¡¡represents¡¡the¡¡square¡¡shape¡£¡¡



Class¡¡Square¡¡¡¡

¡¡¡¡¡¡¡¡Inherits¡¡Shape¡¡



¡¡¡¡¡¡¡¡Private¡¡_width¡¡As¡¡Double¡¡

¡¡¡¡¡¡¡¡Public¡¡Property¡¡Width£¨£©¡¡As¡¡Double¡¡

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

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Return¡¡_width¡¡

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

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

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡_width¡¡=¡¡value¡¡

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

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



¡¡¡¡¡¡¡¡Public¡¡Overrides¡¡Function¡¡CalculateArea£¨£©¡¡As¡¡Double¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Return¡¡Width¡¡*¡¡Width¡¡

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

End¡¡Class¡¡



¡¡¡¡¡¡¡¡¡¡A¡¡square¡¡has¡¡only¡¡one¡¡dimension£»¡¡Width£»¡¡which¡¡represents¡¡the¡¡width¡¡of¡¡a¡¡particular¡¡shape¡£¡¡In¡¡¡¡

the¡¡case¡¡of¡¡a¡¡square£»¡¡width¡¡means¡¡one¡¡of¡¡the¡¡four¡¡sides¡£¡¡We¡¯ve¡¡implemented¡¡the¡¡CalculateArea£¨£©¡¡¡¡

method£»¡¡which¡¡calculates¡¡the¡¡surface¡¡area¡¡of¡¡the¡¡square¡¡by¡¡multiplying¡¡the¡¡Width¡¡property¡¡¡¡

by¡¡itself¡£¡¡

¡¡¡¡¡¡¡¡¡¡A¡¡rectangle¡¡is¡¡a¡¡form¡¡of¡¡square£»¡¡and¡¡therefore¡¡¡¡Rectangle¡¡derives¡¡from¡¡¡¡Square£º¡¡



Class¡¡Rectangle¡¡¡¡

¡¡¡¡¡¡¡¡Inherits¡¡Square¡¡



¡¡¡¡¡¡¡¡Private¡¡_length¡¡As¡¡Double¡¡

¡¡¡¡¡¡¡¡Public¡¡Property¡¡Length£¨£©¡¡As¡¡Double¡¡

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

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Return¡¡_length¡¡

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

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

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡_length¡¡=¡¡value¡¡

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

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



¡¡¡¡¡¡¡¡Public¡¡Overloads¡¡Function¡¡CalculateArea£¨£©¡¡As¡¡Double¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Return¡¡Width¡¡*¡¡Length¡¡

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

End¡¡Class¡¡



¡¡¡¡¡¡¡¡¡¡Rectangle¡¡cannot¡¡be¡¡described¡¡using¡¡a¡¡single¡¡dimension£»¡¡thus¡¡we¡¡need¡¡to¡¡add¡¡the¡¡property¡¡¡¡

Length¡£¡¡In¡¡the¡¡implementation¡¡of¡¡the¡¡Rectangle¡£CalculateArea£¨£©¡¡method¡¡to¡¡calculate¡¡the¡¡area£»¡¡¡¡

the¡¡length¡¡is¡¡multiplied¡¡by¡¡the¡¡width¡£¡¡

¡¡¡¡¡¡¡¡¡¡Take¡¡a¡¡good¡¡look¡¡at¡¡how¡¡CalculateArea£¨£©¡¡is¡¡declared¡£¡¡In¡¡the¡¡case¡¡of¡¡Rectangle¡£¡¡

CalculateArea£¨£©£»¡¡the¡¡Overloads¡¡keyword¡¡is¡¡used£»¡¡not¡¡Overrides¡£¡¡This¡¡is¡¡because¡¡you¡¡want¡¡to¡¡¡¡


¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­Page¡¡192¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­

170¡¡¡¡¡¡¡¡¡¡¡¡¡¡CH¡¡AP¡¡T¡¡E¡¡R¡¡¡¡¡¡7¡¡¡¡¡¡¡ö¡¡¡¡¡¡¡¡L¡¡E¡¡A¡¡R¡¡N¡¡IN¡¡G¡¡¡¡¡¡AB¡¡OU¡¡T¡¡¡¡¡¡CO¡¡M¡¡P¡¡O¡¡N¡¡E¡¡N¡¡TS¡¡¡¡¡¡AN¡¡D¡¡¡¡C¡¡L¡¡AS¡¡S¡¡¡¡H¡¡I¡¡E¡¡R¡¡AR¡¡C¡¡H¡¡IE¡¡S¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡enforce¡¡calculation¡¡consistency¡£¡¡Calculation¡¡consistency¡¡is¡¡when¡¡you¡¡perform¡¡a¡¡specific¡¡calcula

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡tion¡¡on¡¡a¡¡type¡¡and¡¡get¡¡the¡¡answer¡¡expected¡¡of¡¡that¡¡type£»¡¡and¡¡not¡¡some¡¡other¡¡type¡£¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡So£»¡¡say¡¡you¡¡instantiate¡¡Rectangle£»¡¡and¡¡then¡¡cast¡¡it¡¡to¡¡Square¡£¡¡When¡¡you¡¡call¡¡CalculateArea£¨£©£»¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡you¡¡want¡¡it¡¡to¡¡calculate¡¡as¡¡if¡¡the¡¡rectangle¡¡were¡¡a¡¡square£»¡¡not¡¡a¡¡rectangle¡£¡¡Thus£»¡¡by¡¡adding¡¡the¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Overloads¡¡keyword¡¡in¡¡the¡¡¡¡Rectangle¡£CalculateArea£¨£©¡¡method£»¡¡a¡¡square¡¡is¡¡calculated¡¡as¡¡a¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡square£»¡¡and¡¡a¡¡rectangle¡¡is¡¡calculated¡¡as¡¡a¡¡rectangle¡£¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡But¡¡there¡¡is¡¡a¡¡consequence¡£¡¡Let¡¯s¡¡say¡¡Rectangle¡¡is¡¡cast¡¡to¡¡Shape¡£¡¡As¡¡the¡¡inheritance¡¡is¡¡declared¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡when¡¡calling¡¡CalculateArea£¨£©£»¡¡the¡¡area¡¡of¡¡a¡¡square¡¡is¡¡calculated£»¡¡which¡¡is¡¡not¡¡correct¡£¡¡Thus¡¡it¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡would¡¡seem¡¡that¡¡using¡¡New¡¡is¡¡incorrect£»¡¡and¡¡Overrides¡¡should¡¡be¡¡used¡¡instead¡£¡¡So¡¡using¡¡Overrides¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡solves¡¡the¡¡Shape¡£CalculateArea£¨£©¡¡problem£»¡¡but¡¡when¡¡a¡¡rectangle¡¡is¡¡converted¡¡into¡¡a¡¡square£»¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡the¡¡area¡¡represen

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

Äã¿ÉÄÜϲ»¶µÄ