封装和抽象之间的确切区别是什么?
当前回答
这里的大多数答案都关注于OOP,但封装开始得更早:
Every function is an encapsulation; in pseudocode: point x = { 1, 4 } point y = { 23, 42 } numeric d = distance(x, y) Here, distance encapsulates the calculation of the (Euclidean) distance between two points in a plane: it hides implementation details. This is encapsulation, pure and simple. Abstraction is the process of generalisation: taking a concrete implementation and making it applicable to different, albeit somewhat related, types of data. The classical example of abstraction is C’s qsort function to sort data: The thing about qsort is that it doesn't care about the data it sorts — in fact, it doesn’t know what data it sorts. Rather, its input type is a typeless pointer (void*) which is just C’s way of saying “I don't care about the type of data” (this is also called type erasure). The important point is that the implementation of qsort always stays the same, regardless of data type. The only thing that has to change is the compare function, which differs from data type to data type. qsort therefore expects the user to provide said compare function as a function argument.
封装和抽象是密切相关的,因此您可以认为它们确实是不可分割的。就实际而言,这可能是对的;也就是说,这里有一个不太抽象的封装:
class point {
numeric x
numeric y
}
我们封装了点的坐标,但是我们没有实质性地将它们抽象出来,只是在逻辑上对它们进行分组。
这里有一个抽象的例子,它不是封装:
T pi<T> = 3.1415926535
这是一个具有给定值(π)的泛型变量pi,声明并不关心变量的确切类型。诚然,我很难在真实的代码中找到这样的东西:抽象实际上总是使用封装。然而,上面的内容在c++(14)中确实存在,通过变量模板(=变量的通用模板);使用稍微复杂一点的语法,例如:
template <typename T> constexpr T pi = T{3.1415926535};
其他回答
这里的大多数答案都关注于OOP,但封装开始得更早:
Every function is an encapsulation; in pseudocode: point x = { 1, 4 } point y = { 23, 42 } numeric d = distance(x, y) Here, distance encapsulates the calculation of the (Euclidean) distance between two points in a plane: it hides implementation details. This is encapsulation, pure and simple. Abstraction is the process of generalisation: taking a concrete implementation and making it applicable to different, albeit somewhat related, types of data. The classical example of abstraction is C’s qsort function to sort data: The thing about qsort is that it doesn't care about the data it sorts — in fact, it doesn’t know what data it sorts. Rather, its input type is a typeless pointer (void*) which is just C’s way of saying “I don't care about the type of data” (this is also called type erasure). The important point is that the implementation of qsort always stays the same, regardless of data type. The only thing that has to change is the compare function, which differs from data type to data type. qsort therefore expects the user to provide said compare function as a function argument.
封装和抽象是密切相关的,因此您可以认为它们确实是不可分割的。就实际而言,这可能是对的;也就是说,这里有一个不太抽象的封装:
class point {
numeric x
numeric y
}
我们封装了点的坐标,但是我们没有实质性地将它们抽象出来,只是在逻辑上对它们进行分组。
这里有一个抽象的例子,它不是封装:
T pi<T> = 3.1415926535
这是一个具有给定值(π)的泛型变量pi,声明并不关心变量的确切类型。诚然,我很难在真实的代码中找到这样的东西:抽象实际上总是使用封装。然而,上面的内容在c++(14)中确实存在,通过变量模板(=变量的通用模板);使用稍微复杂一点的语法,例如:
template <typename T> constexpr T pi = T{3.1415926535};
让我们以堆栈为例。它可以使用数组或链表来实现。但它支持的操作是推送和弹出。
Now abstraction is exposing only the interfaces push and pop. The underlying representation is hidden (is it an array or a linked list?) and a well-defined interface is provided. Now how do you ensure that no accidental access is made to the abstracted data? That is where encapsulation comes in. For example, classes in C++ use the access specifiers which ensure that accidental access and modification is prevented. And also, by making the above-mentioned interfaces as public, it ensures that the only way to manipulate the stack is through the well-defined interface. In the process, it has coupled the data and the code that can manipulate it (let's not get the friend functions involved here). That is, the code and data are bonded together or tied or encapsulated.
这些有些模糊的概念并不是计算机科学和编程所独有的。我想提供一些额外的想法,可能有助于其他人理解这些重要的概念。
简短的回答
封装——隐藏和/或限制对系统某些部分的访问,同时暴露必要的接口。
抽象性——从具体的现实、具体的对象或实际实例中去除某些特征,从而降低复杂性。
主要的相似之处在于,这些技术旨在提高理解能力和实用性。
主要的区别在于抽象是一种更简单地表示事物的方法(通常是为了使表示更广泛地适用),而封装是一种改变其他事物与事物交互方式的方法。
长回答
封装
下面是一个封装的例子,希望能让大家更清楚:
这里我们有一个Arduino Uno,还有一个Arduino Uno。封装是封装的一个很好的代表。
封装旨在保护某些组件不受外部影响和知识的影响,并公开其他组件应该与之交互的组件。在编程术语中,这涉及到通过访问修饰符隐藏信息,访问修饰符改变了某些变量和/或属性可以读写的程度。
但除此之外,封装还旨在更有效地提供这些外部接口。对于我们的Arduino示例,这可能包括漂亮的按钮和屏幕,这使得用户与设备的交互更加简单。它们为用户提供了简单的方法来影响设备的行为,并获得有关其操作的有用信息,否则将非常困难。
在编程中,这涉及到将各种组件分组为可分离的结构,如函数、类或对象。它还包括提供与这些构造交互的方法,以及获取关于它们的有用信息的方法。
封装在许多其他方面帮助程序员,尤其是提高代码的可维护性和可测试性。
抽象
尽管这里有许多其他的答案将抽象定义为泛化,但我个人认为这个定义是错误的。我想说泛化实际上是一种特定类型的抽象,而不是反过来。换句话说,所有的概括都是抽象,但并非所有的抽象都一定是概括。
以下是我对抽象的看法:
你会说这个图像是一棵树吗?很有可能你会。但它真的是一棵树吗?当然不是!它是一堆像素,看起来像我们可能称之为树的东西。我们可以说它代表了一棵真实树的抽象。注意,树的一些可视化细节被省略了。此外,它不生长,不消耗水,也不产生氧气。怎么可能呢?它只是屏幕上的一堆颜色,由计算机内存中的字节表示。
这就是抽象的本质。这是一种简化事物的方法,这样它们就更容易理解。你脑子里的每一个想法都是对现实的抽象。你脑海中的树和这张图片一样都不是真正的树。
在编程中,我们可以通过创建一个Tree类来利用这一点,其中包含模拟生长、水消耗和氧气生产的方法。我们的创造将代表我们对实际树木的体验,并且只包含我们真正关心的特定模拟元素。我们使用抽象作为一种用字节和数学来表示我们经验的方式。
抽象类
编程中的抽象还允许我们考虑几个“具体”对象类型(实际存在的类型)之间的共性,并在唯一的实体中定义这些共性。例如,我们的Tree类可以继承一个抽象类Plant,它有几个属性和方法适用于我们所有类植物,但删除了那些特定于每种植物类型的属性和方法。这可以显著减少代码重复,并提高可维护性。
抽象类和普通类的实际区别在于,抽象类在概念上没有“真正的”实例。构造一个Plant对象没有意义,因为它不够具体。每一种“真正的”植物也是一种更具体的植物类型。
此外,如果我们希望程序更加实际,我们可能需要考虑Tree类本身可能过于抽象的事实。实际上,每个Tree都是一种更具体的Tree类型,因此我们可以为这些类型创建类,例如Birch、Maple等,它们继承自我们的Tree类(现在可能是抽象的)。
JVM
另一个抽象的好例子是Java虚拟机(JVM),它为Java代码的运行提供了一个虚拟或抽象的计算机。它从本质上拿走了系统中所有平台特定的组件,并提供了一个抽象的“计算机”接口,而不考虑任何特定的系统。
的区别
封装与抽象的不同之处在于,它与事物的“真实”或“准确”没有任何关系。它不会删除某些组件以使其更简单或更广泛地适用。相反,它可以隐藏某些组件来实现类似的目的。
我读得越多,就越困惑。所以,我的理解是:
封装:
我们通常从外面看到手表,它的组件被封装在它的身体里。我们对不同的操作有某种控制。这种隐藏细节和公开控制(例如设置时间)的方式就是封装。
抽象:
到目前为止,我们一直在谈论手表。但我们没有具体说明是哪种手表。可以是数字的,也可以是模拟的,可以是手用的,也可以是墙用的。有很多可能性。我们所知道的是,这是一块手表,它显示时间,这是我们唯一感兴趣的东西,时间。这种隐藏细节和公开通用特性或用例的方法就是抽象。
抽象:以一种简化的/不同的方式来呈现事物的想法,这种方式要么更容易理解和使用,要么更切合实际。
考虑一个发送电子邮件的类……它使用抽象来向你展示自己作为某种信使,所以你可以调用emailSender。发送(邮件,收件人)。它的实际功能——选择POP3 / SMTP、调用服务器、MIME转换等等,都被抽象了出来。你只看到你的信使。
封装:保护和隐藏对象私有的数据和方法的思想。它更多的是让某样东西独立且万无一失。
以我为例。我把我的心率从世界其他地方压缩起来。因为我不希望其他人改变这个变量,我也不需要其他人来设置它来让我运行。它对我来说至关重要,但你不需要知道它是什么,而且你可能根本不在乎。
环顾四周,您会发现几乎所有您接触到的东西都是抽象和封装的例子。例如,你的手机向你展示了一种抽象的功能,它能把你说的话传达给别人——掩盖了GSM、处理器架构、无线电频率以及其他无数你不理解或不关心的东西。它还封装了来自您的某些数据,如序列号、ID号、频率等。
这一切都使世界成为一个更美好的居住地