什么是封装? [英] What is Encapsulation exactly?

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

问题描述

我有两个封装定义,它们不适合一个定义。

I have got two definitions of encapsulation which could not fit into one definition.


  1. 封装是数据隐藏。使用私有受保护公开,将数据打包成单个组件。

  2. 随便更改封装它。保护任何容易发生变化的事情。

  1. Encapsulation is data hiding. With the use of private, protected and public, pack the data into single component.
  2. Whatever changes encapsulate it. Protecting anything which is prone to change.

这两个定义如何讨论相同的概念?

How these two definitions are talking about the same concept?

推荐答案

封装可能是最容易被误解的OOP概念。

Encapsulation is probably the most misunderstood concept of OOP.

封装不是数据隐藏!

封装来自胶囊。这意味着将事物放在一起,将它们关在一个包中,我们在这里讨论的事物是数据和功能。没有封装的编程意味着处理数据的函数在代码中的某处浮动,虽然它们处理您的数据甚至将该特定类型作为输入,但它们与您的数据分开。

"Encapsulation" comes from "capsule". It means putting things together, closing them in a package, and the "things" we are talking about here are data and functions. Programming without encapsulation means that functions dealing with data are "floating around", somewhere in your code, and though they deal with your data and even take that particular type as input, they are separated from your data.

让我举一个例子而不是关注公共等等:如果你有一个处理复数的类,它有一个真实和想象的部分,你可以简单地定义它:

Let me make an example without focusing on "public" and the like: if you have a class that deals with complex numbers, which have a real and imaginary part, you could simply define it like this:

class complex {
    double real;
    double imaginary;
};

使用旧的预封装样式(例如在C中使用)来获取绝对值你可以用这个函数定义一个函数:

With the old, pre-encapsulation style that was used for example in C, to get the absolute value of this number you would define a function like this:

double absolute(double real, double imaginary);

这根本不会连接到班级!当然你也可以定义一个函数,它将一个类复杂作为输入,但它仍然是一个外部函数。因此,要使用它,你必须这样做:

And this wouldn't be connected to the class at all! Of course you could also define a function that takes a class complex as input, but it would still be an external function. Thus, to use it you would have to do this:

complex A;
A.real = 1;
A.imaginary = -3;

并获得您必须致电的绝对值

and to get the absolute value you would have to call

absolute(A.real, A.imaginary);

相反,您可以使用封装和将数据和功能放在一起

Instead, you could use encapsulation and put data and functions together:

class complex {
    double real;
    double imaginary;
    double absolute();  // inside the class, encapsulated into it!
};

然后要获得绝对值,你只需调用方法就像

and then to get the absolute value you would simply have to call the method like

A.absolute();

这根本不需要数据隐藏。优点是代码更易于管理,因为您可以清楚地看到所有相关的事物(即数据和功能)组合在一起,因此您可以一眼就知道您拥有的内容(数据)以及您可以使用的内容它(方法)。

This doesn't require data hiding at all. The advantage is that the code is more manageable, because you can clearly see all the related "things" (that is, data and functions) grouped together, so at a glance you know what you have (data) and what you can do with it (methods).

如果没有这个,就不可能隐藏信息,因为这意味着你限制从外部访问某些成员(私有成员),因此你必须在里面有一些方法,否则你将无法对你的数据做任何事情!

Information hiding wouldn't be possible without this, because it means that you limit access to some members (the private ones) from the outside, therefore you must have some methods inside or you wouldn't be able to do anything with your data!

同时,信息隐藏有助于充分利用封装:如果人们可以从外部访问数据,让其他编码人员编写自己的(未封装的)代码来处理您的数据会有很大的危险,这至少会导致代码重复(即无用的工作)如果实现不完全兼容,则会出现不一致。相反,数据隐藏意味着每个人访问私有数据必须使用提供的公共方法,以便它们对每个人都是相同的。

At the same time, information hiding helps putting encapsulation to good use: if people could access data from the outside, there would be a very high danger to have other coders that write their own (not encapsulated) code to deal with your data, which would at the very least lead to code duplication (i.e., useless efforts) and to inconsistencies if the implementations are not perfectly compatible. Instead, data hiding means that to access private data everybody MUST use the public methods that are provided, so that they are the same for everybody.

因此数据需要封装隐藏有意义,同时它有助于数据隐藏。他们一起运作良好,但他们不是一回事!

So encapsulation is required for data hiding to make sense, and at the same time it is helped by data hiding. Together they work well, but they are not the same thing!

回到你的问题:鉴于此,定义1是错误的。正如CommuSoft所指出的那样,2并不是一个真正的定义,这是一个经验法则。我将补充一点,关于何时使用数据隐藏而不是封装是一个经验法则。

Back to your question: in light of this, definition 1 is wrong. And 2, as noted by CommuSoft, isn't really a definition, it's a rule of thumb. And I will add that it's a rule of thumb about when to use data hiding, not encapsulation.

在旁注中,电子表示这可能是这个问题。我认为值得注意的是,大多数答案都是错误的,包括最佳答案,它提供了封装的一个例子,实际上与封装相反。

On a side note, electrometro suggests this might be a duplicate of this question. I think it is noteworthy to say that most answers there are wrong, including the top answer, which provides an example of encapsulation that actually is the contrary of encapsulation.

如果你想要外部参考,这里有两篇关于此的文章:

If you want external references, here are two articles about this:

封装不是信息隐藏

抽象,封装和信息隐藏(请注意,当他启动一个名为ENCAPSULATION的段落并引用时很多定义,他只是试图表明围绕这个主题的混乱;这些定义是错误的,正如他稍后解释的那样!)

Abstraction, Encapsulation, and Information Hiding (please note that when he starts a paragraph called "ENCAPSULATION" and quotes a lot of definitions, he's just trying to show the confusion surrounding this topic; those definitions are wrong, as he explains later!)

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

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