부모와 자식의 트리 구조를 만들려고합니다. 문제는 난 단지 다른 곳 어린이와 부모 클래스에서 아이의 부모를 asign 수 있기를 원하고 있다는 것입니다하지 않은 C#을 프로그래머가부모와 자식이있는 트리 구조
public class Parent
{
public static Parent Root = new Parent();
private List<Child> children = new List<Child>();
public ReadOnlyCollection<Child> Children
{
get { return children.AsReadOnly(); }
}
public void AppendChild(Child child)
{
child.Parent.RemoveChild(child);
child.children.Add(child);
child.Parent = this; //I need to asign the childs parent in some way
}
public void RemoveChild(Child child)
{
if (this.children.Remove(child))
{
child.Parent = Parent.Root; //here also
}
}
}
public class Child : Parent
{
private Parent parent = Parent.Root;
public Parent Parent
{
get { return this.parent; }
private set { this.parent = value; } //nothing may change the parent except for the Child and Parent classes
}
}
를, 내가 좋아하는 (친구를 사용하는 들었다 C++에서는 구현되지 않았지만 다른 모든 솔루션은 실패했습니다.
보호를 사용해보십시오. – idstam
어떻게 도움이 될까요? – zmbq
Protected는 이미 시도한 것들 중 하나이지만, 하위 클래스에서 다른 부모 인스턴스의 보호 된 필드를 사용할 수는 없습니다. – Safron