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

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

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




          fewer than a dozen variations。 

               The better approach is to add an interface specific to the Canadian tax system。 Think of it  

          as follows。 When you are instantiating the tax engine; you will need to know which tax system  

          to use。 The factory protects you from needing to know which type to instantiate; but there is  

          nothing wrong with giving some extra information that could be used by a factory。 

               Thus; the solution is to define a new interface called ICanadaTaxEngine。 The purpose of  

          ICanadaTaxEngine is to add factory methods that are used to instantiate types with parameters  

          specific to the implementation。 Following is the definition of the ICanadaTaxEngine。 



              Public Enum Province  

                  Alberta 

                  BritishColumbia 

                  Manitoba 

                  NewBrunswick 

                  NewfoundlandLabrador 

                  NovaScotia 

                  Nunavut 

                  Ontario 

                  PrinceEdwardIsland 

                  Quebec 

                  Saskatchewan 

                  Yukon 

              End Enum 



              Public Interface ICanadaTaxEngine  

                  Function CreateTaxAccount(ByVal province As Province; _ 

                    ByVal year As Integer) As ITaxAccount 

                  Function CreateCapitalGain(ByVal amount As Double) As ITaxIne 

              End Interface 


…………………………………………………………Page 209……………………………………………………………

                           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 187 



     The definition of ICanadaTaxEngine contains two additional methods: 



     o CreateTaxAccount() is used to instantiate a tax account specific to a province and year。  



     o CreateCapitalGain() is used to instantiate an ine using the Canadian capital gains  

       calculation。 



     Thus; the implementation of TaxEngine bees the following。 



Friend Class TaxEngine 

    Inherits BaseTaxEngine 

    Implements ICanadaTaxEngine 



    Public Overrides Function CreateTaxAccount() As ITaxAccount  

        Return New TaxAccount(Province。Ontario; 2008) 

    End Function 



    Public Overloads Function CreateTaxAccount(ByVal province As Province; _ 

                                               ByVal year As Integer)  _  

    As ITaxAccount _ 

        Implements ICanadaTaxEngine。CreateTaxAccount 

        Return New TaxAccount(province; year) 

    End Function 



    Public Function CreateCapitalGain(ByVal amount As Double) As ITaxIne _ 

        Implements ICanadaTaxEngine。CreateCapitalGain 

        Return New TaxIne(amount; 0。50) 

    End Function 

End Class 



     In the modified implementation of TaxEngine; the class still derives from BaseTaxEngine;  

fulfilling the requirement of being a general tax engine。 However; for the additional require

ments of the Canadian tax system; we implement the ICanadaTaxEngine interface。  

     Defining a specific interface that implies a certain implementation is fine; because the  

specific interface is not bound to a certain implementation。 The better way to understand  

this implementation technique is to consider the specific interface as a characteristic that  

an implementation is capable of supporting。 This goes back to the shape example; where a  

square can support both the ISquare and IRectangle interfaces。  



Using the Tax Engine 



The last step is to use the tax engine to calculate some taxes。 The following is an example of  

calculating the taxes for Ontario in 2008。 



Dim engine As ITaxEngine = EngineCreator。CreateCanadianTaxEngine() 

Dim canadaEngine As ICanadaTaxEngine = TryCast(engine; ICanadaTaxEngine) 

Dim account As ITaxAccount = canadaEngine。CreateTaxAccount(Province。Ontario; 2008) 



Dim ine As ITaxIne = engine。CreateIne(100) 

Dim capitalGain As ITaxIne = canadaEngine。CreateCapitalGain(100) 


…………………………………………………………Page 210……………………………………………………………

188      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 



          account。AddIne(ine) 

          account。AddIne(capitalGain) 



          Dim deduction As ITaxDeduction = engine。CreateDeduction(20) 

          account。AddDeduction(deduction) 

          Dim taxToPay As Double = engine。CalculateTaxToPay(account) 

          Console。WriteLine(〃Tax to pay (〃 & taxToPay & 〃)〃) 



               Notice the definition of the variables engine and canadaEngine。 This is fine; because what  

          we are doing with the interfaces is choosing a characteristic that can be dynamically queried。 



          Learning More About Inheritance and  

          Type Casting 



          This chapter introduced interfaces and ponents; and delved deeper into inheritance。 Here; I’ll  

          provide more details about inheritance and type casting。 



          More Inheritance Details 



          In this section; I am going to clearly lay out how inheritance works in Visual Basic。 I will present  

          examples of usage for six scenarios。 Each example is followed by a list explaining the key points  

          to understand。 My goal is to illustrate all of the possible scenarios so that you have a reference  

          to how inheritance works。  



          ■Note  All of the examples demonstrate using methods; but it is possible to apply the same inheritance  

          techniques using properties。 



          Scenario 1: Overloading Base Class Functionality 



              Class Base  

                  Public Sub Method() 

                      Console。WriteLine(〃Base。Method〃) 

                  End Sub 

              End Class 



              Class Derived  

                  Inherits Base 

                  Public Overloads Sub Method()  

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