所以我有一个包含信息的 Person 类,(firstName, lastName, mother, father, siblings, spouse)
我想将 Person 类的人添加到字典中。我想通过字典比较解析以确定对象的关系(即给定一个人,找到他们的表亲,兄弟姐妹等)。我的问题有两个:1)我应该如何设置我的 Dictionary<...> 和 2)我如何访问列表中每个人的属性?我第一次尝试:
Dictionary<string, object> dictionary = new Dictionary<string, object>();
var human = Person.AddPerson(); // Person.AddPerson() returns a new instance of a Person
dictionary.Add(human.name, human) // setting the key to full name, value to Person object.
我是否应该尝试一下Dictionary<string, string>
,<firstName, lastName>
一旦我得到所有同名的人的匹配项,然后开始在字典中搜索母亲、父亲等????这似乎非常缓慢,而且不是正确的方法。
编辑:这是我的 Person 类和其他类之一(请记住,我只是在设置它,稍后我将处理所有用户输入等):
public class Person
{
public string name { get; set;}
public Mother mother { get; set; }
public Father father { get; set; }
public Spouse spouse { get; set; }
public Sibling sibling { get; set; }
//List<Sibling> siblings = new List<Sibling>();
public Person()
{ }
public static Person AddPerson()
{
Person newPerson = new Person();
Console.WriteLine("Enter name:");
newPerson.name = Console.ReadLine().Trim();
Console.WriteLine("Enter mother's name:");
string input = Console.ReadLine().Trim();
Mother mom = new Mother(input);
newPerson.mother = mom;
Console.WriteLine("Enter father's name:");
string input1 = Console.ReadLine().Trim();
Father dad = new Father(input1);
newPerson.father = dad;
Console.WriteLine("Enter sibling's name:");
string input2 = Console.ReadLine().Trim();
Sibling sib = new Sibling(input2);
newPerson.sibling = sib;
Console.WriteLine("Enter spouse's name:");
string input3 = Console.ReadLine().Trim();
Spouse partner = new Spouse(input3);
newPerson.spouse = partner;
return newPerson;
}
}
public class Sibling : Person
{
private string name;
public Sibling(string Name)
{
name = Name;
}
public Sibling()
{ }
}
public class Mother : Person
{
private string name;
public Mother(string Name)
{
//Mother mom = new Mother();
name = Name;
}
public Mother()
{ }
}
public class Father : Person
{
private string name;
public Father(string Name)
{
name = Name;
}
public Father()
{ }
}
public class Spouse : Person
{
private string name;
public Spouse(string Name)
{
name = Name;
}
public Spouse()
{ }
}
}