嗔秤戻幣�哉膵�云利匈嬉蝕湊蛸賜�塋床四衲���萩晦編報炎嘔囚^泡仟 ̄云利匈�《超噌�殻窟�嵌虜隆輓麈觚翹瀘卉韮�仍仍�。� 烏御危列
浪慕利 卦指云慕朕村 厘議慕尺 厘議慕禰 TXT畠云和墮 序秘慕杏 紗秘慕禰

VB2008貫秘壇欺娼宥(PDF鯉塀哂猟井)-及65嫗

梓囚徒貧圭�鮗� ○ 賜 ★ 辛酔堀貧和鍬匈��梓囚徒貧議 Enter 囚辛指欺云慕朕村匈��梓囚徒貧圭�鮗� ● 辛指欺云匈競何��
!!!!隆堋響頼��紗秘慕禰厮宴和肝写偬堋響��




          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 method of CalculateTaxToPay�┌� that stores the taxable  

          ine so the subclass has access to it。 

                The original implementation of CalculateTaxToPay�┌� does not consider a surtax�察�so the  

          derived class must contain that functionality。 Since CalculateTaxToPay�┌� can be overridden  

          without the data member _calculatedTaxable�察�the derived class would need to implement the  

          functionality in the base class to calculate whether or not the surtax applies。 Following is an  

          sample derived class implementation of the tax engine for such a situation�察�stored in a namespace  

          called Surtax�察�to distinguish it from the base functionality。 



          Namespace Surtax 

              Friend Class TaxEngine  

                  Inherits BaseTaxEngine 



                  Public Overrides Function CalculateTaxToPay��ByVal account As ITaxAccount�� _ 

                    As Double 

                      Dim taxToPay As Double = MyBase。CalculateTaxToPay��account�� 

                      If _calculatedTaxable 〃 400 Then 

                          taxToPay = taxToPay �� 10 

                      End If 

                      Return taxToPay 

                  End Function 



                  Public Overrides Function CreateTaxAccount�┌� As ITaxAccount  

                      Throw New Exception�─�The method or operation is not implemented。;�� 

                  End Function 

              End Class 

           End Namespace 



                In the implementation of CalculateTaxToPay�┌��察�we replace the Overridable keyword with  

          Overrides�察�implying that the functionality of TaxEngine replaces the functionality of BaseTaxEngine。   

          Since our fictional country calculates the basic tax similarly to most countries�察�the functionality  

          of BaseTaxEngine。CalculateTaxToPay�┌� can be used。 Thus�察�the first line of TaxEngine。 

          CalculateTaxToPay�┌� is MyBase。CalculateTaxToPay�┌��察�meaning the base class ��BaseTaxEngine��  

          method CalculateTaxToPay�┌� is called。 


´´´´´´´´´´´´´´´´´´´´´´Page 199´´´´´´´´´´´´´´´´´´´´´´´

                              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 177 



      Calling the base class results in calculating a basic tax to pay amount。 We need to figure out  

if a surtax applies�察�and that is where the protected data member _calculatedTaxable es into  

play。 Having called BaseTaxEngine。CalculateTaxToPay�┌��察�the data member _calculatedTaxable is  

assigned and contains the amount that is being taxed。 Thus�察�TaxEngine。CalculateTaxToPay�┌�  

can make a decision if more than 400 currency units have been earned。 And if so�察�then the vari

able taxToPay is incremented with another 10 currency units。 Had _calculatedTaxable not  

existed�察�TaxEngine。CalculateTaxToPay�┌� would have needed to call the base class functionality  

to get the basic tax rate�察�and then recalculate the taxable monies to figure out i
卦指朕村 貧匯匈 和匯匈 指欺競何 壘��0�� 家��0��
隆堋響頼��紗秘慕禰厮宴和肝写偬堋響��
梁椣戻幣�� 梁心弌傍議揖扮窟燕得胎��傍竃徭失議心隈才凪万弌誌育断蛍�輌臆惨軼僑〃�燕慕得珊辛參資誼持蛍才将刮襲潜��範寔亟圻幹慕得 瓜寡追葎娼得辛參資誼寄楚署衛、持蛍才将刮襲潜填��