0

我有一个基于节点的层次结构类型的东西,其中所有节点都包含在一个列表中,但列表中的每个项目都包含对其在层次结构中的父级的引用。例如

public class cItem
    public attributes as dictionary(of string, single)
    public parent as cItem
end Class 

....

public class database
    public stuff as new list(of cItem)
    public sub addItem(item as cItem)
        stuff.add(item)
        stuff.last().parent = functiontofindparentwithinstuff
    end sub
end class

问题是 cItem 中的父变量是否会保存对 stuff list 中对象的引用,或者 stuff list 中的项目是否会被复制到 items 变量中?

4

1 回答 1

0

如果你正在做一种树,那么所有项目的添加/删除都应该在一个函数内完成。此函数将负责设置 item 属性的父级。

永远不应允许以任何其他方式修改父变量。这将使您保持孩子和父母之间的联系。

Class TreeItem

    Private _parent As TreeItem = Nothing
    Private _childs As New List(Of TreeItem)

    Public Sub AddChild(ByVal item As TreeItem)
        item._parent = Me
        _childs.Add(item)
    End Sub

    Public Sub RemoveChild(ByVal item As TreeItem)
        item._parent = Nothing
        _childs.Remove(item)
    End Sub

End Class
于 2013-11-12T21:08:31.257 回答