梓囚徒貧圭�鮗� ○ 賜 ★ 辛酔堀貧和鍬匈��梓囚徒貧議 Enter 囚辛指欺云慕朕村匈��梓囚徒貧圭�鮗� ● 辛指欺云匈競何��
!!!!隆堋響頼��紗秘慕禰厮宴和肝写偬堋響��
the subclassed type。 For example�察�notice how ExchangeRate seems to be all on its own�察�without
any object reference。 The lonely reference to ExchangeRate is fine�察�because the base class
CurrencyTrader has an identifier with that name。 The property ExchangeRate can be referenced
locally because of its protected scope The identifier ExchangeRate has an implied Me reference
��Me。ExchangeRate���察�so it is not necessary to add that�察�unless you have multiple identifiers with
the same name or you want to explicitly reference a certain identifier。
And now the tests will not test CurrencyTrader�察�but will test TestCurrencyTrader�察�which
should contain some verification code to make sure everything works correctly。
Understanding Private�察�Protected�察�and Public Scope
Let¨s dig a bit deeper into how the three types of scope work。 To start�察�consider the CurrencyTrader
implementation�此�
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 169´´´´´´´´´´´´´´´´´´´´´´´
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 147
The new class ActiveCurrencyTrader subclasses CurrencyTrader and is written as follows�此�
Public Class ActiveCurrencyTrader
Inherits CurrencyTrader
End Class
The data member CurrencyTrader。_exchangeRate is declared as Private�察�and thus can be
referenced only in CurrencyTrader。 If _exchangeRate had no scope declaration�察�private scope
would be implied。 For example�察�the following code would not pile。
Public Class ActiveCurrencyTrader
Inherits CurrencyTrader
Public Sub Method�┌�
_exchangeRate = 100。0
End Sub
End Class
The ActiveCurrencyTrader class is not part of CurrencyTrader�察�and thus _exchangeRate
cannot be referenced。
Considering the ActiveCurrencyTrader class�察 �ExchangeRate�察�which has been declared as
Protected�察�could be referenced as follows�此�
Public Class ActiveCurrencyTrader
Inherits CurrencyTrader
Public Sub Method�┌�
ExchangeRate = 100。0
End Sub
End Class
Protected scope means only those classes that derive from a class ��and the class itself�� can
view the methods�察�properties�察�or data members。 How many times and levels a class subclasses
another class is not important。
Public scope is the loosest and simplest of all scopes。 You use Public whenever you want
to expose functionality that other classes or derived classes want to reference。
Here are some guidelines for using each scope�此�
Private scope�此�You will use Private for most data member declarations because data member
declarations imply the state of an object。 Sometimes�察�when developing algorithms�察�you will
break apart the logic into several methods。 The broken´apart methods are used to solve a
problem�察�and thus should be used only in the context of the class�察�implying the methods
need to be declared using Private。
Protected scope�此�You will use Protected whenever you want to enforce an inheritance
architecture。 Very often�察�Protected and MustInherit go hand in hand�察�as both are intended
for inheritance。 The main objective behind Protected is to offer a derived class access to
the private state of a parent class�察�or to offer reusable functionality that should be used
only by knowledgeable developers who are creating the subclasses。
´´´´´´´´´´´´´´´´´´´´´´Page 170´´´´´´´´´´´´´´´´´´´´´´´
148 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
Public scope�此�As a rule�察�think carefully before using Public。 You will use public scope for the
most part�察�but it is also the scope that can cause the most problems。 For example�察�once you
declare something as Public�察�trying to later change the scope could wreak havoc in the
code that uses the class。 It might be harder to develop using the other scopes�察�but you will
have code that will have fewer maintenance issues。 It all es down to which methods
and properties you want to expose to the outside world。
Handling Verification
To run the test class TestCurrencyTrader�察�the following code is used。
Dim cls As TestCurrencyTrader = New TestCurrencyTrader�┌�
cls。InitializeExchangeRate�┌�
The modified test code entails instantiating TestCurrencyTrader and then calling the method
InitializeExchangeRate�┌�。 But is this a test�拭�After all�察�the InitializeExchangeRate�┌� method
doesn¨t have a parameter or return value。 Think of it as sending a letter via mail。 You don¨t
know if the letter will arrive�察�but it probably will。 Tests that are probably passed are a really
bad idea。
We need to move the verification code from the test routine to the TestCurrencyTrader
class�察�like this�此�
Class TestCurrencyTrader
Inherits CurrencyTrader
Public Sub InitializeExchangeRate�┌�
ExchangeRate = 100。0
If ExchangeRate 100。0 Then
Throw New Exception�─�100。0 verification failed;��
End If
End Sub
End Class
The bolded code illustrates the verification code used to ensure that the value assigned to
ExchangeRate is the same one that is retrieved。
*Note The tests we¨re using are being more plicated�察�and you may wonder�察 �Why do it that way�拭院 �
For this book�察�we are writing tests and creating our own testing framework。 Normally you would not do that。
You would use a testing framework such as NUnit �─�http��//nunit。org�� or the Microsoft Visual Studio
Professional tools to help create the tests。 But here I want to demonstrate how to use Visual Basic�察�not a
testing tool。 By learning how to write the tests from the ground up�察�you will understand what to expect from
testing frameworks。
Using Conditional Statements
Having verification code within a class is acceptable in the context of the test class
TestCurrencyTrader。 However�察�the problem of testability is still present in those classes that do
not expose their state。
´´´´´´´´´´´´´´´´´´´´´´Page 171´´´´´´´´´´´´´´´´´´´´´´´
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 149
To understand the problem�察�let¨s return to the code for preheating the oven that you saw
earlier in the section about the problems with properties。 Imagine rewriting Oven to include a
verification test�察�like this�此�
Class Oven
Private _temperature As Integer
Public Sub SetTemperature��ByVal temperature