抽象VS信息隐藏VS封装
Can you tell me what is the difference between abstraction and information hiding in software development?
I am confused. Abstraction hides detail implementation and
information hiding abstracts whole details of something.
Update: I found a good answer for these three concepts. See the separate answer below for several citations taken from there.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(22)
抽象
抽象是一种表现基本细节而不包括背景细节的行为。 抽象类只有方法签名,而实现类可以有自己的实现,这样复杂的细节将对用户隐藏。 抽象侧重于外部视图。 换句话说,抽象是将接口与实际实现分离。
封装
封装解释了将数据成员和方法绑定到单个单元中。 信息隐藏是封装的主要目的。 封装是通过使用私有、公共、受保护等访问说明符来实现的。 类成员变量被设为私有,因此外部世界无法直接访问它们。 封装侧重于内部视图。 换句话说,封装是一种用于保护一个对象中的信息免受其他对象影响的技术。
Abstraction
Abstraction is an act of representing essentail details without including the background details. A abstract class have only method signatures and implementing class can have its own implementation, in this way the complex details will be hidden from the user. Abstraction focuses on the outside view. In otherwords, Abstraction is sepration of interfaces from the actual implementation.
Encapsulation
Encapsulation explains binding the data members and methods into a single unit. Information hiding is the main purpose of encapsulation. Encapsulation is acheived by using access specifiers like private, public, protected. Class member variables are made private so that they cann't be accessible directly to outside world. Encapsulation focuses on the inner view. In otherwords, Encapsulation is a technique used to protect the information in an object from the other object.
请参阅 Joel 关于泄漏抽象定律的文章
JoelOnsoftware
基本上,抽象为您提供了以下自由:思考更高层次的概念。 一个非编程类比是,我们大多数人不知道我们的食物从哪里来,也不知道它是如何生产的,但事实上我们(通常)不必担心它,这让我们可以腾出时间去做其他事情,比如编程。
至于信息隐藏,我同意jamting。
See Joel's post on the Law of Leaky Abstractions
JoelOnsoftware
Basically, abstracting gives you the freedom of thinking of higher level concepts. A non-programming analogy is that most of us do not know where our food comes from, or how it is produced, but the fact that we (usually) don't have to worry about it frees us up to do other things, like programming.
As for information hiding, I agree with jamting.
抽象:抽象是用于识别对象的外部视图的概念/技术。 仅使所需的接口可用。
信息隐藏:它是抽象的补充,因为通过信息隐藏可以实现抽象。 隐藏除外部视图之外的所有其他内容。
封装:将数据和相关功能绑定到一个单元中。 它有利于抽象和信息隐藏。 允许在设备上应用成员访问等功能,以实现抽象和信息隐藏
Abstraction : Abstraction is the concept/technique used to identify what should be the external view of an object. Making only the required interface available.
Information Hiding : It is complementary to Abstraction, as through information hiding Abstraction is achieved. Hiding everything else but the external view.
Encapsulation : Is binding of data and related functions into a unit. It facilitates Abstraction and information hiding. Allowing features like member access to be applied on the unit to achieve Abstraction and Information hiding
牛津英语词典(OED)给出的抽象含义最接近此处的含义是“思想分离的行为”。 更好的定义可能是“代表事物的基本特征,而不包括背景或无关紧要的细节”。
信息隐藏的原则是软件组件(例如类)的用户只需要知道如何初始化和访问组件的基本细节,而不需要知道实现的细节。
编辑:在我看来,抽象是决定应该隐藏实现的哪些部分的过程。
所以它不是抽象与信息隐藏。 它是隐藏 VIA 抽象的信息。
The meaning of abstraction given by the Oxford English Dictionary (OED) closest to the meaning intended here is 'The act of separating in thought'. A better definition might be 'Representing the essential features of something without including background or inessential detail.'
Information hiding is the principle that users of a software component (such as a class) need to know only the essential details of how to initialize and access the component, and do not need to know the details of the implementation.
Edit: I seems to me that abstraction is the process of deciding which parts of the implementation that should be hidden.
So its not abstraction VERSUS information hiding. It's information hiding VIA abstraction.
追寻源头! Grady Booch 说道(《面向对象分析与设计》,第 49 页,第二版):
换句话说:抽象=外部的对象; 封装(通过信息隐藏实现)=对象内部,
示例:
在 .NET Framework 中,
System.Text.StringBuilder
类提供了对字符串缓冲区的抽象。 这种缓冲区抽象允许您使用缓冲区而无需考虑其实现。 因此,您可以将字符串附加到缓冲区,而无需考虑StringBuilder
内部如何跟踪指向缓冲区的指针以及在缓冲区满时管理内存(这是通过封装来实现的)通过信息隐藏)。RP
Go to the source! Grady Booch says (in Object Oriented Analysis and Design, page 49, second edition):
In other words: abstraction = the object externally; encapsulation (achieved through information hiding) = the object internally,
Example:
In the .NET Framework, the
System.Text.StringBuilder
class provides an abstraction over a string buffer. This buffer abstraction lets you work with the buffer without regard for its implementation. Thus, you're able to append strings to the buffer without regard for how theStringBuilder
internally keeps track of things such the pointer to the buffer and managing memory when the buffer gets full (which it does with encapsulation via information hiding).rp
OP用他发现的几处引文更新了他的问题,即在 Edward V. Berard 的文章,标题为“抽象、封装和信息隐藏”。 我重新发布了 OP 更新的稍微扩展和重新格式化的版本,因为它本身应该是一个答案。
(所有引文均取自上述文章。)
摘要:
信息隐藏:
封装:
结论:
The OP updated his question with several citations that he had found, namely in an article by Edward V. Berard titled, "Abstraction, Encapsulation, and Information Hiding". I am re-posting a slightly expanded and reformatted version of the OP's update, since it should be an answer in its own right.
(All citations are taken from the article mentioned above.)
Abstraction:
Information Hiding:
Encapsulation:
Conclusion:
封装:绑定数据和作用于数据的方法。 这允许对其他类中的所有其他方法隐藏数据。
示例:
MyList
类,可以添加项目、删除项目和删除所有项目add
、remove
和removeAll
方法作用于不能从外部直接访问的列表(私有数组)。抽象:隐藏不相关的行为和数据。
项目的实际存储、添加或删除方式是隐藏的(抽象的)。
我的数据可能保存在简单数组、ArrayList、LinkedList 等中。
此外,这些方法的实现方式对外部是隐藏的。
Encapsulation: binding data and the methods that act on it. this allows the hiding of data from all other methods in other classes.
example:
MyList
class that can add an item, remove an item, and remove all itemsthe methods
add
,remove
, andremoveAll
act on the list(a private array) that can not be accessed directly from the outside.Abstraction: is hiding the non relevant behavior and data.
How the items are actually stored, added, or deleted is hidden (abstracted).
My data may be held in simple array, ArrayList, LinkedList, and so on.
Also, how the methods are implemented is hidden from the outside.
封装——以受控方式强制访问内部数据或防止直接访问成员。
抽象 - 隐藏某些方法的实现细节称为抽象
让我们通过一个示例来理解: -
现在为抽象定义一个只能访问且用户不知道的方法
该方法的主体是什么以及它是如何工作的
让我们考虑上面的例子,我们可以定义一个方法area来计算矩形的面积。
现在,每当用户使用上述方法时,他只会获得面积而不是计算方式。 我们可以考虑一个 println() 方法的例子,我们只知道它是用于打印的,但我们不知道它如何打印数据。
我已经写了一篇详细的博客,您可以查看下面的链接以获取更多信息
抽象与封装
Encapsulation- enforcing access to the internal data in a controlled manner or preventing members from being accessed directly.
Abstraction- Hiding the implementation details of certain methods is known as abstraction
Let's understand with the help of an example:-
now for abstraction define a method that can only be accessed and user doesnt know
what is the body of the method and how it is working
Let's consider the above example, we can define a method area which calculates the area of the rectangle.
Now, whenever a user uses the above method he will just get the area not the way how it is calculated. We can consider an example of println() method we just know that it is used for printing and we don't know how it prints the data.
I have written a blog in detail you can see the below link for more info
abstraction vs encapsulation
我也对抽象和封装这两个概念感到非常困惑。 但是当我在 myjavatrainer.com 上看到 抽象文章 时,情况变得很清楚对我来说,抽象和封装就像苹果和橘子,你无法真正比较它们,因为两者都是必需的。
封装是对象的创建方式,抽象是外部世界如何查看对象。
I too was very confused about the two concepts of Abstraction and Encapsulation. But when I saw the abstraction article on myjavatrainer.com, It became clear to me that Abstraction and Encapsulation are Apples and Oranges, you can't really compare them because both are required.
Encapsulation is how the object is created, and abstraction is how the object is viewed in the outside world.
封装:将数据成员和成员函数绑定在一起称为封装。 封装是通过类来完成的。
抽象:从使用或视图中隐藏实现细节称为抽象。
前任:
整数x;
我们不知道 int 内部如何工作。 但我们知道 int 会起作用。 这就是抽象。
Encapsulation: binding the data members and member functions together is called encapsulation. encapsulation is done through class.
abstraction: hiding the implementation details form usage or from view is called abstraction.
ex:
int x;
we don't know how int will internally work. but we know int will work. that is abstraction.
简而言之
封装:–信息隐藏
抽象:–隐藏实现
抽象
让您专注于对象做什么
而封装意味着对象如何工作
In very short
Encapsulation:– Information hiding
Abstraction :– Implementation hiding
Abstraction
lets you focus onwhat the object does
while Encapsulation meanshow an object works
抽象允许您将复杂的过程视为简单的过程。 例如,标准的“文件”抽象将文件视为连续的字节数组。 用户/开发人员甚至不必考虑集群和碎片问题。 (抽象通常显示为类或子例程。)
信息隐藏是为了保护您的抽象免受恶意/不称职的用户的侵害。 通过将某些状态(例如硬盘分配)的控制限制给原始开发人员,大量的错误处理变得多余。 如果除了文件系统驱动程序之外没有其他人可以写入硬盘驱动器,则文件系统驱动程序确切地知道写入硬盘驱动器的内容以及写入位置。 (这个概念的通常表现是 OO 语言中的
private
和protected
关键字。)Abstraction allows you to treat a complex process as a simple process. For example, the standard "file" abstraction treats files as a contiguous array of bytes. The user/developer does not even have to think about issues of clusters and fragmentation. (Abstraction normally appears as classes or subroutines.)
Information hiding is about protecting your abstractions from malicious/incompetent users. By restricting control of some state (hard drive allocations, for example) to the original developer, huge amounts of error handling becomes redundant. If nobody else besides the file system driver can write to the hard drive, then the file system driver knows exactly what has been written to the hard drive and where. (The usual manifestation of this concept is
private
andprotected
keywords in OO languages.)请不要将简单的概念复杂化。
封装:将数据和方法包装到一个单元中就是封装(例如类)
抽象:它是一种仅表示本质事物而不包括背景细节的行为。 (例如接口)
示例和更多信息请转到:
http://thecodekey.com/C_VB_Codes/Encapsulation.aspx
http://thecodekey.com/C_VB_Codes/Abstraction.aspx
批准的定义 这里
PS:我还记得我们在第十一课时读到的 Sumita Arora 的一本名为 C++ 的书中的定义;)
Please don't complicate simple concepts.
Encapsulation : Wrapping up of data and methods into a single unit is Encapsulation (e.g. Class)
Abstraction : It is an act of representing only the essential things without including background details. (e.g. Interface)
FOR EXAMPLES AND MORE INFO GOTO :
http://thecodekey.com/C_VB_Codes/Encapsulation.aspx
http://thecodekey.com/C_VB_Codes/Abstraction.aspx
Approved definitions here
P.S.: I also remember the definition from a book named C++ by Sumita Arora which we read in 11th class ;)
抽象
通过在基本功能上提供一个层来隐藏实现细节。信息隐藏
正在隐藏受该实现影响的数据。private
和public
的使用属于此范围。 例如,隐藏类的变量。封装
就是将所有相似的数据和功能归为一组,例如编程中的Class
; 网络中的数据包
。通过使用类,我们实现了所有三个概念 - 抽象、信息隐藏和封装
Abstraction
is hiding the implementation details by providing a layer over the basic functionality.Information Hiding
is hiding the data which is being affected by that implementation. Use ofprivate
andpublic
comes under this. For example, hiding the variables of the classes.Encapsulation
is just putting all similar data and functions into a group e.gClass
in programming;Packet
in networking.Through the use of Classes, we implement all three concepts -
Abstraction
,Information Hiding
andEncapsulation
在一一阅读完上述所有答案后,我无法阻止自己发布该内容
然而,从上面的封装是相当清楚的 ->
参考 wiki
After reading all the above answers one by one I cant stop myself from posting that
Encapsulation is quite clear from above however ->
reference wiki
抽象和封装都是四个基本 OOP 概念中的两个,它们允许您将现实世界的事物建模为对象,以便您可以在程序和代码中实现它们。 许多初学者对抽象和封装感到困惑,因为它们看起来非常相似。 如果你问某人什么是抽象,他会告诉你这是一个 OOP 概念,通过隐藏不必要的细节来关注相关信息,而当你问到封装时,很多人会告诉你这是另一个 OOP 概念,隐藏来自外界的数据。 这些定义并没有错,因为抽象和封装都隐藏了一些东西,但关键的区别在于意图。
抽象通过为您提供更抽象的图片(类似于 10,000 英尺的视图)来隐藏复杂性,而封装则隐藏内部工作,以便您以后可以更改它。 换句话说,抽象隐藏了设计层面的细节,而封装则隐藏了实现层面的细节。
Both Abstraction and Encapsulation are two of the four basic OOP concepts which allow you to model real-world things into objects so that you can implement them in your program and code. Many beginners get confused between Abstraction and Encapsulation because they both look very similar. If you ask someone what is Abstraction, he will tell that it's an OOP concept which focuses on relevant information by hiding unnecessary detail, and when you ask about Encapsulation, many will tell that it's another OOP concept which hides data from outside world. The definitions are not wrong as both Abstraction and Encapsulation does hide something, but the key difference is on intent.
Abstraction hides complexity by giving you a more abstract picture, a sort of 10,000 feet view, while Encapsulation hides internal working so that you can change it later. In other words, Abstraction hides details at the design level, while Encapsulation hides details at the implementation level.
正如您所说,抽象是隐藏实现的细节。
您将某些事物抽象到足够高的程度,以至于您只需做一些非常简单的事情即可执行操作。
信息隐藏就是隐藏实现细节。 编程很难。 你可以有很多事情要处理和处理。 您可能想要/需要密切跟踪某些变量。 隐藏信息可确保没有人因使用您公开公开的变量或方法而意外破坏某些内容。
这两个概念在面向对象编程中紧密相连。
Abstraction is hiding details of implementation as you put it.
You abstract something to a high enough point that you'll only have to do something very simple to perform an action.
Information hiding is hiding implementation details. Programming is hard. You can have a lot of things to deal with and handle. There can be variables you want/need to keep very close track of. Hiding information ensures that no one accidentally breaks something by using a variable or method you exposed publicly.
These 2 concepts are very closely tied together in object-oriented programming.
抽象 - 识别对象本质特征的过程
不包括不相关且乏味的细节。
封装 - 将数据和操作该数据的函数封装到单个单元中的过程。
抽象和封装是相关但互补的概念。
抽象就是过程。 封装是实现抽象的机制。
抽象关注对象的可观察行为。 封装侧重于引起此行为的实现。
信息隐藏 - 这是隐藏对象实现细节的过程。 这是封装的结果。
Abstraction - It is the process of identifying the essential characteristics of an object
without including the irrelevant and tedious details.
Encapsulation - It is the process of enclosing data and functions manipulating this data into a single unit.
Abstraction and Encapsulation are related but complementary concepts.
Abstraction is the process. Encapsulation is the mechanism by which Abstraction is implemented.
Abstraction focuses on the observable behavior of an object. Encapsulation focuses upon the implementation that give rise to this behavior.
Information Hiding - It is the process of hiding the implementation details of an object. It is a result of Encapsulation.
只需添加有关 InformationHiding 的更多详细信息,发现此链接是带有示例的非常好的来源
Just adding on more details around InformationHiding, found This link is really good source with examples
抽象简单地说就是只让用户看到软件的基本细节以帮助用户使用或操作软件,从而不显示该软件的实现细节(变得不可见)的技术。
封装是一种将一个或多个项目封装起来的技术,因此一些信息(特别是程序细节)对用户可见,而另一些则对用户不可见,因此封装是通过信息隐藏来实现的。
总之。 抽象用于可观察的行为(外部),而封装用于不可见性(内部),但这两者确实是互补的。
Abstraction simply means the technique in which only essential details of software is made visible to the user to help the user to use or operate with software, thus implementation details of that software are not shown(are made invisible).
Encapsulation is the technique that have package that hold one or more items and hence some of information (particularly program details) became visible and some not visible to the user, so encapsulation is achieved through information hiding.
In summary. Abstraction is for observable behavior (externally) and encapsulation is for invisibility (internally) but these two are really complementary.
为了抽象某些东西,我们需要隐藏细节,或者隐藏我们需要抽象的东西的细节。
但是,这两者都可以通过封装来实现。
所以,信息隐藏是目标,抽象是过程,封装是技术。
To abstract something we need to hide the detail or to hide the detail of something we need to abstract it.
But, both of them can be achieved by encapsulation.
So, information hiding is a goal, abstraction is a process, and encapsulation is a technique.
值得注意的是,这些术语具有标准化的 IEEE 定义,可以在 https://pascal.computer.org/< /a>.
对象的抽象
信息隐藏
封装
It's worth noting these terms have standardized, IEEE definitions, which can be searched at https://pascal.computer.org/.
abstraction
information hiding
encapsulation