编写一个C#程序定义一个表示学生的类student,包括域(字段)“学号” “班号” “姓名” “性别” “年龄” ,方法 “获得学号” “获得班号” “获得性别” “获得年龄” “修改年龄”。
时间: 2024-06-15 19:09:23 浏览: 14
在C#中定义一个表示学生的类student,可以按照以下方式编写:
```csharp
public class Student
{
// 域(字段)
private string studentId;
private string classId;
private string name;
private string gender;
private int age;
// 构造函数
public Student(string studentId, string classId, string name, string gender, int age)
{
this.studentId = studentId;
this.classId = classId;
this.name = name;
this.gender = gender;
this.age = age;
}
// 方法
public string GetStudentId()
{
return studentId;
}
public string GetClassId()
{
return classId;
}
public string GetGender()
{
return gender;
}
public int GetAge()
{
return age;
}
public void ModifyAge(int newAge)
{
age = newAge;
}
}
```
这样,你就定义了一个名为Student的类,包括了域(字段)"学号"、"班号"、"姓名"、"性别"、"年龄",以及方法"获得学号"、"获得班号"、"获得性别"、"获得年龄"、"修改年龄"。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)