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

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

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




         

        Public Property MyTypeProperty() As MyType 

            Get 

                Return _embedded 

            End Get 

            Set (ByVal value As MyType) 

                _embedded = value 

            End Get 

        End Property 

    End Class 



     The type  EmbeddedMyType has a property that references MyType。 If you were to instantiate  

EmbeddedMyType; you would probably also want to instantiate and assign the property MyType。  

You can do that with object initialization; like this: 



        Dim cls As EmbeddedMyType = _ 

            New EmbeddedMyType() With { _ 

                。MyTypeProperty = _ 

                    New MyType() With {。DataMember = 10}} 



The Important Stuff to Remember 



In this chapter; you learned about writing a kernel; using Visual Basic default properties; and  

the implementing enumeration functionality。 The main items to remember are as follows: 



     o A kernel is a ponent…oriented architecture where you are not in control of certain  

       implementations。 ponents make it possible to modularize a development process  

       so that separate teams have their own tasks。 



     o Interfaces are contracts between modules; and you test against the interface; not the  

       implementation。 



     o Placeholder interfaces are used to make it simpler to group object instances。 



     o Default properties are structural; and thus they serve as a tool to help you acplish a  

       task quicker。 



     o Default properties make it possible for your type to behave like an array。 



     o To iterate something that does not support iteration; you add enumeration function

       ality; which implies implementing the IEnumerable and  IEnumerator interfaces。 


…………………………………………………………Page 250……………………………………………………………

228       CH AP T E R   8   ■    L E A R N IN G   AB OU T   CO M P O N E N TO R IE N T E D  AR C HI TE CT U R E 



           Some Things for You to Do 



           The following are some exercises that will help you apply the concepts you learned in this chapter。 



                1。  The LightingController。AddRoomGrouping() method has a mistake。 Write some tests to  

                    find the error; and then fix the code and rerun your tests to verify that the error has  

                    been fixed。 



                2。  The TestInsert() test method is one example of an insertion test; but not all variations  

                    have been tested。 Write another test method that implements the remaining variation(s)  

                    that need to be tested。 



                3。  The declarations of RoomGrouping and Room are not optimal。 Fix the declarations。 



                4。  Implement a general collection class based on the experience of using the class in the  

                    class LightingController。 Hint: look at how the linked list for Room is declared and figure  

                    out a way to abstract that into some general collection class。 



                5。  When the method  LightingController。AddRoom() is called; the method is tested internally  

                    to see if the handle is of type RoomGrouping。 Can you think of a more defensive pro

                    gramming technique to make sure that the code someone else passes to the kernel will  

                    not cause the kernel to fail? Hint: think about the methods to turn on or off the lights  

                    and think of what could go wrong。 


…………………………………………………………Page 251……………………………………………………………

C  H  A  P  T  E  R     9 



■ ■ ■ 



Learning About Lists; Delegates;  

and Lambda Expressions  



One of the most mon pieces of code that you will write is code that manages many object  

instances。 In the previous examples; many object instances were managed using an array。 In  

Chapter 8; you learned that a linked list used in conjunction with a default property could make a  

plain…vanilla object look like a collection。 This chapter introduces the  collection classes;  

which provide an easy way to manage a set of object instances。 Think of a collection object as  

an infinite sack where things can be added; iterated through; and retrieved。  

     The chapter begins with a discussion of how to manage collections。 Then we will look at  

different ways of iterating the data using a delegate。 Finally; we will look at lambda expressions  

and closures to illustrate another way of processing data。 

     The project structure used in this chapter is a single console application。 As we will not be  

building an overall application; but rather a set of sample code snippets; no tests or libraries  

are involved。 



Managing Collections 



When you have a collection; what you actually have is an object that happens to point to many  

other objects。  

     Visual Basic provides collection classes for managing collections。 Visual Basic 2005 intro

duced a different approach to collections; which solved many of the problems that came up  

in earlier Visual Basic versions。 Here; we’ll look at managing collections both before and after  

Visual Basic 2005; which will help you to understand how collections are used。  



Managing a Collection Before Visual Basic 2005 



Before Visual Basic 2005; the main collection classes were stored in the namespace System。 

Collections (and; of course; these classes are still available for you to use)。 The following are  

some of the classes and interfaces in that namespace: 



                                                                                                          229 


…………………………………………………………Page 252……………………………………………………………

230       CH AP T E R   9   ■    L E A R N IN G   AB OU T   L I ST S;   D E L E G A T E S;   A N D   L A M B DA   E X P R E S SI ON S  



               o  ArrayList: A general collection that manages all of the referenced objects using an  

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