实验报告5 继承与多态

上传人:daj****de 文档编号:170620186 上传时间:2022-11-21 格式:DOCX 页数:12 大小:95.67KB
收藏 版权申诉 举报 下载
实验报告5 继承与多态_第1页
第1页 / 共12页
实验报告5 继承与多态_第2页
第2页 / 共12页
实验报告5 继承与多态_第3页
第3页 / 共12页
资源描述:

《实验报告5 继承与多态》由会员分享,可在线阅读,更多相关《实验报告5 继承与多态(12页珍藏版)》请在装配图网上搜索。

1、实验名称:继承与多态一实验目的:(1)理解继承的含义,掌握派生类的定义方法和实现;(2)理解虚函数在类的继承层次中的作用,虚函数的引入对程序运行时的影响,能够对使 用虚函数的简单程序写出程序结果。(3)编写体现类的继承性(成员变量,成员方法,成员变量隐藏)的程序;(4)编写体现类多态性(成员方法重载,构造方法重载)的程序。(5)理解接口和抽象类、抽象方法的定义和实现方法; (5)理解接口和抽象类的差别。二上机内容:(1)进行类的继承和基类构造方法的应用的练习;(2)进行类的多态性练习(3)进行抽象类和接口的练习(4)整理上机步骤,总结经验和体会。(5)完成实验报告。 三上机步骤:类的继承和基类

2、构造方法的应用(1) 编写一个学生和教师数据输入和显示程序,学生数据有编号、姓名、班级和成绩, 教师数据有编号、姓名、职称和部门。要求将编号、姓名输入和显示设计成一个类 person,并作为学生数据操作类student和教师类数据操作类teacher的基类。using System;using System.Collections.Generic;using System.Text;namespace ConsoleApplication2 class personpublic string bh;/编号public string xm;/姓名public person(string bh,s

3、tring xm) this.bh = bh;this.xm = xm;publicvoid show()class student : personpublic string bj;/班级public int cj;/成绩public student(string sbh,string sxm,string sbj,: base(sbh, sxm)bh = sbh;xm = sxm;bj = sbj; cj = scj;publicnewvoid show() Console.WriteLine(*student*);Console.WriteLine(姓名:0, xm); Console.

4、WriteLine(编号:0, bh);Console.WriteLine(班级:0, bj); Console.WriteLine(成绩:0, cj); classteacher : personpublic string zc;/职称public string bm;/部门public teacher(string tbh, string txm, string tzc, :base(tbh, txm)int scj)string tbm)bh = tbh;xm = txm;zc = tzc;bm = tbm;publicnewvoid show()Console.WriteLine(职称

5、:0, zc);Console.WriteLine(部门:0, bm);classprogramstaticvoid Main(string args)张三, 信管091, 91);范仲淹, 特级教师,教务处);student st =new student(050013, st.show();teacher te =new teacher(046950, te.show();1师、 nt三00管er仲69舅 de张鹏信91C11范 8Console.ReadLine();运行结果:(2)将以上程序尝试改成通过调用基类构造方法的方式 来初始化编号和姓名,并总结调用基类构造方法的应用要 点。us

6、ing System;using System.Collections.Generic;using System.Text;namespace ConsoleApplication2class personpublic string bh;/编号public string xm;/姓名public person(string bh, string xm)this.bh = bh;this.xm = xm;publicvoid show()class student : personpublic string bj;/班级public int cj;/成绩public student(strin

7、g sbh,string sxm,string sbj, int scj): base(sbh, sxm)bj = sbj; cj = scj;publicnewvoid show() Console.WriteLine(*student*);Console.WriteLine(姓名:0, xm);Console.WriteLine(编号:0, bh);Console.WriteLine(班级:0, bj);Console.WriteLine(成绩:0, cj);classteacher : personpublic string zc;/职称public string bm;/部门publi

8、c teacher(string tbh, string txm, string tzc, string tbm) :base(tbh, txm) zc = tzc;bm = tbm;publicnewvoid show()Console.WriteLine(*teacher*);Console.WriteLine(姓名:0, xm);Console.WriteLine(编号:0, bh);Console.WriteLine(职称:0, zc);Console.WriteLine(部门:0, bm);class programstaticvoid Main(string args)studen

9、t st =new student(050013, 张三, 信管091, 91); st.show();n046955*stuident* 姓名丨张三 ?扁号050E13 赃级信管即1 成绩匚91*teacher* 姓名范仲淹 编一teacher te =new teacher( 046950, 范仲淹,特级教师, 教务处); te.show();Console.ReadLine();运行结果:类的多态性练习创建一个 Person 类,要求如下:(1) 包含公共字段:string name,私有字段:int age;(2) 公共属性:int Age对应私有字段age,注意在设置Age的时候要验

10、证年龄的合法性;( 3)构造函数: Person()、Person(string name,int Age)( 4)析构函数: Person();( 5)方法: virtual void ShowMe()、void ForNew()、void ForOverload()、void ForOverload(Person p);创建一个 Student 类,继承 Person 类:( 1 )公共字段: number;(2) 构造函数:Student。、Student(string name,int age,string number)该构造函数要调用父类 的构造函数 Person(string n

11、ame,int Age);(3) 方法:重写父类的ShowMe方法、重载父类的F orOverload方法、隐藏父类的F orNew方 法。using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace ConsoleApplication32class Personpublic string name;private int age;public int Ageset if (value 0 & value 150) age = value;get return age;

12、public Person (string name,int age)this.Age=age; this.name=name;public Person()Console.WriteLine(hello,you are welcom!,your name is0,your age is1,name,age);Person() public virtual void ShowMe()Console.WriteLine(name:0 age:1, name, age);public void ForNew()Console.WriteLine(name:0 age:1, name, age);p

13、ublic void ForOverLoad()Console.WriteLine(name:0 age:1, name, age);public void ForOverLoad(Person p)Console.WriteLine(name=0 age=1, p.name, p.age);class Student:Personpublic string number;public Student() public Student(string name, int age, string number) : base(name, age) this.number = number;publ

14、ic override void ShowMe()Console.WriteLine(name:0 age:1 number:2,name,Age,number);public void ForOverLoad(Student s)Console.WriteLine(name=0 age=1 number:2, s.name, s.Age,s.number);public new void ForNew()Console.WriteLine(name:0 age:1 number:2,name,Age,number);class Programstatic void Main(string a

15、rgs)Student s = new Student (罗瑞,20,090202563);s.ShowMe();s.ForNew();s.ForOverLoad(s);Console.ReadLine();运行结果:age:23 age总 ase=2!$number :090202Ei63 number :090202Ei63 number :090202Ei63ameameame一罗抽象类(1)创建一个 shape 抽象类,要求如下: 属性:公共属性 double length 和抽象属性 double width;方法:虚方法double Perimeter()用于计算形状的周长;抽象方

16、法double Area(),用于计算形 状的面积。(2)创建一个 Rectangle 类继承 shape 类,要求如下: 属性:实现父类的抽象属性;方法:实现父类的抽象方法,并重写虚方法。using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace ConsoleApplication33abstract class shapepublic double length;public abstract double widthset;get;public virtual d

17、ouble Perimeter()return 2 * (width + length); public abstract double Area();class Rectangle : shapeprivate double Width;public override double widthset Width = value; get return Width; public Rectangle(double width, double length) this.width = width; this.length = length; public override double Peri

18、meter()return 2 * (width + length); public override double Area() return width * length;class Programstatic void Main(string args)Rectangle r = new Rectangle(9, 8);Console.WriteLine(*长方形 *);Console.WriteLine(周长:0 ,r.Perimeter();Console.WriteLine(面积:0, r.Area(); Console.ReadLine();接口1)定义一个 IPerson 接口

19、,要求如下: 属性:string Name表示姓名、DateTime Birthday表示出生日期;方法:ushort Age()用于返回年龄、void ShowMessage();(2)定义一个IAddress接口,要求如下:属性:string Street用于表示街道、string City表示所在城市;( 3)定义一个 IAddress2 接口实现对 IAddress 接口的扩展,增加 string Province, string State 属性,分别表示所在省份和国家;增加string ShowAddress()方法,用来按照“国家+省份+ 城市+街道”的形式显示地址;( 4)定义

20、一个 Employee 类实现 IPerson 和 IAddress2 接口;(5)定义一个 ITest接口,包含一个 void ShowMessage()方法;(5)定义一个Student类继承Employee类并实现ITest接口。using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace ConsoleApplication35public interface IPerson / (1)定义一个IPerson接口,要求如下:/属性:st ring Name表示姓名、

21、Dat eTime Bir thday表示出生日期;/方法:ushort Age()用于返回年龄、void ShowMessageO ;string Name set; get; DateTime Birthday set;get; ushort Age();void ShowMessage();public interface IAddress / (2)定义一个IAddress接口,要求如下:/属性:st ring St ree t用于表示街道、st ring Ci ty表示所在城市;string Street set; get; string City set; get; public

22、interface IAddress2 : IAddress /定义一个IAddress2接口实现对IAddress 接口的扩展,/增加st ring Province, st ring State属性,分别表示所在省份和国家;/增加st ring ShowAddress()方法,用来按照“国家+省份+城市+街道”的形式显示地 址;string Province set; get; string State set; get; void ShowAddress();public interface ITest/定义一个ITest接口,包含一个void ShowMessage()方法;void

23、ShowMessage();public class Employee : IPerson, IAddress2/定义一个Employee类实现IPerson和 IAddress2 接口;privatestring state;publicstring Stateset state =value; get return state; privatestring province; publicstring Provinceset province =value; get return province; privatestring city; publicstring Cityset city

24、 =value; get return city; privatestring street; publicstring Streetset street =value;get return street; privatestring name; publicstring Nameset name =value; get return name; private DateTime birthday;public DateTime Birthdaysetbirthday=value;getreturn birthday;public ushort Age()return (ushort)(201

25、1 - int.Parse(Birthday.Year.ToString();public void ShowMessage()Console.WriteLine(name:0 birthday:1 age: 2,Name,Birthday,this.Age();public void ShowAddress() Console.WriteLine(State+Province+City+Street);/定义一个St uden t类继承Employee类并实现ITes t接口 publicclass Student : Employee,ITestpublic Student(string

26、Name,string Birthday, string State,string Street,string City,string Province)this.Name = Name;this.Birthday =DateTime.Parse(Birthday);this.State = State;this.Street = Street;this.City = City; this.Province = Province;public new void ShowMessage() Console.WriteLine(名字:0出生日期:1年龄:2 , Name, Birthday, this.Age();Console.WriteLine(地址:+ State + Province + City + Street); classProgramstaticvoid Main(string args)Student st 二 new Student (罗德彬,1989/10/01,中国,”成龙路,” 成都,四川省);st.ShowMessage();Console.ReadLine();

展开阅读全文
温馨提示:
1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
2: 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
3.本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
关于我们 - 网站声明 - 网站地图 - 资源地图 - 友情链接 - 网站客服 - 联系我们

copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!