按键盘上方向键 ← 或 → 可快速上下翻页,按键盘上的 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