如何使用 item 属性实现/重写自定义集合类的受保护列表.包含()



我不完全确定我在上面的标题上的措辞是否正确。我想解释它的最简单方法是将代码放在下面以供参考,然后给出我想要的预期行为。


Public Class Domino
    Public ReadOnly MinimumSideValue As Integer = 0
    Public ReadOnly MaximumSideValue As Integer = 6
    Public Property key As Integer
    Public Property Side1 As Integer
    Public Property Side2 As Integer
    Public Sub New(side1Value As Integer, side2Value As Integer)
        Me.Side1 = side1Value
        Me.Side2 = side2Value
        ' Key should be a two digit number. side values 5 and 1 -> 51
        Me.key = Integer.Parse(Side1.ToString & Side2.ToString)
    End Sub
    Public Overrides Function ToString() As String
        Return Convert.ToString(Me.key)
    End Function
End Class

Public Class DominoCollection
    Inherits System.Collections.CollectionBase
    Public Sub AddDomino(newDomino As Domino)
        ' Not sure if I should be calling Contains on the Protected List object
        If Not List.Contains(newDomino) Then
            List.Add(newDomino)
        End If    
    End Sub
    Public Function Contains(objDomino as Domino) As Boolean
        ' Need to check if a domino that has the same key already exists
        Dim found As Boolean = False
        For i = 0 To List.Count - 1 'This will fail if the list is empty...
            If DirectCast(List.Item(i), Domino).key = objDomino.key Then found = True
        Next
        Return found
    End Function    
End Class

第一个代码列表是将创建多个实例的类。第二个类是将包含第一个类的实例的集合。集合不应包含重复项。因此,当我从 CollectionBase 调用内置受保护List对象上的 contains 方法时,我希望它搜索项目列表,在第一个类的 key 属性中查找具有相同值的副本。

我不完全确定我是否可以覆盖 List.Contains 方法,因为它是一个受保护的对象和/或我是否真的应该创建自己的包含方法。我尝试创建自己的.包含方法,但如果列表为空,则失败。


编辑

我知道我的代码不是 C#。但是,由于它是 .NET,C# 答案会像 VB 一样有帮助。


编辑:修订代码

在看到下面的一些解决方案后,我得到了以下内容,到目前为止,它看起来可以按我想要的方式工作。但是,我不确定是否正确实现了Item属性。我还没有测试它,因为我还没有达到能够在应用程序的其余部分使用它的地步,我只是想建立一个框架。

Friend Class DominoCollection
    Private domCol As Dictionary(Of Integer, Domino)
    Public ReadOnly Property Keys As System.Collections.Generic.Dictionary(Of Integer, Domino).KeyCollection
        Get
            Return domCol.Keys
        End Get
    End Property
    Public ReadOnly Property Values As System.Collections.Generic.Dictionary(Of Integer, Domino).ValueCollection
        Get
            Return domCol.Values
        End Get
    End Property
    Default Public Property Item(DominoKey As Integer) As Domino
        Get
            If domCol.ContainsKey(DominoKey) Then
                Return domCol.Item(DominoKey)
            Else
                Throw New KeyNotFoundException(String.Format("Cannot find key {0} in internal collection"))
            End If
        End Get
        Set(value As Domino)
            If domCol.ContainsKey(DominoKey) Then
                domCol.Item(DominoKey) = value
            Else
                Throw New KeyNotFoundException(String.Format("Cannot find key {0} in internal collection"))
            End If
        End Set
    End Property
    Default Public Property Item(domino As Domino) As Domino
        Get
            If domCol.ContainsKey(domino.key) Then
                Return domCol.Item(domino.key)
            Else
                Throw New KeyNotFoundException(String.Format("Cannot find key {0} in internal collection"))
            End If
        End Get
        Set(value As Domino)
            If domCol.ContainsKey(domino.key) Then
                domCol.Item(domino.key) = value
            Else
                Throw New KeyNotFoundException(String.Format("Cannot find key {0} in internal collection"))
            End If
        End Set
    End Property
    Public Sub New()
        domCol = New Dictionary(Of Integer, Domino)
    End Sub
    Public Sub Add(dom As Domino)
        If Not domCol.ContainsKey(dom.key) Then
            domCol.Add(dom.key, dom)
        End If
    End Sub
    Public Function Contains(dom As Domino) As Boolean
        Return domCol.ContainsKey(dom.key)
    End Function
    ' flexibility:
    Public Function Remove(dom As Domino) As Boolean
        If domCol.ContainsKey(dom.key) Then
            domCol.Remove(dom.key)
            Return True
        Else
            Return False
        End If
    End Function
    Public Function Remove(key As Integer) As Boolean
        If domCol.ContainsKey(key) Then
            domCol.Remove(key)
            Return True
        Else
            Return False
        End If
    End Function
    Public Function Count() As Integer
        Return domCol.Count
    End Function
    Public Sub Clear()
        domCol.Clear()
    End Sub
End Class

鉴于Domino.Key的核心重要性,我认为使用字典的类会让事情变得最简单。 由于您的代码一直在寻找它来决定是否存在Domino对象等,因此字典将有助于检测并防止欺骗等。

Friend Class DominoCollection
    Private mcol As Dictionary(Of Integer, Domino)
    Public Sub New()
        mcol = New Dictionary(Of Integer, Domino)
    End Sub
    Public Sub Add(dom As Domino)
        If mcol.ContainsKey(dom.key) = False Then
            mcol.Add(dom.key, dom)
        ' optional: replace - dont know if this is needed
        Else
            mcol(dom.key) = dom
        End If
    End Sub
    Public Function Contains(dom As Domino) As Boolean
        Return mcol.ContainsKey(dom.key)
    End Function
    ' flexibility:
    Public Function Remove(dom As Domino) As Boolean
        If mcol.ContainsKey(dom.key) Then
            mcol.Remove(dom.key)
            Return True
        End If
        Return False
    End Function
    Public Function Remove(key As Integer) As Boolean
        If mcol.ContainsKey(key) Then
            mcol.Remove(key)
            Return True
        End If
        Return False
    End Function
End Class

对于一个或多个项目:

Public Function Item(key As Integer) As Domino
    Return mcol(key)
End Function
Public Function Items() As List(Of Domino)
    Return New List(Of Domino)(mcol.Values)
End Function

Items隐藏了迭代通常需要KeyValuePair,并允许您对集合(所需的)执行简单的 For/Each 循环:

Dim doms As New DominoCollection
doms.Add(New Domino(1, 2))
doms.Add(New Domino(2, 3))
doms.Add(New Domino(4, 6))
For Each d As Domino In doms.Items
    Console.WriteLine("s1:{0} s2:{1} k:{2}", d.Side1, d.Side2, d.key.ToString)
Next

输出

S1:

1 S2:2 K:12
S1:2 S2:3 K:23
S1:4 S2:6 K:46

是的,您绝对应该从泛型集合类继承或实现泛型集合接口。

我不确定 VB 的确切语法,但我认为与其List.Contains,不如将 Linq 扩展Any与 lambda 一起使用。我认为它是这样的:

List.Any(Function(d as Domino) d.key = newDomino.key)

如果List中的任何元素与键匹配,这将返回 true。

相关内容

  • 没有找到相关文章