公共变量和私有变量之间的区别
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
来自 辅助功能级别,位于 MSDN:
From the accessibility levels at MSDN:
公共变量可以从外部类访问,但私有变量只能由当前类和内部类访问:
Public variables are accessible from out side classes but private one just accessible for current class and inner classes:
其他对象可以访问 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 doYour_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#
一般来说,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.
搜索“封装”。
有很多简单的材料可以研究它。
Search for "Encapsulation".
There are so many easy materials to study it.
用最简单的术语来说,
numberOne
被标记为公共意味着,如果您创建hithere
类的实例,则该成员变量将是可访问的。例如:numberTwo
是私有的意味着它不可访问,只能在hithere
类中使用。因此,进一步考虑上面的示例:最后一行将导致编译器错误,因为您无法访问 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 yourhithere
class this member variable will be accessible. For example:numberTwo
being private means that this is not accessible and can only be used within thehithere
class. So taking the above example further: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 thehithere
class.public
和private
是成员的访问修饰符。这是指谁可以通过代码直接访问会员。public
表示访问不受限制,任何人都可以直接通过代码访问该会员。private
表示访问仅限于包含的类。所以每个人都可以通过代码直接访问numberOne
,但只有包含类可以通过代码直接访问numberTwo
。共有五个访问修饰符:
public
:访问不受限制protected
:访问仅限于包含类或从包含类派生的类internal< /code>:访问仅限于包含的程序集
protected internal
:这是protected
和internal
的 OR,因此访问仅限于包含类或从包含类派生的类或包含程序集private
:访问仅限于包含类请注意,“直接通过代码”子句在这里至关重要;可以使用反射来访问这些成员。
C# 规范的相关部分是 §3.5,尤其是 §3.5.2。
public
andprivate
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 accessnumberOne
directly through code, but only the containing class can accessnumberTwo
directly through code.There are a total of five access modifiers:
public
: access is not limitedprotected
: access is limited to the containing class or classes derived from the containing classinternal
: access is limited to the containing assemblyprotected internal
: this is an OR ofprotected
andinternal
so that access is limited to the containing class or classes derived from the containing class OR the containing assemblyprivate
: access is limited to the containing classNote 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.
这意味着如果您有另一个这样的类:
第二个错误,但第一个没问题。
It means that if you have another class like this:
The second one errors, but the first is OK.