Cannot change access modifiers when overriding 'public' inherited member '

匆匆过客 提交于 2019-12-18 09:26:29

问题


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();
        }
    }
}

回答1:


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.



来源:https://stackoverflow.com/questions/50383620/cannot-change-access-modifiers-when-overriding-public-inherited-member

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!