在c#中使用构造函数而不使用此
我有一段代码是我声明的默认构造函数和参数构造函数在c#中使用构造函数而不使用此,c#,C#,我有一段代码是我声明的默认构造函数和参数构造函数 Student_APP Secondstudent = new Student_APP("2345"); Secondstudent.score1 = 30; Secondstudent.score2 = 20; Secondstudent.score3 = 10; Console.WriteLine("Student Number: " + Seco
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“StudentApp WithOutGetSet.Student_APP”不包含
采用7个参数的构造函数C:\Users\hariharan.v\Desktop\Office
Training\MyFirstProgrammer\StudentApp\StudentApp Without GetSet\StudentApp Without GetSet\Program.cs 47 39 StudentApp Without GetSet
请帮帮我。如果我理解正确,每次你做一个新的,例如
Student_APP LastStudent = new Student_APP("2255", "yxx", "xyy",94,54,74,"CS");
您正在实例化学生应用程序
的对象。在括号中传递的参数(“2255”、“yxx”、“xyy”、94,54,74、“CS”)
和错误消息似乎表明您需要为学生应用程序
创建一个包含7个参数的构造函数
因此,在您的课堂学生应用程序中
您需要:
public Student_APP(string firstParam, string secondParam, string thirdParam, int fourthParam, int fifthParam, int sixthParam, string seventhParam)
{
_firstParam = firstParam;
_secondParam = secondParam;
//....
_seventhParam = seventhParam;
}
请你也给我们举个例子,说明你所说的“这”是什么意思,以便我们能更清楚地回答这个问题。我认为这是误解的根源。你有一个接受7个参数的构造函数吗? 尝试添加:
public Student_APP(string p1, string p2, string p3, int p4, int p5, int p6, string p7)
{
//...
}
该问题与该关键字无关,但与它自身的构造函数有关。 在您的类中没有接受7个参数的构造函数,因此您应该在对象中使用它之前在类中编写它
public Student_APP(string p1, string p2, string p3, int p4, int p5, int p6, string p7)
{
//...
}
假设你的学生应用程序类是这样的
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;
}
}
现在可以使用
当然,如果您的目的是消除Student\u APP
类的公共属性,那么您需要添加一个适当的构造函数,该构造函数采用其他答案中解释的7个参数
在我上面的例子中,
Student\u APP
类有三个构造函数,但没有一个构造函数具有代码所需的7个参数签名,因此您得到了提到的错误。为Student\u APP
类添加代码。.“我没有在这里使用此关键字”是什么意思?在您的错误消息中,我没有看到任何与这个
关键字相关的内容。我接受您的,但我也这样编码。有什么区别,请解释一下学生应用程序Secondstudent=新学生应用程序(“2345”);Secondstudent.major=“Eng”;Secondstudent.Score 1=30;Secondstudent.Score 2=20;Secondstudent.Score 3=10;这两种方法之间没有实际区别。对象初始值设定项语法(由C#3.0引入)只是编写相同行的一种更简洁的方法。它被称为“语法糖”,你可以找到很多关于这个话题的文章,包括
Student_APP s = new Student_APP(2255, "xyz", "xyz") {score1 = 94, score2=54, score3=74, major = "CS"};