Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I want to do adapter template.
There is error in AutoToTravelAdapterMove(); 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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
324 views
Welcome To Ask or Share your Answers For Others

1 Answer

The title is very clear - an override method must match the virtual method it overrides, not only by it's signature (name and parameters), but also by it's access modifiers and return type.
Why? because of (at least) Polymorphism and method overloading rules.

Polymorphism which is a base principle of object oriented programming is basically the ability to look at a derived class as if it was it's base class. This means that if the base class have a method like public void move(), the derived class also have this method - either inherited unchanged or overrided in the derived class.

The rule for method overloading is very simple - you can have multiple methods with the same name but different signature. The signature of the method is the combination of it's name and it's arguments - so overloads that differs only by return type or access modifiers are not permitted.

Imagine if the compiler would allow you to change the access modifier in inheritance - you would end up with a class like this:

WARNING: Incorrect code ahead!

public class Transport
{
   public virtual void Move() { Console.WriteLine("trans Moves"); }
}

public class AutoToTravelAdapter : Transport
{
    private Auto auto = new Auto();
    private override void  Move()
    {
        auto.Drive();
    }
}

So AutoToTravelAdapter would have, in fact, two Move methods with identical signature - one private that is declared in AutoToTravelAdapter class, and one public that is inherited from Transport.
Obviously, this would make calling the Move() method from inside the AutoToTravelAdapter class impossible, since the compiler would have no way to distinguish between the two methods.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...