在 OOP 中,私有成员对谁来说是私有的?

发布于 2024-12-10 05:07:13 字数 137 浏览 0 评论 0原文

例如,在 OOP 中为什么需要放置一些 Private 。我知道任何私有成员都不能被访问但具有相同的类对象。但为什么我需要这样做,而我是我的项目的唯一编码员。同样的问题延伸到Protected,免受谁的侵害!

In OOP why need to put something Private , for example. I know that any private member can not be accessed but with the same class objects. But why I need to do that while I am the only coder of my project. The same question extends to Protected, protected from who!

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

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

发布评论

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

评论(8

沩ん囻菔务 2024-12-17 05:07:13

privateprotected 并不是为了防止其他编码人员访问类的内部,而是为了防止您自己创建程序没有明确定义的接口。

如果项目中的每个类都可以修改其他所有类,那么您不仅容易因为巨大的状态空间而引入错误,而且还会阻止您自己:

  • 更改任何类的实现(同时保持接口相同)。
  • 曾经向项目中介绍过不熟悉所有类的所有内部结构的人。除非你有完美的记忆力并且能够背诵你曾经写过的每一行代码,包括未来的你。
  • 模拟对象以进行单元测试
  • 与程序/库的其他版本进行交互。假设您确实更改了一个类的内部结构,并且您设法跟踪项目中对该内部属性的每个引用。即使这样,您可能也需要再次与旧版本的程序交互。如果使用属性而不是 getter/setter 方法,这将变得尤其困难。

private and protected are not there to prevent other coders from accessing the internals of a class, but (also) to prevent yourself from creating a program without clearly defined interfaces.

If every class in your project can modify every other class, you're not only prone to introduce bugs because of the huge state space, but also preventing yourself from:

  • Changing the implementation (while keeping the interface the same) of any class.
  • Ever introducing anyone not familiar with all the internals of all the classes to the project. Unless you have a perfect memory and can recite every line of code you've ever written, that includes future you.
  • Mocking up objects for unit testing
  • Interacting with other versions of your program/library. Suppose you do change internals of one class, and you manage to track down every reference to that internal property in your project. Even then, you may need to interface with the old version of your program again. This becomes especially hard if used properties instead of getter/setter methods.
臻嫒无言 2024-12-17 05:07:13

访问修饰符实现了两个不同的目的:

  1. 它们限制了可能导致副作用的代码量,从而更容易建立不变量。

  2. 它们保护类的客户端免受内部表示更改的影响。

对于小型项目,这些优势可能不会立即显现出来,尤其是对于初学者而言。

Access modifiers achieve two different things:

  1. They limit the amount of code that can cause side effects, making it easier to establish invariants.

  2. They protect clients of the class from changes to the internal representation.

For small projects, these advantages might not be immediately visible, especially for beginners.

梦开始←不甜 2024-12-17 05:07:13

保护未来的自己,否则他可能会意外地忘记对象的哪些部分是应该与系统其他部分解耦的细节,以及哪些部分是系统其他部分可以依赖的可靠接口。

Protected from your future self, who could otherwise accidentally forget what parts of an object are a detail that should be decoupled from the rest of the system, and which parts are a solid interface that can be relied on by the rest of the system.

☆獨立☆ 2024-12-17 05:07:13

该语言试图强迫您编写“好”代码。 “好”意味着代码是结构化的、干净的并且不易出错。所以你必须声明类型、私有成员等等。如果你不想这样,你可以使用在这方面较少的语言,比如 python。但这意味着,您的程序可能(可能!)更加不安全,或者如果它变得非常大,则很容易被误解。与评论相同。你不必写它们。尤其是当你是唯一的程序员时。但这是一种很好的风格,如果半年后你再次阅读你的程序,你会非常感激。

the language tries to force you, to write "good" code. "good" means that the code is structured, clean and not susceptible to error. so you have to declare types, private members and so on. if you don't want that, you could use a language thats lesser in this aspects, like python. but this means, that your program could (could!) be more insecure or if it gets very big, easy to misunderstand. it's the same as with comments. you haven't to write them. especially when you are the only programmer. but it's a good style and you will be very thankfull for that if you read your program again, in a half year.

聚集的泪 2024-12-17 05:07:13

您将类的成员标记为private,不得从类外部访问这些成员。例如,您可以使用它来隐藏实现细节,这样您就可以更改实现,而不会影响使用您的类的其他代码。隐藏实现细节是 OOP(封装)的关键方面之一。如果您创建一个 Car 类,然后编写大量使用该 Car 类的代码,然后您突然意识到您的实现性能非常差,需要重构如果所有 Car 实现细节都是 Car 类私有的,那么您知道没有任何代码使用 Car< /code> 访问这些东西,你可以随意更改它们。如果您没有将它们标记为私有,您可能会在其他代码中使用其中的一些,然后这些代码就会中断。

类似地,protected(无论如何,在 Java 中)具有相同的目的,但允许从您的类派生的类访问这些成员。这是相当弱的保护,因为这当然意味着您无法在不影响派生类的情况下更改基类的实现细节。

You mark the members of a class private that must not be accessed from outside the class. E.g., you use it to hide implementation details, so you can change the implementation without affecting other code using your class. Hiding implementation details is one of the key aspects of OOP (encapsulation). If you create a Car class, and then you write a lot of code that uses the Car class, and you suddenly realize that your implementation performs very poorly and you need to refactor it, if all of the Car implementation details are private to the Car class, you know that none of the code using Car accesses those things and you can change them at will. If you didn't mark them private, you might have used some of them in that other code, which would then break.

Similarly, protected (in Java, anyway) is for the same purpose but allows classes derived from your class to access those members. This is fairly weak protection, because of course it means that you can't change the implementation details of the base class without affecting derived classes.

亚希 2024-12-17 05:07:13

这样想:保护级别定义了您以后可以更改的内容,而无需关心除此类(私有)之外的任何其他代码段,无需关心除此类以及从此类继承的每个类(受保护)之外的任何其他代码段)并且除了使用此类(公共)的每段代码之外,无需关心任何其他代码。

Think about it this way: The protection level defines what you can change later without care for any other piece of code besides this class (private), without care for any other piece of code besides this class and every class inheriting from this class (protected) and without care for any other piece of code besides every piece of code using this class (public).

伊面 2024-12-17 05:07:13

privateprotected 来自封装概念。它来自数据隐藏的概念。我相信这个归纳至少对我来说是清晰且有用的:

封装是将数据和函数组合成一个整体的过程
称为类的单个单元。采用封装的方法,
程序员无法直接访问数据。数据只能访问
通过类内部现有的函数。数据封装
引出了数据隐藏的重要概念。数据隐藏是
对用户隐藏的类的实现细节。这
限制访问的概念导致程序员编写专门的
对隐藏成员执行操作的函数或方法
班级的。必须注意确保班级
设计得当。 (Sripriya Rajagopalan

注意: 答案很好,这个答案就是为了完成它们

private or protected come from encapsulation concept. and it comes from data hiding concept. I believe this intoduction is clear and useful at least for me :

Encapsulation is the process of combining data and functions into a
single unit called class. Using the method of encapsulation, the
programmer cannot directly access the data. Data is only accessible
through the functions existing inside the class. Data encapsulation
led to the important concept of data hiding. Data hiding is the
implementation details of a class that are hidden from the user. The
concept of restricted access led programmers to write specialized
functions or methods for performing the operations on hidden members
of the class. Attention must be paid to ensure that the class is
designed properly. (Sripriya Rajagopalan)

Note: Answers are well, and this answer is to complete them

毁梦 2024-12-17 05:07:13

如果您将类的成员(变量或方法)定义为私有,您将无法从外部使用它,使用另一个类,使用点运算符。 Protected 帮助您保护成员变量或方法不被继承。

If you define a member (variable or method) of a class as private, you won't be able to use it from outside, using another class, using the dot operator. Protected helps you to protect the member variable or method from being inherited.

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