如何用静态方法打印收藏品?

时间:2018-10-13 02:19:32

标签: c#

class Program
{
    static void Main(string[] args)
    {
        List<Family> Families = new List<Family>();
        Family fam1 = new Family();
        fam1.Nickname = "Family One";
        fam1.FamilyId = 1;
        Adults father = new Adults();
        father.name = " Jim";
        fam1.father = father;
        Adults mother = new Adults();
        mother.name = "Amy";
        fam1.mother = mother;

        fam1.father.age = 34;
        fam1.mother.age = 33;
        fam1.father.job = "Programmer";
        fam1.mother.job = "Nurse";
        fam1.father.licNumber = 2344454;
        fam1.mother.licNumber = 88888;

        List<Person> Children = new List<Person>();

        Person Child1 = new Person();
        Child1.name = "Bob";
        Child1.age = 3;

        Children.Add(Child1);                    
        Families.Add(fam1);

        foreach (var family in Families)
        {
            Console.WriteLine($"{family.Nickname} ({family.FamilyId})");
            Console.WriteLine("Parents : ");
            Console.WriteLine($"{fam1.father.name} - {fam1.father.job} - { fam1.father.licNumber}");
            Console.WriteLine($"{fam1.mother.name} - {fam1.mother.job} - {fam1.mother.licNumber}");
            Console.WriteLine("Kids");
            Console.WriteLine($"{Child1.name} - {Child1.age}");
        }
    }
}

如您所见,我可以通过foreach在控制台上打印此内容。

但是,我试图创建一种可以采用参数(在这种情况下为family)并像在foreach块中那样打印家庭信息的方法。

PrintFamily(fam1);

private static void PrintFamily(Family family)
{
    //I'm stuck here
}

3 个答案:

答案 0 :(得分:1)

您的代码中确实有很多事情需要改进。您需要尊重pascal大小写的属性名称。同样,应该使用对象初始化器,而不是部分实例化对象然后进入其属性。它消除了很多行,使初始化和在一个地方完成的工作更有意义。

        public static void Main(string[] args)
    {
        List<Family> Families = new List<Family>();
        Adults father = new Adults { Name = "Jim", Age = 34, Job = "Programmer", LicNumber = "2344454" };
        Adults father = new Adults { Name = "Amy", Age = 33, Job = "Nurse", LicNumber = "88888" };
        Family fam1 = new Family { Nickname = "Family One", FamilyId = 1, Father = father, Mother = mother };
    }

    private static void DisplayFamilyMemberInformation(Family familyMember)
    {
        Console.WriteLine($"{family.Nickname} ({family.FamilyId})");
        Console.WriteLine("Prents : ");
        Console.WriteLine($"{fam1.father.name} - {fam1.father.job} - { fam1.father.licNumber}");
        Console.WriteLine($"{fam1.mother.name} - {fam1.mother.job} - {fam1.mother.licNumber}");
    }

答案 1 :(得分:0)

尝试一下:

<View style={{ height: '100%', width: '100%' }}>
  <Text>Other content</Text>
  <ScrollView contentContainerStyle={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
    <View style={{ flexDirection: 'row', flexWrap: 'wrap', padding: 20, backgroundColor: 'red' }}>
      <Text>children</Text>
    </View>
  </ScrollView> 
</View>

答案 2 :(得分:0)

“您还能教我如何打印孩子吗?”

A ..同样的错误杰夫·塞申斯(Jeff Sessions)去年5月在墨西哥边境犯了同样的错误。如果您这样定义儿童,他们将会迷路!

您已经定义了FamilyId。 在您的个人记录中也使用FamilyId ,例如

class Person
{
    public string name;
    public int age;
    public int FamilyId; // THIS !
}

确保像这样将孩子与家人联系起来

        Person Child1 = new Person();
        Child1.name = "Bob";
        Child1.age = 3;
        Child1.FamilyId = fam1.FamilyId; // THIS ! connect it
        Children.Add(Child1);

..这将允许您报告孩子,例如

    static void Report(List<Family> Families, List<Person> children)
    {
        foreach (var family in Families)
        {
            Console.WriteLine($"{family.Nickname} ({family.FamilyId})");
            Console.WriteLine("Prents : ");
            Console.WriteLine($"{family.father.name} - {family.father.job} - { family.father.licNumber}");
            Console.WriteLine($"{family.mother.name} - {family.mother.job} - {family.mother.licNumber}");
            Console.WriteLine("Kids");
            foreach (Person child in children)
              if (child.FamilyId == family.FamilyId)
                 Console.WriteLine("Child: " + child.name);
         }
    }