封装和抽象之间的确切区别是什么?
当前回答
从这个
OOPS中封装和抽象的区别
抽象和封装是两个重要的面向对象编程(oop)概念。封装和抽象都是相互关联的术语。
封装和抽象的现实区别
封装意味着隐藏。封装也称为数据隐藏。你可以把胶囊想象成胶囊(药片),里面藏着药。封装是包装,只是隐藏属性和方法。封装用于将代码和数据隐藏在单个单元中,以保护数据不受外界的影响。类是封装的最佳示例。
抽象指的是只向预期的用户显示必要的细节。顾名思义,抽象是“任何事物的抽象形式”。我们在编程语言中使用抽象来创建抽象类。抽象类表示类的方法和属性的抽象视图。
封装和抽象之间的实现差异
Abstraction is implemented using interface and abstract class while Encapsulation is implemented using private and protected access modifier. OOPS makes use of encapsulation to enforce the integrity of a type (i.e. to make sure data is used in an appropriate manner) by preventing programmers from accessing data in a non-intended manner. Through encapsulation, only a predetermined group of functions can access the data. The collective term for datatypes and operations (methods) bundled together with access restrictions (public/private, etc.) is a class.
其他回答
上面提供了很多很好的答案,但我将在这里介绍我的(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.
详情请点击这里。
复杂的逻辑是在电路板中,封装在一个触摸板中,并提供了一个漂亮的界面(按钮)来将其抽象给用户。
附注:以上链接是我的个人博客。
抽象是对用户隐藏无用的数据 封装是将数据绑定到一个胶囊(类)中。 我认为封装是我们实现抽象的方式。
抽象是广义的术语。即封装是抽象的子集。
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示例
从这个
OOPS中封装和抽象的区别
抽象和封装是两个重要的面向对象编程(oop)概念。封装和抽象都是相互关联的术语。
封装和抽象的现实区别
封装意味着隐藏。封装也称为数据隐藏。你可以把胶囊想象成胶囊(药片),里面藏着药。封装是包装,只是隐藏属性和方法。封装用于将代码和数据隐藏在单个单元中,以保护数据不受外界的影响。类是封装的最佳示例。
抽象指的是只向预期的用户显示必要的细节。顾名思义,抽象是“任何事物的抽象形式”。我们在编程语言中使用抽象来创建抽象类。抽象类表示类的方法和属性的抽象视图。
封装和抽象之间的实现差异
Abstraction is implemented using interface and abstract class while Encapsulation is implemented using private and protected access modifier. OOPS makes use of encapsulation to enforce the integrity of a type (i.e. to make sure data is used in an appropriate manner) by preventing programmers from accessing data in a non-intended manner. Through encapsulation, only a predetermined group of functions can access the data. The collective term for datatypes and operations (methods) bundled together with access restrictions (public/private, etc.) is a class.
许多答案和例子都具有误导性。
封装是将“数据”和“对该数据进行操作的函数”打包到单个组件中,并限制对某些对象组件的访问。 封装意味着对象的内部表示通常隐藏在对象定义之外的视图中。
抽象是一种表示基本特性而不包括实现细节的机制。
封装:——信息隐藏。 抽象:——实现隐藏。
示例(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的实现是隐藏的。——>抽象。