封装和抽象之间的确切区别是什么?
当前回答
让我用简单的代码示例来尝试一下
抽象=数据隐藏+封装
// Abstraction
interface IOperation
{
int GetSumOfNumbers();
}
internal class OperationEven : IOperation
{
// data hiding
private IEnumerable<int> numbers;
public OperationEven(IEnumerable<int> numbers)
{
this.numbers = numbers;
}
// Encapsulation
public int GetSumOfNumbers()
{
return this.numbers.Where(i => i % 2 == 0).Sum();
}
}
其他回答
抽象或封装并不严格要求信息隐藏。信息可能会被忽略,但不一定要隐藏。
封装是一种将事物视为单一事物的能力,即使它可能由许多复杂的部分或思想组成。例如,我可以说我坐在一把“椅子”上,而不是指这把椅子上的许多不同部分,每个部分都有特定的设计和功能,它们精确地组合在一起,目的是让我的屁股舒适地离地板几英尺远。
Abstraction is enabled by encapsulation. Because we encapsulate objects, we can think about them as things which relate to each other in some way rather than getting bogged down in the subtle details of internal object structure. Abstraction is the ability to consider the bigger picture, removed from concern over little details. The root of the word is abstract as in the summary that appears at the top of a scholarly paper, not abstract as in a class which can only be instantiated as a derived subclass.
I can honestly say that when I plop my butt down in my chair, I never think about how the structure of that chair will catch and hold my weight. It's a decent enough chair that I don't have to worry about those details. So I can turn my attention toward my computer. And again, I don't think about the component parts of my computer. I'm just looking at a part of a webpage that represents a text area that I can type in, and I'm communicating in words, barely even thinking about how my fingers always find the right letters so quickly on the keyboard, and how the connection is ultimately made between tapping these keys and posting to this forum. This is the great power of abstraction. Because the lower levels of the system can be trusted to work with consistency and precision, we have attention to spare for greater work.
上面提供了很多很好的答案,但我将在这里介绍我的(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.
详情请点击这里。
复杂的逻辑是在电路板中,封装在一个触摸板中,并提供了一个漂亮的界面(按钮)来将其抽象给用户。
附注:以上链接是我的个人博客。
封装意味着隐藏数据,比如使用getter和setter等。
抽象意味着-隐藏实现使用抽象类和接口等。
抽象
抽象是提取所有现有和可预见实现的公共属性和字段的过程。
例如: 轿车是轿车、掀背车、SUV、双门跑车、敞篷车的抽象概念。 Car将具有所有类型的汽车所共有的所有属性和字段。
封装
封装是对用户隐藏不需要的细节的过程。这个术语来自于胶囊。就像药物隐藏在胶囊里一样。各种机器和设备和设备的细节,从搅拌机,自行车,洗衣机,收音机,电视到飞机。您不希望用户可以看到机器的所有细节。
在编程方面: 让我们考虑一个等级汽车。 在下面的例子中,所有用户需要知道的是转动键(turnKey()方法),他不知道内部函数。用户不需要了解任何内部功能或内部组件。
在这种情况下,所有的私有方法都是内部函数,像“活塞p1”这样的私有字段是用户不需要知道的内部数据。
public class Car{
private void startMotor(){ //do something }
private void generateVoltage(){ //do something }
private void sparkPlugIgnition(){ //do something }
private void fuelFlowFromTankToInjector(){ //do something }
private void pushPistonsDown() {
p1.doAction();
p2.doAction();
//do something }
private void moveCrankShaft(){ //do something }
private Piston p1;
private Piston p2;
public void turnKey(){
startMotor();
generateVoltage();
sparkPlugIgnition();
fuelFlowFromTankToInjector();
pushPistonsDown();
moveCrankShat();
...
}
}
抽象—隐藏实现—在设计—使用接口/抽象类
封装—隐藏数据—在开发时—使用访问修饰符(公共/私有)
推荐文章
- 什么是依赖倒置原则?为什么它很重要?
- 为什么在Python方法中需要显式地有“self”参数?
- c#中朋友的对等物是什么?
- 如何在方法中访问“静态”类变量?
- 为什么c#不提供c++风格的'friend'关键字?
- String, StringBuffer和StringBuilder
- 存储库和服务层的区别?
- DDD -实体不能直接访问存储库的规则
- 为什么STL如此严重地基于模板而不是继承?
- 如何在Objective-C中声明类级属性?
- 面向方面编程与面向对象编程
- c++中类似于java的instanceof
- 在python中遍历对象属性
- 将类代码分离为头文件和cpp文件
- 在PHP中使用getter和setter而不是函数或简单的公共字段有什么优点?