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

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

作者:闵涛 文章来源:闵涛的学习笔记 点击数:1511 更新时间:2009/4/23 15:41:40

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)

若是没有了泛型,你就不得不为任何一种你想用来比较的对象类型分别定义一个比较类(比如说,ProductComparerOrderComparer)。

在不少常见的场合,泛型都在很大程度上减轻了你的代码负担。想象一下吧,当你有着式样繁多的类,而它们仅仅在操作的对象类型上有所差别时,使用泛型会是多么利便的事。


@以下附上原文供大家参考@

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新力量
教程录入: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……
    咸宁网络警察报警平台