转至繁体中文版     | 网站首页 | 图文教程 | 资源下载 | 站长博客 | 图片素材 | 武汉seo | 武汉网站优化 | 
最新公告:     敏韬网|教学资源学习资料永久免费分享站!  [mintao  2008年9月2日]        
您现在的位置: 学习笔记 >> 图文教程 >> 软件开发 >> VB.NET程序 >> 正文
[译]Visual Basic 2005在语言上的增强(四)泛型         ★★★★

[译]Visual Basic 2005在语言上的增强(四)泛型

作者:闵涛 文章来源:闵涛的学习笔记 点击数:1515 更新时间:2009/4/23 15:41:40
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] 


[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新力量
教程录入:mintao    责任编辑:mintao 
  • 上一篇教程:

  • 下一篇教程:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
      注:本站部分文章源于互联网,版权归原作者所有!如有侵权,请原作者与本站联系,本站将立即删除! 本站文章除特别注明外均可转载,但需注明出处! [MinTao学以致用网]
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)

    同类栏目
    · C语言系列  · VB.NET程序
    · JAVA开发  · Delphi程序
    · 脚本语言
    更多内容
    热门推荐 更多内容
  • 没有教程
  • 赞助链接
    更多内容
    闵涛博文 更多关于武汉SEO的内容
    500 - 内部服务器错误。

    500 - 内部服务器错误。

    您查找的资源存在问题,因而无法显示。

    | 设为首页 |加入收藏 | 联系站长 | 友情链接 | 版权申明 | 广告服务
    MinTao学以致用网

    Copyright @ 2007-2012 敏韬网(敏而好学,文韬武略--MinTao.Net)(学习笔记) Inc All Rights Reserved.
    闵涛 投放广告、内容合作请Q我! E_mail:admin@mintao.net(欢迎提供学习资源)

    站长:MinTao ICP备案号:鄂ICP备11006601号-18

    闵涛站盟:医药大全-武穴网A打造BCD……
    咸宁网络警察报警平台