学习泛型
来源:互联网 发布:讨厌杨过 知乎 编辑:程序博客网 时间:2024/04/28 04:06
Imports System.Collections.Generic
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'定义客户对象
Dim Customers As New List(Of Customer)
Dim custA As New Customer("老虎", 1000)
Dim custB As New Customer("豹子", 2000)
Customers.Add(custA)
Customers.Add(custB)
'输出客户信息
For Each c As Customer In Customers
Response.Write("用户名: " & c.Name & ", 贷款额度: " & c.CreditLimit.ToString + "<br>")
Next
'实现比较
Dim cc As New Comparer(Of Customer)
Dim lc As Customer = cc.GetLargest(custA, custB)
Response.Write("贷款额度较高的客户:" & lc.Name + "<br>")
'定义产品对象
Dim Products As New List(Of Product)
Dim prod1 As New Product("老虎", 10)
Dim prod2 As New Product("豹子", 100)
Products.Add(prod1)
Products.Add(prod2)
'输出产品信息
For Each p As Product In Products
Response.Write("产品名称:" & p.Name & ",产品价格:" & p.Price.ToString + "<br>")
Next
'实现比较
Dim pc As New Comparer(Of Product)
Dim lp As Product = pc.GetLargest(prod1, prod2)
Response.Write("产品价格较高的是:" & lp.Name + "<br>")
End Sub
'客户类
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
'实现IComparable接口的比较方法
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
'产品类
Public Class Product
Implements IComparable
'产品名称
Public Name As String
'产品价格
Public Price As Decimal
Public Sub New(ByVal _Name As String, ByVal _Price As Decimal)
Name = _Name
Price = _Price
End Sub
'实现IComparable接口的比较方法
Public Function CompareTo(ByVal obj As Object) As Integer Implements IComparable.CompareTo
Dim c As Product = CType(obj, Product)
If Price > c.price Then Return 1
If Price < c.Price Then Return -1
Return 0
End Function
End Class
'对象比较类,泛型实现
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
End Class
- 【Java学习】泛型接口学习
- 学习泛型
- JAVA泛型学习
- java泛型学习
- 泛型学习
- 泛型学习总结
- 泛型学习笔记
- 泛型学习笔记
- 泛型学习笔记
- 学习泛型
- java泛型学习
- c#泛型学习
- 泛型学习笔记
- 泛型学习总结
- 泛型学习笔记
- Java泛型学习
- 泛型学习
- 泛型学习笔记
- eclipse的初装
- Community Server专题四:HttpHandler
- JDOM操作XML文件(法老修正版)
- DataGrid中文本框联动问题
- Community Server专题五:IHttpHandlerFactory
- 学习泛型
- Community Server专题六:Delegates & Events
- 修改监听端口
- JCreator调试全攻略
- Community Server专题七: Job & Timer
- 《解剖PetShop》系列之一
- JSP中href(url)与sumbit的提交数据方式对比(土地资源项目Javabean技术应用心得)
- 利用Eclipse的Update组件实现适合企业应用的更新组件(1)
- 《解剖PetShop》系列之二