在 OOP 中,私有成员对谁来说是私有的?
例如,在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
private
和protected
并不是为了防止其他编码人员访问类的内部,而是为了防止您自己创建程序没有明确定义的接口。如果项目中的每个类都可以修改其他所有类,那么您不仅容易因为巨大的状态空间而引入错误,而且还会阻止您自己:
private
andprotected
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:
访问修饰符实现了两个不同的目的:
它们限制了可能导致副作用的代码量,从而更容易建立不变量。
它们保护类的客户端免受内部表示更改的影响。
对于小型项目,这些优势可能不会立即显现出来,尤其是对于初学者而言。
Access modifiers achieve two different things:
They limit the amount of code that can cause side effects, making it easier to establish invariants.
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.
保护未来的自己,否则他可能会意外地忘记对象的哪些部分是应该与系统其他部分解耦的细节,以及哪些部分是系统其他部分可以依赖的可靠接口。
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.
该语言试图强迫您编写“好”代码。 “好”意味着代码是结构化的、干净的并且不易出错。所以你必须声明类型、私有成员等等。如果你不想这样,你可以使用在这方面较少的语言,比如 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.
您将类的成员标记为
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 aCar
class, and then you write a lot of code that uses theCar
class, and you suddenly realize that your implementation performs very poorly and you need to refactor it, if all of theCar
implementation details are private to theCar
class, you know that none of the code usingCar
accesses those things and you can change them at will. If you didn't mark themprivate
, 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.这样想:保护级别定义了您以后可以更改的内容,而无需关心除此类(私有)之外的任何其他代码段,无需关心除此类以及从此类继承的每个类(受保护)之外的任何其他代码段)并且除了使用此类(公共)的每段代码之外,无需关心任何其他代码。
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).
private
或protected
来自封装概念。它来自数据隐藏的概念。我相信这个归纳至少对我来说是清晰且有用的:注意: 答案很好,这个答案就是为了完成它们
private
orprotected
come from encapsulation concept. and it comes from data hiding concept. I believe this intoduction is clear and useful at least for me :Note: Answers are well, and this answer is to complete them
如果您将类的成员(变量或方法)定义为私有,您将无法从外部使用它,使用另一个类,使用点运算符。 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.