用c#创建一个学生类,里面有如下字段:学号、姓名、年龄,并在启动类中创建学生对象,对各个字段赋值并输出
时间: 2024-09-27 07:03:37 浏览: 104
在 C# 中,我们可以创建一个名为 `Student` 的类,包含学号 (`StudentID`)、姓名 (`Name`) 和年龄 (`Age`) 这几个属性。以下是这个类的简单示例:
```csharp
public class Student
{
public int StudentID { get; set; }
public string Name { get; set; }
public int Age { get; set; }
// 构造函数用于初始化对象
public Student(int id, string name, int age)
{
StudentID = id;
Name = name;
Age = age;
}
// 输出学生信息的方法
public void ShowInfo()
{
Console.WriteLine($"学生编号:{StudentID}, 姓名:{Name}, 年龄:{Age}");
}
}
```
在主程序(通常放在 `Program.cs` 文件里)中,我们可以通过以下方式创建 `Student` 对象并为其赋值,然后输出信息:
```csharp
using System;
class Program
{
static void Main(string[] args)
{
// 创建一个学生对象
Student student = new Student(001, "张三", 18);
// 调用 ShowInfo 方法显示学生信息
student.ShowInfo();
//
阅读全文
相关推荐
![rar](https://img-home.csdnimg.cn/images/20241231044955.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)