学生类代码:

class Student
{
    public string Name { get; set; }
    public int Age { get; set; }

    public void SetName(string name)
    {
        this.Name = name;
    }

    public void SetAge(int age)
    {
        this.Age = age;
    }

    public void Show()
    {
        Console.WriteLine("Name: {0}", this.Name);
        Console.WriteLine("Age: {0}", this.Age);
    }
}

子类代码:

class GraduateStudent : Student
{
    public string Degree { get; set; }

    public GraduateStudent(string name, int age, string degree)
    {
        this.Name = name;
        this.Age = age;
        this.Degree = degree;
    }

    public void Show()
    {
        base.Show();
        Console.WriteLine("Degree: {0}", this.Degree);
    }
}

测试类代码:

class Program
{
    static void Main(string[] args)
    {
        // 创建学生对象
        Student s = new Student();
        s.SetName("张三");
        s.SetAge(18);
        s.Show();

        // 创建研究生对象
        GraduateStudent gs = new GraduateStudent("李四", 25, "硕士");
        gs.Show();

        Console.ReadKey();
    }
}
``

原文地址: https://www.cveoy.top/t/topic/ckTZ 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录