不是一个家庭作业问题,尽管场景很奇怪。我刚刚替换了我正在使用的真实对象来简化示例。
这让我从这里开始,但不确定如何继续。我正在尝试编写一个包含集合的类,而我迷失在 and 的世界中IEnumerator
,IEnumerable
我对它很陌生(甚至不完全确定我走在正确的道路上)。假设我有一堂课:
Public Class BakedBean
Private Shape As String
Private Variety As String
Private Flavour As String
'etc
End Class
另一个代表BakedBean
s 集合的类:
Public Class TinOfBeans
'?
End Class
我希望能够以集合的形式Beans
进入TinOfBeans
课堂,这样我就可以进行这样的调用,但不会被绑定到特定的集合类型:
Dim myTin As New TinOfBeans()
myTin.Add(New BakedBean(...))
For Each bean As BakedBean in myTin
'...
Next
myTin(0).Flavour = "beany"
我一直在看IEnumerable
and IEnumerator
,到目前为止我有这么多,但我对它感到非常迷茫:
烤豆类
Public Class BakedBean
Private Shape As String
Private Variety As String
Private Flavour As String
'etc
End Class
BeansEnumerator 类
Public Class BeansEnumerator
Implements IEnumerator
Private Position As Integer = -1
Public ReadOnly Property Current As Object Implements System.Collections.IEnumerator.Current
Get
'???
End Get
End Property
Public Function MoveNext() As Boolean Implements System.Collections.IEnumerator.MoveNext
'???
End Function
Public Sub Reset() Implements System.Collections.IEnumerator.Reset
Position = -1
End Sub
End Class
TinOfBeans 类
Public Class TinOfBeans
Implements IEnumerable
Private beansEnum As BeansEnumerator
Public Function GetEnumerator() As System.Collections.IEnumerator Implements System.Collections.IEnumerable.GetEnumerator
Return beansEnum
End Function
End Class
在这一点上,我陷入了困境,不知道如何进行(或者,正如我在开始时所说,如果这甚至是正确的方法)。有什么建议么?