using System;
namespace 接口隔离
{
class Program
{
static void Main(string[] args)
{
var driver = new Driver(new Car());
driver.Drive();
}
}
class Driver
{
private IVehicle _vehicle;
public Driver(IVehicle vehicle)
{
_vehicle = vehicle;
}
public void Drive()
{
_vehicle.Run();
}
}
interface IVehicle{ void Run();}
class Car : IVehicle
{
public void Run() { Console.WriteLine("Car is running..."); }
}
class Truck : IVehicle
{
public void Run(){ Console.WriteLine("Truck is running...");}
}
interface IWeapon
{
void Fire();
}
interface ITank:IWeapon,IVehicle { }
class LightTank : ITank
{
public void Fire()
{
Console.WriteLine("Boom!");
}
public void Run()
{
Console.WriteLine("Ka! Ka! Ka! .....");
}
}
class MediaTank : ITank
{
public void Fire()
{
Console.WriteLine("Boom!!");
}
public void Run()
{
Console.WriteLine("Ka!! Ka!! Ka!! .....");
}
}
class HeaveTank : ITank
{
public void Fire()
{
Console.WriteLine("Boom!!!");
}
public void Run()
{
Console.WriteLine("Ka!!! Ka!!! Ka!!! .....");
}
}
}