用c#设计一个学生类 Student 和它的一个子类 Undergracduate ,并进行测试。提示: (1) Student 类有 Name (姓名)属性和 Age (年龄)属性和两个方法,一个包含两个参数的构造方法,用于给 Name 属性和 Age 属性赋值,一个 Show ()方法打印 Student 类的属性信息。 (2)本科生类 Undergraduate 增加一个 Degree (学位)属性。一个包含3个参数的构造方法,前两个参数用于给继承的 Name 属性和 Age 属性赋值,第三个参数用于给 Degree 属性赋值;一个 Show ()方法用于打印 Undergraduate 的属性信息。 (3)在测试类中分别创建 Student 对象和 Undergraduate 对象,调用它们的 Show ()方法。
时间: 2024-02-28 10:55:46 浏览: 75
好的,以下是代码实现:
```
using System;
public class Student
{
public string Name { get; set; }
public int Age { get; set; }
public Student(string name, int age)
{
Name = name;
Age = age;
}
public void Show()
{
Console.WriteLine($"Name: {Name}, Age: {Age}");
}
}
public class Undergraduate : Student
{
public string Degree { get; set; }
public Undergraduate(string name, int age, string degree) : base(name, age)
{
Degree = degree;
}
public new void Show()
{
Console.WriteLine($"Name: {Name}, Age: {Age}, Degree: {Degree}");
}
}
public class Test
{
public static void Main()
{
Student student = new Student("Tom", 20);
student.Show();
Undergraduate undergraduate = new Undergraduate("Jerry", 22, "Bachelor");
undergraduate.Show();
}
}
```
运行结果:
```
Name: Tom, Age: 20
Name: Jerry, Age: 22, Degree: Bachelor
```
阅读全文