1

所以我有一个包含信息的 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()
    { }
}

}
4

2 回答 2

1

重要的key是 adictionary必须是唯一的。

所以,如果你怀疑你会有多个Person相同的,first name那么你可能不能只first name使用key.

因此,key可以是Person属性的组合,例如string.concat(firstName, lastName)再次假设此组合是唯一的,也可以是某些属性random number(例如 GUID)。

编辑:

为了帮助打印,您可以覆盖ToString每个班级的 , 。下面是一个例子

public class Father : Person
    {
        private string name;
        public Father(string Name)
        {
            name = Name;

        }
        public Father()
        { }

        public override string ToString()
        {
            return this.name;
        }
    }

这应该可以工作:

Console.WriteLine("{0} {1} {2}", person.Value.father, person.Value.mother, person.Value.sibling);
于 2013-05-13T03:10:29.287 回答
-1

如果你坚持使用字典,你应该定义字典为new Dictionary<string, Person>(),这样你可以像这样循环字典:

foreach (var person in dictionary)
{
    if(person.Value.firstName=="somename")
    // do something
}

. 或者您可以使用new Dictionary<string, object>()转换objectPerson这样的

foreach (var person in dictionary)
{
    Person _person = (Person)person.Value;
    if(_person.firstName=="somename")
    // do something
}

.

我建议第一种解决方案,第二种方式在打包和拆包方面会产生额外的费用。

编辑:我的错

于 2013-05-13T02:38:27.980 回答