中重写受保护的内部与保护! [英] overriding protected internal with protected!

查看:148
本文介绍了中重写受保护的内部与保护!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个扩展此<一个href=\"http://stackoverflow.com/questions/2375556/overriding-and-overridden-methods-must-have-same-accessibility-so-why-isnt-same/2375590#2375590\">question一小时前问。

我们不能修改在覆盖一个的虚拟方法时获得的访问修饰符类。考虑控制的System.Web.UI 命名

 公共类控制:IComponent接口,IDisposable接口,...
{
   受保护的内部虚拟无效的CreateChildControls()
   {}
   。
   。
}

现在考虑这

 公共类SomeClass的:System.Web.UI.Control
    {
       //这不应该编译,但它确实
        保护覆盖无效的CreateChildControls()
        {}       //这应该编译,但它不
        受保护的内部覆盖无效的CreateChildControls()
        {}
    }

任何机构可以解释一下吗?谢谢


解决方案

  

在派生类中重写虚拟方法时,我们不能修改访问修饰符。


这种说法是错误的。你可以和必须当precisely你的情况描述更改访问修饰符。在其他情况下,你不能改变访问修饰符。

我是指你的规范,其中规定第10.6.4:


  

一个覆盖声明不能改变
  虚拟的无障碍
  方法。但是,如果重写
  基方法是受保护的内部和
  它是在一个不同的组件声明
  不是包含装配
  覆盖方法则覆盖
  方法的声明可访问必须的
  被保护。


的理由很简单。

您,阿萨德,拥有银行账户,的BankAccount。

您有房子。你租在众议院一个房间你最好的朋友查理。

查理有一个儿子,大卫,谁住在公寓里。

您有一个儿子,埃尔罗伊,谁住在一个公寓。

埃尔罗伊有一个儿子,你的孙子,弗兰克,谁住在一个蒙古包。

埃尔罗伊有一个最好的朋友格雷格谁与他住在公寓。

您授予访问您的BankAccount给自己,给任何人住在家里去,你的任何后代。那么,谁可以访问银行账户的人都是阿萨德,查理,埃尔罗伊和弗兰克。

大卫没有得到访问,因为他既不是你,也不是你的后代,也不是他住在房子。他是你室友的孩子是无关紧要的;他没有得到您的BankAccount访问。

格雷格没有得到进入您的银行帐户无论是。他是不是你的后裔。他并不住在房子。他是生活在你的后代的事实,并不给他同样的权利,你的后裔。

现在我们来到了问题的症结所在。 埃尔罗伊不允许访问扩展到您的BankAccount格雷格。您自己认为的BankAccount,和你说:我自己,我的后代,我的室友。您的孩子没有延长的BankAccount超出你最初设置了无障碍的权利。

在埃尔罗伊介绍了访问他要的BankAccount,他只被允许说:我授权访问这我自己和我的后代,因为那是你已经允许的。他不能说我给予我自己,我的后代和公寓的其他居民获得的BankAccount。

只要是明确的:


  • 我和我的后代可以访问=保护访问

  • 我和我的室友可以访问=内部访问

  • 我和我的后代,我的室友可以访问=保护内部访问

  • 控制=阿萨德

  • 的CreateChildControls =的BankAccount

  • 众议院= System.Web.dll中

  • 查理=任何类型的System.Web.dll中

  • 大卫=组装查理派生类型Apartment.DLL

  • 埃尔罗伊= SomeClass的

  • 公寓=您的包含SomeClass的组装

  • 格雷格=在Condo.DLL其他类

  • 弗兰克=在Yurt.DLL派生SomeClass的类型

  • 蒙古包=一些其他装配

This is an extension for this question asked an hour ago.

We cannot modify the access modifiers, when overriding a virtual method in derived class. Consider Control class in System.Web.UI namespace

public class Control : IComponent, IDisposable,...
{ 
   protected internal virtual void CreateChildControls()
   { }
   .
   .
}

Now Consider This

public class someClass : System.Web.UI.Control
    { 
       // This should not compile but it does
        protected override void CreateChildControls()
        { }

       // This should compile but it does not
        protected internal override void CreateChildControls()
        { }  
    }

can any body explain this ? Thanks

解决方案

We cannot modify the access modifiers when overriding a virtual method in derived class.

That statement is false. You can and must change the access modifiers when in precisely the situation you describe. In other situations you must not change the access modifiers.

I refer you to section 10.6.4 of the specification, which states:

an override declaration cannot change the accessibility of the virtual method. However, if the overridden base method is protected internal and it is declared in a different assembly than the assembly containing the override method then the override method’s declared accessibility must be protected.

The reasoning is straightforward.

You, Asad, have a bank account, BankAccount.

You have a House. You rent a room in House to your best friend Charlie.

Charlie has a son, David, who lives in an Apartment.

You have a son, Elroy, who lives in a Condo.

Elroy has a son, your grandson, Frank, who lives in a Yurt.

Elroy has a best friend Greg who lives in the Condo with him.

You grant access to your BankAccount to yourself, to anyone living in House, and to any of your descendents. So the people who can access the bank account are Asad, Charlie, Elroy, and Frank.

David does not get access because he is neither you, nor your descendent, nor is he living in House. That he is a child of your housemate is irrelevant; he doesn't get access to your BankAccount.

Greg does not get access to your bank account either. He is not your descendent. He does not live in House. The fact that he is living with your descendent does not grant him the same rights as your descendent.

Now we come to the crux of the matter. Elroy is not allowed to extend access to your BankAccount to Greg. You own that BankAccount, and you said "myself, my descendents and my housemates". Your children don't have the right to extend the accessibility of BankAccount beyond what you initially set up.

When Elroy describes what access he has to BankAccount, he is only allowed to say "I grant access to this to myself and my descendents", because that is what you already allowed. He cannot say "I grant access to BankAccount to myself, my descendents and to the other residents of Condo".

Just to be clear:

  • I and my descendents get access = protected access
  • I and my housemates get access = internal access
  • I and my descendents and my housemates get access = protected internal access
  • Control = Asad
  • CreateChildControls = BankAccount
  • House = System.Web.DLL
  • Charlie = any type in System.Web.DLL
  • David = derived type of Charlie in assembly Apartment.DLL
  • Elroy = someClass
  • Condo = your assembly containing SomeClass
  • Greg = some other class in Condo.DLL
  • Frank = derived type of someClass in Yurt.DLL
  • Yurt = some other assembly

这篇关于中重写受保护的内部与保护!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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