《实验十五 C#多态实现》由会员分享,可在线阅读,更多相关《实验十五 C#多态实现(5页珍藏版)》请在金锄头文库上搜索。
1、【实验目的】1. 深入理解多态的含义;2. 掌握使用抽象类实现多态的方法;3. 掌握使用接口实现多态的方法。【实验准备】复习配套教材相关章节的内容;思考多态的意义和实现方法。【实验内容】项目一:使用抽象类实现多态。1、在俄罗斯方块程序中,有L形,T形,田形等 多种形状,它们是图形的多种形态,可以创建一个 名为Shape的基类,而后派生L形,T形等,之后 可以在运行时动态绘制各种形状。1)创建一个名位Teris的控制台应用程序、2)各个类之间的关系如下图所示:3)创建名为Shape的抽象类,包括ShapeType属性 和显示信息方法,以及抽象方法绘制:public abstract class
2、Shapeprivate string shapeType;public string ShapeType get return shapeType; set shapeType = value; public void DisplayInfo()Console.WriteLine(当前图形类型”+ shapeType);public abstract void Draw();4) 创建名为ShapeL的派生类,实现基类的绘制public class ShapeL : Shapepublic ShapeL()ShapeType = L 形”;public override void Draw(
3、)Console.WriteLine(T);Console.WriteLine(T);Console.Write(|);Console.WriteLine(_);5) 创建名为ShapeT的派生类,实现基类的绘制public class ShapeT : Shapepublic ShapeT()ShapeType = T 形”;public override void Draw()Console.WriteLine();Console.WriteLine( |);Console.WriteLine( |);6) 创建名为ShapeZ的派生类,实现基类的绘制public class ShapeZ
4、 : Shapepublic ShapeZ()ShapeType = Z 形”;public override void Draw()Console.WriteLine();Console.WriteLine(|);Console.WriteLine(|);Console.WriteLine( );7) 创建名为ShapeBlock(田字形)的派生类,实现基 类的绘制class ShapeBlock : Shapepublic ShapeBlock()ShapeType =田形”;public override void Draw()Console.WriteLine();Console.Wr
5、iteLine(|”);Console.WriteLine(|”);Console.WriteLine();Console.WriteLine(|”);Console.WriteLine(|”);Console.WriteLine();8)在Program.cs中添加如下代码,实现随机生成 图形:class Programstatic void Main(string args)Random rnd = new Random();while (true)Shape shape = null;int type = rnd.Next(4);switch (type)case 0:shape = n
6、ewShapeBlock();break;case 1:shape = new ShapeL();break;case 2:shape = new ShapeT();break;case 3:shape = new ShapeZ();break;if (shape != null)shape.DisplayInfo();shape.Draw();Console.WriteLine();Thread.Sleep(1000);/ 防止刷 新太快,让系统停止1秒项目二:使用接口实现多态。2、利用接口实现项目一的问题。接口 Shapeinterface Shapevoid DisplayInfo();
7、void Draw();类 ShapeBlockpublic class ShapeBlock : Shapestring shapeType =田形”;public void DisplayInfo()Console.WriteLine(当前图形类型+ shapeType);public void Draw()Console.WriteLine();Console.WriteLine(|”);Console.WriteLine(|”);Console.WriteLine(”);Console.WriteLine(|”);Console.WriteLine(|”);Console.WriteL
8、ine(”);类ShapeLpublic class ShapeL : Shapestring shapeType = L形”;public void DisplayInfo()Console.WriteLine(当前图形类型 + shapeType);public void Draw()Console.WriteLine(|”);Console.WriteLine(|”);Console.Write(|”);Console.WriteLine(_);类ShapeTpublic class ShapeT : Shapestring shapeType = T形”;public void Dis
9、playInfo()Console.WriteLine(当前图形类型 + shapeType);public void Draw()Console.WriteLine();Console.WriteLine( |);Console.WriteLine( |);类ShapeZpublic class ShapeZ : Shapestring shapeType = Z形”;public void DisplayInfo()Console.WriteLine(当前图形类型 + shapeType);public void Draw()Console.WriteLine();Console.Writ
10、eLine(|);Console.WriteLine(|);Console.WriteLine( );主方法Mainclass Programstatic void Main(string args)Random rnd = new Random();while (true)Shape shape = null;int type = rnd.Next(4);switch (type)case 0:shape = new ShapeBlock();break;case 1:shape = new ShapeL();break;case 2:shape = new ShapeT();break;case 3:shape = new ShapeZ();break;if (shape != null)shape.DisplayInfo();shape.Draw();Console.WriteLine();Thread.Sleep(1000);/ 防 止刷新太快,让系统停止秒【总结与体会】