1、 实验报告 河南财经政法大学计算机与信息工程学院实验报告2013年-2014年 第一学期专 业:_ 管理信息系统_课程名称:_ _C#高级程序设计 _ _班 级:_ _姓 名:_ _ 学号:_指导教师:_ _实验一:派生类实验目的:1. 了解继承和多态的基本概念。2. 掌握派生类、虚方法的使用方法。3. 掌握抽象类的定义方法。实验内容:一、1运行课本例8.1,仔细理解派生类、基类对象的引用及其转换方法。2参照课本例8.3,创建基类Person和派生类Teacher。基类Person包含实例字段name和age;虚函数GetInfo()显示个人信息(显示name和age的值)。派生类Teache
2、r除了包含基类的name和age字段,还包括自己的TeacherID字段,并使用关键字override来重写方法GetInfo()显示教师信息(显示name、age和TeacherID的值)。运行效果如图所示。3创建抽象类基类Shape和派生类Rectangle、Circle、Square。利用多态性实现Area(计算面积)和Show(显示图形名称和面积)抽象方法。具体要求如下:l 抽象类Shape中有字段name(名称)及两个抽象方法Show(显示名称及面积)、Area(面积)。l 派生类Rectangle(矩形)中有字段weigh(宽度)、height(高度)及两个覆盖方法Show(显示名
3、称及面积)、Area(计算矩形的面积)。l 派生类Circle(圆)中有字段radius(半径)及两个覆盖方法Show(显示名称及面积)、Area(计算圆的面积)。l 派生类Square(正方形)中有字段weigh(边长)及两个覆盖方法Show(显示名称及面积)、Area(计算正方形的面积)。运行效果如图所示。实验数据记录及分析(或程序及运行结果)1namespace ConsoleApplication1 class Program public class Parent public Parent() public void MethodA() Console.WriteLine(调用Me
4、thodA(); public class Child:Parent public Child() public void MethodB() Console.WriteLine(调用MethodB(); class Test static void Main() Parent oParent=new Parent(); oParent.MethodA(); Child oChild=new Child(); oChild.MethodB(); oChild.MethodA(); Parent oParent1=(Parent)oChild; oParent.MethodA(); Consol
5、e.ReadLine(); Child oChild1=(Child)oParent1; oChild1.MethodB(); oChild1.MethodA(); Console.ReadKey(); 2namespace ConsoleApplication2 class Program public class Person public string name; public uint age; public Person(string name, uint age) this.name = name; this.age = age; public virtual void GetIn
6、fo() Console.WriteLine(Name:0, name); Console.WriteLine(Age:0, age); public class Teacher : Person public string teacherID; public Teacher(string name, uint age, string id) : base(name, age) this.teacherID = id; public override void GetInfo() base.GetInfo(); Console.WriteLine(TeacherID:0, teacherID)
7、; public class TestPersonTeacher static void Main(string args) Teacher objteacher = new Teacher(Mr.Yu, 40, 1990108001); objteacher.GetInfo(); Console.ReadKey(); 3public abstract class Shape public string name; public Shape(string name) this.name = name ; public abstract double Area(); public abstrac
8、t void Show(); public class Rectangle : Shape public int weight, height; public Rectangle(int weight1, int height1, string name) : base(name) weight = weight1; height = height1; public override void Show() Console.WriteLine(Rectangle :0,Area:1, name, weight * height); public override double Area() r
9、eturn weight * height; public class Circle : Shape public int radius; public Circle(int r, string id) : base(id) radius = r; public override void Show() Console.WriteLine(Rectangle :0,Area:1, name, radius * radius * System.Math.PI); public override double Area() return radius * radius *System.Math.PI ; public class Square : Shape public int weight; public Square(int weight1, string id) : base(id) weight = weight1; public override void Show() Console.WriteLine(Rectangle :0,Area:1, name, weight * weight ); public override double Area() return weight * weight ; class Test
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1