为什么我不能有一个从内部类继承的公共类?
我不明白下面展示的可访问性限制
public abstract class Base { }
internal class Common : Base { }
public class Instance : Common { }
这将无法编译。
可访问性不一致:基类“Common”的可访问性低于类“Instance”
我可以使用公共抽象类 Common 完成我想要的任务,但为什么我不能根本不公开名称?
编辑:我要问的是为什么它会这样工作!每个人都在回答规则是什么,而不是解释为什么规则是这样的。
I do not understand the accessibility limitations exhibited below
public abstract class Base { }
internal class Common : Base { }
public class Instance : Common { }
This won't compile.
Inconsistent accessibility: base class 'Common' is less accessible than class 'Instance'
I can accomplish what I wanted with public abstract class Common but why can't I simply not expose the name at all?
Edit: What I'm asking is WHY it works this way! Everyone is answering with what the rules are, not explaining why the rules are that way.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
类的继承者不能扩大基类的可访问范围。
一旦仅限于该程序,
内部类
的所有继承者都必须保持内部
或采用较低的可访问性(受保护的内部
> 或私有
)。根据 C# 规范,第 §3.5.4 辅助功能部分限制:
。此外:
如果您尝试创建一个
Common
类,其功能您不希望外部代码访问,则应该 优先选择组合而不是继承。例如:Inheritors of a class cannot widen the scope of accessibility of the base class.
Once limited to the program, all inheritors of a,
internal class
must remaininternal
or assume lesser accessibility (protected internal
orprivate
).Per the C# specification, section §3.5.4 Accessibility constraints:
Also:
If you are trying to create a
Common
class with functionality you prefer not to make accessible to external code, you should prefer composition over inheritance. For example:问题在于代码对其他程序集的可见程度。通过将 Common 设置为内部,您将限制对 Common 的程序集的访问,而通过将 Instance 设置为公共,您将使得任何引用程序集都可以访问 Instance。如果引用程序集无法访问类型的基类,它如何访问从该基类继承的任何成员?
It's a matter of how visible the code is to other assemblies. By making Common internal you're limiting access to Common to its assembly whereas by making Instance public you're making Instance accessible to any referencing assembly. If a referencing assembly can't access a type's base class how could it access any members inherited from that base class?
如果
Common
包含一个属性,比如Foo
:,那么
Instance
类会自动公开该属性。你可以这样想:由于
Instance
需要公开Common
拥有的所有内容,因此基类上的访问修饰符不能少公开。但是,您可以将属性
Foo
创建为内部属性来完成大致相同的任务。If
Common
contained a property, lets sayFoo
:than the
Instance
class would automatically expose that property. You can think of it this way:Since
Instance
needs to expose everythingCommon
has, the access modifier on the base class cannot be less exposed.You could, however, create the property
Foo
as internal to accomplish much the same.