打印本文 打印本文 关闭窗口 关闭窗口
[译]Visual Basic 2005在语言上的增强(四)泛型
作者:武汉SEO闵涛  文章来源:敏韬网  点击数1523  更新时间:2009/4/23 15:41:40  文章录入:mintao  责任编辑:mintao
End Sub
End Class

You can declare a list of Customers using the generic collection List class like so:
Dim Customers As New System.Collections.Generic.List(Of Customer)

With this single line of code, you''''ve declared a strongly typed list that stores only Customer types and gives you full IntelliSense on the contained Customer objects. You could just as easily create a list of Product or Order objects, effectively creating a set of custom collections without writing all the custom collection code for each type. You can now write code like this:
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
''''compile error: Product cannot be converted to Customer
Dim prodA As New Product("ProdA", CDec(29.95))
Customers.Add(prodA)

You also get a performance advantage using generic collections because the stored objects are strongly typed and not kept internally as type Object.

There are other collection generics available in the System.Collections.Generic namespace. For example, the Dictionary(of K,V) collection allows you to specify types for the keys and values. The LinkedList(of T) and Stack(of T) generic collections behave like regular linked lists and stacks, except you''''re allowed to specify what kinds of objects they''''ll contain.

You can create your own generic types using the new generic type declaration syntax. Consider a Comparer class that lets you compare different kinds of objects:
Public Class Comparer(Of itemType)
    ''''...
End Class

You can define multiple type placeholders in a comma-separated list. You can also define constraints restricting which classes can be provided to the generic when it''''s instantiated:
Public Class Comparer(Of itemType As IComparable)
    ''''...
End Class

This constraint ensures that Comparer(of T) instances can only be created with classes implementing the IComparable interface. Multiple constraints can also be applied to the class declaration.

As an example, consider an expanded Customer class that implements IComparable:
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

A similar Product class is implemented, except that the CompareTo function compares the product prices instead of the customer''''s credit limits:
Public Class Product
    Implements IComparable
    Public Name As String
    Public Price As Decimal
    Public Sub New(...)...
    Public Function CompareTo(...)...
End Class

Now the Comparer class provides the generic comparison operation:
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

You can now instantiate Comparers with objects of different types:
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)

Without generics, you''''d have to define a comparison class for each type of object you want to compare (for example, ProductComparer and OrderComparer).

Generics can significantly reduce your coding efforts in many common scenarios. Consider using generics when you have multiple classes that vary only by the type of object on which they operate.

上一页  [1] [2] 

打印本文 打印本文 关闭窗口 关闭窗口