小牛电子书 > 其他电子书 > VB2008从入门到精通(PDF格式英文版) >

第60章

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

小说: VB2008从入门到精通(PDF格式英文版) 字数: 每页3500字

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




          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 it as using a calculator and your main concern is getting  

        the results of the calculations。 



     o  Properties should be used instead of exposing data members publicly。 



     o  Many developers dislike properties because they promote bad programming practices。  

        You need to get beyond those ments and think in terms of the credit card and the  

        cashier。 Properties have their time and place。 



     o  Classes should; in general; not expose their state。 To get around exposing state; create  

        methods that implement the general intent of the purpose of the class。 



     o  Inheritance is a fundamental part of Visual Basic and you will need to know how to use it。  



     o  Overriding is when you keep the interface the same and change behavior。  



     o  Overloading is when you derive a class and define an identifier that happens to be the  

        same as some base class identifier。 Your overloaded identifier may change behavior and  

        usage。 



     o  Conditional pilation statements can be effective when you want to include code in  

        certain configurations。 



     o  Partial classes are very useful when you want to separate functionality that serves a  

        specific purpose。 In this chapter’s example; it meant having the ability to add test code  

        without violating the rule of exposing state。 


…………………………………………………………Page 184……………………………………………………………

162       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 



           Some Things for You to Do 



           To apply what you’ve learned in this chapter; you can do the following exercises。 



                1。 The code for HotelCurrencyTrader。ConvertTo() and HotelCurrencyTrader。 

                   ConvertFrom() contains a potentially big bug。 Identify the bug and fix it。 



                2。 In the examples; the exchange rate is assigned by the caller of the HotelCurrencyTrader  

                   and ActiveCurrencyTrader。 Implement functionality such that the exchange rate can be  

                   retrieved dynamically。  



                3。 The currency ExchangeRate property is a Double。 Implement ExchangeRate using the  

                   Decimal type。 



                4。 Write test code for ActiveCurrencyTrader and HotelCurrencyTrader。 


…………………………………………………………Page 185……………………………………………………………

C  H  A  P  T  E  R     7 



■ ■ ■ 



Learning About ponents  

and Class Hierarchies 



The previous chapter covered the basics of object…oriented programming。 You learned how  

one class can subclass another class; thus building a hierarchy。 Using a class hierarchy; classes  

can share functionality。 

     This chapter will focus on the details of class hierarchies; including how to extend them so  

that derived classes can specialize shared functionality。 To demonstrate these concepts; we will  

build a tax application。 This type of application is a good example of using class hierarchies;  

because the general idea of paying taxes is identical; regardless of the country; but the details  

are different。 

     From a technical perspective; we’ll cover the following topics: 



    o  Interfaces; which are the basis of software ponents 



    o  Details of overloading and overriding methods 



    o  How and when to use factories 



     We’ll begin with some basic tax concepts that relate to our sample application。 



Understanding Some Basic Tax Concepts 



Taxes and death are two sure things in life。 Regardless of where you live; the government has  

managed to create a system where you pay taxes (for the good of the people; of course)。 As a  

result; people hate paying taxes; and many figure out creative ways to not pay taxes。 Honest people  

who would normally not break the law find clever ways of bending the rules in their favor。 In  

some countries; they even expect that you will cheat; and will calculate your “true” tax figure。 

     Taxes seem like a plex topic; but they are rather simple。 The volumes of rules associ

ated with taxes are what make them plicated。 Taxes boil down to the fact that if you buy or  

do X; then you can deduct or charge Y。 Sometimes; the taxes make sense; sometimes they don’t。  

Overall though; the tax rules are similar in logic; and that means a hierarchy of classes can  

be designed。 

     Taxable ine is the sum of monies that you earned for which a tax rate is applied。 The  

taxable ine may or may not be equal to the sum of money that you earned during a year。  



                                                                                                            163 


…………………………………………………………Page 186……………………………………………………………

164       CH AP T E R   7   ■    L E A R N IN G   AB OU T   CO M P O N E N TS   AN D  C L AS S  H I E R AR C H IE S 



           Taxable ine can be less due to deductions and ine deferment。 And in some cases;  

           taxable ine can be more due to deferment。 

                Deductions are items that can be charged against taxable ine。 People mistakenly believe  

           that deductions can be charged directly against the tax payable amount。 Or they mistakenly  

           believe a tax deduction is always an advantage。 For example; let’s say you have 1;000 of tax  

           deductions。 If your ine is 2;000;000; then the deduction is not worth much; since your  

           taxable ine is still 1;999;000。 

                Partial taxation is when a partial amount of earned monies is added to the total taxable  

           ine。 Often; partial taxation is applied to capital gains。  Capital gains refer to when you have  

           bought something like a property and then sold it at a higher price than you paid for it。 The  

           difference between the two prices is subje

返回目录 上一页 下一页 回到顶部 2 2

你可能喜欢的