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

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

问题描述

这里是封装和抽象的简要定义.

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.

封装:

它基本上是在帮助下隐藏对象的状态像private、public、protected等修饰符,我们通过暴露状态仅在需要时才使用公共方法.

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.

我们使用 privatepublic 等修饰符实现的效果也隐藏了外部世界不必要的细节,这不过是一个抽象概念

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. 封装与抽象现实世界示例

推荐答案

封装 是一种用作抽象一部分的策略.封装是指对象的状态——对象封装自己的状态,对外隐藏;类的外部用户通过其方法与其交互,但不能直接访问类状态.所以这个类抽象掉了与其状态相关的实现细节.

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 是对一系列项的抽象,按它们的位置索引,List 的具体例子是ArrayListLinkedList.与 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天全站免登陆