为什么我们不能改变访问修饰符,而覆盖在C#中的方法呢? [英] Why can't we change access modifier while overriding methods in C#?

查看:254
本文介绍了为什么我们不能改变访问修饰符,而覆盖在C#中的方法呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C#中,我们不能同时覆盖从基类中的方法更改访问修饰符。
例如:

In C#, we can not change access modifier while overriding a method from base class. e.g.

Class Base
{
   **protected** string foo()
   {
       return "Base";
   }
}

Class Derived : Base
{
   **public** override string foo()
   {
       return "Derived";
   }
}

这是不是在C#有效,它会给编译时错误。

This is not valid in C#, It will give compile time error.

我想知道原因,为什么它不允许的。是否有任何技术问题,也可以导致东西是不相符的访问限制方面???

I want to know the reason, why it's not allowed. Is there any technical problem or can it lead to something which is not consistent in terms of access restriction???

推荐答案

更改在派生类的方法的访问修饰符是没有意义的,这就是为什么它是不允许的:

Changing the access modifier of a method in a derived type is pointless that's why it's not allowed:

案例1:用更严格的访问覆盖

Case 1: Override with a more restrictive access

这情况下,显然是不允许的,由于以下情况:

This case is obviously not allowed due to the following situation:

class Base
{
    public virtual void A() {}
}

class Derived: Base
{
    protected override void A()
}

现在我们可以说:

List<Base> list;
list.Add(new Derived());
list[0].A() //Runtime access exception



案例2:重写用较少限制的访问修饰符

Case 2: Overriding with a less restrictive access modifier

要点是什么?隐藏的方法和你做。
显然,如果有人要求通过基本类型,他们将无法访问在派生类中定义的新方法,但是,它与基本类型的作者是如何想的东西保持一致,那么你就没有权利改变这种状况。如果你想从派生类的派生类中调用的具体情况,在这种情况下,方法工作完全正常。

What is the point? Hide the method and you are done. Obviously if someone calls through the base type they will not have access to the new method defined in the derived type but that is consistent with how the author of the base type wanted things to be so you have no "right" to change that. If you want the specifics of the derived class call from the derived class, in which case the new method works perfectly fine.

编辑:扩大的情况下2

我试图在情况2的说法,就是你已经有了更改辅助手段。(虚拟与否),如果你想改变无障碍设施的任何方法

What I am trying to say in case 2, is that you already have the means to change accessibility of any method (virtual or not) if you want to change accessibility.

考虑下面的代码:

public class Base
{
    protected virtual string WhoAmI()
    {
        return "Base";
    }
}

public class Derived : Base
{
    public new virtual string WhoAmI()
    {
        return "Derived";
    }
}

public class AnotherDerived : Derived
{
    public override string WhoAmI()
    {
        return "AnotherDerived";
    }
}



随着关键字,您已经有效地创建具有相同名称和签名的派生类一个新的虚拟方法。注意到,它被允许申报方法虚拟,所以任何类从派生源性将被允许将其覆盖

With the new keyword you have effectively created a new virtual method for your Derived class with the same name and signature. Take note that it is ALLOWED to declare a new method virtual, so any class deriving from Derived will be allowed to override it.

什么是不允许的是有一个人做到以下几点:

What is not allowed is to have someone do the following:

 Base newBaseObject = new Derived();
 newBaseObject.WhoAmI() //WhoAmI is not accessible.



但这一事实无关,与能够覆盖 WHOAMI() 与否。无论如何这种情况绝不可能,因为基本不声明公共 WHOAMI()

But this fact has nothing to do with being able to override WhoAmI() or not. Whatever the case this situation can never be because Base does not declare a public WhoAmI().

因此,在理论上C#,其中 Derived.WhoAmI()可以覆盖 Base.WhoAmI()有这样做,因为你将永远无法反正调用基类中的虚方法,没有任何实际的好处,让选项已经满足您的要求。

So in a theoretical C# where Derived.WhoAmI() could override Base.WhoAmI() there is no practical benefits in doing so because you will never be able to call the virtual method from the base class anyways, so the new option already meets your requirements.

我希望这是更清晰。

这篇关于为什么我们不能改变访问修饰符,而覆盖在C#中的方法呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆