Not a homework question,despite the bizarreness of the scenario. I’ve just substituted the real objects I’m working with to simplify the examples.
This让我从这里开始,但不确定如何继续.我正在尝试编写一个包含集合的类,并且我迷失在IEnumerator和IEnumerable的世界中,这是我非常新的(甚至不能完全确定我是在正确的道路上).假设我有一节课:
- Public Class BakedBean
- Private Shape As String
- Private Variety As String
- Private Flavour As String
- 'etc
- End Class
另一个代表BakedBeans集合的类:
- Public Class TinOfBeans
- '?
- End Class
我希望能够将TinOfBeans类中的Beans作为集合,这样我就可以进行这样的调用,但不依赖于特定的集合类型:
- Dim myTin As New TinOfBeans()
- myTin.Add(New BakedBean(...))
- For Each bean As BakedBean in myTin
- '...
- Next
- myTin(0).Flavour = "beany"
我一直在关注IEnumerable和IEnumerator,到目前为止我已经有了这么多,但是我已经迷失了它:
BakedBean类
- 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
在这一点上,我变得非常紧张,并且不知道如何继续(或者,正如我在开始时说的,如果这是正确的方法).有什么建议?