I want to do adapter
template.
There is error in AutoToTravelAdapter
Move(); method
How can I override
and inherit
Transport method? I try to use virtual but it does not works. I change to public override void Move() in both adapters and that works! Thanks, Zohar Peled!
using System;
namespace Adapter
{
class Program
{
static void Main(string[] args)
{
Traveller traveller = new Traveller();
Transport camelTransport = new CamelToTravelAdapter();
Transport autoTransport = new AutoToTravelAdapter();
traveller.Travel(camelTransport);
traveller.Travel(autoTransport);
Console.Read();
}
}
public class Transport
{
virtual public void Move() { Console.WriteLine("trans Moves"); }
}
class Auto
{
public void Drive()
{
Console.WriteLine("Car drive");
}
}
class Traveller
{
public void Travel(Transport transport)
{
transport.Move();
}
}
class Camel
{
public void Move()
{
Console.WriteLine("Camel Moves");
}
}
public class CamelToTravelAdapter : Transport
{
private Camel camel = new Camel();
private new void Move()
{
camel.Move();
}
}
public class AutoToTravelAdapter : Transport
{
private Auto auto = new Auto();
**private override void Move()**
{
auto.Drive();
}
}
}
See Question&Answers more detail:os