简体   繁体   English

了解默认访问修饰符

[英]Understanding default access modifiers

I am confused about access modifiers, so I thought I would ask a couple of quick questions for clarification: 我对访问修饰符感到困惑,因此我想问几个简单的问题以进行澄清:

Is it always the case that in the absence of access modifiers for data members of a class, the default is private , though the class itself is internal ? 是否总是在缺少类的数据成员的访问修饰符的情况下,尽管类本身是internal ,但默认值为private

class A
{
    int x;
}

So, int x is private int x and class A is internal class A ? 因此, int xprivate int xclass Ainternal class A

========================================= ========================================

Also, why would the following code not compile? 另外,为什么以下代码无法编译?

class A
{
    protected int x;
}

public class B : A
{}

As the documentation states, classes and structs are by default internal and their members are private. 文档所述,默认情况下,类和结构是内部的,它们的成员是私有的。

The code won't compile because, as the error message will state, you cannot inherit from a less accessible class. 该代码将无法编译,因为正如错误消息所指出的那样,您无法从难以访问的类继承。 In this case the child class would be public and the parent internal. 在这种情况下,子类将是公共类,父类是内部类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM