抽象和封装的区别? [英] Difference between abstraction and encapsulation?

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

问题描述

封装和抽象之间的确切区别是什么?

What is the precise difference between encapsulation and abstraction?

推荐答案

这里的大多数答案都集中在 OOP,但封装开始得更早:

Most answers here focus on OOP but encapsulation begins much earlier:

  • 每个函数都是一个封装;在伪代码中:

  • Every function is an encapsulation; in pseudocode:

point x = { 1, 4 }
point y = { 23, 42 }

numeric d = distance(x, y)

这里,distance 封装了平面内两点之间(欧几里得)距离的计算:它隐藏了实现细节.这就是封装,纯粹而简单.

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.

抽象的过程概括:采取具体的实施方式,使其适用于不同的、尽管有些相关的数据类型.抽象的经典例子是 C 的 qsort 函数对数据进行排序:

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:

关于 qsort 的事情是它不关心它排序的数据——事实上,它不知道它排序的数据.相反,它的输入类型是一个无类型指针(void*),这只是 C 语言表达我不关心数据类型"的方式(这也称为类型擦除).重要的一点是 qsort 的实现始终保持不变,无论数据类型如何.唯一必须改变的是比较函数,它因数据类型而异.qsort 因此期望用户提供所述比较函数作为函数参数.

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.

封装和抽象密切相关,以至于您可以指出它们是真正不可分割的.出于实际目的,这可能是正确的;话虽如此,这是一个没有多少抽象的封装:

Encapsulation and abstraction go hand in hand so much so that you could make the point that they are truly inseparable. For practical purposes, this is probably true; that said, here’s an encapsulation that’s not much of an abstraction:

class point {
    numeric x
    numeric y
}

我们封装了点的坐标,但除了逻辑分组之外,我们并没有将它们从物质上抽象出来.

We encapsulate the point’s coordinate, but we don’t materially abstract them away, beyond grouping them logically.

这是一个非封装的抽象示例:

And here’s an example of abstraction that’s not encapsulation:

T pi<T> = 3.1415926535

这是一个泛型变量 pi 具有给定值 (π),并且声明不关心变量的确切类型.诚然,我很难在实际代码中找到这样的东西:抽象几乎总是使用封装.然而,上述确实确实存在于 C++(14) 中,通过 变量模板(=变量的通用模板);使用稍微复杂的语法,例如:

This is a generic variable pi with a given value (π), and the declaration doesn’t care about the exact type of the variable. Admittedly, I’d be hard-pressed to find something like this in real code: abstraction virtually always uses encapsulation. However, the above does actually exist in C++(14), via variable templates (= generic templates for variables); with a slightly more complex syntax, e.g.:

template <typename T> constexpr T pi = T{3.1415926535};

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

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