资源预览内容
第1页 / 共7页
第2页 / 共7页
第3页 / 共7页
第4页 / 共7页
第5页 / 共7页
第6页 / 共7页
第7页 / 共7页
亲,该文档总共7页全部预览完了,如果喜欢就下载吧!
资源描述
实验 5 多态抽象类实验目的:掌握继承的定义、特征以及语法格式明确多态的概念,理解编译时多态和运行时多态的区别以及定义方法掌握虚方法重载和隐藏基类方法的区别熟练使用抽象类和抽象方法进行面向对象编程实验要求:理解和掌握在编程中去实现继承理解和掌握通过把子类实例的地址传给基类实例,然后通过基类实例去调用虚方法,这一种方式真正体现出的多态。理解和掌握抽象方法在编程过程中的使用。通过调试,能够使得程序正确运行,并输出结果。实验内容:1. 编写代码,定义一个基类 MyClass,其中包含虚拟方法GetString().这个方法应返回存储在受保护字段 myString 中的字符串,该字段可以通过只写公共属性 ContainedString 来初始化。 (初始化请通过控制台由用户来输入)2. 从类 MyClass 中派生一个类 MyDerivedClass.重写 GetString()方法,使用该方法的基类执行代码从基类中返回一个字符串,但在返回的字符串中添加文本”output from derived class”.using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace _2class Programstatic void Main(string args)MyDerivedClass tt = new MyDerivedClass();MyClass mm = new MyClass();tt.ContainedString = Console.ReadLine();mm = tt;Console.WriteLine();Console.WriteLine(mm.GetString();Console.ReadKey();class MyClassprotected string myString;public virtual string GetString() return myString; public string ContainedStringset myString = value; class MyDerivedClass : MyClasspublic override string GetString()return base.GetString() + output from derived class;using System;using System.Collections.Generic;using System.Text;namespace di1class Programstatic void Main(string args)MyDerivedClass d = new MyDerivedClass();MyClass c = d;c.ContainedString = Console.ReadLine();Console.WriteLine(基类?返?回?的?字?符?串?是? + c.GetString();Console.ReadLine(); class MyClassprotected string myString;public string ContainedStringsetmyString = value;public virtual string GetString()return myString;class MyDerivedClass : MyClasspublic override string GetString() return base.GetString() + (output from derived class);3. 利用多态性计算圆和三角形的面积以及周长。 (半径、底以及高的值都通过实例化派生类的同时来指定)using System;using System.Collections.Generic;using System.Text;namespace ConsoleApplication1class Programstatic void Main(string args)Circle cc = new Circle(3); Shape ss = cc;Console.WriteLine(圆2的?面?积y是?:阰 + ss.GetArea();Rectangle rr = new Rectangle(5, 3);ss = rr;Console.WriteLine(矩?形?的?面?积y是?:阰 + ss.GetArea();Console.ReadLine();class Shapepublic virtual double GetArea()return 0; class Circle : Shapeprivate double r;public Circle(double r) this.r = r; public override double GetArea() return Math.PI * r * r; class Rectangle : Shapeprivate double l, w;public Rectangle(double l, double w)this.l = l;this.w = w;public override double GetArea()return l * w;4. 利用抽象方法和抽象类编程实现圆和正方形的面积。 (半径、长、宽的值通过实例化派生类的同时来指定)using System;using System.Collections.Generic;using System.Text;namespace ConsoleApplication1class Program static void Main(string args)Circle cc = new Circle(3); Console.WriteLine(圆2的?面?积y是?:阰 + cc.GetArea();Console.WriteLine(圆2的?周长是?:阰 + cc.GetZhC();SanJiao rr = new SanJiao(5, 3);Console.WriteLine(三角?形?的?面?积y是?:阰 + rr.GetArea();rr = new SanJiao(4, 5, 6);Console.WriteLine(三角?形?的?周长是?:阰 + rr.GetZhC();Console.ReadLine();abstract class Shapepublic abstract double GetArea();public abstract double GetZhC();class Circle : Shapeprivate double r;public Circle(double r) this.r = r; public override double GetArea() return Math.PI * r * r; public override double GetZhC()return 2 * Math.PI * r;class SanJiao : Shapedouble d, g;double a, b, c;public SanJiao(double d, double g) this.d = d;this.g = g;public SanJiao(double a, double b,double c)this.a = a;this.b = b;this.c = c;public override double GetArea()return 0.5 *d * g;public override double GetZhC()return a + b + c;
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号