using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Duotai_test
{
//定义一个基类
public class DrawingObject
{
public virtual void Draw()
{
Console.WriteLine("一个通用的图像对象");
}
}
//定义一个直线类
public class Line : DrawingObject
{
public override void Draw()
{
Console.WriteLine("一条直线");
}
}
//定义一个圆周类
public class Circle : DrawingObject
{
public override void Draw()
{
Console.WriteLine("一个圆");
}
}
//定义一个正方形类
public class Square : DrawingObject
{
public override void Draw()
{
Console.WriteLine("一个正方形");
}
}
public class DrawDemo
{
public static void Main()
{
DrawingObject[] dObj = new DrawingObject[4]; //重点
dObj[0] = new Line();
dObj[1] = new Circle();
dObj[2] = new Square();
dObj[3] = new DrawingObject();
foreach (DrawingObject drawObj in dObj)
{
drawObj.Draw();
}
}
}
}
C#之多态
本文转载:CSDN博客