Collection <t>与自定义工具类之间的区别

时间:2018-12-18 09:34:57

标签: vb.net

有人建议从Collection继承以获得Collection类。其他一些人建议从头开始拥有类并实现接口。我想了解何时使用一个。

当我使用时,我会看到:

class MyCollection 
       Inherits Collection(Of SomeObject)

我可以为每个其他内容添加,插入,可能是因为Collection<T> uses internally List<T>

但是,如果我只是这样做:

class MyCollection : IList(Of SomeObject), IEnumerable<SomeObject>, IEnumerable(Of SomeObject)
          myList As List(Of SomeObject)

我还可以实现诸如Add(), Remove(), for each

之类的东西

使用Collection(Of T)是否很好,因为它已经包含了所有已实现的接口和内部List(Of T),而不是自己从头开始在自己的类中实现所有接口?人们建议从COllection(Of T)继承的重点是什么?

编辑(待进一步讨论):

  Public Class Merge
        Property Size As Integer
        Property Datee As Date
        Property Min As Integer
        Property Max As Integer?
        Property Value As Double

        Public Sub New(min As Integer, max As Integer?, value As Integer)
            Me.Min = min
            Me.Max = max
            Me.Value = value
        End Sub      
    End Class

    Public Enum SortCriteria
        MinThenMax
        MaxThenMin
    End Enum

  Public Class MergeComparer
        Implements IComparer(Of Merge)   'do oddzielnej klasy sortowania obiektu jak tutaj potrzebujemy IComparer a nie IComparable (ten jest bezposrednio na klasie)

        Public SortBy As SortCriteria = SortCriteria.MinThenMax

        Public Function Compare(x As Merge, y As Merge) As Integer Implements IComparer(Of Merge).Compare
        'to be implemented
        End Function


    End Class


      Public Class MergeCollection
      Inherits Collection(Of Merge)

    Public SortBy As SortCriteria = SortCriteria.MinThenMax


    ''' <summary>
    ''' Ovveride because
    ''' There could be only one item on list which contains Max prop = Nothing
    ''' </summary>
    ''' <param name="index"></param>
    ''' <param name="item"></param>
    Protected Overrides Sub InsertItem(index As Integer, item As Merge)
        if IsNothing(item.Max)
            If Items.Any(Function(myObject) IsNothing(Items.Max)) Then
                Return
            End If
        End If

        MyBase.InsertItem(index, item)
    End Sub   

    Public Sub Sort()
        Dim allItems = Items.ToArray()

        Array.Sort(allItems)

        For i = 0 To allItems.GetUpperBound(0)
            Items(i) = allItems(i)
        Next
    End Sub

    Public Sub Sort(comparison As Comparison(Of Merge))
        Dim allItems = Items.ToArray()

        Array.Sort(allItems, comparison)

        For i = 0 To allItems.GetUpperBound(0)
            Items(i) = allItems(i)
        Next
    End Sub

    Public Sub Sort(comparer As IComparer(Of Merge))
        Dim allItems = Items.ToArray()

        Array.Sort(allItems, comparer)

        For i = 0 To allItems.GetUpperBound(0)
            Items(i) = allItems(i)
        Next
    End Sub

End Class

1 个答案:

答案 0 :(得分:1)

这是一个自定义集合的示例,该集合实现Collection(Of T),然后添加它自己的Sort方法:

Public Class StringCollection
    Inherits Collection(Of String)

    Public Sub Sort()
        Dim allItems = Items.ToArray()

        Array.Sort(allItems)

        For i = 0 To allItems.GetUpperBound(0)
            Items(i) = allItems(i)
        Next
    End Sub

End Class

样品用量:

Dim strings As New StringCollection

strings.Add("Peter")
strings.Add("Paul")
strings.Add("Mary")

strings.Sort()

For Each s In strings
    Console.WriteLine(s)
Next

Sort方法依赖于项本身的IComparable实现。如果您想支持其他排序方法,或者您的商品未实现IComparable,则可以实现不同的Sort方法,例如

Public Class StringCollection
    Inherits Collection(Of String)

    Public Sub Sort()
        Dim allItems = Items.ToArray()

        Array.Sort(allItems)

        For i = 0 To allItems.GetUpperBound(0)
            Items(i) = allItems(i)
        Next
    End Sub

    Public Sub Sort(comparison As Comparison(Of String))
        Dim allItems = Items.ToArray()

        Array.Sort(allItems, comparison)

        For i = 0 To allItems.GetUpperBound(0)
            Items(i) = allItems(i)
        Next
    End Sub

End Class

样品用量:

Dim strings As New StringCollection

strings.Add("Peter")
strings.Add("Paul")
strings.Add("Mary")

strings.Sort(Function(a, b) a.Length.CompareTo(b.Length))

For Each s In strings
    Console.WriteLine(s)
Next

在这种情况下,我们通过它们的String而不是它们的隐式“字母” Length实现来显式比较IComparable项。

您可以为Sort的每个相关重载添加Array.Sort的重载,并且/或者可以根据需要提供自己的显式比较。请注意,这些重载之一采用IComparer。这是您可能会使用的方法:

Public Class StringCollection
    Inherits Collection(Of String)

    Public Sub Sort()
        Dim allItems = Items.ToArray()

        Array.Sort(allItems)

        For i = 0 To allItems.GetUpperBound(0)
            Items(i) = allItems(i)
        Next
    End Sub

    Public Sub Sort(comparison As Comparison(Of String))
        Dim allItems = Items.ToArray()

        Array.Sort(allItems, comparison)

        For i = 0 To allItems.GetUpperBound(0)
            Items(i) = allItems(i)
        Next
    End Sub

    Public Sub Sort(comparer As IComparer(Of String))
        Dim allItems = Items.ToArray()

        Array.Sort(allItems, comparer)

        For i = 0 To allItems.GetUpperBound(0)
            Items(i) = allItems(i)
        Next
    End Sub

End Class

这是一个比较器示例:

Public Class StringLengthComparer
    Implements IComparer, IComparer(Of String)

    Public Function Compare(x As Object, y As Object) As Integer Implements IComparer.Compare
        Return Compare(CStr(x), CStr(y))
    End Function

    Public Function Compare(x As String, y As String) As Integer Implements IComparer(Of String).Compare
        Return x.Length.CompareTo(y.Length)
    End Function

End Class

样品用量:

Dim strings As New StringCollection

strings.Add("Peter")
strings.Add("Paul")
strings.Add("Mary")

Dim comparer As New StringLengthComparer

strings.Sort(comparer)

For Each s In strings
    Console.WriteLine(s)
Next

我们将再次按照项目Length对项目进行排序,但是这次,我们使用的是实现IComparer的对象而不是Comparison(Of T)委托进行比较。但是,您应该注意到,在上一个示例中,Compare的{​​{1}}方法采用了与上一个示例中用于创建IComparer委托的Lambda完全相同的形式。