公共变量和私有变量之间的区别

发布于 2024-10-07 18:00:59 字数 248 浏览 4 评论 0原文

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

         static void yo()
         {
         }
    }
}

有人可以告诉我这段代码摘录中变量 numberOne 和 numberTwo 之间的区别吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(8

榆西 2024-10-14 18:00:59

来自 辅助功能级别,位于 MSDN

公共访问不受限制。

受保护 访问权限仅限于包含类或派生自的类型
包含类。

内部访问仅限于当前程序集。

受保护的内部访问权限仅限于当前程序集或
派生自包含的类型
类。

私有访问仅限于包含类型。

From the accessibility levels at MSDN:

public Access is not restricted.

protected Access is limited to the containing class or types derived from
the containing class.

internal Access is limited to the current assembly.

protected internal Access is limited to the current assembly or
types derived from the containing
class.

private Access is limited to the containing type.

久伴你 2024-10-14 18:00:59

公共变量可以从外部类访问,但私有变量只能由当前类和内部类访问:

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);
    }
}

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);
    }
}
伊面 2024-10-14 18:00:59

其他对象可以访问 NumberOne,但不能访问 numberTwo。

所以我可以做 Your_Object.numberOne = 14; 但我不能做 Your_Object.numberTwo= 14;

现在我也许可以通过反射访问 numberTwo,具体取决于在应用程序中设置权限,但我不能直接设置。

C# 中的变量
C# 中的反射
C# 中的访问器

Other objects can access NumberOne, but they can't access numberTwo.

So I can do Your_Object.numberOne = 14; but I can't do 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.

Variables in C#
Reflection in C#
Accessors in C#

不念旧人 2024-10-14 18:00:59

一般来说,PUBLIC 意味着类之外的任何其他代码都可以更改其值。 PRIVATE 只能在类本身中使用/更改。此外,如果您构建从另一个派生的类,并且希望子级类能够使用/更改其父级的值,但不能使用该类之外的其他通用代码,请使用 PROTECTED。

in general, PUBLIC means any other code outside the class can change its value. PRIVATE are only able to be used/changed IN the class itself. 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.

不疑不惑不回忆 2024-10-14 18:00:59

搜索“封装”。
有很多简单的材料可以研究它。

Search for "Encapsulation".
There are so many easy materials to study it.

书信已泛黄 2024-10-14 18:00:59

用最简单的术语来说,numberOne 被标记为公共意味着,如果您创建 hithere 类的实例,则该成员变量将是可访问的。例如:

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

numberTwo 是私有的意味着它不可访问,只能在 hithere 类中使用。因此,进一步考虑上面的示例:

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

最后一行将导致编译器错误,因为您无法访问 numberTwo,因为这是私有的。

花一些时间阅读访问修饰符可能是值得的,快速谷歌会找到很多例子,例如:

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

另外,您的 hithere 类没有定义访问修饰符,因此编译器会假定此是私人的。因此,如果您要从另一个库引用您的库,您将无法创建 hithere 类的实例。

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. 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. 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.

It is probably worth spending some time reading up on access modifiers, a quick google will find many examples, e.g:

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. As such if you were to reference your library from another you would not be able to create instances of the hithere class.

从来不烧饼 2024-10-14 18:00:59

publicprivate 是成员的访问修饰符。这是指谁可以通过代码直接访问会员。 public表示访问不受限制,任何人都可以直接通过代码访问该会员。 private 表示访问仅限于包含的类。所以每个人都可以通过代码直接访问numberOne,但只有包含类可以通过代码直接访问numberTwo

共有五个访问修饰符:

public:访问不受限制

protected:访问仅限于包含类或从包含类派生的类

internal< /code>:访问仅限于包含的程序集

protected internal:这是 protectedinternal 的 OR,因此访问仅限于包含类或从包含类派生的类或包含程序集

private:访问仅限于包含类

请注意,“直接通过代码”子句在这里至关重要;可以使用反射来访问这些成员。

C# 规范的相关部分是 §3.5,尤其是 §3.5.2。

public and private are access modifiers for members. 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. private means that access is limited only to the containing class. So everyone can access numberOne directly through code, but only the containing class can access numberTwo directly through code.

There are a total of five access modifiers:

public: access is not limited

protected: access is limited to the containing class or classes derived from the containing class

internal: access is limited to the containing assembly

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

private: access is limited to the containing class

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.

血之狂魔 2024-10-14 18:00:59

这意味着如果您有另一个这样的类:

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

第二个错误,但第一个没问题。

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.

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