简体   繁体   English

公共和私人变量之间的差异

[英]Difference between public and private variables

namespace hi
{
    class hithere
    {
         public int numberOne = 12;
         private int numberTwo = 12;

         static void yo()
         {
         }
    }
}

Can someone tell me the difference between the variables numberOne and numberTwo in this code excerpt? 在这段代码摘录中有人能告诉我变量numberOne和numberTwo之间的区别吗?

From the accessibility levels at MSDN : MSDN可访问性级别

public Access is not restricted. 公共访问不受限制。

protected Access is limited to the containing class or types derived from the containing class. protected Access仅限于从包含类派生的包含类或类型。

internal Access is limited to the current assembly. 内部访问仅限于当前程序集。

protected internal Access is limited to the current assembly or types derived from the containing class. protected internal Access仅限于从包含类派生的当前程序集或类型。

private Access is limited to the containing type. private Access仅限于包含类型。

Other objects can access NumberOne, but they can't access numberTwo. 其他对象可以访问NumberOne,但是它们无法访问numberTwo。

So I can do Your_Object.numberOne = 14; 所以我可以做Your_Object.numberOne = 14; but I can't do Your_Object.numberTwo= 14; 但我不能做Your_Object.numberTwo= 14;

Now I might be able to access the numberTwo through reflection depending on the permissions set up in the application, but I can't directly. 现在我可以通过反射访问numberTwo,具体取决于应用程序中设置的权限,但我无法直接访问。

Variables in C# C#中的变量
Reflection in C# C#中的反思
Accessors in C# C#中的访问器

In the simplest terms numberOne being marked as public means that if you create an instance of your hithere class this member variable will be accessible. 简单来说, numberOne被标记为public意味着如果你创建了一个hithere类的实例, hithere这个成员变量将是可访问的。 For example: 例如:

hithere h = new hithere()
h.numberOne = 42;

numberTwo being private means that this is not accessible and can only be used within the hithere class. numberTwo是私有的意味着这是不可访问的,只能在hithere类中使用。 So taking the above example further: 所以进一步采用上面的例子:

hithere h = new hithere()
h.numberOne = 42;
h.numberTwo = 84;

The last line would cause a compiler error as you cannot access numberTwo as this is private. 最后一行会导致编译器错误,因为您无法访问numberTwo,因为这是私有的。

It is probably worth spending some time reading up on access modifiers, a quick google will find many examples, eg: 可能值得花一些时间阅读访问修饰符,快速谷歌会找到很多例子,例如:

http://www.devsource.com/c/a/Techniques/Understanding-C-Class-and-Member-Modifiers/ http://msdn.microsoft.com/en-us/library/ba0a1yw2%28v=vs.80%29.aspx http://www.devsource.com/c/a/Techniques/Understanding-C-Class-and-Member-Modifiers/ http://msdn.microsoft.com/en-us/library/ba0a1yw2%28v=vs。 80%29.aspx

Additionally your hithere class does not have an access modifier defined therefore the compiler assumes this to be private. 此外,您的hithere类没有定义访问修饰符,因此编译器假定它是私有的。 As such if you were to reference your library from another you would not be able to create instances of the hithere class. 因此,如果您要从另一个引用您的库,则无法创建hithere类的实例。

Public variables are accessible from out side classes but private one just accessible for current class and inner classes: 公共变量可以从外部类访问,但私有的只能访问当前的类和内部类:

public class VarClass
{
    public int publicID = 10;
    private int privateID = 100;
    public VarClass()
    {
        Console.WriteLine(publicID);
        Console.WriteLine(privateID);
    }

    public class InnerClass
    {
        public InnerClass()
        {
            VarClass c = new VarClass();
            Console.WriteLine(c.privateID);
            Console.WriteLine(c.publicID);
        }
    }
}

public class OuterClass
{
    public OuterClass()
    {
        VarClass c = new VarClass();
        Console.WriteLine(c.privateID); // Compile Error
        Console.WriteLine(c.publicID);
    }
}

in general, PUBLIC means any other code outside the class can change its value. 通常,PUBLIC意味着类外的任何其他代码都可以更改其值。 PRIVATE are only able to be used/changed IN the class itself. PRIVATE只能在类本身中使用/更改。 Additionally, if you build classes that are derived from another, and you want the child-level class to be able to use/change values of its parent, but not other generic code outside the class, use PROTECTED. 此外,如果您构建派生自另一个的类,并且您希望子级别的类能够使用/更改其父级的值,而不是该类之外的其他通用代码,请使用PROTECTED。

public and private are access modifiers for members. publicprivate是成员的访问修饰符。 This refers to who can access the members directly through code. 这指的是谁可以通过代码直接访问成员。 public means that access is not limited so that anyone can access the member directly through code. public意味着访问不受限制,以便任何人都可以通过代码直接访问该成员。 private means that access is limited only to the containing class. private表示访问仅限于包含类。 So everyone can access numberOne directly through code, but only the containing class can access numberTwo directly through code. 所以,每个人都可以访问numberOne直接通过代码,但只包含类可以访问numberTwo直接通过代码。

There are a total of five access modifiers: 共有五个访问修饰符:

public : access is not limited public :访问不受限制

protected : access is limited to the containing class or classes derived from the containing class protected :access仅限于包含的类或从包含类派生的类

internal : access is limited to the containing assembly internal :访问仅限于包含程序集

protected internal : this is an OR of protected and internal so that access is limited to the containing class or classes derived from the containing class OR the containing assembly protected internal :这是一个protectedinternal的OR,因此访问仅限于包含的类或从包含类派生的类或包含的程序集

private : access is limited to the containing class private :访问仅限于包含类

Note that the clause "directly through code" is critical here; 请注意,“直接通过代码”这一条款在这里至关重要; it is possible to access these members using reflection. 可以使用反射访问这些成员。

The relevant section of the C# specification is §3.5, especially §3.5.2. C#规范的相关部分是§3.5,尤其是§3.5.2。

Search for "Encapsulation". 搜索“封装”。 There are so many easy materials to study it. 研究它有很多简单的材料。

It means that if you have another class like this: 这意味着如果你有另一个这样的类:

namespace hi
{
    class hithere2
    {
         static void yo2()
         {
             hithere h = new hithere();
             h.numberOne = 2;
             h.numberTwo = 3;
         }
    }
}

The second one errors, but the first is OK. 第二个错误,但第一个是好的。

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

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