在简单的学校系统中,访问器和变量的以下问题的解决方法是什么? [英] What is the fix to the following problems with Accessors and Mutators in a simple school system?
问题描述
大家好,
我正在学习C#,现在我正在尝试通过开发一个简单的学校系统来练习所学的知识.我遇到了很多问题,我也不知道为什么.像这样编译它们时,我有很多错误:
错误11``SchoolSystem.Student.setGrade(int)''的最佳重载方法匹配具有一些无效参数
请帮我.
我的代码是:
Hello everybody,
I am learning C# and now I am trying to practice what I learned by developing a simple school system. I faced a lot of problems and I do not know why. I have a lot of errors when I compiled them like:
Error 11 The best overloaded method match for ''SchoolSystem.Student.setGrade(int)'' has some invalid arguments
Please help me.
My code is:
using System;
namespace SchoolSystem {
public class Student {
//Variables
string name;
string id;
double grade;
//Constructors
public Student() : this("Default Constructor") { }
public Student(string name)
{
this.Name = name;
}
public Student(string name, string id)
{
this.Name = name;
this.Id = id;
}
public Student(string name, string id, double grade) {
this.Name = name;
this.Id = id;
this.Grade = grade;
}
//Accessors
public string getName(){
return name;
}
public string getId(){
return id;
}
public int getGrade()
{
return grade;
}
//Mutators
public string setName(string newName){
name = newName;
}
public string setId(string newId){
id = newId;
}
public int setGrade(int newGrade){
grade = newGrade;
}
}
public class SchoolSystem{
public static void Main(String [] args){
Console.WriteLine("-----------------------------------");
Console.WriteLine("Welcome to Dhahran Secondary School");
Console.WriteLine("-----------------------------------");
Student s1 = new Student();
Console.WriteLine("Please enter the student name: ");
s1.setName(Console.ReadLine());
Console.WriteLine("Please enter the student id: ");
s1.setId(Console.ReadLine());
Console.WriteLine("Please enter the student grade: ");
s1.setGrade(Console.ReadLine());
Student s2 = new Student();
Console.WriteLine("Please enter the student name: ");
s2.setName(Console.ReadLine());
Console.WriteLine("Please enter the student id: ");
s2.setId(Console.ReadLine());
Console.WriteLine("Please enter the student grade: ");
s2.setGrade(Console.ReadLine());
Student s3 = new Student();
Console.WriteLine("Please enter the student name: ");
s3.setName(Console.ReadLine());
Console.WriteLine("Please enter the student id: ");
s3.setId(Console.ReadLine());
Console.WriteLine("Please enter the student grade: ");
s3.setGrade(Console.ReadLine());
Console.WriteLine("The System Information");
Console.WriteLine("The information of: {0}", s1.getName(), "\nID: ", s1.getId(), "\nGrade: ", s1.getGrade());
Console.WriteLine("The information of: {0}", s2.getName(), "\nID: ", s2.getId(), "\nGrade: ", s2.getGrade());
Console.WriteLine("The information of: {0}", s3.getName(), "\nID: ", s3.getId(), "\nGrade: ", s3.getGrade());
Console.ReadLine();
}
}
}
推荐答案
很抱歉,我看到了一个未提出的问题的答案,所以我再给它一个镜头: )
问题是这些线
Sorry I see gave an answer to a question that wasn''t asked, so I''ll give it another shot :)
the problem is these lines
s1.setGrade(Console.ReadLine());
s2.setGrade(Console.ReadLine());
s3.setGrade(Console.ReadLine());
Console.ReadLine()
返回一个字符串,但setGrade
接受一个整数,您可以使用 Converter.ToInt32 [ ^ ]将字符串转换为整数,但如果不能,则会引发异常,因此您可能想使用 int.TryParse [ ^ ]例如:
Console.ReadLine()
returns a string but setGrade
accepts an integer you can use Converter.ToInt32[^] to convert a string to an integer, but it throws an exception if it cannot so you may want to use int.TryParse[^] eg.:
int i;
if(int.TryParse("100", out i))
{
// the string is representing an integer, the integer is now in i
}
else
{
// the string is NOT representing an integer, i is 0
}
这是原始的错误"答案,但是您可能仍然希望看到它.
好吧,它们本身没有问题.但是在C#中,我们更喜欢使用属性.
属性(C#编程指南)
[^ ]
因此,而不是
Here is the original "wrong" answer, but you may wnat to see it anyhow.
Well there is no problem with them per se. But in C# we like to use properties instead.
Properties (C# Programming Guide)
[^]
So instead of
public string getName(){
return name;
}
public string setName(string newName){
name = newName;
}
你可以写
you can write
public string Name
{
get { return name; }
set { name = value; }
}
甚至
or even
public string Name {get; set; }
如果您不使用字段name
.编译器将为auto属性创建后备字段.
if you do not use the field name
. The compiler will make the backing field for the auto property.
这篇关于在简单的学校系统中,访问器和变量的以下问题的解决方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!