简体   繁体   English

由于其保护级别而无法访问?

[英]Inaccessible due to its protection level?

I'm still quite new to coding in general, and while this simple program is only meant to be a test to learn how constructors work, I'd still like to know why I'm getting this error.总的来说,我对编码还是很陌生,虽然这个简单的程序只是为了测试构造函数的工作原理,但我仍然想知道为什么我会收到这个错误。

    using System;

    public class methodTest
    {
        int a;
        int b;
        int c;
         public methodTest(int i, int j, int k)
        {
            a = i;
            b = j;
            c = k;
        }
    }

    public class methodObj
    {
        static void Main()
        {
        methodTest obj = new methodTest(10, 20, 30);
        Console.WriteLine("obj = " + obj.b);
        Console.ReadKey();
        }
    }

I'm not entirely sure why I'm getting the error.我不完全确定为什么我收到错误。 The problem is with the Console.WriteLine, where it states it cannot access obj.b.问题出在 Console.WriteLine 上,它声明它无法访问 obj.b。 The variables seem to be declared within a public class, so why can they not be accessed?变量似乎是在公共类中声明的,为什么不能访问它们? I tried searching for a solution to this, but all the questions I found were much too convoluted for me to get an answer I could translate to my own understanding.我尝试寻找解决方案,但我发现的所有问题都太复杂了,我无法得到可以转化为我自己理解的答案。 All help appreciated!所有帮助表示赞赏!

Even though the variables are in a public class, they must be declared as public as they are private by default.即使变量在公共类中,它们也必须声明为公共类,因为默认情况下它们是私有的。

See: Access Modifiers请参阅:访问修饰符

Class members, including nested classes and structs, can be public, protected internal, protected, internal, or private.类成员,包括嵌套类和结构,可以是 public、protected internal、protected、internal 或 private。 The access level for class members and struct members, including nested classes and structs, is private by default .类成员和结构成员(包括嵌套类和结构)的访问级别默认为私有

It is best practice to use capitalized names and properties for public variables.最佳做法是对公共变量使用大写的名称和属性

public A { get; set; }

Properties allow you to control the access of reading/writing of the member, as well as adding logic when they are read or set.属性允许您控制成员的读/写访问,以及在读取或设置它们时添加逻辑。

Access modifiers are keywords used to specify the declared accessibility of a member or a type.访问修饰符是用于指定成员或类型声明的可访问性的关键字。 This section introduces the four access modifiers:本节介绍四种访问修饰符:

The following five accessibility levels can be specified using the access modifiers:可以使用访问修饰符指定以下五个可访问性级别:

  1. public : Access is not restricted. public :访问不受限制。
  2. protected : Access is limited to the containing class or types derived from the containing class. protected :访问仅限于包含类或从包含类派生的类型。
  3. Internal : Access is limited to the current assembly.内部:访问仅限于当前程序集。
  4. protected internal: Access is limited to the current assembly or types derived from the containing class. protected internal:访问仅限于从包含类派生的当前程序集或类型。
  5. private : Access is limited to the containing type. private :访问仅限于包含类型。

Every members in C# are implicitly private, so in your question the a,b and c are defined as private and so you could not access to them from outside of methodTest . C#中的每个成员都是隐式私有的,因此在您的问题中, a,bc被定义为private ,因此您无法从methodTest外部访问它们。 for more information you may need to look at this page : Access Modifiers (C# Programming Guide)有关更多信息,您可能需要查看此页面:访问修饰符(C# 编程指南)

Good Luck!祝你好运! :) :)

This is not caused by the constructor.不是由构造函数引起的。 You get the error because the field methodTest.b is private (in C#, fields are private by default; you need to specify an explicit modifier, so something like public int b; ).您收到错误是因为字段methodTest.b是私有的(在 C# 中,字段默认是私有的;您需要指定一个显式修饰符,例如public int b; )。

Your problem is that, in C#, variables (in this case, fields ) are marked as private if they are not specifically marked with an access modifier.您的问题是,在 C# 中,如果变量(在本例中为fields )没有用访问修饰符专门标记,则它们被标记为private

https://msdn.microsoft.com/en-us/library/wxh6fsc7.aspx https://msdn.microsoft.com/en-us/library/wxh6fsc7.aspx

Classes and structures default to internal , fields, methods, events, properties, etc (basically, all members of classes and structures ) default to private .类和结构默认为internal 、字段、方法、事件、属性等(基本上, classesstructures所有成员)默认为private

Changing int a to public int a (and the same for b/c) will fix it.int a更改为public int a (对于 b/c 也是如此)将修复它。

Though, I recommend not doing that.不过,我建议要这样做。 Instead, make a property for each.相反,为每个属性创建一个属性。

public A { get { return a; } set { a = value; } }
public B { get { return b; } set { b = value; } }
public C { get { return c; } set { c = value; } }

If you want to access your integers a, b, and c from outside of the class they are instantiated in, you have to declare them as public .如果您想从它们被实例化的类之外访问您的整数 a、b 和 c,您必须将它们声明为public However a cleaner option is to use a property, such as:但是,更简洁的选择是使用属性,例如:

public int A {get; set;}
public int B {get; set}
public int C {get; set;}

This sets up you up to potentially limit write access from outside classes, while still leaving the the properties open for reading, such as:这使您可以潜在地限制来自外部类的写访问,同时仍然保持属性打开以供读取,例如:

public int A {get; private set;}
public int B {get; private set}
public int C {get; private set;}

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

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