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

µÚ45²¿·Ö

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

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

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






Class¡¡Oven¡¡

¡¡¡¡¡¡¡¡Private¡¡_temperature¡¡As¡¡Integer¡¡



¡¡¡¡¡¡¡¡Public¡¡Sub¡¡SetTemperature£¨ByVal¡¡temperature¡¡As¡¡Integer£©¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡_temperature¡¡=¡¡temperature¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡If¡¡¡¡_temperature¡¡¡¡100¡£0¡¡Then¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Throw¡¡New¡¡Exception£¨¡¨100¡£0¡¡verification¡¡failed¡¨£©¡¡

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

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

¡¡¡¡¡¡¡¡Public¡¡Function¡¡AreYouPreHeated£¨£©¡¡As¡¡Boolean¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡'¡¡Check¡¡if¡¡oven¡¡temperature¡¡matches¡¡prescribed¡¡temperature¡¡¡¡

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

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

End¡¡Class¡¡



¡¡¡¡¡¡¡¡¡¡The¡¡bolded¡¡code¡¡illustrates¡¡a¡¡verification¡¡much¡¡like¡¡that¡¡used¡¡in¡¡CurrencyTrader£»¡¡which¡¡¡¡

verifies¡¡the¡¡parameter¡¡temperature¡¡for¡¡a¡¡specific¡¡value¡£¡¡While¡¡the¡¡verification¡¡is¡¡useful¡¡for¡¡a¡¡¡¡

particular¡¡test£»¡¡it¡¡is¡¡not¡¡useful¡¡in¡¡the¡¡big¡¡picture¡£¡¡As¡¡the¡¡code¡¡is¡¡written£»¡¡the¡¡only¡¡valid¡¡value¡¡of¡¡¡¡

temperature¡¡is¡¡100¡£0£»¡¡anything¡¡else¡¡will¡¡generate¡¡an¡¡exception¡£¡¡It¡¡is¡¡definitely¡¡not¡¡a¡¡solution¡£¡¡

¡¡¡¡¡¡¡¡¡¡To¡¡get¡¡around¡¡this¡¡problem£»¡¡you¡¡could¡¡use¡¡Visual¡¡Basic¡¡conditional¡¡statements¡£¡¡Condi

tional¡¡statements¡¡are¡¡special¡¡keywords¡¡that¡¡allow¡¡a¡¡developer¡¡to¡¡define¡¡whether¡¡a¡¡piece¡¡of¡¡¡¡

source¡¡code¡¡is¡¡piled¡£¡¡Following¡¡is¡¡an¡¡example¡¡of¡¡source¡¡code¡¡that¡¡includes¡¡conditional¡¡¡¡

statements¡£¡¡



Class¡¡TestCurrencyTrader¡¡

¡¡¡¡¡¡¡¡Inherits¡¡CurrencyTrader¡¡

¡¡¡¡¡¡¡¡Public¡¡Sub¡¡InitializeExchangeRate£¨£©¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ExchangeRate¡¡=¡¡100¡£0¡¡

#If¡¡INTEGRATE_TESTS¡¡Then¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡if¡¡¡¡ExchangeRate¡¡¡¡100¡£0¡¡Then¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Throw¡¡New¡¡Exception£¨¡¨100¡£0¡¡verification¡¡failed¡¨£©¡¡

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

#End¡¡If¡¡

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

End¡¡Class¡¡



¡¡¡¡¡¡¡¡¡¡The¡¡conditional¡¡statement¡¡always¡¡starts¡¡with¡¡a¡¡hash¡¡character¡¡£¨#£©£»¡¡immediately¡¡followed¡¡¡¡

by¡¡a¡¡keyword¡ªIf¡¡in¡¡this¡¡example¡£¡¡Between¡¡the¡¡#If¡¡and¡¡#End¡¡If¡¡is¡¡code¡¡that¡¡is¡¡conditionally¡¡¡¡

piled¡£¡¡This¡¡is¡¡known¡¡as¡¡a¡¡preprocessor¡¡directive¡£¡¡In¡¡this¡¡example£»¡¡the¡¡condition¡¡means¡¡¡¡

to¡¡pile¡¡the¡¡code¡¡contained¡¡within¡¡the¡¡#If¡¡and¡¡#End¡¡If¡¡block¡¡if¡¡the¡¡value¡¡¡¡INTEGRATE_TESTS¡¡¡¡

is¡¡true¡£¡¡

¡¡¡¡¡¡¡¡¡¡You¡¡can¡¡define¡¡a¡¡pilation¡¡identifier¡¡like¡¡¡¡INTEGRATE_TESTS¡¡in¡¡the¡¡source¡¡code¡¡or¡¡in¡¡your¡¡¡¡

IDE¡£¡¡In¡¡Visual¡¡Basic¡¡Express£»¡¡you¡¡can¡¡assign¡¡INTEGRATE_TESTS¡¡as¡¡follows£º¡¡


¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­Page¡¡172¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­

150¡¡¡¡¡¡¡¡¡¡¡¡¡¡CH¡¡AP¡¡T¡¡E¡¡R¡¡¡¡¡¡6¡¡¡¡¡¡¡ö¡¡¡¡¡¡¡¡L¡¡E¡¡A¡¡R¡¡N¡¡IN¡¡G¡¡¡¡¡¡T¡¡HE¡¡¡¡¡¡B¡¡AS¡¡IC¡¡S¡¡¡¡O¡¡F¡¡¡¡¡¡OB¡¡J¡¡E¡¡CT¡¡OR¡¡I¡¡E¡¡N¡¡T¡¡E¡¡D¡¡¡¡¡¡P¡¡R¡¡O¡¡G¡¡R¡¡AM¡¡M¡¡IN¡¡G¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡1¡£¡¡¡¡Right¡­click¡¡your¡¡project¡¡and¡¡select¡¡Properties¡£¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡2¡£¡¡¡¡Select¡¡the¡¡pile¡¡tab¡£¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡3¡£¡¡¡¡Click¡¡the¡¡Advanced¡¡pile¡¡Options¡¡button¡£¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡4¡£¡¡¡¡Add¡¡the¡¡¡¡INTEGRATE_TESTS¡¡value¡¡to¡¡the¡¡Custom¡¡Constants¡¡text¡¡box¡£¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Using¡¡Partial¡¡Classes¡¡for¡¡Verification¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Conditional¡¡pilation¡¡is¡¡useful¡¡when¡¡you¡¡want¡¡to¡¡include¡¡or¡¡remove¡¡code¡¡depending¡¡on¡¡a¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡configuration¡£¡¡However£»¡¡some¡¡programmers¡¡will¡¡frown¡¡upon¡¡having¡¡conditional¡¡pilation¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡code¡¡within¡¡a¡¡function£»¡¡as¡¡that¡¡would¡¡be¡¡a¡¡maintenance¡¡nightmare¡£¡¡Another¡¡solution¡¡is¡¡to¡¡use¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡the¡¡Partial¡¡class¡¡keyword¡¡in¡¡conjunction¡¡with¡¡conditional¡¡pilation¡¡statements¡£¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Thus¡¡far¡¡in¡¡all¡¡of¡¡the¡¡examples£»¡¡whenever¡¡we¡¡defined¡¡a¡¡class£»¡¡all¡¡of¡¡the¡¡methods¡¡and¡¡code¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡were¡¡declared¡¡between¡¡the¡¡Class/End¡¡Class¡¡pair¡£¡¡By¡¡using¡¡partial¡¡classes£»¡¡it¡¡is¡¡possible¡¡to¡¡declare¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡a¡¡class¡¡in¡¡multiple¡¡places¡£¡¡When¡¡the¡¡Visual¡¡Basic¡¡piler¡¡piles¡¡the¡¡source¡¡code£»¡¡the¡¡individual¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡class¡¡pieces¡¡will¡¡be¡¡assembled¡¡into¡¡a¡¡single¡¡class¡¡definition¡£¡¡For¡¡our¡¡test¡¡code£»¡¡we¡¡can¡¡create¡¡an¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡implementation¡¡partial¡¡class¡¡and¡¡a¡¡conditionally¡¡piled¡¡test¡¡partial¡¡class¡¡implementation¡£¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡following¡¡is¡¡the¡¡modified¡¡TestCurrencyTrader¡¡class¡¡that¡¡could¡¡be¡¡used¡¡to¡¡test¡¡the¡¡state¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡without¡¡exposing¡¡the¡¡state¡£¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Partial¡¡Class¡¡TestCurrencyTrader¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Inherits¡¡CurrencyTrader¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Public¡¡Sub¡¡InitializeExchangeRate£¨£©¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ExchangeRate¡¡=¡¡100¡£0¡¡

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

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



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡#if¡¡¡¡INTEGRATE_TESTS¡¡Then¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Partial¡¡Class¡¡TestCurrencyTrader¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Inherits¡¡CurrencyTrader¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Public¡¡Sub¡¡VerifyExchangeRate£¨ByVal¡¡value¡¡As¡¡Double£©¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡If¡¡ExchangeRate¡¡¡¡value¡¡Then¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Throw¡¡New¡¡Exception£¨¡¨ExchangeRate¡¡verification¡¡failed¡¨£©¡¡

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

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

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

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡#End¡¡If¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡keyword¡¡¡¡Partial¡¡prefixes¡¡the¡¡Class¡¡keyword¡£¡¡The¡¡first¡¡implementation¡¡of¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡TestCurrencyTrader¡¡is¡¡an¡¡example¡¡of¡¡not¡¡exposing¡¡state¡£¡¡The¡¡second¡¡implementation¡¡of¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡TestCurrencyTrader£»¡¡which¡¡is¡¡declared¡¡in¡¡the¡¡context¡¡of¡¡a¡¡conditional¡¡pilation¡¡statement£»¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡contains¡¡the¡¡method¡¡VerifyExchangeRate£¨£©¡£¡¡This¡¡is¡¡a¡¡verification¡¡method¡¡that¡¡tests¡¡the¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ExchangeRate¡¡property¡¡for¡¡a¡¡particular¡¡value¡£¡¡


¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­Page¡¡173¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡CH¡¡A¡¡PT¡¡E¡¡R¡¡¡¡¡¡6¡¡¡¡¡¡¡ö¡¡¡¡¡¡¡¡L¡¡E¡¡A¡¡R¡¡N¡¡I¡¡N¡¡G¡¡¡¡¡¡T¡¡HE¡¡¡¡¡¡B¡¡AS¡¡IC¡¡S¡¡¡¡O¡¡F¡¡¡¡¡¡O¡¡B¡¡J¡¡E¡¡CT¡¡OR¡¡I¡¡E¡¡N¡¡TE¡¡D¡¡¡¡¡¡P¡¡R¡¡O¡¡G¡¡R¡¡AM¡¡M¡¡IN¡¡G¡¡151¡¡



¡öNote¡¡¡¡You¡¡can¡¡use¡¡partial¡¡classes¡¡only¡¡in¡¡the¡¡context¡¡of¡¡a¡¡single¡¡assembly£»¡¡as¡¡the¡¡Partial¡¡keyword¡¡cannot¡¡¡¡

be¡¡used¡¡across¡¡assembly¡¡boundaries¡£¡¡When¡¡I¡¡say¡¡¡°single¡¡assembly£»¡±¡¡I¡¡am¡¡referring¡¡to¡¡the¡¡piled¡¡pieces¡¡of¡¡¡¡

¡¡source¡¡code¡¡illustrated¡¡in¡¡Chapter¡¡1¡£¡¡In¡¡other¡¡words£»¡¡if¡¡you¡¡define¡¡a¡¡partial¡¡class¡¡in¡¡a¡¡library£»¡¡then¡¡all¡¡¡¡

pieces¡¡of¡¡the¡¡partial¡¡class¡¡need¡¡to¡¡be¡¡defined¡¡in¡¡the¡¡library¡£¡¡¡¡



¡¡¡¡¡¡¡¡¡¡Partial¡¡classes¡¡make¡¡it¡¡simple¡¡to¡¡separate¡¡functionality¡¡into¡¡various¡¡source¡¡code¡¡files¡£¡¡This¡¡¡¡

example¡¡demonstrates¡¡using¡¡partial¡¡classes¡¡to¡¡manipulate¡¡internal¡¡state¡¡of¡¡a¡¡class¡¡without¡¡¡¡

violating¡¡the¡¡do¡­not¡­expose¡­internal¡­state¡¡rule¡£¡¡Another¡¡use¡¡of¡¡partial¡¡classes¡¡is¡¡in¡¡the¡¡context¡¡¡¡

of¡¡code¡¡generators£»¡¡where¡¡one¡¡source¡¡code¡¡file¡¡contains¡¡the¡¡custom¡¡code£»¡¡and¡¡the¡¡other¡¡source¡¡¡¡

code¡¡file¡¡contains¡¡the¡¡generator¡¡code¡£¡¡¡¡



¡öNote¡¡¡¡Only¡¡one¡¡part¡¡of¡¡a¡¡partial¡¡class¡¡needs¡¡to¡¡be¡¡explicitly¡¡qualified¡¡as¡¡Partial¡£¡¡The¡¡piler¡¡is¡¡smart¡¡¡¡

enough¡¡to¡¡realize¡¡that¡¡if¡¡it¡¡sees¡¡one¡¡part¡¡as¡¡Partial£»¡¡then¡¡all¡¡the¡¡other¡¡parts¡¡must¡¡also¡¡be¡¡partial¡¡£¨even¡¡if¡¡they¡¡¡¡

aren¡¯t¡¡explicitly¡¡qualified¡¡with¡¡the¡¡Partial¡¡keyword£©¡£¡¡



Finishing¡¡the¡¡Base¡¡Class¡¡



The¡¡ExchangeRate¡¡property¡¡is¡¡one¡¡of¡¡the¡¡pieces¡¡of¡¡shared¡¡functionality¡£¡¡Another¡¡piece¡¡of¡¡shared¡¡¡¡

functionality¡¡we¡¡want¡¡to¡¡implement¡¡is¡¡the¡¡calculation¡¡of¡¡the¡¡exchange¡¡rate¡£¡¡We¡¯ll¡¡do¡¡this¡¡with¡¡¡¡

ConvertValue£¨£©¡¡and¡¡ConvertValueInverse£¨£©¡¡methods£»¡¡which¡¡convert¡¡a¡¡currency¡¡from¡¡one¡¡value¡¡¡¡

to¡¡another¡¡using¡¡multiplication¡¡or¡¡division¡£¡¡The¡¡following¡¡shows¡¡the¡¡methods¡¡in¡¡the¡¡pleted¡¡¡¡

base¡¡class¡¡implementation¡¡of¡¡CurrencyTrader¡£¡¡



Public¡¡MustInherit¡¡Class¡¡CurrencyTrader¡¡

¡¡¡¡¡¡¡¡Private¡¡_exchangeRate¡¡As¡¡Double¡¡



¡¡¡¡¡¡¡¡Protected¡¡Property¡¡ExchangeRate£¨£©¡¡As¡¡Double¡¡

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

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Return¡¡_exchangeRate¡¡

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

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

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡_exchangeRate¡¡=¡¡Value¡¡

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

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

¡¡¡¡¡¡¡¡Protected¡¡Function¡¡ConvertValue£¨ByVal¡¡input¡¡As¡¡Double£©¡¡As¡¡Double¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Return¡¡_exchangeRate¡¡*¡¡input¡¡

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

¡¡¡¡¡¡¡¡Protected¡¡Function¡¡ConvertValueInverse£¨ByVal¡¡input¡¡As¡¡Double£©¡¡As¡¡Double¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Return¡¡input¡¡/¡¡_exchangeRate¡¡

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

End¡¡Class¡¡


¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­Page¡¡174¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­

152¡¡¡¡¡¡¡¡¡¡¡¡¡¡CH¡¡AP¡¡T¡¡E¡¡R¡¡¡¡¡¡6¡¡¡¡¡¡¡ö¡¡¡¡¡¡¡¡L¡¡E¡¡A¡¡R¡¡N¡¡IN¡¡G¡¡¡¡¡¡T¡¡HE¡¡¡¡¡¡B¡¡AS¡¡IC¡¡S¡¡¡¡O¡¡F¡¡¡¡¡¡OB¡¡J¡¡E¡¡CT¡¡OR¡¡I¡¡E¡¡N¡¡T¡¡E¡¡D¡¡¡¡¡¡P¡¡R¡¡O¡¡G¡¡R¡¡AM¡¡M¡¡IN¡¡G¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡bolded¡¡code¡¡highlights¡¡the¡¡methods¡¡that¡¡convert¡¡the¡¡currency¡¡from¡¡one¡¡unit¡¡to¡¡another¡£¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Notice¡¡that¡¡there¡¡is¡¡no¡¡declaration¡¡of¡¡specific¡¡currency¡¡units£»¡¡because¡¡the¡¡base¡¡class¡¡is¡¡a¡¡utility¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡class¡¡used¡¡to¡¡help¡¡us¡¡realize¡¡an¡¡active¡¡trader¡¡or¡¡hotel¡¡trader¡¡implementation¡£¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡öNote¡¡¡¡Base¡¡class¡¡functionality£»¡¡even¡¡when¡¡appearing¡¡trivial£»¡¡is¡¡defined¡¡to¡¡ensure¡¡consistency¡¡in¡¡implemen

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡tation¡£¡¡Without¡¡consistency£»¡¡you¡¡encounter¡¡the¡¡problem¡¡where¡¡one¡¡implementation¡¡does¡¡one¡¡thing¡¡and¡¡another¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡implementation¡¡does¡¡something¡¡pletely¡¡different¡£¡¡¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡This¡¡pletes¡¡our¡¡test¡¡code¡£¡¡Now¡¡we¡¡will¡¡implement¡¡the¡¡active¡¡trader¡¡and¡¡hotel¡¡trader¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ponents¡¡of¡¡the¡¡currency¡¡exchange¡¡application¡£¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Writing¡¡the¡¡Active¡¡Trader¡¡and¡¡Hotel¡¡Trader¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Currency¡¡Converters¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡With¡¡the¡¡TestCurrencyTrader¡¡test¡¡solution¡¡pleted£»¡¡it¡¯s¡¡time¡¡to¡¡turn¡¡our¡¡attention¡¡to¡¡the¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡CurrencyTrader¡¡solution¡£¡¡As¡¡mentioned¡¡earlier£»¡¡this¡¡consists¡¡of¡¡the¡¡active¡¡trader¡¡and¡¡hotel¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡trader¡¡currency¡¡converter¡¡ponents¡£¡¡Here£»¡¡you¡¯ll¡¡see¡¡more¡¡clearly¡¡what¡¡it¡¡means¡¡to¡¡use¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡inheritance¡£¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Implementing¡¡ActiveCurrencyTrader¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡ActiveCurrencyTrader¡¡class¡¡implements¡¡the¡¡logic¡¡of¡¡the¡¡active¡¡currency¡¡trader¡£¡¡To¡¡begin£»¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡we¡¯ll¡¡add¡¡its¡¡constructor¡£¡¡¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Adding¡¡a¡¡Constructor¡¡to¡¡ActiveCurrencyTrader¡¡¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡To¡¡give¡¡ActiveCurrencyTrader¡¡some¡¡default¡¡state£»¡¡we¡¡use¡¡a¡¡constructor¡£¡¡However£»¡¡the¡¡constructor¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡will¡¡serve¡¡another¡¡purpose£»¡¡in¡¡that¡¡any¡¡class¡¡that¡¡instantiates¡¡ActiveCurrencyTrader¡¡will¡¡consider¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡the¡¡instance¡¡as¡¡immutable¡£¡¡Immutable¡¡means¡¡that¡¡once¡¡data¡¡has¡¡been¡¡assigned¡¡to¡¡the¡¡instance£»¡¡it¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡cannot¡¡be¡¡altered¡£¡¡In¡¡other¡¡words£»¡¡it¡¡is¡¡unchangeable¡£¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡öNote¡¡¡¡The¡¡String¡¡type¡¡is¡¡immutable¡¡because¡¡once¡¡a¡¡string¡¡variable¡¡has¡¡been¡¡assigned£»¡¡it¡¡cannot¡¡be¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡changed¡£¡¡Take¡¡a¡¡look¡¡at¡¡the¡¡methods¡¡associated¡¡with¡¡String£»¡¡and¡¡you¡¡will¡¡see¡¡nothing¡¡that¡¡allows¡¡you¡¡to¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡modify¡¡the¡¡contents¡£¡¡An¡¡immutable¡¡type¡¡is¡¡good¡¡because¡¡it¡¡allows¡¡you¡¡to¡¡implement¡¡a¡¡set¡­it¡­and¡­forget¡­it¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡object£»¡¡and¡¡it¡¡prevents¡¡other¡¡classes¡¡from¡¡accidentally¡¡changing¡¡the¡¡contents¡£¡¡Overall£»¡¡an¡¡immutable¡¡type¡¡is¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡robust¡¡and¡¡predictable¡¡£¨though¡¡they¡¡are¡¡the¡¡exception£»¡¡not¡¡the¡¡rule£»¡¡most¡¡types¡¡allow¡¡you¡¡to¡¡modify¡¡state£©¡£¡¡¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡following¡¡shows¡¡the¡¡constructor¡¡code¡¡added¡¡to¡¡ActiveCurrencyTrader¡£¡¡


¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­Page¡¡175¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡CH¡¡A¡¡PT¡¡E¡¡R¡¡¡¡¡¡6¡¡¡¡¡¡¡ö¡¡¡¡¡¡¡¡L¡¡E¡¡A¡¡R¡¡N¡¡I¡¡N¡¡G¡¡¡¡¡¡T¡¡HE¡¡¡¡¡¡B¡¡AS¡¡IC¡¡S¡¡

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

Äã¿ÉÄÜϲ»¶µÄ