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


当前回答

抽象或封装并不严格要求信息隐藏。信息可能会被忽略,但不一定要隐藏。

封装是一种将事物视为单一事物的能力,即使它可能由许多复杂的部分或思想组成。例如,我可以说我坐在一把“椅子”上,而不是指这把椅子上的许多不同部分,每个部分都有特定的设计和功能,它们精确地组合在一起,目的是让我的屁股舒适地离地板几英尺远。

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.

其他回答

我认为封装是实现抽象的一种方式。看看下面的链接。

抽象和封装

封装要求模块化。它要求您创建具有数据和处理数据的方法的对象。在这种情况下,您可以将其视为一个模块。

抽象为您提供了类的一般视图。

让我用简单的代码示例来尝试一下

抽象=数据隐藏+封装

 // 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();
        }
    }

从这个

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.

一种防止特定对象的数据被外部函数故意或意外误用的机制叫做“数据封装”。

在不包括背景细节或解释的情况下表现基本特征的行为被称为抽象