.NET程序设计——面向对象程序设计01

一、任务

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

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

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

覆盖Work 方法。

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

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

二、代码

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Text;
 4 
 5 namespace Tutorial2
 6 {
 7     public abstract class People
 8     {
 9         private string name;
10         public int age;
11 
12         public string Name
13         {
14             get { return name; }
15             set { name = value; }
16             
17         }
18 
19         public abstract void Work();
20         
21     }
22 }
People.CS
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Text;
 4 
 5 namespace Tutorial2
 6 {
 7     public class Student:People
 8     {
 9         
10         public  string school;
11         
12 
13         public override void Work()
14         {
15             Console.WriteLine("我在学校学习.......");
16         }
17     }
18 }
Student.CS
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Text;
 4 
 5 namespace Tutorial2
 6 {
 7     public class Employer:People
 8     {
 9         
10         public  string workAdress;
11         
12 
13         public override void Work()
14         {
15             Console.WriteLine("我在单位工作.......");
16         }
17     }
18 }
Employer.CS

三、截图

原文地址:https://www.cnblogs.com/Lizhichengweidashen/p/14903860.html