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

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

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




HotelCurrencyTrader。 



Implementing HotelCurrencyTrader 



The difference in features between HotelCurrencyTrader and ActiveCurrencyTrader is the  

existence of a sizable spread。 



Adding a Constructor to HotelCurrencyTrader  



Again; we’ll begin by adding a constructor。 The HotelCurrencyTrader constructor needs an  

additional parameter value referencing a spread。 Following is the code for the  

HotelCurrencyTrader class; including its constructor。 



Public Class HotelCurrencyTrader 

    Inherits CurrencyTrader 

    Private _fromCurrency As String 

    Private _toCurrency As String 

    Private _spread As Double 



    Public Sub New (ByVal currExchange As Double; ByVal spread As Double; 

        ByVal fromCurrency As String; ByVal toCurrency As String)  

        ExchangeRate = currExchange 

        _fromCurrency = fromCurrency 

        _toCurrency = toCurrency 

        _spread = spread 

    End Sub 



       Public ReadOnly Property FromCurrency() As String 

        Get 

            Return _fromCurrency 

        End Get 

    End Property 

    Public ReadOnly Property ToCurrency() As String 

        Get  

            Return _toCurrency 

        End Get 

    End Property 

End Class 


…………………………………………………………Page 178……………………………………………………………

156      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 



               In the constructor of HotelCurrencyTrader; you can see the additional parameter spread。  

          The spread parameter is assigned to the data member _spread and represents a calculation  

          that modifies the exchange rate。 



          Adding the Conversion Methods to HotelCurrencyTrader 



          Remember in the previous section how the methods ConvertTo() and ConvertFrom() seemed  

          to add no extra value。 For HotelCurrencyTrader; they will add value and illustrate why it is useful  

          to create indirect exposure。 Calculating what people will receive for their currency depends on  

          the exchange rate; and in the case of the hotel; the exchange rate has a spread。 As explained  

          earlier in this chapter; this means you will never get as much as you hoped and will always pay  

          more than you expected。  

               The following is the source code for the HotelCurrencyTrader implementations of  

          ConvertTo() and ConvertFrom()。 



           Public Class HotelCurrencyTrader 

              Inherits CurrencyTrader 

              Private _fromCurrency As String 

              Private _toCurrency As String 

              Private _spread As Double 



              Public Sub New (ByVal currExchange As Double; ByVal spread As Double; 

                  ByVal fromCurrency As String; ByVal toCurrency As String)  

                  ExchangeRate = currExchange 

                  _fromCurrency = fromCurrency 

                  _toCurrency = toCurrency 

                  _spread = spread 

              End Sub 



                 Public ReadOnly Property FromCurrency() As String 

                  Get 

                      Return _fromCurrency 

                  End Get 

              End Property 

              Public ReadOnly Property ToCurrency() As String 

                  Get 

                      Return _toCurrency 

                  End Get 

              End Property 



              Public Function ConvertTo(ByVal value As Double) As Double 

                  Dim realExchange As Double = ExchangeRate 

                  ExchangeRate = realExchange _spread 

                  Dim retval As Double = ConvertValue(value) 

                  ExchangeRate = realExchange 

                  Return retval 

              End Function 


…………………………………………………………Page 179……………………………………………………………

                        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 157 



    Public Function ConvertFrom(ByVal value As Double) As Double 

        Dim realExchange As Double = ExchangeRate 

        ExchangeRate = realExchange + _spread 

        Dim retval As Double = ConvertValueInverse(value) 

        ExchangeRate = realExchange 

        Return retval 

    End Function 

End Class 



     The ConvertTo() and ConvertFrom() methods have the added logic in that the spread will  

be added or subtracted from the exchange rate。 The methods read the current exchange rate;  

assign it to a temporary variable; define a new exchange rate taking into account the spread;  

calculate the currency; and then reassign the exchange rate。 

     The ConvertTo() and ConvertFrom() methods swap values to achieve the desired calcula

tions。 There is absolutely nothing wrong with doing this; and you will often swap values in your  

own code。 What is important is to restrict access on what classes can do this。 Because ExchangeRate  

is a property with protected access; it means only those classes that subclass can assign and  

change the value of ExchangeRate。 And this implies that the class doing the subclassing knows  

what it is doing to the data。 It is a valid assumption and one that you can use to your benefit。  

The caller is not aware of this swapping; because HotelCurrencyTrader is taking advantage of  

the object…oriented technique to not expose the state of the type。 

     That’s it—we’re finished with the currency exchange application。 For the remainder of  

this chapter; I will fill in some extra details that are important。 



Learning More About Preprocessor Directives;  

Properties; and the MustOverride Keyword 



Some details about preprocessor directives; properties; and the MustOverride keyword that did  

not e up in this chapter’s example are worth mentioning。 These are things you should  

know about when writing your own code。 



More Preprocessor Directive Details 



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