为什么封装是OOP语言的一个重要特征? [英] Why encapsulation is an important feature of OOP languages?

查看:169
本文介绍了为什么封装是OOP语言的一个重要特征?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在不同的地方接受采访时有人问我为什么来使用封装?谁的要求实际上是封装?它是对程序的用户?抑或是为同事?或者是为了保护code黑客?

I came across different interview where question was asked to me why encapsulation is used? Whose requirement actually is encapsulation? Is it for users of program? Or is it for co-workers? Or is it to protect code from hackers?

推荐答案

封装有助于从接触到类的客户(其他类/函数,使用这个类)的行为分离的实施细则,并为您提供更多的控制权连接在code。考虑下面这个例子,一个类似于在罗伯特·马丁的书清洁code

Encapsulation helps in isolating implementation details from the behavior exposed to clients of a class (other classes/functions that are using this class), and gives you more control over coupling in your code. Consider this example, similar to the one in Robert Martin's book Clean Code:

public class Car
{
//...
public float GetFuelPercentage() { /* ... */ };

//...

private float gasoline;
//...
}

请注意,使用它给你的燃油量在车内功能的客户端并不关心什么类型的燃料做汽车的使用。这种抽象分离不重要的关注(燃料量)(在这方面)详情:无论是气,油或其他任何东西。

Note that the client using the function which gives you the amount of fuel in the car doesn't care what type of fuel does the car use. This abstraction separates the concern (Amount of fuel) from unimportant (in this context) detail: whether it is gas, oil or anything else.

第二件事是,类的作者可以自由地做自己想做的类的内部事情,例如更换汽油油,和其他的东西,只要他们不改变自己的行为。这是由于这样的事实,即他们可以确保没有人依赖于这些细节,因为他们是私有的。的依赖越少,在code中的更灵活,更容易维护它。

The second thing is that author of the class is free to do anything they want with the internals of the class, for example changing gasoline to oil, and other things, as long as they don't change its behaviour. This is thanks to the fact, that they can be sure that no one depends on these details, because they are private. The fewer dependencies there are in the code the more flexible and easier to maintain it is.

还有一件事,在被低估的答案 /用户/ 186997 / utnapistim> utnapistim :低耦合也有助于测试code和维护这些测试。在不太复杂类的接口,来测试它就越容易。如果没有封装,一切暴露这将是很难的COM prehend测试什么和如何。

One other thing, correctly noted in the underrated answer by utnapistim: low coupling also helps in testing the code, and maintaining those tests. The less complicated class's interface is, the easier to test it. Without encapsulation, with everything exposed it would be hard to comprehend what to test and how.

要重申的一些讨论,评论:

To reiterate some discussions in the comments:


  • 没有,封装不是的最重要的东西OOP。我敢甚至说,这是不是很重要。重要的事情是这些鼓励封装 - 像松耦合。但它不是必需的 - 精心开发人员可以保持松散耦合无封装变量等。正如 vlastachu ,Python中指出,是没有机制来执行封装语言的一个很好的例子,但它仍然是OOP是可行的。

  • No, encapsulation is not the most important thing in OOP. I'd dare even to say that it's not very important. Important things are these encouraged by encapsulation - like loose coupling. But it is not essential - a careful developer can maintain loose couplings without encapsulating variables etc.. As pointed out by vlastachu, Python is a good example of a language which does not have mechanisms to enforce encapsulation, yet it is still feasible for OOP.

没有,你躲在背后的字段访问的未封装的。如果你做的唯一事情是在前面的变量写上私人,然后盲目地为他们每个人获取/设置对,那么实际上它们没有被封装。有人在code一个遥远的地方仍然可以与你的类的内部染指,而且还可以的依赖的他们(很好,这是当然的的更好了他们依靠的方法,而不是一个字段)。

No, hiding your fields behind accessors is not encapsulation. If the only thing you've done is write "private" in front of variables and then mindlessly provide get/set pair for each of them, then in fact they are not encapsulated. Someone in a distant place in code can still meddle with internals of your class, and can still depend on them (well, it is of course a bit better that they depend on a method, not on a field).

没有,封装的主要目标不是避免错误。主要目标是至少类似于上面列出的,并认为封装将捍卫你犯错误是幼稚的。只是有很多其他的方式,使改变私有变量旁边一个错误。和改变私有变量是不是很难发现和修复。再次 - Python是这种说法的缘故一个很好的例子,因为它可以不封装它执行

No, encapsulation's primary goal is not to avoid mistakes. Primary goals are at least similar to those listed above, and thinking that encapsulation will defend you from making mistakes is naive. There are just lots of other ways to make a mistake beside altering a private variable. And altering a private variable is not so hard to find and fix. Again - Python is a good example for sake of this argument, as it can have encapsulation without enforcing it.

这篇关于为什么封装是OOP语言的一个重要特征?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆