如何创建一个可以有父母和子女关系的班级

时间:2011-02-23 16:23:18

标签: c# generics collections hierarchy

我在这里看到了很多关于我的问题的文章但没有一个真正回答我的要求。我正在创建一个我的Branch对象类,您可以像TreeView控件的TreeNode对象一样设想它。每个分支可以在其下方(因此在其上方)具有任意数量的分支子项。这是我相当简单的课程:

public class Branch {
    public string Name { get; set; }
    public string Link { get; set; }
    public Branch Parent { get; private set; }
    public List<Branch> Children { get; set; }

    internal Branch(string Name, string Link) {
        this.Name = Name;
        this.Link = Link;
        this.Children = new List<Branch>();
    } // Branch - Constructor - Overload

    internal Branch(string Name, string Link, List<Branch> Children) {
        this.Name = Name;
        this.Link = Link;
        this.Children = Children;

        this.Children.ForEach(delegate(Branch branch) {
            branch.Parent = this;
        });
    } // Branch - Constructor - Overload

    public bool HasChildren {
        get { return this.Children.Count > 0; }
    } // HasChildren - Property - ReadOnly

    public string Path {
        get {
            string Result = "";

            Branch parent = this;
            while (parent != null) {
                Result = string.Format("{0}/{1}", parent.Name, Result);
                parent = parent.Parent;
            } // while stepping up the tree

            return string.IsNullOrWhiteSpace(Result) ? "" : Result.Substring(0, Result.Length - 1);
        } // get
    } // Path - Property - ReadOnly

如果我在实例化时添加子项如下所示,那么这很有效:

List<Branch> Branches = new List<Branch>() {
    new Branch("First", "#"),
    new Branch("Second", "#"),
    new Branch("Third", "#", new List<Branch>() {
        new Branch("ThirdSub1", "#"),
        new Branch("ThirdSub2", "#")
    }),
    new Branch("Fourth", "#"),
    new Branch("Fifth", "#"),
    new Branch("Sixth", "#", new List<Branch>() {
        new Branch("SixthSub1", "#"),
        new Branch("SixthSub2", "#", new List<Branch>() {
            new Branch("SixthSub2Sub1", "#"),
            new Branch("SixthSub2Sub2", "#"),
            new Branch("SixthSub2Sub3", "#", new List<Branch>() {
                new Branch("Deep Deep Deep Undercover", "#"),
            }),
        }),
    }),
    new Branch("Seventh", "#"),
    new Branch("Eighth", "#"),
};

但如果我这样做:

List<Branch> Branches = new List<Branch>();
Branch Test = Branches.Add(new Branch("Something", ""));
Test.Children.Add(new Branch("Child Here", ""));

“Child Here”节点没有与之关联的Parent。因此它被破坏了,当然Path属性不起作用。

我以为我可以覆盖List的Add方法但不允许这样做。处理这个问题的最佳方法是什么?目前我不是像MyBranches那样创建我自己的Collection Class,但是如果在实现IList或ISet或Collection时有一种方法可以做我需要的,那么我愿意这样做。但请提供一个例子。

谢谢!

2 个答案:

答案 0 :(得分:4)

对于未来寻找同样解决方案的人来说,这是完整的课程:

public class Branch {
    public string Name { get; set; }
    public string Link { get; set; }
    public Branch Parent { get; set; }
    public TreeBranches Children { get; private set; }

    internal Branch(string Name, string Link) {
        this.Name = Name;
        this.Link = Link;
        this.Children = new TreeBranches(this);
    } // Branch - Constructor - Overload

    internal Branch(string Name, string Link, TreeBranches Children) {
        this.Name = Name;
        this.Link = Link;
        this.Children = Children;

        this.Children.ToList().ForEach(delegate(Branch branch) {
            branch.Parent = this;
        });
    } // Branch - Constructor - Overload

    /// <summary>
    /// Returns a boolean indicating if the given Branch has any child Branches.
    /// </summary>
    public bool HasChildren {
        get { return this.Children.Count > 0; }
    } // HasChildren - Property - ReadOnly

    /// <summary>
    /// Gets the path from the oldest ancestor to the current Branch.
    /// </summary>
    public string Path {
        get {
            string Result = "";

            Branch parent = this;
            while (parent != null) {
                Result = string.Format("{0}/{1}", parent.Name, Result);
                parent = parent.Parent;
            } // while stepping up the tree

            return string.IsNullOrWhiteSpace(Result) ? "" : Result.Substring(0, Result.Length - 1);
        } // get
    } // Path - Property - ReadOnly

} // Branch - Class

public class TreeBranches : IList<Branch> {
    private List<Branch> branches = new List<Branch>();
    private Branch owner;

    public TreeBranches() {
        this.owner = null;
    }

    public TreeBranches(Branch owner) {
        this.owner = owner;
    }

    public void Add(Branch branch) {
        branch.Parent = this.owner;
        this.branches.Add(branch);
    }

    #region Standard IList Method Implementation

    IEnumerator<Branch> IEnumerable<Branch>.GetEnumerator() { return this.branches.GetEnumerator(); }
    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return this.branches.GetEnumerator(); }
    public int IndexOf(Branch item) { return this.branches.IndexOf(item); }
    public void Insert(int index, Branch item) { this.branches.Insert(index, item); }
    public void RemoveAt(int index) { this.branches.RemoveAt(index); }
    public Branch this[int index] {
        get { return this.branches[index]; }
        set { this.branches[index] = value; }
    }

    public void Clear() { this.branches.Clear(); }
    public bool Contains(Branch item) { return this.branches.Contains(item); }
    public void CopyTo(Branch[] array, int arrayIndex) { this.branches.CopyTo(array, arrayIndex); }
    public int Count { get { return this.branches.Count(); } }
    public bool IsReadOnly { get { return this.IsReadOnly; } }
    public bool Remove(Branch item) { return this.branches.Remove(item); }

    #endregion Standard IList Method Implementation
} // TreeBranches - Class

答案 1 :(得分:0)

您可以从Collection<T>而不是List<T>派生,List<T>更快,并且针对性能进行了优化,但Collection<T>更具可扩展性,允许您覆盖{{ 1}}和其他人。

如果性能不是问题,那么使用Add(),如果性能问题,请使用Reed在您自己的类中包含Collection<T>的示例。