Unknown model· unknown
from ShareGPT 90K (shared ChatGPT conversations)
C#设计一个学生类和一个计算机系学生类 (1) 具有姓名、学号,两个字段 (2) 为上述字段设置对应的属性 (3) 添加一个方法用于介绍自己特长
Private research annotation
Sign in to add a private research label or note.
- User
C#设计一个学生类和一个计算机系学生类 (1) 具有姓名、学号,两个字段 (2) 为上述字段设置对应的属性 (3) 添加一个方法用于介绍自己特长
- Assistant
以下是C#中设计一个学生类和一个计算机系学生类的示例代码:
csharp// 学生类 public class Student { // 姓名 private string name; public string Name { get { return name; } set { name = value; } } // 学号 private string studentID; public string StudentID { get { return studentID; } set { studentID = value; } } // 构造函数 public Student(string name, string studentID) { this.name = name; this.studentID = studentID; } // 自我介绍方法 public virtual void Introduce() { Console.WriteLine($"我是{name},我的学号是{studentID}。"); } } // 计算机系学生类,继承自学生类 public class ComputerStudent : Student { // 特长 private string specialty; public string Specialty { get { return specialty; } set { specialty = value; } } // 构造函数 public ComputerStudent(string name, string studentID, string specialty) : base(name, studentID) { this.specialty = specialty; } // 自我介绍方法,重写了父类的方法 public override void Introduce() { Console.WriteLine($"我是{name},我的学号是{studentID},我的特长是{specialty}。"); } } - User
写 个main函数运行上面的代码
- Assistant
以下是一个简单的
Main函数来运行上面的代码:csharpclass Program { static void Main(string[] args) { // 创建一个学生对象 Student student1 = new Student("张三", "20230001"); student1.Introduce(); // 输出 "我是张三,我的学号是20230001。" // 创建一个计算机系学生对象 ComputerStudent student2 = new ComputerStudent("李四", "20230002", "编程"); student2.Introduce(); // 输出 "我是李四,我的学号是20230002,我的特长是编程。" Console.ReadKey(); } }在上面的
Main函数中,我们先创建了一个Student对象student1,并调用了它的自我介绍方法Introduce()。接着,我们创建了一个
ComputerStudent对象student2,并调用了它的自我介绍方法Introduce(),这里输出的信息中包括特长字段Specialty。最后,我们使用
Console.ReadKey()来等待用户按下任意键,以便在控制台窗口中保留输出结果。