实验报告5 继承与多态.doc_第1页
实验报告5 继承与多态.doc_第2页
实验报告5 继承与多态.doc_第3页
实验报告5 继承与多态.doc_第4页
实验报告5 继承与多态.doc_第5页
已阅读5页,还剩7页未读, 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

实验名称:继承与多态 一 实验目的:(1)理解继承的含义,掌握派生类的定义方法和实现;(2)理解虚函数在类的继承层次中的作用,虚函数的引入对程序运行时的影响,能够对使用虚函数的简单程序写出程序结果。(3)编写体现类的继承性(成员变量,成员方法,成员变量隐藏)的程序;(4)编写体现类多态性(成员方法重载,构造方法重载)的程序。(5)理解接口和抽象类、抽象方法的定义和实现方法;(5)理解接口和抽象类的差别。 二上机内容:(1)进行类的继承和基类构造方法的应用的练习;(2)进行类的多态性练习(3)进行抽象类和接口的练习(4)整理上机步骤,总结经验和体会。(5)完成实验报告。三上机步骤:类的继承和基类构造方法的应用(1) 编写一个学生和教师数据输入和显示程序,学生数据有编号、姓名、班级和成绩,教师数据有编号、姓名、职称和部门。要求将编号、姓名输入和显示设计成一个类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); Console.WriteLine(部门:0, bm); class program static void Main(string args) student st = new student(050013, 张三, 信管091, 91); st.show(); teacher te = new teacher(046950, 范仲淹, 特级教师, 教务处); te.show(); Console.ReadLine(); 运行结果:(2)将以上程序尝试改成通过调用基类构造方法的方式来初始化编号和姓名,并总结调用基类构造方法的应用要点。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) 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) zc = tzc; bm = tbm; public new void show() Console.WriteLine(*teacher*); Console.WriteLine(姓名:0, xm); Console.WriteLine(编号:0, bh); Console.WriteLine(职称:0, zc); Console.WriteLine(部门:0, bm); class program static void Main(string args) student st = new student(050013, 张三, 信管091, 91); st.show(); teacher te = new teacher( 046950, 范仲淹,特级教师, 教务处); te.show(); Console.ReadLine(); 运行结果:类的多态性练习创建一个Person类,要求如下:(1)包含公共字段:string name,私有字段:int age;(2)公共属性:int Age对应私有字段age,注意在设置Age的时候要验证年龄的合法性;(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 name,int Age);(3)方法:重写父类的ShowMe方法、重载父类的ForOverload方法、隐藏父类的ForNew方法。using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace ConsoleApplication32 class Person public string name; private int age; public int Age set if (value 0 & value 150) age = value; get return age; public Person (string name,int age) this.Age=age; =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); public void ForOverLoad() Console.WriteLine(name:0 age:1, name, age); public void ForOverLoad(Person p) Console.WriteLine(name=0 age=1, , p.age); class Student:Person public string number; public Student() public Student(string name, int age, string number) : base(name, age) this.number = number; public 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.Age,s.number); public new void ForNew() Console.WriteLine(name:0 age:1 number:2,name,Age,number); class Program static void Main(string args) Student s = new Student(罗瑞,20,090202563); s.ShowMe(); s.ForNew(); s.ForOverLoad(s); Console.ReadLine(); 运行结果:抽象类(1)创建一个shape抽象类,要求如下:属性:公共属性double length和抽象属性double width;方法:虚方法double Perimeter()用于计算形状的周长;抽象方法double Area(),用于计算形状的面积。(2)创建一个Rectangle类继承shape类,要求如下:属性:实现父类的抽象属性;方法:实现父类的抽象方法,并重写虚方法。using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace ConsoleApplication33 abstract class shape public double length; public abstract double widthset;get; public virtual double Perimeter() return 2 * (width + length); public abstract double Area(); class Rectangle : shape private double Width; public override double width set Width = value; get return Width; public Rectangle(double width, double length) this.width = width; this.length = length; public override double Perimeter() return 2 * (width + length); public override double Area() return width * length; class Program static 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接口,要求如下:属性:string Name表示姓名、DateTime Birthday表示出生日期;方法:ushort Age()用于返回年龄、void ShowMessage();(2)定义一个IAddress接口,要求如下:属性:string Street用于表示街道、string City表示所在城市;(3)定义一个IAddress2接口实现对IAddress接口的扩展,增加string Province,string State属性,分别表示所在省份和国家;增加string ShowAddress()方法,用来按照“国家+省份+城市+街道”的形式显示地址;(4)定义一个Employee类实现IPerson和IAddress2接口;(5)定义一个ITest接口,包含一个void ShowMessage()方法;(5)定义一个Student类继承Employee类并实现ITest接口。using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace ConsoleApplication35 public interface IPerson /(1)定义一个IPerson接口,要求如下:/属性:string Name表示姓名、DateTime Birthday表示出生日期;/方法:ushort Age()用于返回年龄、void ShowMessage(); string Name set; get; DateTime Birthday set; get; ushort Age(); void ShowMessage(); public interface IAddress /(2)定义一个IAddress接口,要求如下: /属性:string Street用于表示街道、string City表示所在城市; string Street set; get; string City set; get; public interface IAddress2 : IAddress /定义一个IAddress2接口实现对IAddress接口的扩展, /增加string Province,string State属性,分别表示所在省份和国家; /增加string ShowAddress()方法,用来按照“国家+省份+城市+街道”的形式显示地址; string Province set; get; string State set; get; void ShowAddress(); public interface ITest/定义一个ITest接口,包含一个void ShowMessage()方法; void ShowMessage(); public class Employee : IPerson, IAddress2/定义一个Employee类实现IPerson和IAddress2接口; private string state; public string State set state = value; get return state; private string province; public string Province set province = value; get return province; private string city; public string City set city = value; get return city; private string street; public string Street set street = value; get return street; private string name; public string Name set name = value; get return name; private DateTime birthday; public DateTime Birthday setbirthday=value; getreturn birthday; public ushort Age() return (ushort)(2011 - int.Pars

温馨提示

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

评论

0/150

提交评论