实验报告5 继承与多态.doc
文本预览下载声明
实验名称:继承与多态
实验目的:
((1)理解继承的含义,掌握派生类的定义方法和实现;
(2)理解虚函数在类的继承层次中的作用,虚函数的引入对程序运行时的影响,能够对使用虚函数的简单程序写出程序结果。
(3)编写体现类的继承性(成员变量,成员方法,成员变量隐藏)的程序;
(4)编写体现类多态性(成员方法重载,构造方法重载)的程序。
(5)理解接口和抽象类、抽象方法的定义和实现方法;
(5)理解接口和抽象类的差别。
二.上机内容:
(1)进行类的继承和基类构造方法的应用的练习;
(2)进行类的多态性练习
(3)进行抽象类和接口的练习
(4)整理上机步骤,总结经验和体会。
(5)完成实验报告。
三.上机步骤:
类的继承和基类构造方法的应用
编写一个学生和教师数据输入和显示程序,学生数据有编号、姓名、班级和成绩,教师数据有编号、姓名、职称和部门。要求将编号、姓名输入和显示设计成一个类person,并作为学生数据操作类student和教师类数据操作类teacher的基类。
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication2
{
class person
{
public string bh;//编号
public string xm;//姓名
public person(string bh, string xm)
{
this.bh = bh;
this.xm = xm;
}
public void show()
{
Console.WriteLine(姓名:{0}, xm);
Console.WriteLine(编号:{0}, bh);
}
}
class student : person
{
public string bj;//班级
public int cj;//成绩
public student(string sbh, string sxm, string sbj, int scj)
: base(sbh, sxm)
{
bh = sbh;
xm = sxm;
bj = sbj;
cj = scj;
}
public new void show()
{
Console.WriteLine(***student***);
Console.WriteLine(姓名:{0}, xm);
Console.WriteLine(编号:{0}, bh);
Console.WriteLine(班级:{0}, bj);
Console.WriteLine(成绩:{0}, cj);
}
}
class teacher : person
{
public string zc;//职称
public string bm;//部门
public teacher(string tbh, string txm, string tzc, string tbm)
: base(tbh, txm)
{
bh = tbh;
xm = txm;
zc = tzc;
bm = tbm;
}
public new void show()
{
Console.WriteLine(***teacher***);
Console.WriteLine(姓名:{0}, xm);
Console.WriteLine(编号:{0}, bh);
Console.WriteLine(职称:{0}, zc);
显示全部