c#设计一个成绩类,该类能够记录学生姓名、班级、课程名称、成绩,并能输出成绩。
时间: 2024-09-23 13:11:06 浏览: 50
在C#中,你可以设计一个名为`StudentScore`的简单类来表示学生的成绩信息。这个类可能会包含以下几个私有字段:`name`, `classRoom`, `courseName`, 和 `score`。然后你会有一个公有的构造函数用于初始化这些属性,以及一些公共方法来进行获取和设置成绩,以及输出成绩信息。下面是一个简单的示例:
```csharp
public class StudentScore
{
private string name;
private string classRoom;
private string courseName;
private double score;
// 构造函数
public StudentScore(string name, string classRoom, string courseName, double score)
{
this.name = name;
this.classRoom = classRoom;
this.courseName = courseName;
this.score = score;
}
// 获取和设置方法
public string GetName() => name;
public void SetName(string name) => this.name = name;
public string GetClassRoom() => classRoom;
public void SetClassRoom(string classRoom) => this.classRoom = classRoom;
public string GetCourseName() => courseName;
public void SetCourseName(string courseName) => this.courseName = courseName;
public double GetScore() => score;
public void SetScore(double score) => this.score = score;
// 输出成绩方法
public void PrintScore()
{
Console.WriteLine($"学生 {name},来自班级 {classRoom},在{courseName}课程中获得了分数:{score}");
}
}
```
阅读全文