[英]Adding an object to a collection: collection is declared in different class
class Family
{
public int FamilyId { get; set; }
public string Nickname { get; set; }
public Adults father { get; set; }
public Adults mother { get; set; }
public List<Person> Children { get; set; }
}
class Person
{
public int Age { get; set; }
public string Name { get; set; }
}
class Adults : Person
{
public string Job { get; set; }
public int LicNumber { get; set; }
}
class Program
{
static void Main(string[] args)
{
List<Family> Families = new List<Family>();
Adults father = new Adults { Name = "Jim", Age = 34, Job = "Programmer", LicNumber = 2344454 };
Adults mother = new Adults { Name = "Amy", Age = 33, Job = "Nurse", LicNumber = 88888 };
Family fam1 = new Family { Nickname = "Family One", FamilyId = 1, father = father, mother = mother , };
Person child1 = new Person { Name = "Bob", Age = 4 };
Families.Add(fam1);
PrintFamily(fam1);
}
private static void PrintFamily(Family family)
{
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");
Console.WriteLine($"( I WANT TO PRINT KIDS NAME AND AGE HERE, but it's not LETTING ME);
//I want to print Kids information as well. Like I did it with parents.
Console.ReadKey();
}
}
我的問題是:如何將Child1添加到集合中? // Family類中的兒童集合? 我在這里做錯了什么? 我基於Person類創建了一個對象,並輸入了姓名和年齡信息,但這並不能讓我打印孩子的信息。
在“ printFamily”方法中,您永遠不會訪問child1對象的成員屬性。 我建議為類型為Person的對象添加第二個參數,然后使用另一行代碼訪問Person對象的成員屬性。
您可以這樣做:首先將兒童列表初始化:
class Family
{
public int FamilyId { get; set; }
public string Nickname { get; set; }
public Adults father { get; set; }
public Adults mother { get; set; }
public List<Person> Children = new List<Person>();
}
接下來添加Children:
static void Main(string[] args)
{
List<Family> Families = new List<Family>();
Adults father = new Adults { Name = "Jim", Age = 34, Job = "Programmer", LicNumber = 2344454 };
Adults mother = new Adults { Name = "Amy", Age = 33, Job = "Nurse", LicNumber = 88888 };
Family fam1 = new Family { Nickname = "Family One", FamilyId = 1, father = father, mother = mother , };
Person child1 = new Person { Name = "Bob", Age = 4 };
fam1.Children.add(child1); //Here add child to collection
Families.Add(fam1);
PrintFamily(fam1);
}
下一張兒童名單:
private static void PrintFamily(Family family)
{
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:");
//read list and pirnt
foreach(Person per in family.Children){
Console.WriteLine($"{per.Name} ({per.Age})");
}
Console.ReadKey();
}
希望對您有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.