C#实现抽象类的继承
1. 设计编写一个控制台应用程序,练习类的继承。
(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;
namespace C_Lab2
{
class Program
{
abstract class people {
public String name;
public int age;
public void work() {
Console.WriteLine("工作:");
}
}
class student : people {
public student(String name, int age, String school)
{
this.age = age;
this.name = name;
this.school = school;
}
String school;
public new void work()
{
Console.WriteLine("学生学习");
}
}
class employer : people
{
public employer(String name, int age, String workarea)
{
this.age = age;
this.name = name;
this.workarea = workarea;
}
String workarea;
public new void work()
{
Console.WriteLine("职工工作");
}
}
static void Main(string[] args)
{
student a = new student("黎明",18,"石家庄铁道大学");
a.work();
employer b = new employer("李来", 45, "教务处");
b.work();
}
}
}
浙公网安备 33010602011771号