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


当前回答

我认为封装是实现抽象的一种方式。看看下面的链接。

抽象和封装

其他回答

抽象是广义的术语。即封装是抽象的子集。

Abstraction Encapsulation
It solves an issue at the design level. Encapsulation solves an issue at implementation level.
hides the unnecessary detail but shows the essential information. It hides the code and data into a single entity or unit so that the data can be protected from the outside world.
Focuses on the external lookout. Focuses on internal working.
Lets focus on what an object does instead of how it does it. Lets focus on how an object does something.
Example: Outer look of mobile, like it has a display screen and buttons. Example: Inner details of mobile, how button and display screen connect with each other using circuits.

示例:解决方案架构师是创建整个解决方案的高级抽象技术设计的人,然后将该设计移交给开发团队进行实现。 在这里,解决方案架构师充当抽象,而开发团队充当封装。


举例:用户数据的封装(组网)

图片由

Abstraction (or modularity) – Types enable programmers to think at a higher level than the bit or byte, not bothering with low-level implementation. For example, programmers can begin to think of a string as a set of character values instead of as a mere array of bytes. Higher still, types enable programmers to think about and express interfaces between two of any-sized subsystems. This enables more levels of localization so that the definitions required for interoperability of the subsystems remain consistent when those two subsystems communicate. Source

Java示例

让我们以堆栈为例。它可以使用数组或链表来实现。但它支持的操作是推送和弹出。

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.

抽象—隐藏实现—在设计—使用接口/抽象类

封装—隐藏数据—在开发时—使用访问修饰符(公共/私有)

许多答案和例子都具有误导性。

封装是将“数据”和“对该数据进行操作的函数”打包到单个组件中,并限制对某些对象组件的访问。 封装意味着对象的内部表示通常隐藏在对象定义之外的视图中。

抽象是一种表示基本特性而不包括实现细节的机制。

封装:——信息隐藏。 抽象:——实现隐藏。

示例(c++):

class foo{
    private:
        int a, b;
    public:
        foo(int x=0, int y=0): a(x), b(y) {}

        int add(){    
            return a+b;   
        } 
}  

foo类的任何对象的内部表示都隐藏在该类的外部。——>封装。 foo对象的任何可访问成员(data/function)都是受限的,只能由该对象访问。

foo foo_obj(3, 4);
int sum = foo_obj.add();

方法add的实现是隐藏的。——>抽象。

上面提供了很多很好的答案,但我将在这里介绍我的(Java)观点。

数据封装简单地说就是包装和控制类中逻辑分组数据的访问。它通常与另一个关键字-数据隐藏相关联。这是在Java中使用访问修饰符实现的。

一个简单的例子是定义一个私有变量,并使用getter和setter方法访问它,或者将一个方法设置为私有,因为它只在类中使用。用户不需要知道这些方法和变量。

注意:不要误解封装只是关于数据隐藏。当我们说封装时,重点应该是将相关数据和行为分组、打包或捆绑在一起。

另一方面,数据抽象是泛化的概念,这样底层的复杂逻辑就不会暴露给用户。在Java中,这是通过使用接口和抽象类实现的。

的例子,

Lets say we have an interface Animal and it has a function makeSound(). There are two concrete classes Dog and Cat that implement this interface. These concrete classes have separate implementations of makeSound() function. Now lets say we have a animal(We get this from some external module). All user knows is that the object that it is receiving is some Animal and it is the users responsibility to print the animal sound. One brute force way is to check the object received to identify it's type, then typecast it to that Animal type and then call makeSound() on it. But a neater way is to abstracts thing out. Use Animal as a polymorphic reference and call makeSound() on it. At runtime depending on what the real Object type is proper function will be invoked.

详情请点击这里。

复杂的逻辑是在电路板中,封装在一个触摸板中,并提供了一个漂亮的界面(按钮)来将其抽象给用户。

附注:以上链接是我的个人博客。