在俄罗斯方块程序中,有 L形,T形,田形等多种形状,它们 是图形的多种形态,创建一个名为 Shape的基类,而后派生 L形, T形等,之后在运行时动态绘制各种形状。
(1)创建一个名为 Teris的控制台应用程序;
(2)各个类之间的关系如下图所示:
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace 抽象类练习{public abstract class Shape{public String ShapeType{get;set;}public abstract void Draw();public void DisplayInfo(){Console.WriteLine(\"当前图形类型:\" + ShapeType);}}public class ShapeL : Shape{public ShapeL(){this.ShapeType = \"L形\";}public override void Draw(){Console.WriteLine(\"||\" + Environment.NewLine+ \"||\" + Environment.NewLine+ \"||____\");}}public class ShapeT:Shape{public ShapeT(){this.ShapeType = \"T形\";}public override void Draw(){Console.WriteLine(\"______\" + Environment.NewLine+ \" \" + \"||\" + Environment.NewLine+ \" \" + \"||\");}}public class ShapeBlock : Shape{public ShapeBlock(){this.ShapeType = \"田形\";}public override void Draw(){Console.WriteLine(\"_________\" + Environment.NewLine+ \"|\" + \" \" + \"|\" + \" \" + \"|\" + Environment.NewLine+\"|___\"+\"|\"+\"___|\" + Environment.NewLine+\"|\" + \" \" + \"|\" + \" \" + \"|\" + Environment.NewLine+\"|___\"+\"|\"+\"___|\");}}public class ShapeZ:Shape{public ShapeZ(){this.ShapeType = \"Z形\";}public override void Draw(){Console.WriteLine(\"____\" + Environment.NewLine+ \" \" + \"|\" + Environment.NewLine+ \" \" + \"|____\");}}class Program{static void Main(string[] args){Shape[] shapes ={new ShapeL (),new ShapeT (),new ShapeBlock (),new ShapeZ ()};foreach(Shape shape in shapes){shape.DisplayInfo();shape.Draw();}Console.ReadKey();}}}
程序运行结果: