嗔秤戻幣�哉膵�云利匈嬉蝕湊蛸賜�塋床四衲���萩晦編報炎嘔囚^泡仟 ̄云利匈�《超噌�殻窟�嵌虜隆輓麈觚翹瀘卉韮�仍仍�。� 烏御危列
浪慕利 卦指云慕朕村 厘議慕尺 厘議慕禰 TXT畠云和墮 序秘慕杏 紗秘慕禰

VB2008貫秘壇欺娼宥(PDF鯉塀哂猟井)-及54嫗

梓囚徒貧圭�鮗� ○ 賜 ★ 辛酔堀貧和鍬匈��梓囚徒貧議 Enter 囚辛指欺云慕朕村匈��梓囚徒貧圭�鮗� ● 辛指欺云匈競何��
!!!!隆堋響頼��紗秘慕禰厮宴和肝写偬堋響��




144        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 modified implementation of Oven�察�the data member _temperature is not externally  

           exposed。 And in this situation�察�the role of the data member _temperature is not to represent the  

           temperature of the oven�察�but to act as an upper limit to which the oven should be heated。 The  

           upper limit is assigned using the SetTemperature�┌� method。 To check the temperature of the  

           oven�察�you don¨t retrieve the temperature of the oven�察�but call the AreYouPreheated�┌� method。  

           The caller receives either a True or  False value to indicate whether or not the oven is ready。 

                 The caller of Oven has the responsibility only of setting the upper temperature and asking  

           if the oven is preheated。 From this example�察�it would seem that you don¨t need properties。  

           However�察�you still need properties because Oven in its current form represents an easy´to´use  

           class that can be integrated at the architectural business´logic level。 

                 The challenge of the developer is to bridge the gap between a raw structural class and the  

           exposure of an architectural business´logic´level class。 That challenge will be met when the  

           hotel currency trader and active currency trader are implemented。 

                 Even with these arguments and the distinction between base class and architectural business

           logic´level classes�察�some still naysay properties。 The reason has to do with controlling access。  

                 Suppose you are in a grocery store�察�waiting at the cash register。 The cashier tallies up the  

           items�察�arrives at a total�察�and asks you to pay。 Do you open your purse or wallet and let the cashier  

           get the credit card or cash�拭�Along the same lines�察�why can¨t you just place the cash into the cash  

           register�拭�The answer to these questions is one of trust。 As trustworthy as the cashier and shopper  

           might be�察�we feel better when we know we have the control。  

                 Let¨s go back to an example I mentioned at the beginning of the chapter�此�allowing people  

           to grab into your pockets。 The previously defined Temperature property is allowing someone to  

           grab into your pockets。 You wouldn¨t generally allow it�察�but what if that person were your spouse  

           or your mom�拭�Would you still disallow it�拭�The answer is very different�察�because you probably  

           trust your mom or spouse。 In the same way�察�often state and its exposure are a matter of trust  

           and using the right scope。 



           *Note  In this discussion of properties and object´oriented design�察�my goal is to explain that there is a place  

           and time for both�察�and you should not feel biased toward one approach or the other。 When you design a type  

           that does not reveal its state�察�you are designing a type that fulfills an abstract intention。 When you design a  

           type that reveals its state ��to some extent�� through properties�察�you are designing a type that is used at the  

           lower technical level。 Also keep in mind that sometimes internal state is external state�察�such as the exchange  

           rate example。 You cannot abstract away the exchange rate state because it is a number used in a calculation。 



           Understanding Inheritance and Scope Modifiers 



           At this point�察�the  ExchangeRate property is a mechanical property that will be used by any class  

           that subclasses CurrencyTrader。 So now we need to decide whether access to the property  

           should be restricted。 The answer is that it should be restricted to only those developers who  

           truly understand how to convert currencies。 Access should be restricted to those classes that  

           will subclass CurrencyTrader。 Following is the rewritten version of CurrencyTrader。 


´´´´´´´´´´´´´´´´´´´´´´Page 167´´´´´´´´´´´´´´´´´´´´´´´

                        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 145 



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 

End Class 



     The bolded code highlights three examples of scope access�此�



     Public�此�The type�察�method�察�data member�察�or property can be accessed and referenced by  

     any other type。 In the context of a crowd of people�察�it means anyone can reach into your  

     pocket and inspect your wallet。 



     Private�此�The method�察�data member�察�or property can be accessed and referenced only by  

     the type declaring the method�察�data member�察�or property。 In the context of a crowd of  

     people�察�it means only you can reach into your pocket and inspect your wallet。 



     Protected�此�The method�察�data member�察�or property can be accessed and referenced by the  

     type declaring the method�察�data member�察�or property or by types that subclass the declaring  

     type。 In the context of a crowd of people�察�it means only you and people who you have  

     allowed can reach into your pocket and inspect your wallet。 



     If you happen to declare a type�察�method�察�or property without a scope modifier�察�the default  

is assumed�察�which means public is implied。 Data members are private by default。 The Private  

and Protected modifiers cannot be assigned to a type。 You¨ll learn more about other modifiers  

and details about type scope declarations in the next chapter。 



Using Visual Basic Inheritance to Subclass Another Type 



The rewritten version of CurrencyTrader will cause the test code to break�察�because it uses the  

MustInherit keyword�察�and thus cannot be instantiated directly。 Here is the broken code�此�



Dim cls As CurrencyTrader = New CurrencyTrader�┌� 

cls。ExchangeRate = 123。44 



     The code will not work for two reasons�此�



     o  CurrencyTrader uses the MustInherit keyword and thus cannot be instantiated。 



     o  ExchangeRate is protected and cannot be referenced externally。 



     This broken code puts us in a bind。 Up to this point�察�whenever we tested code�察�we assumed  

all of the pieces that were to be tested could be referenced。 One solution would be to change  

the scope declarations and remove the MustInherit and  Protected keywords。 Yes�察�that solves the  

problem�察�but it is a cop´out。 The better approach is to test CurrencyTrader as it was intended to be  


´´´´´´´´´´´´´´´´´´´´´´Page 168´´´´´´´´´´´´´´´´´´´´´´´

146       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 



          used�此�as a class that is derived from。 So�察�the solution is to use inheritance and create a test class  

          that derives from CurrencyTrader�察�as follows�此�



          Public Class TestCurrencyTrader 

              Inherits CurrencyTrader 

              Public Sub InitializeExchangeRate�┌�  

                  ExchangeRate = 100。0 

              End Sub 

          End Class 



               TestCurrencyTrader is a test class that is added to the test source code。 To inherit from a  

          class�察�you define a class�察�and in the following line�察�use the keyword Inherits。 This line says that  

          the class TestCurrencyTrader is subclassing CurrencyTrader。  

               To expose a method outside the class declaration�察�you use the Public modifier。 

               Inheritance means identifiers that are scoped  Protected or  Public can be referenced in  

          the subclassed type。 For example�察�notice how ExchangeRate
卦指朕村 貧匯匈 和匯匈 指欺競何 壘��0�� 家��0��
隆堋響頼��紗秘慕禰厮宴和肝写偬堋響��
梁椣戻幣�� 梁心弌傍議揖扮窟燕得胎��傍竃徭失議心隈才凪万弌誌育断蛍�輌臆惨軼僑〃�燕慕得珊辛參資誼持蛍才将刮襲潜��範寔亟圻幹慕得 瓜寡追葎娼得辛參資誼寄楚署衛、持蛍才将刮襲潜填��