Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
332 views
in Technique[技术] by (71.8m points)

c# - Cannot change access modifiers when overriding 'public' inherited member '

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

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

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...