封装与抽象? [英] Encapsulation vs Abstraction?

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

问题描述

以下是封装和抽象的简要定义。

Here are the brief definitions of encapsulation and abstraction.

抽象:


Java中的抽象过程用于隐藏某些细节,
仅显示对象的基本特征。换句话说,
它处理对象(接口)的外部视图。我在不同网站上看到的唯一好例子是
界面。

The process of abstraction in Java is used to hide certain details and only show the essential features of the object. In other words, it deals with the outside view of an object (interface). The only good example i see for this across different sites is interface.

封装:


它基本上是在私有,公共,受保护等
修饰符的帮助下隐藏对象的状态。我们通过
public公开状态方法仅在需要时。

Its basically about hiding the state of object with the help of modifiers like private,public,protected etc. we expose the state thru public methods only if require.

我们使用修饰符如 private public 还隐藏了来自外部世界的不必要的细节,这只是一个抽象概念

What we achieve with modifiers like private, public also hides unnecessary details from out side world which is nothing but also a abstraction concept

所以,从上面的解释看起来像封装是抽象的一部分,或者我们可以说它是抽象的一个子集。但是,为什么封装术语是在我们只能用抽象处理的时候发明的呢?我确信应该有一些区别对待它们的主要区别,但网上的大多数材料对两者都说几乎相同。

So, from above explanation looks like encapsulation is a part of abstraction or we can say it's a subset of abstraction. But why then encapsulation term is invented when we could deal it with abstraction only? I am sure there should be some major difference which distinguishes them but most of material on net says almost same thing for both of them.

虽然这个问题已经提出来了论坛早些时候,但我再次发布它有特定的疑问。一些回复还说抽象是一个概念,封装是实现。但我不买这个 - 如果这是真的,那么我可以认为提供这两个不同的概念来混淆我们。

Though this question has been raised on this forum earlier too but I am posting it again with specific doubts. Some replies also says abstraction is a concept and encapsulation is implementation. But I don't buy this - If it is true, then I can think these two different concepts are provided to confuse us.

更新: - 5年后我得出了自己的答案,这是基于这篇文章及以下答案的要点

Update:- After 5 years i have come up with my own answer whichs is the gist based on answers in this post and below ones


  1. 抽象和封装之间的区别?

  2. 封装与抽象真实世界的例子

  1. difference between abstraction and encapsulation?
  2. encapsulation vs abstraction real world example


推荐答案

封装是一种用作抽象一部分的策略。封装是指对象的状态 - 对象封装其状态并将其隐藏在外部;类的外部用户通过其方法与之交互,但无法直接访问类状态。因此,类抽象远离与其状态相关的实现细节。

Encapsulation is a strategy used as part of abstraction. Encapsulation refers to the state of objects - objects encapsulate their state and hide it from the outside; outside users of the class interact with it through its methods, but cannot access the classes state directly. So the class abstracts away the implementation details related to its state.

抽象是一个更通用的术语,它也可以通过(其中)子类化来实现。例如,标准库中的接口 List 是项目序列的抽象,按其位置索引,列表的具体示例 ArrayList LinkedList 。与 List 交互的代码对其正在使用的列表类型的详细信息进行抽象。

Abstraction is a more generic term, it can also be achieved by (amongst others) subclassing. For example, the interface List in the standard library is an abstraction for a sequence of items, indexed by their position, concrete examples of a List are an ArrayList or a LinkedList. Code that interacts with a List abstracts over the detail of which kind of a list it is using.

抽象是通过封装隐藏基础状态通常是不可能的 - 如果一个类暴露其内部状态,它不能改变其内部工作,因此不能被抽象。

Abstraction is often not possible without hiding underlying state by encapsulation - if a class exposes its internal state, it can't change its inner workings, and thus cannot be abstracted.

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

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