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

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

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






Class Oven 

    Private _temperature As Integer 



    Public Sub SetTemperature��ByVal temperature As Integer��  

        _temperature = temperature 

        If  _temperature  100。0 Then 

            Throw New Exception�─�100。0 verification failed;�� 

        End If 

    End Sub 

    Public Function AreYouPreHeated�┌� As Boolean 

       ' Check if oven temperature matches prescribed temperature  

       Return False 

    End Function 

End Class 



     The bolded code illustrates a verification much like that used in CurrencyTrader�察�which  

verifies the parameter temperature for a specific value。 While the verification is useful for a  

particular test�察�it is not useful in the big picture。 As the code is written�察�the only valid value of  

temperature is 100。0�察�anything else will generate an exception。 It is definitely not a solution。 

     To get around this problem�察�you could use Visual Basic conditional statements。 Condi

tional statements are special keywords that allow a developer to define whether a piece of  

source code is piled。 Following is an example of source code that includes conditional  

statements。 



Class TestCurrencyTrader 

    Inherits CurrencyTrader 

    Public Sub InitializeExchangeRate�┌� 

        ExchangeRate = 100。0 

#If INTEGRATE_TESTS Then 

        if  ExchangeRate  100。0 Then 

            Throw New Exception�─�100。0 verification failed;�� 

        End If 

#End If 

    End Sub 

End Class 



     The conditional statement always starts with a hash character ��#���察�immediately followed  

by a keyword!If in this example。 Between the #If and #End If is code that is conditionally  

piled。 This is known as a preprocessor directive。 In this example�察�the condition means  

to pile the code contained within the #If and #End If block if the value  INTEGRATE_TESTS  

is true。 

     You can define a pilation identifier like  INTEGRATE_TESTS in the source code or in your  

IDE。 In Visual Basic Express�察�you can assign INTEGRATE_TESTS as follows�此�


´´´´´´´´´´´´´´´´´´´´´´Page 172´´´´´´´´´´´´´´´´´´´´´´´

150       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 



               1。  Right´click your project and select Properties。 



               2。  Select the pile tab。 



               3。  Click the Advanced pile Options button。 



               4。  Add the  INTEGRATE_TESTS value to the Custom Constants text box。 



          Using Partial Classes for Verification 



          Conditional pilation is useful when you want to include or remove code depending on a  

          configuration。 However�察�some programmers will frown upon having conditional pilation  

          code within a function�察�as that would be a maintenance nightmare。 Another solution is to use  

          the Partial class keyword in conjunction with conditional pilation statements。 

               Thus far in all of the examples�察�whenever we defined a class�察�all of the methods and code  

          were declared between the Class/End Class pair。 By using partial classes�察�it is possible to declare  

          a class in multiple places。 When the Visual Basic piler piles the source code�察�the individual  

          class pieces will be assembled into a single class definition。 For our test code�察�we can create an  

          implementation partial class and a conditionally piled test partial class implementation。  

          The following is the modified TestCurrencyTrader class that could be used to test the state  

          without exposing the state。 



          Partial Class TestCurrencyTrader 

              Inherits CurrencyTrader 

              Public Sub InitializeExchangeRate�┌�  

                  ExchangeRate = 100。0 

              End Sub 

          End Class 



          #if  INTEGRATE_TESTS Then 

          Partial Class TestCurrencyTrader  

              Inherits CurrencyTrader 

              Public Sub VerifyExchangeRate��ByVal value As Double�� 

                  If ExchangeRate  value Then 

                      Throw New Exception�─�ExchangeRate verification failed;�� 

                  End If 

              End Sub 

          End Class 

          #End If 



               The keyword  Partial prefixes the Class keyword。 The first implementation of  

          TestCurrencyTrader is an example of not exposing state。 The second implementation of  

          TestCurrencyTrader�察�which is declared in the context of a conditional pilation statement�察 �

          contains the method VerifyExchangeRate�┌�。 This is a verification method that tests the  

          ExchangeRate property for a particular value。 


´´´´´´´´´´´´´´´´´´´´´´Page 173´´´´´´´´´´´´´´´´´´´´´´´

                         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 151 



*Note  You can use partial classes only in the context of a single assembly�察�as the Partial keyword cannot  

be used across assembly boundaries。 When I say ^single assembly�察院�I am referring to the piled pieces of  

 source code illustrated in Chapter 1。 In other words�察�if you define a partial class in a library�察�then all  

pieces of the partial class need to be defined in the library。  



     Partial classes make it simple to separate functionality into various source code files。 This  

example demonstrates using partial classes to manipulate internal state of a class without  

violating the do´not´expose´internal´state rule。 Another use of partial classes is in the context  

of code generators�察�where one source code file contains the custom code�察�and the other source  

code file contains the generator code。  



*Note  Only one part of a partial class needs to be explicitly qualified as Partial。 The piler is smart  

enough to realize that if it sees one part as Partial�察�then all the other parts must also be partial ��even if they  

aren¨t explicitly qualified with the Partial keyword��。 



Finishing the Base Class 



The ExchangeRate property is one of the pieces of shared functionality。 Another piece of shared  

functionality we want to implement is the calculation of the exchange rate。 We¨ll do this with  

ConvertValue�┌� and ConvertValueInverse�┌� methods�察�which convert a currency from one value  

to another using multiplication or division。 The following shows the methods in the pleted  

base class implementation of CurrencyTrader。 



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 

    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 

End Class 


´´´´´´´´´´´´´´´´´´´´´´Page 174´´´´´´´´´´´´´´´´´´´´´´´

152       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 



                The bolded code highlights the methods that convert the currency from one unit to another。  

           Notice that there is no declaration of specific currency units�察�because the base class is a utility  

           class used to help us realize an active trader or hotel trader implementation。 



           *Note  Base class functionality�察�even when appearing trivial�察�is defined to ensure consistency in implemen

           tation。 Without consistency�察�you encounter the problem where one implementation does one thing and another  

           implementation does something pletely different。  



                This pletes our test code。 Now we will implement the active trader and hotel trader 
卦指朕村 貧匯匈 和匯匈 指欺競何 壘��0�� 家��0��
隆堋響頼��紗秘慕禰厮宴和肝写偬堋響��
梁椣戻幣�� 梁心弌傍議揖扮窟燕得胎��傍竃徭失議心隈才凪万弌誌育断蛍�輌臆惨軼僑〃�燕慕得珊辛參資誼持蛍才将刮襲潜��範寔亟圻幹慕得 瓜寡追葎娼得辛參資誼寄楚署衛、持蛍才将刮襲潜填��