为什么不是所有字段/属性/方法都是公开的?
我知道这听起来很愚蠢,但我真的很想知道:) 我目前正在学习 C#,
正如您所知,您需要将“对象”(按钮、标签、文本、变量等)设置为公共或任何您喜欢的内容。
但是,您仍然需要编写这样的代码:
// my point is you cant just type label1.text you need to type class.label1.text
// so there is no chance of getting bugged
//because there is label1 in each of forms/classes
class Classlol = new class();
classlol.label1.blabla
那么,使其在其他形式中无法访问有什么意义呢?为什么每件事都不公开或者默认情况下不公开?
谢谢。
I know this may sound stupid, but i really want to know :)
im learning c# currently,
and as you know you need to set "object"(button,label,text,variable, etc.) public or whatever you like.
However, you still need to write a code like this:
// my point is you cant just type label1.text you need to type class.label1.text
// so there is no chance of getting bugged
//because there is label1 in each of forms/classes
class Classlol = new class();
classlol.label1.blabla
So what's the point of making it unreachable in other forms ? why every thing isnt public or its not public by default ?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用类的目的是能够将代码分成逻辑相关的部分。这使您的代码更易于理解和维护。
例如,如果您需要修改类中的代码,您可以更多地关注类的功能方式,而不是项目的其他部分。但是,类的公共成员在某种程度上限制了这种分离,因为如果您修改公共成员,可能会影响项目的其他部分。
尽可能保持类的私有性(同时仍然可以从应用程序中使用)可以最大限度地分离它与应用程序的其余部分。它使您可以更轻松地仅考虑正在处理的类中的逻辑,并且允许您修改这些私有成员,而不必考虑应用程序的其他部分可能会受到影响。
The point of using classes is to be able to separate your code into logically related pieces. This makes your code easier to understand and maintain.
For example, if you need to modify code in a class, you can focus more on the way that class functions and less on other parts of your project. However, public members of your class limit this separation somewhat because, if you modify a public member, that can affect other parts of your project.
Keeping as much of your class private as possible (while still usable from your application) maximizes the separation between it and the rest of your application. It makes it easier to think about only the logic in the class you are working on, and it allows you to modify those private members without having to think what other parts of your application might be affected.
简单来说,这与你穿衣服的原因几乎相同。并不是所有东西都应该始终暴露在公众面前。选定的事物需要是公开的,以便其他人可以与它们交互,但其他事物是私有的,应该保留在该类的内部。
不过,我可能不应该在最后一句中使用“internal”这个词,因为还有第三个选项:
internal
访问修饰符。 VB.NET 中使用的名称(Friend
)可能更清晰。这表明一段数据应该对单个程序集中的所有其他类可见,但对外部隐藏。类似的类比也适用:有些事情你可能会与最亲密的朋友分享,但仍然不想公开。还有其他更复杂的原因,例如启用信息隐藏、最大化特定类与应用程序其余部分之间的分离,以及即使版本之间的实现细节可能已更改,也保持一致的公共接口,所有这些都有助于良好的面向对象设计。如果您真的想了解本质,我建议您阅读一本关于面向对象编程的好书。如果没有对基础知识有充分的了解,要掌握像 C# 这样的面向对象语言,即使不是不可能,也是非常困难的。
默认情况下,事物不是公开的,因为它们可能包含敏感信息,或者至少包含您不想作为类的公共接口的一部分公开的信息。与简单地将某件事设为私有相比,将某件事公开是一个更大的决定,风险也更大,因此您必须明确做出该决定。
Simply speaking, pretty much the same reason that you wear clothes. Not everything should be exposed to the public at all times. Selected things need to be public so that others can interact with them, but other things are private and should be kept internal to that class.
Although, I probably shouldn't have used the word internal there in that last sentence, because there's a third option: the
internal
access modifier. The name used in VB.NET (Friend
) is probably clearer. This indicates that a piece of data should be visible to all of the other classes within a single assembly, but hidden from outside. A similar analogy applies: there are things that you might share with your closest friends, but still don't want to be public.There are other more complicated reasons, like to enable information hiding, to maximize the separation between a particular class and the rest of an application, and to maintain a consistent public interface even though implementation details may have been changed between versions, all of which contribute to good object-oriented design. If you really want to understand the nitty-gritty, I suggest picking up a good book on object-oriented programming. It's very difficult, if not impossible, to master an object-oriented language like C# without a solid understanding of the fundamentals.
Things aren't public by default because they might contain sensitive information, or at least information that you don't want to expose as part of the class's public interface. Making something public is a bigger decision with more risks than simply making it private, so you are forced to make that decision explicitly.
我建议您阅读更多有关面向对象编程中的抽象的内容。也许关于抽象的维基百科文章是一个很好的起点。
编辑:康拉德是绝对正确的,抽象并不自动意味着“隐藏”信息。你可以说这是面向对象编程中封装的作用。
我想我想说的是,这个问题并不是特定于 C# 的,而是需要阅读一些关于一般面向对象编程原理的内容。
I suggest that you read more about abstraction in object oriented programming. Maybe the Wikipedia article on abstraction is a good place to start.
EDIT: Konrad is absolutely right, abstraction does not automatically imply "hiding" information. You could say that it's the role of encapsulation in object oriented programming.
I guess what I wanted to say is that this question is not specific to C#, but rather begs for a bit of reading on general object oriented programming principles.
默认访问修饰符是
internal
,这意味着它在同一程序集中是公共的,在程序集外部是私有的。如果您想将某些数据公开为公共,例如某些标签的文本,最佳实践是创建公共只读属性,如下所示:
要访问它,您必须使用这样的代码:
这样标签本身不是公共的,但每个人都可以阅读其文本。
The default access modifier is
internal
which means it's public inside the same assembly and private outside the assembly.If you want to expose certain data as public, for example text of some Label, the best practice is creating public readonly property like this:
To access it you'll have to use such code:
This way the Label itself is not public, but its text can be read by everyone.