为什么我不能有一个从内部类继承的公共类?

发布于 2024-12-24 17:07:30 字数 360 浏览 0 评论 0原文

我不明白下面展示的可访问性限制

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

国粹 2024-12-31 17:07:30

类的继承者不能扩大基类的可访问范围。

公共:访问不受限制
内部:访问仅限于该程序

一旦仅限于该程序,内部类的所有继承者都必须保持内部或采用较低的可访问性(受保护的内部 > 或私有)。

根据 C# 规范,第 §3.5.4 辅助功能部分限制:

实例构造函数的参数类型必须至少为
可作为实例构造函数本身访问。

在示例中

A 类 {...}

公共类 B:A {...}

B 类会导致编译时错误,因为 A 至少不是
与 B 一样容易访问。

。此外:

类类型的直接基类必须至少具有可访问性
作为类类型本身(第 3.5.2 节)。例如,它是一个编译时
公共类从私有类或内部类派生时出错。

如果您尝试创建一个 Common 类,其功能您不希望外部代码访问,则应该 优先选择组合而不是继承。例如:

public abstract class Base
{
...
}

internal class Common : Base
{
...
}

public class Instance
{
    internal Instance(Common common)
    {
        ...
    }
...
}

Inheritors of a class cannot widen the scope of accessibility of the base class.

public: Access not limited
internal: Access limited to this program

Once limited to the program, all inheritors of a, internal class must remain internal or assume lesser accessibility (protected internal or private).

Per the C# specification, section §3.5.4 Accessibility constraints:

The parameter types of an instance constructor must be at least as
accessible as the instance constructor itself.

In the example

class A {...}

public class B: A {...}

the B class results in a compile-time error because A is not at least
as accessible as B.

Also:

The direct base class of a class type must be at least as accessible
as the class type itself (§3.5.2). For example, it is a compile-time
error for a public class to derive from a private or internal class.

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:

public abstract class Base
{
...
}

internal class Common : Base
{
...
}

public class Instance
{
    internal Instance(Common common)
    {
        ...
    }
...
}
dawn曙光 2024-12-31 17:07:30

问题在于代码对其他程序集的可见程度。通过将 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?

洒一地阳光 2024-12-31 17:07:30

如果 Common 包含一个属性,比如 Foo:

public string Foo { get; set; }

,那么 Instance 类会自动公开该属性。你可以这样想:

public void Test()
{
    Common myInstance = new Instance();
    System.Console.WriteLine(myInstance.Foo);
}

由于 Instance 需要公开 Common 拥有的所有内容,因此基类上的访问修饰符不能少公开。

但是,您可以将属性 Foo 创建为内部属性来完成大致相同的任务。

If Common contained a property, lets say Foo:

public string Foo { get; set; }

than the Instance class would automatically expose that property. You can think of it this way:

public void Test()
{
    Common myInstance = new Instance();
    System.Console.WriteLine(myInstance.Foo);
}

Since Instance needs to expose everything Common 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文