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

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

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




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