用对象填充数组(日期时间)

本文关键字:日期 时间 数组 对象 填充 | 更新日期: 2023-09-27 18:36:08

我的错误在于我的for语句,语法错误是说我的构造函数不接受三个参数。

        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" };
        DateTime[] birth = new DateTime[20];
        birth[0] = new DateTime(1987, 22, 7);
        birth[1] = new DateTime(1962, 15, 9);
        birth[2] = new DateTime(1984, 21, 4);
        birth[3] = new DateTime(1977, 24, 1);
        birth[4] = new DateTime(1983, 12, 8);
        birth[5] = new DateTime(1979, 14, 1);
        birth[6] = new DateTime(1965, 19, 9);
        birth[7] = new DateTime(1968, 21, 2);
        birth[8] = new DateTime(1980, 22, 7);
        birth[9] = new DateTime(1982, 20, 7);
        birth[10] = new DateTime(1984, 19, 4);
        birth[11] = new DateTime(1968, 11, 9);
        birth[12] = new DateTime(1968, 21, 8);
        birth[13] = new DateTime(1975, 5, 2);
        birth[14] = new DateTime(1945, 15, 3);
        birth[15] = new DateTime(1969, 14, 6);
        birth[16] = new DateTime(1987, 141, 4);
        birth[17] = new DateTime(1976, 23, 5);
        birth[18] = new DateTime(1989, 28, 6);
        birth[19] = new DateTime(1988, 23, 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]);
        }

我的类看起来像这样

public class Person
{
    private string _firstname;
    private string _lastname;
    private DateTime _birthDate;
    public void Personn(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 void Personn(string firstname, string lastname, DateTime birthDate)

 public Person(string firstname, string lastname, DateTime birthDate)

你认为的构造函数只是一个方法。类名拼写错误。

更改此设置:

    public void Personn(string firstname, string lastname, DateTime birthDate)

自:

    public Person(string firstname, string lastname, DateTime birthDate)

检查你的构造函数,它不应该有返回类型,应该与类同名。

我认为你应该用这行代替:

 public void Personn(string firstname, string lastname, DateTime birthDate)

这个:

 public Person(string firstname, string lastname, DateTime birthDate)

因为目前它是一个 void 方法而不是构造函数,但是您有一个默认构造函数,在这种情况下没有参数。这可能是您问题的补充!

安德鲁

构造函数没有任何返回类型。从构造函数中删除void