-1

我的错误在我的 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; } }
}
4

5 回答 5

4

代替

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

利用

 public Person(string firstname, string lastname, DateTime birthDate)
于 2012-06-04T09:34:02.137 回答
2

您认为构造函数只是一种方法。您拼错了班级名称。

改变这个:

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

至:

    public Person(string firstname, string lastname, DateTime birthDate)
于 2012-06-04T09:34:32.950 回答
0

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

于 2012-06-04T09:38:34.297 回答
0

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

于 2012-06-04T09:34:54.890 回答
0

我认为你应该使用而不是这一行:

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

这个:

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

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

安德鲁

于 2012-06-04T09:37:00.327 回答