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

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

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




ideas。 ing up with ideas and then implementing them is part of the development process  

called  test…driven architecture。 

      To start test…driven architecting; you think of the requirements and then e up with a  

general solution。 In the case of our example; the requirement is to create a tax engine to calcu

late ine tax。 Generally speaking; it means figuring out what the total taxable ine is;  

subtracting the total deductions; and applying a tax rate to the remaining sum to calculate the  

total tax。 

      In programming terms; the general ideas are converted into source using Visual Basic  

interfaces。 


…………………………………………………………Page 188……………………………………………………………

166       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 



           Representing Ideas Using Visual Basic Interfaces 



           Think of Visual Basic interfaces as programmatic constructs you can use to jot down ideas。 When  

           you jot down ideas to solve a problem; you usually implement a bread…crumb trail approach— 

           you start with one idea; and then follow it to another idea; which leads to another idea; until  

           you have jotted down all of your ideas。 

                Your first idea is the central idea。 In our tax application; the central idea is the tax engine  

           itself。 The tax engine is used to perform a tax calculation and to pull together all of the other  

           pieces。 The other pieces are some nebulous things that you need to plete the tax engine。 I  

           like to call these other pieces dependencies。 The dependencies are ideas that you need to finish  

           the previous idea; hence the metaphor of following bread crumbs to your solution。 

                Ideas by themselves are designs that solve a particular problem or give an inkling of how  

           to solve the particular problem。 When an idea is converted into source code; it bees a blue

           print that forces the implementation to take a certain shape。 Ideas when coded are Visual Basic  

           interfaces; which cannot be executed by themselves。 From a programmatic point of view; an  

           interface is like a MustInherit base class in that you can reference an interface; but you cannot  

           instantiate an interface。 To get a working idea; you write an implementation; or implement an  

           interface。 

                The following is an example of a Visual Basic interface。 



           Interface IExample 

           End Interface 



                The keyword Interface is associated with an identifier  IExample; and from a syntax perspec

           tive is used like the Class keyword。 You can associate the public scope with an interface。 The  

           interface contains methods and properties that determine the behavior of the classes that  

           implement the interface。 Consider the following source code; which defines an interface with  

           a single method and single property。 



           Interface IExample  

               Sub Method() 

               Property ExampleProperty As Integer 

           End Interface 



                When you define an interface; the included methods and properties do not have an imple

           mentation because you are defining a signature。 A class that implements an interface will play  

           a game called “match the properties and method signatures。”  



           ■Note  Generally speaking; the naming convention used to define a type is verbose and self…explanatory。  

           When I define interfaces; I use identifiers prefixed with a capital I; like IExample。 The capital I is a mon  

           convention used to identify interfaces。 You should use this notation to be consistent with other  code。 



                The following code is a rudimentary implementation of the defined interface。 


…………………………………………………………Page 189……………………………………………………………

                            CH AP T E R   7   ■    L E AR N IN G   AB O U T   CO M P O N E N TS   AN D   C L AS S  H I E R AR C HI E S 167 



Class ExampleImplementation  

    Implements IExample 



    Public Sub Method() Implements IExample。Method 

    End Sub 



    Public Property ExampleProperty() As Integer Implements IExample。ExampleProperty 

        Get 

            Return 0 

        End Get 

        Set(ByVal value As Integer) 

        End Set 

    End Property 

End Class 



      ExampleImplementation implements the interface IExample using an explicit cross…reference  

of the method or property signature。 The cross…reference of interface to implementation in the  

sample method is prefixed with the keyword Implements; and the appropriate interface and  

method。 The method name doesn’t need to be the same as the interface’s method name。 All  

that matters is the signature of the method; or property。  



■Note  Visual Basic and the CLR are single…inheritance models in that a class can subclass only a single class。  

But a Visual Basic class can implement as many interfaces as necessary。 If a class subclasses another class; it uses  

Inherits; if it implements interfaces; it uses Implements。 The class is the first identifier after the colon。  



     When implementing an interface; all of the methods and properties of the interface must be  

implemented in the class。 If they are not; then when the source code is piled; the piler will  

plain about missing methods or properties and consider the implementation as inplete。 

     As inheritance is used; you can think of  IExample as a base class that has no implementa

tion。 The following code illustrates how to instantiate the implementation and assign the  

instance to a variable that has the interface as its type。 



Dim cls As IExample = New ExampleImplementation() 

cls。Method() 



     In the example; the class ExampleImplementation is instantiated and assigned to a variable  

cls of type  IExample。 Th
返回目录 上一页 下一页 回到顶部 0 0
未阅读完?加入书签已便下次继续阅读!
温馨提示: 温看小说的同时发表评论,说出自己的看法和其它小伙伴们分享也不错哦!发表书评还可以获得积分和经验奖励,认真写原创书评 被采纳为精评可以获得大量金币、积分和经验奖励哦!