编程语言
首页 > 编程语言> > .NET程序设计实验2

.NET程序设计实验2

作者:互联网

设计编写一个控制台应用程序,练习类的继承。

(1) 编写一个抽象类 People,具有”姓名”,”年龄”字段,”姓名”属性,Work 方法。

(2) 由抽象类 People 派生出学生类 Student 和职工类 Employer,继承 People 类,并

覆盖Work 方法。

(3) 派生类 Student 增加”学校”字段,派生类 Employer 增加”工作单位”字段。

(4) 在 Student 和 Employer 实例中输出各自不同的信息。

代码示例:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NET_test2_1
{
    public abstract class People{
        String name;
        String age;
        public virtual void work()
        {
            
        }
    }
    public class Student : People
    {
        String school;
        public override void work(){
            Console.WriteLine("子类学生学习中");           
    }
    }
    public class Employer : People
    {
        String workspace;
        public override void work()
        {
            Console.WriteLine("子类职工工作中");           
        }
    }
    class Program
    {
        static void Main(string[] args)
        {      
            Student a = new Student();
            Employer b=new Employer();
            a.work();
            b.work();           
            Console.ReadKey();
        }
    }
}

运行截图:

 

标签:People,System,Employer,实验,Student,using,程序设计,NET,public
来源: https://www.cnblogs.com/zyj3955/p/15409640.html