1.封装
2.继承
3.多态
不扯别的,看代码就好…
using System;
//程序的声明
namespace demo0709
{
//类名
class Program
{
//主程序的入口
static void Main(string[] args)
{
//多态性
Transport transport = new Bus();
transport.reduceSpeed();
transport.increaseSpeed();
Console.ReadKey();
}
}
//交通工具类
class Transport
{
private int id;
private string name;
public Transport()
{
}
public Transport(int id, string name)
{
this.id = id;
this.name = name;
}
public override string ToString()
{
return this.id+"----"+this.name;
}
//车类有2个方法
public virtual void reduceSpeed() => Console.WriteLine("减速慢行");
public virtual void increaseSpeed() => Console.WriteLine("高速路段请加速");
}
//Bus类
class Bus:Transport
{
//重写父类的方法
public override void reduceSpeed() => Console.WriteLine("减速啦~~biubiu---");
public override void increaseSpeed() => Console.WriteLine("加速啦~~~biubiu---");
}
}
来,破而后立!
看报错提示,自己领悟~



