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

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

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




              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  

           ponents of the currency exchange application。 



           Writing the Active Trader and Hotel Trader  

           Currency Converters 



           With the TestCurrencyTrader test solution pleted; it’s time to turn our attention to the  

           CurrencyTrader solution。 As mentioned earlier; this consists of the active trader and hotel  

           trader currency converter ponents。 Here; you’ll see more clearly what it means to use  

           inheritance。 



           Implementing ActiveCurrencyTrader 



           The ActiveCurrencyTrader class implements the logic of the active currency trader。 To begin;  

           we’ll add its constructor。  



           Adding a Constructor to ActiveCurrencyTrader  



           To give ActiveCurrencyTrader some default state; we use a constructor。 However; the constructor  

           will serve another purpose; in that any class that instantiates ActiveCurrencyTrader will consider  

           the instance as immutable。 Immutable means that once data has been assigned to the instance; it  

           cannot be altered。 In other words; it is unchangeable。 



           ■Note  The String type is immutable because once a string variable has been assigned; it cannot be  

           changed。 Take a look at the methods associated with String; and you will see nothing that allows you to  

           modify the contents。 An immutable type is good because it allows you to implement a set…it…and…forget…it  

           object; and it prevents other classes from accidentally changing the contents。 Overall; an immutable type is  

           robust and predictable (though they are the exception; not the rule; most types allow you to modify state)。  



                The following shows the constructor code added to ActiveCurrencyTrader。 


…………………………………………………………Page 175……………………………………………………………

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