2013-05-13 4 views
1

그래서 (firstName, lastName, mother, father, siblings, spouse) 정보가 들어있는 Person 클래스가 있고 Person 클래스의 사람들을 사전에 추가하려고합니다. 사전을 통해 구문 분석을 통해 객체의 관계를 확인하고 싶습니다 (즉, 주어진 사람, 사촌, 형제 등을 찾습니다). 내 질문은 두 가지입니다 : 1) 내 사전 <을 설정해야합니까?> 및 2) 목록에서 각 사람의 속성에 액세스하려면 어떻게합니까? 내가 먼저 시도 :C#에서 사전 내의 객체를 비교 분석하는 방법

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> 같은 것을 시도하고 내가 같은 이름을 가진 사람의 모든 경기를 일단, 다음 등 어머니, 아버지에 대한 사전 검색을 시작해야 ???. 이것은 몹시 천천히 가고 옳은 길로 보이지 않습니다.
편집 :

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() 
    { } 
} 

} 
+0

으악, 내 사전 선언했다 : 사전 <문자열, 개체> 사전 = 새로운 사전 <문자열, 개체>(); – user2359626

+0

여러분의 요구는 무엇입니까? Person 객체 중 하나를 속성으로 가져올 수있는 것이 있습니까? – jarvanJiang

+0

왜 사전이 필요합니까? 트리 데이터 구조가 필요한 것 같습니다. –

답변

-1

당신이 주장하는 경우 : 여기에 (나는 모든 사용자 입력 등을 나중에 처리 할거야, 난 그냥이 설정에있어 명심) 내 사람 클래스와 다른 클래스 중 하나입니다 사전을 사용하여,이 방법으로, new Dictionary<string, Person>()로 사전을 정의해야합니다 당신이 할 수있는이 같은 루프 사전 :

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

. 또는이

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

처럼 Person로 변환 object에 의해 new Dictionary<string, object>()을 사용할 수 있습니다.

첫 번째 해결 방법을 제안합니다. 두 번째 방법은 포장 및 포장 풀기에 추가 비용이 부과됩니다.

편집 : 중요하게 dictionarykey가 고유해야합니다

+1

에 대한 인터뷰에서 질문이었습니다. 컴파일되지 않습니다. 당신 "person"변수는'KeyValuePair '을 가질 것입니다. – joshuahealy

+0

"포장 및 포장 풀기"? 나는 당신이 복싱과 unboxing, 여기에 발생하지 않습니다 가정합니다. –

+0

KeyValuePair 사용하기 (Person) person.Value.Father/Mother/Sibling/Spouse를 인쇄 할 수 없습니다 : Console.WriteLine ("{0} {1} {2} {3}", person.Key, (Person) person.Value.father, person.Value.mother, person.Value.sibling); // 계통 학을 인쇄합니다. 아버지, 계보, 어머니 등 – user2359626

1

내 잘못.

그래서, 당신이 의심되는 경우 동일한 first name 다음 당신은 first namekey으로 만 사용하지 못할 수도 가진 두 개 이상의 Person있을 것입니다.

는 따라서 keystring.concat(firstName, lastName) 다시이 조합을 가정 같은 Person 속성의 조합이 될 수있는 고유 또는 (GUID 같은) 일부 random number.

편집 :

당신이 각 클래스의, 당신의 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); 
+0

좋아, 나는이 시점을 놓쳤다. 그러나 나는 무엇을 가치로 사용할 것인가? 사전에 저장된 객체의 속성에 어떻게 액세스합니까? – user2359626

+0

'새 사전 '은 좋을 것입니다. 왜 당신이 (object)를 사용해야하는지 이유는 알지 못합니다. (hv에게 질문하지 않는 한) .. –

+0

나는 여전히 인쇄하려고하는 데 문제가 있습니다. 각 Person (엄마, 아빠 등)의 Console 행에 대한 속성 : Console.WriteLine ("{0} {1} {2} {3}", person.Key, (Person) person.Value.father, person .Value.mother, person.Value.sibling); – user2359626