C# 不一致的可访问性:基类的可访问性低于类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12135711/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 22:12:08  来源:igfitidea点击:

Inconsistent Accessability: Base Class is less accessible than class

c#inheritance

提问by user1618490

I've got the code below and I'm trying to do some inheritance exercises but when I try to run this code it gives me an error:

我有下面的代码,我正在尝试做一些继承练习,但是当我尝试运行此代码时,它给了我一个错误:

Inconsistent Accessability: Base Class is less accessible than class

The code:

编码:

class Program
{
    static void Main()
    {

        FoodProducts Test = new FoodProducts();

        Test.Limit();



    }
}

public class FoodProducts : Products
{
    public void FoodProduct()
    {
        Console.WriteLine("This is food product");
    }

    public void Limit()
    {
        Console.WriteLine("This is an Attribute of a Product");
    }

}

Would someone be able to help me?

有人可以帮助我吗?

回答by Candide

Probably the class Productsis not public. Add publicto the Productsclass definition.

可能课程Products不是公开的。添加publicProducts类定义。

If you have something like:

如果你有类似的东西:

class Products {
 ...
}

The C# compiler interprets the Productsclass as internal.

C# 编译器将Products类解释为内部类。

回答by Jonathon Reinhart

What line is the error on, and what is the specific error text? Also, where is the definition of Products?

错误在哪一行,具体的错误文本是什么?另外,定义在Products哪里?

You are probably getting CS0060: "Inconsistent accessibility: base class 'class1' is less accessible than class 'class2'" Thus, I'm assuming your Productsclass is not marked as public.

您可能会收到CS0060:“不一致的可访问性:基类 'class1' 的可访问性低于类 'class2'” 因此,我假设您的Products类未标记为public.

This problem happens when a base class is marked as something other than public(internal, for example), but then you try to make a publicderived class.

例如,当基类被标记为public( internal)以外的其他内容时,会发生此问题,但随后您尝试创建public派生类。

回答by KingPancake

Add the publicdirective to the class you are trying to inherit from.

public指令添加到您尝试继承的类。

回答by Midrel

That happens when, for example, the base class is private, but the derived class is public. A contradiction, so to speak.

例如,当基类是私有的,而派生类是公共的时,就会发生这种情况。可以说是矛盾。

回答by Riash.R

public class Products

Make you class public(as shown above) in order for it to be inherited or accessible.

将您的类设为 public(如上所示),以便它可以被继承或访问。

回答by programmer

this means if you wish a public child class, the parent class must be public also.

这意味着如果你想要一个公开的子类,父类也必须是公开的。

回答by Benjol

Just for future reference for someone thick like me, I got this in the following situation and couldn't figure out what was going wrong:

仅供像我这样厚的人将来参考,我在以下情况下得到了这个并且无法弄清楚出了什么问题:

public class Foo : Base<Bar> {} <-- Inconsistent accessibility

public class Base<T> {}

It took me a while to work out that the culprit was here:

我花了一段时间才弄清楚罪魁祸首就在这里:

internal class Bar {}

回答by Gagansadashiv

One of the probable reason for this issue may be , you have more than one main class , Make sure you have only one main class.

此问题的可能原因之一可能是,您有多个主类,请确保您只有一个主类。

回答by RayLuo

Lots of answers here suggest to change your base class into public too. In a sense they are correct, but then they miss an equally valid alternative, which is to change your derived class to internal (to match what the base class's accessibility).

这里的很多答案都建议将您的基类也更改为公共类。从某种意义上说,它们是正确的,但随后它们错过了一个同样有效的替代方案,即将派生类更改为内部类(以匹配基类的可访问性)。

So, the real (yet short) answer is to let your base class and derived class to have same accessibility.

因此,真正(但简短)的答案是让您的基类和派生类具有相同的可访问性。

For those who wonder why, the long answer is: when a public derived class attempts to inherit an internal or private base class, it would argurably (more on this later) become semantically unclear whether the sub-class would also want to expose those public methods in the internal/private base class. Eric Lippert gave an detailed explanation in his blog post: Why is deriving a public class from an internal class illegal?, quoted below (with minor edit):

对于那些想知道为什么的人,长答案是:当一个公共派生类试图继承一个内部或私有基类时,它可以说(稍后会详细介绍)在语义上变得不清楚子类是否也想公开那些公共内部/私有基类中的方法。Eric Lippert 在他的博文中给出了详细的解释:为什么从内部类派生一个公共类是非法的?,引述如下(稍作修改):

On the one hand, it is a public method of a base class, and so it seems like it should be accessible (to the derived class too). On the other hand, the fact that Base is internal is evidence that its internal method is supposed to be inaccessible outside the assembly. A basic design principle of C# is that when the intention is unclear, the compiler brings this fact to your attention by failing.

一方面,它是基类的公共方法,因此它似乎应该是可访问的(对派生类也是如此)。另一方面,Base 是 internal 的事实证明它的内部方法应该在程序集之外是不可访问的。C# 的一个基本设计原则是,当意图不明确时,编译器会通过失败来提醒您注意这一事实。

PS: That being said, one may argue that, the compiler could possibly just stick with "one of those 2 hands" and it would still be deterministic. So why was the design decision NOT chosen on one specific way? For such a follow-up question, the answer is, it all boils down to design philosophy that (again, quoted from Eric's blog post):

PS:话虽如此,有人可能会争辩说,编译器可能只是坚持使用“那两只手中的一只”,它仍然是确定性的。那么为什么没有以一种特定的方式选择设计决策呢?对于这样一个后续问题,答案是,这一切都归结为设计哲学(再次引用 Eric 的博客文章):

this rule of the language encourages you to use inheritance relationships to model the business domain semantics rather than as a mechanism for code reuse.

该语言规则鼓励您使用继承关系来建模业务领域语义,而不是作为代码重用的机制。

So that was the choice C# already made.

所以这是 C# 已经做出的选择。

Lastly, for the sake of completeness, it is worth to mention that, the design philosophy above is not necessarily the universal and only way to use inheritance. (Heck, some other languages do not even have the public/internal concept in the first place and they are still successful). Personally I see nothing wrong if we would also want to use inheritance mechanism for code reuse. But C# already chose to only use inheritance for business domain semantics. So, it is what it is.

最后,为了完整起见,值得一提的是,上面的设计理念不一定是通用的,唯一的继承方式。(哎呀,其他一些语言甚至一开始就没有公共/内部概念,但它们仍然是成功的)。如果我们还想使用继承机制进行代码重用,我个人认为没有错。但是 C# 已经选择只对业务领域语义使用继承。所以,就是这样。