2017-06-11 51 views
2

我需要使用下面的代码创建一个家谱结构。我只能在ShowFamily方法中添加代码。我可以回大爷叔叔阿姨爸爸。但由于某种原因,我无法回到我和姐姐身边。可以在任何你帮助我知道这一定是简单的谢谢大家创建家谱结构

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Xml.Linq; 

namespace FamilyPrinter 
{ 
// Classes defining the structure of a family 
abstract class Person 
{ 
    public string Name { get; set; } 
} 

class Child : Person 
{ 
} 

class Parent : Person 
{ 
    public List<Person> Children { get; set; } 

} 

class Ancestor : Parent 
{ 

} 


class Program 
{ 
    static void Main(string[] args) 
    { 

     Ancestor myAncestor = new Ancestor() 
     { 
      Name = "GrandDad", 
      Children = new List<Person>() 
      { 
       new Child() { Name = "Aunt" }, 
       new Child() { Name = "Uncle" }, 
       new Parent() 
       { 
        Name = "Dad", 
        Children = new List<Person>() 
        { 
         new Child { Name = "Me" }, 
         new Child { Name = "Sister" } 
        } 
       } 
      } 
     }; 
     ShowFamily(myAncestor); 
    } 


    private static void ShowFamily(Ancestor a) 
    { 
     Console.WriteLine("*"+a.Name); 
     foreach (var value in a.Children) 
     { 
      Console.WriteLine("-"+value.Name); 
     }   
    } 
} 
+0

用哪个调试你观看的节目流? – lit

回答

1

这里的另一种方式,您可以使用递归(和一些缩进表示关系)做到这一点。因为所有类型都基于Person,所以我们可以使用该类型作为输入,然后在方法内部检查以查看此人是否为Parent。这将允许您从相同的方法(父母有孩子的父母有子女是父母的孩子,以及父母的兄弟姐妹)绘制多个扩展树。继你的榜样,父母有*他们的名字旁边一个星号:

private static void ShowFamily(Person person, int indent = 0) 
{ 
    var indentLines = new string(' ', indent); 

    if (person is Parent) 
    { 
     Console.WriteLine(indentLines + "*" + person.Name); 
     var parent = person as Parent; 
     foreach (var child in parent.Children) 
     { 
      ShowFamily(child, indent + 2); 
     } 
    } 
    else 
    { 
     Console.WriteLine(indentLines + "-" + person.Name); 
    } 
} 

一个更复杂的家族树的方法可能是:

static void Main(string[] args) 
{ 
    Ancestor myAncestor = new Ancestor 
    { 
     Name = "GrandDad", 
     Children = new List<Person> 
     { 
      new Parent 
      { 
       Name = "Aunt", 
       Children = new List<Person> 
       { 
        new Child { Name = "Cousin1" }, 
        new Parent 
        { 
         Name = "Cousin2", 
         Children = new List<Person> 
         { 
          new Child { Name = "FirstCousinOnceRemoved" } 
         } 
        }, 
        new Child { Name = "Cousin3" } 
       } 
      }, 
      new Child { Name = "Uncle" }, 
      new Parent 
      { 
       Name = "Dad", 
       Children = new List<Person>() 
       { 
        new Child { Name = "Me" }, 
        new Parent 
        { 
         Name = "Sister", 
         Children = new List<Person> 
         { 
          new Child { Name = "Niece" } 
         } 
        } 
       } 
      } 
     } 
    }; 

    ShowFamily(myAncestor); 
} 

输出

enter image description here

0

你应该改变你的课堂结构:

public class Person 
{ 
    public string Name { get; set; } 
    public List<Person> Children { get; set; } = new List<Person>(); 
} 

然后,任何Person可以有孩子。

现在你可以定义你的家庭是这样的:

var myAncestor = new Person() 
{ 
    Name = "GrandDad", 
    Children = new List<Person>() 
    { 
     new Person() { Name = "Aunt" }, 
     new Person() { Name = "Uncle" }, 
     new Person() 
     { 
      Name = "Dad", 
      Children = new List<Person>() 
      { 
       new Person() { Name = "Me" }, 
       new Person() { Name = "Sister" }, 
      } 
     } 
    } 
}; 

然后ShowFamily看起来像这样(在两种方法):

private static void ShowFamily(Person a) 
{ 
    ShowFamily(a, 0);  
} 

private static void ShowFamily(Person a, int level) 
{ 
    Console.WriteLine("".PadLeft(level * 4) + (a.Children.Any() ? "*" : "-") + a.Name); 
    foreach (var c in a.Children) 
    { 
     ShowFamily(c, level + 1); 
    }    
} 

现在呼吁ShowFamily(myAncestor);将输出这样的:

 
*GrandDad 
    -Aunt 
    -Uncle 
    *Dad 
     -Me 
     -Sister 

你也可以尽可能深入:

var myAncestor = new Person() 
{ 
    Name = "GrandDad", 
    Children = new List<Person>() 
    { 
     new Person() { Name = "Aunt" }, 
     new Person() { Name = "Uncle" }, 
     new Person() 
     { 
      Name = "Dad", 
      Children = new List<Person>() 
      { 
       new Person() 
       { 
        Name = "Me", 
        Children = new List<Person>() 
        { 
         new Person() { Name = "John" }, 
         new Person() 
         { 
          Name = "Jill", 
          Children = new List<Person>() 
          { 
           new Person() { Name = "Sally" }, 
           new Person() { Name = "Simon" }, 
          } 
         }, 
        } 
       }, 
       new Person() { Name = "Sister" }, 
      } 
     } 
    } 
}; 

现在ShowFamily给出了这样的:

 
*GrandDad 
    -Aunt 
    -Uncle 
    *Dad 
     *Me 
      -John 
      *Jill 
       -Sally 
       -Simon 
     -Sister