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


当前回答

抽象和封装都是用来隐藏数据的。但是有很大的不同。

封装

封装是将数据和对数据进行操作的代码绑定或包装成一个称为Class的单元的过程。

封装在实现级别解决了问题。

在类中,可以使用私有或受保护的访问修饰符来隐藏数据。

抽象

抽象是隐藏无关细节的概念。换句话说,通过对用户隐藏不必要的细节,使复杂的系统变得简单。

抽象在设计层面解决了问题。

可以通过在Java中创建接口和抽象类来实现抽象。

在ruby中,你可以通过创建模块来实现抽象。

示例:我们使用(收集,映射,减少,排序…)方法的枚举模块与数组和ruby哈希。

其他回答

另一个例子:

假设我创建了一个不可变的Rectangle类,如下所示:

class Rectangle {
 public:
  Rectangle(int width, int height) : width_(width), height_(height) {}
  int width() const { return width_; }
  int height() const { return height_; }

 private:
  int width_;
  int height_;
}

现在很明显,我已经封装了宽度和高度(访问受到某种限制),但我没有抽象任何东西(好吧,也许我忽略了矩形在坐标空间中的位置,但这是示例的缺陷)。

好的抽象通常意味着好的封装。

一个好的抽象例子是通用数据库连接类。它的公共接口与数据库无关,非常简单,但允许我对连接做我想做的事情。你看到了吗?这里还有封装,因为类内部必须有所有低级句柄和调用。

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

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示例

抽象是我们将要执行的实现的契约。实现可能会在一段时间内发生变化。各种实现本身可能隐藏,也可能不隐藏,而是隐藏在抽象后面。

假设我们在一个接口中定义了一个类的所有api,然后要求代码的用户依赖于该接口中定义的api。我们可以自由地改进或修改实现,但必须遵守设定的合同。用户与我们的实现没有耦合。

我们在抽象中暴露所有必要的规则(方法),规则的实现留给实现者实体,实现也不是抽象的一部分。正是签名和声明使抽象成为现实。

封装只是通过减少对状态和行为的访问来隐藏内部细节。封装的类可能有也可能没有定义良好的抽象。

java.util.List是java.util.ArrayList的抽象。使用非公共访问修饰符标记的java.util.ArrayList的内部状态是封装。

Edit Suppose a class Container.nava implements IContainer , IContainer may declare methods like addElement, removeElements, contains, etc. Here IContainer represents the abstraction for its implementing class. Abstraction is declaring the APIs of the class or a module or a system to the outer world. These APIs become the contract. That system may be or may not be developed yet. The users of the system now can depend on the declared APIs and are sure any system implementing such a contract will always adhere to the APIs declared, they will always provide tge implementation for those APIs. Once we are writing some concrete entity then deciding to hide our internal states is encapsulation

两者之间存在差异

ABSTRAACTION

and

封装

1.    First difference between Abstraction and Encapsulation is that, Abstraction is implemented in Java using interface and abstract class while Encapsulation is implemented using private, package-private and protected access modifier.

2.    Data abstraction simply means generalizing something to hide the complex logic that goes underneath where Encapsulation is DATA HIDING.


3.    Encapsulation is combining related logic data (variables and methods) where as Abstraction is hiding internal implementation details and expose only relevant details to the user. In a way you can Abstraction is achieved by Encapsulation.

封装把一些东西放在一个盒子里,给你一个窥视孔;这使你从混乱与齿轮。

抽象完全忽略了无关紧要的细节,比如物体是否有齿轮、棘轮、飞轮或核心;他们只是“走了”

封装的例子:

内裤 工具箱 钱包 手提包 胶囊 冷冻carbonite 一个盒子,上面有或没有按钮 一个墨西哥卷饼(严格来说,是卷饼周围的玉米粉圆饼)

抽象的例子:

“一组事物”是一种抽象(我们称之为聚合) “包含其他事物的事物”是一种抽象(我们称之为组合) “容器”是另一种“物装物”的抽象;注意,所有的封装示例都是不同种类的容器,但并不是所有的容器都展示/提供封装。例如,篮子是一种不封装其内容的容器。