-1

我的课是:

public class Person
{
    private string _firstname;
    private string _lastname;
    private DateTime _birthdate;

    public Person(string firstname, string lastname, DateTime birthdate)
    {
        _firstname = firstname;
        _lastname = lastname;
        _birthdate = birthdate;
    }

    public string Firstname
    { get { return _firstname; } }

    public string Lastname
    { get { return _lastname; } }

    public DateTime Birthdate
    { get { return _birthdate; } }

这是我为获取每个人的年龄而访问的方法:

public int getAge()
{
    TimeSpan ts =DateTime.Now - _birthdate;
    int year = (int)ts.TotalDays / 365;
    return year;
}

我的表格:

namespace May22_StructClassObj_HW
{
    public partial class Form1 : Form
    {
        DateTime[] birth = new DateTime[20];
        Person[] People = new Person[20];

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // Create a class Person with the following fields _firstname, _lastname, _birthDate(DateTime Type) Add constructor, properties (get only) and a method GetAge that returns the age (int) of a person. 
            // In Form1, Create an array of Person objects to hold 20 people
            // In Form1_Load: Populate the array with 20 Person objects

            // Add Gui to display all the people in the list (first and last names, birthdate, and age
            // Add Gui 
            //people[0] = new Person("John","Stockton", DateTime.)

            string[] first = new string[20] { "Scott", "Ramona", "Todd", "Melissa", "Naomi", "Leland", "Conor", "Julie", "Armondo", "Leah", "Frank", "Peter", "Ila", "Mandy", "Sammy", "Gareth", "Garth", "Wayne", "Freddy", "Mark" };

            string[] last = new string[20] { "Kennedy", "Kennedy", "Kennedy", "Kennedy", "Kennedy", "Kennedy", "Carrel", "MaloyTheBeautiful", "Johnson", "Smith", "Sinatra", "Clemens", "Eels", "Johnson", "Eels", "Thompson", "Brooks", "World", "Crugar", "Thomas" };

            birth[0] = new DateTime(1987, 2, 7);
            birth[1] = new DateTime(1962, 5, 9);
            birth[2] = new DateTime(1984, 1, 4);
            birth[3] = new DateTime(1977, 4, 1);
            birth[4] = new DateTime(1983, 2, 8);
            birth[5] = new DateTime(1979, 4, 1);
            birth[6] = new DateTime(1965, 9, 9);
            birth[7] = new DateTime(1968, 1, 2);
            birth[8] = new DateTime(1980, 2, 7);
            birth[9] = new DateTime(1982, 2, 7);
            birth[10] = new DateTime(1984, 12, 4);
            birth[11] = new DateTime(1968, 11, 9);
            birth[12] = new DateTime(1968, 2, 8);
            birth[13] = new DateTime(1975, 5, 2);
            birth[14] = new DateTime(1945, 5, 3);
            birth[15] = new DateTime(1969, 4, 6);
            birth[16] = new DateTime(1987, 1, 4);
            birth[17] = new DateTime(1976, 3, 5);
            birth[18] = new DateTime(1989, 8, 6);
            birth[19] = new DateTime(1988, 2, 9);

            // Populate Array Person[] People = new Person[20];   
            for (int i = 0; i < People.Length; i++)
            {
                People[i] = new Person(first[i], last[i], birth[i]);
            }

        }

        private void btnDisAll_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < People.Length; i++)
            {
                richTxtDisplay.AppendText("Name: " + People[i].Firstname + "\t" + People[i].Lastname + "\t" + " BirthDate: " + People[i].Birthdate + "\n\n");
                //richTxtDisplay.AppendText(People[i].ToString());
                //richTxtDisplay.AppendText(People[i].Firstname + People[i].Lastname + People[i].Birthdate + "\n");
            }
        }
    }

这里的问题:

这是我用来调用该方法的按钮,并让它告诉我 Person 数组中每个人的年龄。但我知道这是错误的。所以有人请指导我完成它。

private void btnGetAge_Click(object sender, EventArgs e)
{
    for (int i = 0; i < People.Length; i++)
    {
        Person Per = 
            new Person(People[i].Firstname, People[i].Lastname, People[i].Birthdate);
        Per.getAge();
    }
}

我已经列出了我的问题所在。基本上我想做的就是调用该方法并在我的 Person 数组中显示每个人的所有年龄。有二十个人,我想显示他们的年龄。我确实相信我的代码非常适合在我的班级中获得我的方法的年龄,但我不确定我是否正确地创建了一个新实例以便使用该方法。

4

4 回答 4

4

您可以使用 LINQ 来获取所有人的年龄。但由于您没有告诉我们您想在哪里显示此信息以及您想如何加入所有年龄段,我假设您想在 TextBox (或其他地方)中将其显示为字符串并将行拆分为Environment.NewLine

var allPersonAge = People.Select(p => p.GetAge());
richTxtDisplay.Text = string.Join(Environment.NewLine, allPersonAge);

顺便说一句,这是计算年龄的更准确的方法:

public int getAge()
{
    DateTime now = DateTime.Today;
    int age = now.Year - _birthdate.Year;
    if (_birthdate> now.AddYears(-age)) age--;
    return age;
}
于 2012-06-06T12:45:17.140 回答
2
for (int i = 0; i < Person.Length; i++)
{
   Person[i].getAge();
}
于 2012-06-06T12:43:11.197 回答
1
foreach(Person person in People)
{
int age = person.getAge();
//Do something with the age
}
于 2012-06-06T12:46:25.530 回答
0

如何使用 LINQ

List<int> result = People.Select(p => p.GetAge()).ToList();
于 2012-06-06T12:47:16.440 回答