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

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

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