友情提示:如果本网页打开太慢或显示不完整,请尝试鼠标右键“刷新”本网页!阅读过程发现任何错误请告诉我们,谢谢!! 报告错误
狗狗书籍 返回本书目录 我的书架 我的书签 TXT全本下载 进入书吧 加入书签

VB2008从入门到精通(PDF格式英文版)-第86章

按键盘上方向键 ← 或 → 可快速上下翻页,按键盘上的 Enter 键可回到本书目录页,按键盘上方向键 ↑ 可回到本页顶部!
————未阅读完?加入书签已便下次继续阅读!




    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 
返回目录 上一页 下一页 回到顶部 0 0
未阅读完?加入书签已便下次继续阅读!
温馨提示: 温看小说的同时发表评论,说出自己的看法和其它小伙伴们分享也不错哦!发表书评还可以获得积分和经验奖励,认真写原创书评 被采纳为精评可以获得大量金币、积分和经验奖励哦!