我需要修改SchoolGroup
中的子项,而不是School
中的子项。
我如何在SchoolGroup
类上实现相同的属性,知道我需要从任何Collective
迭代子项,但只能从SchoolGroup
....添加和删除
Class Collective
' just iteration '
Public MustOverride Children as ReadonlyList(Of Child)
Class School
Inherits Collective
' just iteration foreach SchoolGroup'
Public Overrides Property Children as ReadonlyList(Of Child)
Class SchoolGroup
Inherits Collective
' iteration and modification '
Public Overrides Property Children as ReadonlyList(Of Child)
' CANT USE THE SAME NAME.... '
Public Property Children as List(Of Child)
答案 0 :(得分:1)
在SchoolGroup中隐藏属性,以便您能够使用不同的返回类型实现它。
Public Class Collective
Public Overridable Property Children As ReadonlyList(Of Child)
End Class
Public Class School
Inherits Collective
Public Overrides Property Children As ReadonlyList(Of Child)
End Class
Public Class SchoolGroup
Inherits Collective
Public Shadows Property Children As List(Of Child)
End Class
顺便说一句:您可以将ReadonlyList(Of Child)
转储为IEnumerable(Of Child)
,这基本上是一个只读列表,用于枚举。
答案 1 :(得分:1)
我个人更喜欢这种情况下的界面,因为Collective没有任何实际的儿童数据,学校和学校集团都没有通过覆盖财产获得任何真正的优势。
例如:
Public Interface IChildren
Function GetEnumerator() As System.Collections.Generic.IEnumerator(Of Child)
End Interface
Public MustInherit Class Collective
Implements IChildren
Public MustOverride Function GetEnumerator() As System.Collections.Generic.IEnumerator(Of Child) Implements IChildren.GetEnumerator
End Class
Public Class School
Inherits Collective
Public Property Children As New ReadOnlyList(Of Child)
Public Overrides Function GetEnumerator() As System.Collections.Generic.IEnumerator(Of Child)
Return Children.GetEnumerator
End Function
End Class
Public Class SchoolGroup
Inherits Collective
Public Property Children As New List(Of Child)
Public Overrides Function GetEnumerator() As System.Collections.Generic.IEnumerator(Of Child)
Return Children.GetEnumerator
End Function
End Class
然后可以将其用作:
Dim oGroup As New SchoolGroup
oGroup.Children.Add(New Child)
ProcessChildren(oGroup)
进程子进程接受接口实现者
Public Sub ProcessChildren(oCollection As IChildren)
For Each oChild As Child In oCollection
' Do something
Next
End Sub