Chinaunix首页 | 论坛 | 博客
  • 博客访问: 188408
  • 博文数量: 106
  • 博客积分: 3810
  • 博客等级: 中校
  • 技术积分: 1007
  • 用 户 组: 普通用户
  • 注册时间: 2009-03-18 13:35
文章分类

全部博文(106)

文章存档

2014年(17)

2011年(5)

2010年(75)

2009年(9)

我的朋友

分类:

2010-04-27 09:07:09

Option Strict Off
' 声明一个使用 IComparable 接口的类 Vector
Public Class Vector : Implements IComparable
    Public X As Integer
    Public Y As Integer

    Public Sub New()
        X = 0
        Y = 0
    End Sub

    Public Sub New(ByVal vX As Integer, ByVal vY As Integer)
        X = vX
        Y = vY
    End Sub '更多.net源码和教程,来自[乐博网 www.lob.cn]

    Public Sub Show() '用来显示向量坐标 (X,Y)
        Console.Write("({0},{1}) ", X, Y)
    End Sub

    '实现出 IComparable 接口中的 CompareTo 方法
    Public Function CompareTo(ByVal obj As Object) As _
                Integer Implements IComparable.CompareTo
        Return (X ^ 2 + Y ^ 2) - (obj.X ^ 2 + obj.Y ^ 2)
    End Function
End Class

Module Module1

    Sub Main()
        Dim I As Integer
        Dim vecArray() As Vector

        '定义一个内含五个向量的数组
        vecArray = New Vector() {New Vector(20, 10), _
                                 New Vector(50, 20), _
                                 New Vector(90, 40), _
                                 New Vector(10, 10), _
                                 New Vector(40, 30)}
        Console.WriteLine("乐博网提示:排序前 ...")
        For I = 0 To UBound(vecArray)
            vecArray(I).Show()
        Next
        Console.WriteLine()

        Array.Sort(vecArray) '调用 System.Array 类的 Sort 方法

        Console.WriteLine("乐博网提示:排序后 ...")
        For I = 0 To UBound(vecArray)
            vecArray(I).Show()
        Next
        Console.WriteLine()
        Console.Read()
    End Sub

End Module


阅读(745) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~