ÓÑÇéÌáʾ£ºÈç¹û±¾ÍøÒ³´ò¿ªÌ«Âý»òÏÔʾ²»ÍêÕû£¬Çë³¢ÊÔÊó±êÓÒ¼ü¡°Ë¢Ð¡±±¾ÍøÒ³£¡
VB2008´ÓÈëÃŵ½¾«Í¨(PDF¸ñʽӢÎÄ°æ)-µÚ47²¿·Ö
¿ì½Ý²Ù×÷: °´¼üÅÌÉÏ·½Ïò¼ü ¡û »ò ¡ú ¿É¿ìËÙÉÏÏ·ҳ °´¼üÅÌÉ쵀 Enter ¼ü¿É»Øµ½±¾ÊéĿ¼ҳ °´¼üÅÌÉÏ·½Ïò¼ü ¡ü ¿É»Øµ½±¾Ò³¶¥²¿! Èç¹û±¾ÊéûÓÐÔĶÁÍ꣬ÏëÏ´μÌÐø½Ó×ÅÔĶÁ£¬¿ÉʹÓÃÉÏ·½ "Êղص½ÎÒµÄä¯ÀÀÆ÷" ¹¦ÄÜ ºÍ "¼ÓÈëÊéÇ©" ¹¦ÄÜ£¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡Class¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡SquareImpl¡¡class¡¡implements¡¡the¡¡behavior¡¡of¡¡ISquare¡¡and¡¡IRectangle£»¡¡and¡¡models¡¡real¡¡life£»¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡where¡¡a¡¡square¡¡is¡¡also¡¡a¡¡rectangle¡£¡¡And¡¡RectangleImpl¡¡implements¡¡only¡¡the¡¡behavior¡¡IRectangle£»¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡illustrating¡¡that¡¡a¡¡rectangle¡¡can¡¡only¡¡be¡¡a¡¡rectangle¡¡and¡¡not¡¡a¡¡square¡£¡¡Now¡¡you¡¡cannot¡¡write¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡code¡¡where¡¡the¡¡implementation¡¡produces¡¡inconsistent¡¡results¡£¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡öNote¡¡¡¡When¡¡defining¡¡ideas£»¡¡the¡¡resulting¡¡interfaces¡¡could¡¡be¡¡considered¡¡as¡¡behavior¡¡characteristics¡¡that¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡an¡¡implementation¡¡can¡¡have¡£¡¡It¡¡is¡¡entirely¡¡possible¡¡for¡¡an¡¡implementation¡¡to¡¡have¡¡multiple¡¡behavior¡¡character
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡istics¡£¡¡For¡¡example£»¡¡this¡¡could¡¡mean¡¡that¡¡an¡¡implementation¡¡could¡¡be¡¡both¡¡a¡¡square¡¡and¡¡rectangle¡¡at¡¡the¡¡same¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡time¡£¡¡In¡¡terms¡¡of¡¡the¡¡restaurant£»¡¡waiters¡¡are¡¡humans¡¡with¡¡hobbies£»¡¡feelings£»¡¡and¡¡desires£»¡¡although¡¡you¡¡don¡¯t¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡know¡¡these¡¡other¡¡aspects£»¡¡as¡¡you¡¡see¡¡only¡¡the¡¡waiters¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡An¡¡optimization¡¡could¡¡have¡¡been¡¡the¡¡following¡¡interface¡¡inheritance¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Interface¡¡ISquare¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Inherits¡¡IShape¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Property¡¡Width£¨£©¡¡As¡¡Double¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡Interface¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Interface¡¡IRectangle¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Inherits¡¡ISquare¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Property¡¡Length£¨£©¡¡As¡¡Double¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡Interface¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡or¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Interface¡¡ISquare¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Inherits¡¡IRectangle¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡Interface¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Interface¡¡IRectangle¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Inherits¡¡IShape¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Property¡¡Width£¨£©¡¡As¡¡Double¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Property¡¡Length£¨£©¡¡As¡¡Double¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡Interface¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Page¡¡195¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡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¡¡173¡¡
¡¡¡¡¡¡¡¡¡¡However£»¡¡this¡¡isn¡¯t¡¡a¡¡good¡¡idea£»¡¡because¡¡at¡¡the¡¡interface¡¡level£»¡¡you¡¡are¡¡implying¡¡that¡¡a¡¡square¡¡¡¡
and¡¡rectangle¡¡are¡¡related¡£¡¡They¡¡might¡¡be¡¡at¡¡the¡¡implementation¡¡level£»¡¡but¡¡they¡¡also¡¡might¡¡not¡¡¡¡
be¡£¡¡For¡¡example£»¡¡imagine¡¡you¡¡are¡¡creating¡¡a¡¡super¡¡shape¡¡that¡¡has¡¡the¡¡characteristics¡¡of¡¡a¡¡rect
angle¡¡and¡¡a¡¡triangle¡£¡¡When¡¡creating¡¡a¡¡relationship¡¡between¡¡interfaces£»¡¡you¡¡are¡¡implying¡¡that¡¡¡¡
the¡¡super¡¡shape¡¡depending¡¡on¡¡the¡¡interface¡¡inheritance¡¡used¡¡must¡¡have¡¡the¡¡characteristics¡¡of¡¡¡¡
a¡¡square£»¡¡even¡¡though¡¡it¡¡might¡¡not¡£¡¡Thus£»¡¡when¡¡using¡¡inheritance¡¡with¡¡interfaces£»¡¡IShape¡¡as¡¡a¡¡¡¡
base¡¡interface¡¡of¡¡IRectangle¡¡and¡¡ISquare¡¡is¡¡acceptable£»¡¡but¡¡a¡¡relation¡¡between¡¡¡¡IRectangle¡¡and¡¡¡¡
ISquare¡¡is¡¡not¡£¡¡¡¡
¡¡¡¡¡¡¡¡¡¡Now¡¡that¡¡you¡¡have¡¡an¡¡idea¡¡of¡¡the¡¡differences¡¡between¡¡inheritance¡¡and¡¡ponents£»¡¡we¡¡¡¡
can¡¡get¡¡started¡¡with¡¡our¡¡tax¡¡application¡£¡¡As¡¡we¡¡work¡¡through¡¡the¡¡example£»¡¡you¡¯ll¡¡learn¡¡the¡¡¡¡
details¡¡of¡¡implementing¡¡interfaces¡£¡¡
Implementing¡¡a¡¡Tax¡¡Engine¡¡
So¡¡far£»¡¡we¡¯ve¡¡discussed¡¡some¡¡basic¡¡tax¡¡concepts£»¡¡the¡¡features¡¡of¡¡the¡¡tax¡¡application£»¡¡and¡¡the¡¡¡¡
theory¡¡of¡¡inheritance£»¡¡interfaces£»¡¡and¡¡ponents¡£¡¡We¡¯re¡¡ready¡¡to¡¡implement¡¡the¡¡tax¡¡engine¡£¡¡¡¡
The¡¡preferred¡¡approach¡¡is¡¡to¡¡design¡¡the¡¡core¡¡idea¡¡and¡¡then¡¡create¡¡the¡¡other¡¡pieces£»¡¡or¡¡¡¡
dependencies¡£¡¡
Defining¡¡the¡¡Interfaces¡¡
Putting¡¡everything¡¡together¡¡and¡¡starting¡¡with¡¡the¡¡tax¡¡engine£»¡¡we¡¡can¡¡create¡¡the¡¡following¡¡¡¡
interface¡¡structure¡£¡¡
Public¡¡Interface¡¡ITaxIne¡¡¡¡
¡¡¡¡¡¡¡¡ReadOnly¡¡Property¡¡RealAmount£¨£©¡¡As¡¡Double¡¡
¡¡¡¡¡¡¡¡ReadOnly¡¡Property¡¡TaxableAmount£¨£©¡¡As¡¡Double¡¡
End¡¡Interface¡¡
Public¡¡Interface¡¡ITaxDeduction¡¡¡¡
¡¡¡¡¡¡¡¡ReadOnly¡¡Property¡¡Amount£¨£©¡¡As¡¡Double¡¡
End¡¡Interface¡¡
Public¡¡Interface¡¡ITaxAccount¡¡¡¡
¡¡¡¡¡¡¡¡Sub¡¡AddDeduction£¨ByVal¡¡deduction¡¡As¡¡ITaxDeduction£©¡¡
¡¡¡¡¡¡¡¡Sub¡¡AddIne£¨ByVal¡¡ine¡¡As¡¡ITaxIne£©¡¡
¡¡¡¡¡¡¡¡Function¡¡GetTaxRate£¨ByVal¡¡ine¡¡As¡¡Double£©¡¡As¡¡Double¡¡
¡¡¡¡¡¡¡¡ReadOnly¡¡Property¡¡Deductions£¨£©¡¡As¡¡ITaxDeduction£¨£©¡¡
¡¡¡¡¡¡¡¡ReadOnly¡¡Property¡¡Ine£¨£©¡¡As¡¡ITaxIne£¨£©¡¡
End¡¡Interface¡¡
Public¡¡Interface¡¡ITaxEngine¡¡¡¡
¡¡¡¡¡¡¡¡Function¡¡CalculateTaxToPay£¨ByVal¡¡account¡¡As¡¡ITaxAccount£©¡¡As¡¡Double¡¡
¡¡¡¡¡¡¡¡Function¡¡CreateDeduction£¨ByVal¡¡amount¡¡As¡¡Double£©¡¡As¡¡ITaxDeduction¡¡
¡¡¡¡¡¡¡¡Function¡¡CreateIne£¨ByVal¡¡amount¡¡As¡¡Double£©¡¡As¡¡ITaxIne¡¡
¡¡¡¡¡¡¡¡Function¡¡CreateTaxAccount£¨£©¡¡As¡¡ITaxAccount¡¡
End¡¡Interface¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Page¡¡196¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡
174¡¡¡¡¡¡¡¡¡¡¡¡¡¡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¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡We¡¡have¡¡four¡¡interfaces¡¡here£º¡¡¡¡ITaxIne£»¡¡ITaxDeduction£»¡¡ITaxAccount£»¡¡and¡¡ITaxEngine¡£¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ITaxIne¡¡and¡¡ITaxDeduction¡¡are¡¡pure¡¡behavioral¡¡interfaces¡£¡¡Pure¡¡behavioral¡¡means¡¡the¡¡inter
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡face¡¡does¡¡one¡¡thing£»¡¡but¡¡it¡¡might¡¡be¡¡implemented¡¡in¡¡conjunction¡¡with¡¡other¡¡interfaces¡£¡¡ITaxEngine¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡and¡¡ITaxAccount¡¡are¡¡behavioral¡¡functional¡¡interfaces¡£¡¡Behavioral¡¡functional¡¡means¡¡that¡¡the¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡interfaces¡¡are¡¡typically¡¡implemented¡¡by¡¡themselves£»¡¡and¡¡usually¡¡not¡¡with¡¡another¡¡interface¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡For¡¡example£»¡¡you¡¡might¡¡implement¡¡a¡¡Swiss¡¡tax¡¡system¡¡where¡¡there¡¡are¡¡two¡¡classes£»¡¡defined¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡as¡¡follows£º¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Class¡¡SwissTaxEngine¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Implements¡¡ITaxEngine¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡Class¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Class¡¡SwissTaxAccount¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Implements¡¡ITaxAccount¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡Class¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡And¡¡if¡¡you¡¡wish¡¡to¡¡implement¡¡an¡¡American¡¡tax¡¡system£»¡¡the¡¡two¡¡classes¡¡would¡¡be¡¡defined¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡as¡¡follows£º¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Class¡¡AmericanTaxEngine¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Implements¡¡ITaxEngine¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡Class¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Class¡¡AmericanTaxAccount¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Implements¡¡ITaxAccount¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡Class¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡user¡¡of¡¡either¡¡the¡¡American¡¡or¡¡Swiss¡¡tax¡¡system¡¡will¡¡not¡¡know¡¡the¡¡specific¡¡details¡¡of¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡those¡¡tax¡¡systems¡£¡¡Initially£»¡¡users¡¡would¡¡need¡¡to¡¡determine¡¡which¡¡tax¡¡system¡¡they¡¡wished¡¡to¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡use¡£¡¡This¡¡decision¡¡is¡¡made¡¡using¡¡something¡¡called¡¡a¡¡factory£»¡¡as¡¡explained¡¡in¡¡the¡¡uping¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡°Abstracting¡¡Instantiations¡¡with¡¡Factories¡±¡¡section¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Implementing¡¡a¡¡Base¡¡Class¡¡Tax¡¡Engine¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Whenever¡¡you¡¡define¡¡interfaces£»¡¡you¡¡will¡¡need¡¡to¡¡implement¡¡them¡£¡¡In¡¡most¡¡cases£»¡¡you¡¡will¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡create¡¡a¡¡MustInherit¡¡base¡¡class¡¡that¡¡provides¡¡a¡¡certain¡¡amount¡¡of¡¡default¡¡functionality¡£¡¡The¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡MustInherit¡¡base¡¡class¡¡serves¡¡the¡¡same¡¡purpose¡¡as¡¡outlined¡¡in¡¡the¡¡previous¡¡chapter£º¡¡to¡¡provide¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡a¡¡certain¡¡amount¡¡of¡¡basic¡¡functionality¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡In¡¡the¡¡case¡¡of¡¡the¡¡tax¡¡engine£»¡¡we¡¡need¡¡to¡¡implement¡¡the¡¡ITaxEngine¡¡interface¡¡and¡¡provide¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡default¡¡implementations¡¡for¡¡some¡¡methods¡£¡¡The¡¡following¡¡is¡¡the¡¡plete¡¡base¡¡class¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡implementation¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Public¡¡MustInherit¡¡Class¡¡BaseTaxEngine¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Implements¡¡ITaxEngine¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Protected¡¡_calculatedTaxable¡¡As¡¡Double¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Public¡¡Overridable¡¡Function¡¡CalculateTaxToPay£¨ByVal¡¡account¡¡As¡¡ITaxAccount£©¡¡_¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡As¡¡Double¡¡Implements¡¡ITaxEngine¡£CalculateTaxToPay¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Page¡¡197¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡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¡¡175¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡_calculatedTaxable¡¡=¡¡0¡£0¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡For¡¡Each¡¡ine¡¡As¡¡ITaxIne¡¡In¡¡account¡£Ine¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡If¡¡ine¡¡IsNot¡¡Nothing¡¡Then¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡_calculatedTaxable¡¡=¡¡_calculatedTaxable¡¡£«¡¡ine¡£TaxableAmount¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡If¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Next¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡For¡¡Each¡¡deduction¡¡As¡¡ITaxDeduction¡¡In¡¡account¡£Deductions¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡If¡¡¡¡deduction¡¡IsNot¡¡Nothing¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡_calculatedTaxable¡¡=¡¡_calculatedTaxable¡¡deduction¡£Amount¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡If¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Next¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Return¡¡account¡£GetTaxRate£¨_calculatedTaxable£©¡¡*¡¡_calculatedTaxable¡¡
¡¡¡¡¡¡¡¡End¡¡Function¡¡
¡¡¡¡¡¡¡¡Public¡¡Overridable¡¡Function¡¡CreateDeduction£¨ByVal¡¡amount¡¡As¡¡Double£©¡¡_¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡As¡¡ITaxDeduction¡¡Implements¡¡ITaxEngine¡£CreateDeduction¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Return¡¡New¡¡TaxDeduction£¨amount£©¡¡
¡¡¡¡¡¡¡¡End¡¡Function¡¡
¡¡¡¡¡¡¡¡Public¡¡Overridable¡¡Function¡¡CreateIne£¨ByVal¡¡amount¡¡As¡¡Double£©¡¡As¡¡ITaxIne¡¡_¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Implements¡¡ITaxEngine¡£CreateIne¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡'¡¡Second¡¡argument¡¡is¡¡the¡¡tax¡¡rate¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Return¡¡New¡¡TaxIne£¨amount£»¡¡1¡£0£©¡¡
¡¡¡¡¡¡¡¡End¡¡Function¡¡
¡¡¡¡¡¡¡¡Public¡¡MustOverride¡¡Function¡¡CreateTaxAccount£¨£©¡¡As¡¡ITaxAccount¡¡_¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Implements¡¡ITaxEngine¡£CreateTaxAccount¡¡
End¡¡Class¡¡
¡¡¡¡¡¡¡¡¡¡The¡¡base¡¡class¡¡must¡¡implement¡¡all¡¡interface¡¡methods£»¡¡regardless¡¡of¡¡whether¡¡or¡¡not¡¡the¡¡method¡¡¡¡
has¡¡an¡¡implementation¡£¡¡The¡¡CalculateTaxToPay£¨£©£»¡¡CreateDeduction£¨£©£»¡¡and¡¡CreateIne£¨£©¡¡¡¡
methods¡¡have¡¡implementations¡£¡¡The¡¡CreateTaxAccount£¨£©¡¡method¡¡does¡¡not¡¡have¡¡an¡¡implemen
tation¡¡and¡¡is¡¡declared¡¡as¡¡MustOverride¡£¡¡The¡¡methods¡¡with¡¡implementations¡¡have¡¡an¡¡Overridable¡¡¡¡
keyword¡¡associated¡¡with¡¡them£»¡¡indicating¡¡any¡¡class¡¡derived¡¡from¡¡BaseTaxEngine¡¡can¡¡override¡¡¡¡
the¡¡functionality¡¡if¡¡they¡¡don¡¯t¡¡like¡¡the¡¡default¡¡functionality¡£¡¡¡¡
¡¡¡¡¡¡¡¡¡¡In¡¡the¡¡implementation¡¡of¡¡CalculateTaxToPay£¨£©£»¡¡the¡¡ine¡¡£¨account¡£Ine£©¡¡is¡¡added¡¡¡¡
together¡¡and¡¡deductions¡¡£¨account¡£Deductions£©¡¡are¡¡subtracted¡¡from¡¡the¡¡ine¡£¡¡The¡¡resulting¡¡¡¡
total¡¡is¡¡used¡¡as¡¡a¡¡query¡¡amount¡¡£¨account¡£GetTaxRate£¨£©£©¡¡to¡¡retrieve¡¡the¡¡actual¡¡tax¡¡rate¡¡used¡¡to¡¡¡¡
calculate¡¡against¡¡the¡¡payable¡¡tax¡£¡¡
¡öNote¡¡¡¡The¡¡implementation¡¡of¡¡CalculateTaxToPay£¨£©¡¡is¡¡a¡¡shared¡¡functionality£»¡¡which¡¡implies¡¡that¡¡there¡¡¡¡
cannot¡¡be¡¡any¡¡code¡¡specific¡¡to¡¡a¡¡derived¡¡type¡£¡¡All¡¡of¡¡the¡¡calculations¡¡and¡¡data¡¡manipulations¡¡are¡¡executed¡¡¡¡
against¡¡an¡¡interface£»¡¡making¡¡it¡¡possible¡¡to¡¡generalize¡¡operations¡£¡¡Whenever¡¡you¡¡implement¡¡base¡¡class¡¡methods¡¡or¡¡¡¡
shared¡¡pieces¡¡of¡¡code£»¡¡you¡¡should¡¡try¡¡to¡¡keep¡¡the¡¡source¡¡code¡¡derived¡¡class¡agnostic¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Page¡¡198¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡
176¡¡¡¡¡¡¡¡¡¡¡¡¡¡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¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Overriding¡¡for¡¡Specialized¡¡Functionality¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡In¡¡the¡¡base¡¡class¡¡implementation£»¡¡the¡¡data¡¡member¡¡_calculatedTaxable¡¡is¡¡declared¡¡as¡¡Protected¡£¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡As¡¡you¡¡learned¡¡in¡¡the¡¡previous¡¡chapter£»¡¡this¡¡means¡¡that¡¡_calculatedTaxable¡¡can¡¡be¡¡manipu
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡lated¡¡in¡¡a¡¡derived¡¡class¡£¡¡However£»¡¡if¡¡you¡¡look¡¡at¡¡how¡¡the¡¡data¡¡member¡¡is¡¡used£»¡¡you¡¡will¡¡see¡¡that¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡only¡¡CalculateTaxToPay£¨£©¡¡assigns¡¡the¡¡data¡¡member¡£¡¡The¡¡purpose¡¡of¡¡the¡¡data¡¡member¡¡is¡¡to¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡provide¡¡information¡¡about¡¡the¡¡operation¡¡CalculateTaxToPay£¨£©¡¡without¡¡giving¡¡the¡¡exact¡¡details¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡of¡¡the¡¡operation¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡idea¡¡behind¡¡_calculatedTaxable¡¡and¡¡the¡¡declaration¡¡of¡¡CalculateTaxToPay£¨£©¡¡is¡¡to¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡provide¡¡a¡¡mechanism¡¡where¡¡the¡¡derived¡¡class¡¡does¡¡not¡¡need¡¡to¡¡calculate¡¡things¡¡again¡£¡¡Consider¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡the¡¡example¡¡of¡¡a¡¡country¡¡where£»¡¡if¡¡your¡¡taxable¡¡ine¡¡is¡¡above¡¡400¡¡currency¡¡units£»¡¡a¡¡surtax¡¡of¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡10¡¡currency¡¡units¡¡is¡¡calculated¡£¡¡You¡¡don¡¯t¡¡know¡¡what¡¡your¡¡taxable¡¡ine¡¡is¡¡until¡¡the¡¡function¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡CalculateTaxToPay£¨£©¡¡is¡¡executed£»¡¡and¡¡that¡¡function¡¡returns¡¡only¡¡the¡¡total¡¡payable¡¡taxes¡£¡¡So¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡how¡¡do¡¡you¡¡know¡¡if¡¡you¡¡should¡¡apply¡¡the¡¡surtax¡¡in¡¡this¡¡situation£¿¡¡One¡¡solution¡¡is¡¡to¡¡reverse
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡calculate¡¡the¡¡payable¡¡taxes£»¡¡but¡¡that¡¡would¡¡involve¡¡quite¡¡a¡¡few¡¡additional¡¡steps¡£¡¡An¡¡easier¡¡solution¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡is¡¡to¡¡write¡¡some¡¡code¡¡in¡¡the¡¡base¡¡class¡¡meth
¿ì½Ý²Ù×÷: °´¼üÅÌÉÏ·½Ïò¼ü ¡û »ò ¡ú ¿É¿ìËÙÉÏÏ·ҳ °´¼üÅÌÉ쵀 Enter ¼ü¿É»Øµ½±¾ÊéĿ¼ҳ °´¼üÅÌÉÏ·½Ïò¼ü ¡ü ¿É»Øµ½±¾Ò³¶¥²¿!
ÎÂÜ°Ìáʾ£º ο´Ð¡ËµµÄͬʱ·¢±íÆÀÂÛ£¬Ëµ³ö×Ô¼ºµÄ¿´·¨ºÍÆäËüС»ï°éÃÇ·ÖÏíÒ²²»´íŶ£¡·¢±íÊéÆÀ»¹¿ÉÒÔ»ñµÃ»ý·ÖºÍ¾Ñé½±Àø£¬ÈÏÕæдԴ´ÊéÆÀ ±»²ÉÄÉΪ¾«ÆÀ¿ÉÒÔ»ñµÃ´óÁ¿½ð±Ò¡¢»ý·ÖºÍ¾Ñé½±ÀøŶ£¡