在没有这个的情况下在c#中使用Constructor

时间:2015-02-15 09:07:51

标签: c#

我有一段代码,我声明了默认构造函数和参数构造函数。

Student_APP Secondstudent = new Student_APP("2345");
            Secondstudent.score1 = 30;
            Secondstudent.score2 = 20;
            Secondstudent.score3 = 10;
            Console.WriteLine("Student Number: " + Secondstudent.studentNumber + "\nAverage: {0:F1}",
Secondstudent.CalculateAverage());
            Student_APP Thirdstudent = new Student_APP("5432", "xyz", "zxy");
            Thirdstudent.major = "Maths";
            Thirdstudent.score1 = 97;
            Thirdstudent.score2 = 56;
            Thirdstudent.score3 = 76;
            Console.WriteLine("\n\n Third student ");
            Console.WriteLine(Thirdstudent);
            Student_APP LastStudent = new Student_APP("2255", "yxx", "xyy",94,54,74,"CS");
            //LastStudent.major = "CS";
            //LastStudent.score1 = 94;
            //LastStudent.score2 = 54;
            //LastStudent.score3 = 74;

我这里没有使用过这个关键字,但是我得到了像

这样的编译错误
  

错误4'StudentAppWithoutgetset.Student_APP'不包含   带有7个参数的构造函数C:\ Users \ hariharan.v \ Desktop \ Office   Training \ MyfirstProgCarpet \ StudentApp \ StudentAppWithoutgetset \ StudentAppWithoutgetset \ Program.cs 47 39 StudentAppWithoutgetset

请帮帮我。

4 个答案:

答案 0 :(得分:1)

你有一个带7个参数的构造函数吗? 尝试添加:

public Student_APP(string p1, string p2, string p3, int p4, int p5, int p6, string p7)
{
    //...
}

答案 1 :(得分:1)

与此关键字无关的问题,但它与自己的构造函数有关。 在你的类中,没有构造函数可以使用7个参数,因此在将它用于对象之前,应该在类中编写它。

public Student_APP(string p1, string p2, string p3, int p4, int p5, int p6, string p7)
{
    //...
}

答案 2 :(得分:0)

如果我理解正确,每次你做一个新的,例如

Student_APP LastStudent = new Student_APP("2255", "yxx", "xyy",94,54,74,"CS");

您正在实例化Student_APP的对象。您在括号("2255", "yxx", "xyy",94,54,74,"CS")中传递的参数和错误消息似乎表明您需要Student_APP的构造函数,该构造函数包含7个参数。

所以在课堂Student_APP中你需要:

public Student_APP(string firstParam, string secondParam, string thirdParam, int fourthParam, int fifthParam, int sixthParam, string seventhParam)
{
    _firstParam = firstParam;
    _secondParam = secondParam;
    //....
    _seventhParam = seventhParam;
}

您能否举例说明'this'的含义,以便我们更清晰地解决问题。我认为这是误解的根源。

答案 3 :(得分:0)

假设您的Student_APP类是这样的

public class Student_APP
{
    // Public properties with get/set and an automatic backing field
    public int score1 {get; set;}
    public int score2 {get; set;}
    public int score3 {get; set;}
    public string major {get; set;}

    // Internal properties
    private int _id;
    private string _surname;
    private string _name;
    public Student()
    {}
    public Student(int idnumber)
    {
        _id = idnumber;
    }
    public Student(int idnumber, string surname, string name)
    {
        _id = idnumber;
        _surname = surname;
        _name = name;
    }
}

现在,您可以使用Object and Collection Initializer syntax

声明并初始化此类的实例
Student_APP s = new Student_APP(2255, "xyz", "xyz") {score1 = 94, score2=54, score3=74, major = "CS"};

当然,如果您的目的是消除Student_APP类的公共属性,那么您需要添加一个带有7个参数的appropriace构造函数,如其他答案中所述。

在上面的示例中,Student_APP类有三个构造函数,但它们都没有代码所需的7个参数签名,因此您得到了上述错误。