0
我試圖在C#中實現自反聯想,但在網上找不到任何示例。 我想出了以下Reflexive Association C#
class Employee
{
public string Name { get; set; }
public Employee Boss { get; set; }
public List<Employee> Junoirs;
public Employee (string name)
{
Junoirs = new List<Employee>();
Name = name;
}
static void Main(string[] args)
{
Employee tom = new Employee("Tom");
Employee marry = new Employee("Marry");
Employee jhon = new Employee("Jhon");
Employee foo = new Employee("Foo");
Employee bar = new Employee("Bar");
tom.Junoirs.AddRange(new Employee[] { marry, jhon });
marry.Boss = tom;
jhon.Boss = tom;
marry.Junoirs.AddRange(new Employee[] { foo, bar });
foo.Boss = marry;
bar.Boss = marry;
}
}
這是自反關聯的一個有效的例子嗎? 我如何自動添加tom
作爲Boss
員工marry
和jhon
我將它們添加到湯姆的列表Junoirs
?
請將該類的名稱更改爲「Juniors」 – Phiter
創建一個名爲例如「 「AddJunior(...)」它設置關係的兩邊 – stuartd
@PhiterFernandes btw'Junoirs'不是一個類,感謝指出錯字,雖然 –