嗔秤戻幣�哉膵�云利匈嬉蝕湊蛸賜�塋床四衲���萩晦編報炎嘔囚^泡仟 ̄云利匈��
源平慎弌傍利 卦指云慕朕村 紗秘慕禰 厘議慕尺 厘議慕禰 TXT畠云和墮 〆辺茄欺厘議箝誓匂〇

VB2008貫秘壇欺娼宥(PDF鯉塀哂猟井)-及32何蛍

酔楯荷恬: 梓囚徒貧圭�鮗� ○ 賜 ★ 辛酔堀貧和鍬匈 梓囚徒貧議 Enter 囚辛指欺云慕朕村匈 梓囚徒貧圭�鮗� ● 辛指欺云匈競何! 泌惚云慕短嗤堋響頼���誅卒亮茂�俊彭堋響��辛聞喘貧圭 "辺茄欺厘議箝誓匂" 孔嬬 才 "紗秘慕禰" 孔嬬��





           #Const ACTIVATE_1 = True 



           Namespace TestDefine 

               Class Example 

           #If ACTIVATE_1 Then 

                   Private _dataMember As Integer 

           #ElseIf Not NO_ACTIVATE_10 Then 

                   Private _dataMember3 As Integer 

           #Else 

                   Private _defaultValue As Integer 

           #End If 

               End Class 

           End Namespace 



                In general�察�you will use preprocessor directives when you are building debug mode or  

           release mode code。 Debug mode means that the code is piled such that it can be debugged  

           and analyzed using pretty symbols。 Release mode means when the code is piled�察�the code  

           can still be debugged�察�but the pretty symbols are missing。  



           *Note  Visual Basic Express does not give you the flexibility to choose debug mode or release mode for  

           development。 To get this feature�察�you need the full version of Visual Studio。 



                Think of the pretty symbols as milestones on the road。 Every time your code executes in  

           debug mode�察�the code is saying�察 �You are now calling this method in this source code file�察院�or  

           ^Oops�察�that property code in the source code file is not working properly。 ̄ In release mode�察�the  

           pretty symbols are more like road signs saying�察 �Place X y Miles/Kilometers。 ̄ The signs help  


´´´´´´´´´´´´´´´´´´´´´´Page 181´´´´´´´´´´´´´´´´´´´´´´´

                       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 159 



guide you�察�but the information you get from them is very limited�察�that is�察�you don¨t know which  

towns you¨re passing。 



More Property Scope Details 



In this chapter¨s examples�察�the Get and Set parts of the properties always had the same scope。  

However�察�properties can have mixed scope�察�that is�察�the Get and Set parts can have different  

scopes。 Scope splitting works only if you specify both the Get and Set parts of the property。 The  

idea behind splitting the scope is to enable the implementation of the logic where classes in the  

inheritance chain are allowed to assign a property�察�and classes external to the inheritance  

chain are allowed only to read the property。 The following is an example where a property is  

declared as Public and the assignment of the property is  Protected。 



Class PropertyScopeExample 

    Private _value As Integer 



    Public Property Value�┌� As Integer 

        Protected Set��ByVal value As Integer��  

            _value = value 

        End Set 

        Get 

            Return _value 

        End Get 

    End Property 

End Class 



The MustOverride Keyword 



In this chapter¨s example�察�the keyword MustInherit was used to declare a class that could be  

referenced�察�but not instantiated。 It is also possible to define methods that need to be imple

mented。 In Visual Basic�察�to require a derived class to implement the method�察�you use MustOverride。  

The idea behind declaring methods that need to be overridden is that it allows a developer to  

define an intention in a base class that can then be implemented in the subclass。  

     In the implementation of HotelCurrencyTrader and ActiveCurrencyTrader�察�two methods  

were defined�此�ConvertTo�┌� and ConvertFrom�┌�。 A developer could e to the conclusion that  

the methods are mon to both classes�察�and thus they could be defined in the base class  

CurrencyTrader。 That is not a bad idea。 Let¨s revisit the CurrencyTrader class and see how these  

two methods could be added to CurrencyTrader as MustOverride methods。 



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 


´´´´´´´´´´´´´´´´´´´´´´Page 182´´´´´´´´´´´´´´´´´´´´´´´

160       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 



              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 

              Public MustOverride Function ConvertTo��ByVal value As Double�� As Double 

              Public MustOverride Function ConvertFrom��ByVal value As Double�� As Double 

          End Class 



               A MustOverride method is declared without an implementation。 Any class that subclasses  

          CurrencyTrader is required to implement ConvertTo�┌� and ConvertFrom�┌�。 In the case of  

          HotelCurrencyTrader and ActiveCurrencyTrader�察�this is not a problem�察�because the methods  

          are already implemented。 However�察�the methods need to be changed slightly�察�as follows�此�



              Public Overrides Function ConvertTo��ByVal value As Double�� As Double 

                  ' 。 。 。  

              End Function 

              Public Overrides Function ConvertFrom��ByVal value As Double�� As Double 

                   ' 。 。 。 

              End Function 



               The slight change is the addition of the keyword Overrides to indicate that the ConvertTo�┌�  

          and ConvertFrom�┌� methods in HotelCurrencyTrader and ActiveCurrencyTrader override the  

          functionality in CurrencyTrader。  

               While you have seen all of the technical aspects�察�the bigger question is why you would do  

          this in the first place。 Let¨s go back to the implementation of ActiveCurrencyTrader�察�which has  

          no MustOverride method implementations。 To use the class and the method ConvertTo�┌��察�you  

          would write the following code。 



          Dim cls As ActiveCurrencyTrader = New ActiveCurrencyTrader��1。46�察 �USD;�察 �EUR;�� 

          Dim converted As Double = cls。ConvertTo��100。0�� 



               Imagine the situation where the values to convert to and from are text。 To keep things general�察 �

          you would write the following code ��which takes the value from a text box in a Windows form��。 



          Public Function ConvertToTextField��ByVal cls As ActiveCurrencyTrader�� As Double 

              Return cls。ConvertTo��Double。Parse��text1。Text���� 

          End Function 



               The implementation of ConvertToTextField�┌� makes one major mistake in that it assumes  

          that you will always be converting a currency using the ActiveCurrencyTrader implementation。  

          If you wanted to use the class HotelCurrencyTrader�察�you would need to implement another  

          method with a parameter of type HotelCurrencyTrader。 

               This problem is a classical polymorphism issue and is solved by using MustOverride methods  

          ��as you¨ll see in Chapter 7�察�the Overridable keyword also allows polymorphism��。 Consider the  

          following rewritten ConvertToTextField�┌� method。 


´´´´´´´´´´´´´´´´´´´´´´Page 183´´´´´´´´´´´´´´´´´´´´´´´

                         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 161 



Public Function ConvertToTextField��ByVal cls As CurrencyTrader�� As Double 

    Return cls。ConvertTo��Double。Parse��text1。Text���� 

End Function 



     This implementation of ConvertToTextField�┌� uses the CurrencyTrader base class�察�and  

since ConvertTo�┌� and ConvertFrom�┌� are declared�察�they can be referenced。 

     How you call ConvertToTextField�┌� and how you instantiate HotelCurrencyTrader or  

ActiveCurrencyTrader will not be covered in this chapter�察�because polymorphism is the subject  

of the next chapter。 Just keep what you¨ve learned about the MustInherit and MustOverride  

keywords in mind as you read that chapter�察�which covers concepts such as interfaces�察�because  

you can do the same types of things with them。  



The Important Stuff to Remember 



In this chapter�察�you learned some of the basics of object´oriented programming。 Here are the  

key points to remember�此�



     o  Your code will be posed of structural ��or base class functionality�� and architectural  

        business´related functionality。 



     o  Base class functionality is focused on a particular problem。 Base class functionality  

        requires specific knowledge of the field。 Think of it as implementing a calculator where  

        your main concern is ensuring that the calculations are correct。 



     o  Architectural business´related functionality is higher´level functionality where general  

        business knowledge is needed。 The idea is to take the base classes and use them to solve  

        a business problem。 Think of it as using a calculator and your main concern is getting  

        the results of the calculations。 



     o  Properties should be used instead of exposing data members publicly。 



     o  Many developers dislike properties because they promote bad programming practices。  

        You need to get beyond those ments and think in terms of the credit card and the  

        cashier。 Properties have their time and place。 



     o  Classes should�察�in general�察�not expose their state。 To get around exposing state�察�create  

        methods that implement the general intent of the purpose of the class。 



     o  Inheritance is a fundamental part of Visual Basic and you will need to know how to use it。  



     o  Overriding is when you keep the interface the same and change behavior。  



     o  Overloading is when you derive a class and define an identifier that happens to be the  

        same as some base class identifier。 Your overloaded identifier may change behavior and  

        usage。 



     o  Conditional pilation statements can be effective when you want to include code in  

        certain configurations。 



     o  Partial classes are very useful when you want to separate functionality that serves a  

        specific purpose。 In this chapter¨s example�察�it meant having the ability to add test code  

        without violating the rule of exposing state。 


´´´´´´´´´´´´´´´´´´´´´´Page 184´´´´´´´´´´´´´´´´´´´´´´´

162       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 



           Some Things for You to Do 



           To apply what you¨ve learned in this chapter�察�you can do the following exercises。 



                1。 The code for HotelCurrencyTrader。ConvertTo�┌� and HotelCurrencyTrader。 

                   ConvertFrom�┌� contains a potentially big bug。 Identify the bug and fix it。 



                2。 In the examples�察�the exchange rate is assigned by the caller of the HotelCurrencyTrader  

                   and ActiveCurrencyTrader。 Implement functionality such that the exchange rate can be  

                   retrieved dynamically。  



                3。 The currency ExchangeRate property is a Double。 Implement ExchangeRate using the  

                   Decimal type。 



                4。 Write test code for ActiveCurrencyTrader and HotelCurrencyTrader。 


´´´´´´´´´´´´´´´´´´´´´´Page 185´´´´´´´´´´´´´´´´´´´´´´´

C  H  A  P  T  E  R     7 



* * * 



Learning About ponents  

and Class Hierarchies 



The previous chapter covered the basics of object´oriented programming。 You learned how  

one class can subclass another class�察�thus building a hierarchy。 Using a class hierarchy�察�classes  

can share functionality。 

     This chapter will focus on the details of class hierarchies�察�including how to extend them so  

that derived classes can specialize shared functionality。 To demonstrate these concepts�察�we will  

build a tax application。 This type of application is a good example of using class hierarchies�察 �

because the general idea of paying taxes is identical�察�regardless of the country�察�but the details  

are different。 

     From a technical perspective�察�we¨ll cover the following topics�此�



    o  Interfaces�察�which are the basis of software ponents 



    o  Details of overloading and overriding methods 



    o  How and when to use factories 



     We¨ll begin with some basic tax concepts that relate to our sample application。 



Understanding Some Basic Tax Concepts 



Taxes and death are two sure things in life。 Regardless of where you live�察�the government has  

managed to create a system where you pay taxes ��for the good of the people�察�of course��。 As a  

result�察�people hate paying taxes�察�and many figure out creative ways to not pay taxes。 Honest people  

who would normally not break the law find clever ways of bending the rules in their favor。 In  

some countries�察�they even expect that you will cheat�察�and will calculate your ^true ̄ tax figure。 

     Taxes seem like a plex topic�察�but they are rather simple。 The volumes of rules associ

ated with taxes are what make them plicated。 Taxes boil down to the fact that if you buy or  

do X�察�then you can deduct or charge Y。 Sometimes�察�the taxes make sense�察�sometimes they don¨t。  

Overall though�察�the tax rules are similar in logic�察�and that means a hierarchy of classes can  

be designed。 

     Taxable ine is the sum of monies that you earned for which a tax rate is applied。 The  

taxable ine may or may not be equal to the sum of money that you earned during a year。  



                                                                                                            163 


´´´´´´´´´´´´´´´´´´´´´´Page 186´´´´´´´´´´´´´´´´´´´´´´´

164       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 



           Taxable ine can be less due to deductions and ine deferment。 And in some cases�察 �

           taxable ine can be more due to deferment。 

                Deductions are items that can be charged against taxable ine。 People mistakenly believe  

           that deductions can be charged directly against the tax payable amount。 Or they mistakenly  

           believe a tax deduction is always an advantage。 For example�察�let¨s say you have 1��000 of tax  

           deductions。 If your ine is 2��000��000�察�then the deduction is not worth much�察�since your  

           taxable ine is still 1��999��000。 

                Partial taxation is when a partial amount of earned monies is added to the total taxable  

           ine。 Often�察�partial taxation is applied to capital gains。  Capital gains refer to when you have  

           bought something like a property and then sold it at a higher price than you paid for it。 The  

           diffe
卦指朕村 貧匯匈 和匯匈 指欺競何 壘��16�� 家��13��
酔楯荷恬: 梓囚徒貧圭�鮗� ○ 賜 ★ 辛酔堀貧和鍬匈 梓囚徒貧議 Enter 囚辛指欺云慕朕村匈 梓囚徒貧圭�鮗� ● 辛指欺云匈競何!
梁椣戻幣�� 梁心弌傍議揖扮窟燕得胎��傍竃徭失議心隈才凪万弌誌育断蛍�輌臆惨軼僑〃�燕慕得珊辛參資誼持蛍才将刮襲潜��範寔亟圻幹慕得 瓜寡追葎娼得辛參資誼寄楚署衛、持蛍才将刮襲潜填��