BCL(Base Class Library,涕淌注)现在提供了System.Collections.Generic的命名空间,其中包括了不少定义了范型集合的类。之所以称之为“泛型”,是因为在定义的时候,你只是为其包含的对象指派了一个类型占位符,而不是将它定义为某个确定的类型。你只有在创建该集合的某个实例时,才会给它存储的对象指定一个特定的类型。
泛型极大地节省了我们的时间。如果你曾经创建过自定义集合,你就会体会到个中要添加多少的代码了。Visual Basic中的泛型可以让你只用一行的代码量,就创建出一个自定义集合的匹配物。你再也不必费神为你想存储的每种类型的对象创建各自独立的集合了。你仅需提供在你初始化一个泛型集合时你喜欢的某个类型。
想要了解泛型是如何工作的,最简单的方式是一起来看一个例子。假设你有一个简易的Customer类,它包含了两个公开的属性(为了简约,在下面被定义成了公共变量): Public Class Customer Public Name As String Public CreditLimit As Decimal Public Sub New(ByVal CustomerName As String, ByVal CustCreditLimit As Decimal) Name = CustomerName CreditLimit = CustCreditLimit End Sub End Class
你可以像下面那样声明一系列的Customers,其中使用到了泛型集合的List类: Dim Customers As New System.Collections.Generic.List(Of Customer)
就仅仅这么一行代码,你就已经声明了一个仅能存储Customer类型的强类型列表,并且得到了关于其包含的Customer对象的完整的智能感知。你也同样可以创建一系列的Product或是Order对象,高效率地创建一套自定义集合,而无须为所有类型填写各自的代码。你可以像下面这样写: Dim custA As New Customer("CustA", 1000) Customers.Add(custA) Dim custB As New Customer("CustB", 2000) Customers.Add(custB) For Each c As Customer In Customers MessageBox.Show("Customer: " & c.Name & ", limit: " & c.CreditLimit) Next ''''编译错误:Product类型无法转换成Customer类型 Dim prodA As New Product("ProdA", CDec(29.95)) Customers.Add(prodA)
泛型集合的执行效率也毫不逊色,因为存储的对象是强类型的,而不是内在地被指定为Object类型。
在System.Collections.Generic命名空间下,还能找到其他的范型集合。譬如,Dictionary(Of K, V)集合允许你为关键字和值限定类型。LinkedList(Of T)及Stack(Of T)泛型集合的作用方式和普通的链表和堆栈无甚差别,只是你可以指定它们要存储的对象类型而已。
你可以使用新的泛型声明句法来创建你自己的泛型。我们来看看一个Comparer类,这个类可以用来比较两个不同类型的对象: Public Class Comparer(Of itemType) ''''... End Class
你可以通过用逗号隔开的列表方式定义多重类型的占位符。你也可以定义一个约束条件来限制哪些类能在泛型初始化后提供给该泛型。 Public Class Comparer(Of itemType As IComparable) ''''... End Class
这个约束条件确保了Comparer(Of T)的实例只能通过那些实现了IComparable接口的类创建。多重约束条件也一样适用于类的声明。
作为一个示例,我们再来看一下这个扩充了的、实现了IComparable接口的Customer类: Public Class Customer Implements IComparable
Public Name As String Public CreditLimit As Decimal Public Sub New(ByVal CustomerName As String, ByVal CustCreditLimit As Decimal) Name = CustomerName CreditLimit = CustCreditLimit End Sub
Public Function CompareTo(ByVal obj As Object) As Integer _ Implements IComparable.CompareTo Dim c As Customer = CType(obj, Customer) If CreditLimit > c.CreditLimit Then Return 1 If CreditLimit < c.CreditLimit Then Return -1 Return 0 End Function
End Class
类似地,Product类也可以被实现,不过CompareTo函数比较的是产品的价格而不是客户的存款限额: Public Class Product Implements IComparable Public Name As String Public Price As Decimal Public Sub New(...)... Public Function CompareTo(...)... End Class
Comparer类提供了泛型比较操作: Public Class Comparer(Of itemType As IComparable) Public Function GetLargest(ByVal Item1 As itemType, _ ByVal Item2 As itemType) As itemType Dim i As Integer = Item1.CompareTo(Item2) If i > 0 Then Return Item1 If i < 0 Then Return Item2 Return Nothing End Function End Class
你现在可以用不同类型的对象来实例化Comparers(这里用复数没有别的意思,只是说你可以实例化多个Comparer实例,涕淌注)了: Dim pc As New Comparer(Of Product) Dim prod1 As New Product("LittleOne", 10) Dim prod2 As New Product("BigOne", 100) Dim lp As Product = pc.GetLargest(prod1, prod2) MessageBox.Show("The more expensive product is: " & lp.Name) Dim cc As New Comparer(Of Customer) Dim cust1 As New Customer("SmallCo", 1000) Dim cust2 As New Customer("LargeCo", 5000) Dim lc As Customer = cc.GetLargest(cust1, cust2) MessageBox.Show("The customer with a higher limit is: " & lc.Name)
若是没有了泛型,你就不得不为任何一种你想用来比较的对象类型分别定义一个比较类(比如说,ProductComparer和OrderComparer)。
在不少常见的场合,泛型都在很大程度上减轻了你的代码负担。想象一下吧,当你有着式样繁多的类,而它们仅仅在操作的对象类型上有所差别时,使用泛型会是多么利便的事。
@以下附上原文供大家参考@
Generics
The BCL now provides the System.Collections.Generic namespace, which contains several classes defining generic collections. They''''re called generic because at declaration you specify a type placeholder for the contained objects instead of a specific type. You give the stored objects a specific type only when you create an instance of the collection.
Generics are a huge time saver. If you''''ve ever created custom collections, you know how much code can be involved. Generics in Visual Basic let you create the equivalent of a custom collection in one line of code. You no longer need to create separate custom collections for each type of object you want to store. You simply provide the desired type as you instantiate the generic collection.
The easiest way to see how generics work is with an example. Assume you have a simple Customer class with two public properties (shown as public variables for brevity): Public Class Customer Public Name As String Public CreditLimit As Decimal Public Sub New(ByVal CustomerName As String, ByVal CustCreditLimit As Decimal) Name = CustomerName CreditLimit = CustCreditLimit   [1] [2] 下一页 [VB.NET程序]Visual Basic 6 逆向工程与反逆向工程 (2) [VB.NET程序]Visual Basic 6 逆向工程与反逆向工程 (1) [VB.NET程序]用Visual Basic创建复杂窗体 [VB.NET程序]Visual Basic的类对于面向对象的支持 [VB.NET程序]如何在Visual Basic 6.0 中连接加密的Access数据库 [VB.NET程序]使用Visual Basic操纵XML文档 [VB.NET程序]Visual Basic.Net连各种数据库的几种方法 [VB.NET程序]Visual Basic .NET资源工具包 [VB.NET程序]效率提高两倍!——Visual Basic.net 新力量 [VB.NET程序]复杂和高效——Visual Basic.net新力量
|