using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 交通工具_多态_
{
class Vehicle //定义一个交通工具类
{
string name; //定义字段
public string Name //定义属性为字段赋值
{
get {return name; }
set {name = value; }
}
public virtual void Move()
{
Console.WriteLine("{0}在铁轨上行驶",Name);
}
}
class Train : Vehicle //
{
public override void Move() //重写方法输出交通工具状态
{
Console.WriteLine("{0}在铁轨上行驶",Name);
}
}
class Car : Vehicle
{
public override void Move()
{
Console.WriteLine("{0}在公路上行驶",Name);
}
}
class Program
{
static void Main(string[] args)
{
Vehicle vehicle = new Vehicle();
Train train = new Train();
Car car = new Car();//创建实例对象
//使用基类和派生类对象创建Vehicle类型数组
Vehicle[] vehicles = { vehicle,train,car};
vehicle.Name = "交通工具"; //设置交通工具的名字
train.Name = "火车";
car.Name = "汽车";
vehicles[0].Move();
vehicles[1].Move();
vehicles[2].Move();//输出交通工具的形态
Console.ReadLine();
}
}
}