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


当前回答

class Aeroplane : IFlyable, IFuelable, IMachine
{ // Aeroplane's Design says:
  // Aeroplane is a flying object
  // Aeroplane can be fueled
  // Aeroplane is a Machine
}
// But the code related to Pilot, or Driver of Aeroplane is not bothered 
// about Machine or Fuel. Hence,
// pilot code:
IFlyable flyingObj = new Aeroplane();
flyingObj.Fly();
// fighter Pilot related code
IFlyable flyingObj2 = new FighterAeroplane();
flyingObj2.Fly();
// UFO related code 
IFlyable ufoObj = new UFO();
ufoObj.Fly();
// **All the 3 Above codes are genaralized using IFlyable,
// Interface Abstraction**
// Fly related code knows how to fly, irrespective of the type of 
// flying object they are.

// Similarly, Fuel related code:
// Fueling an Aeroplane
IFuelable fuelableObj = new Aeroplane();
fuelableObj.FillFuel();
// Fueling a Car
IFuelable fuelableObj2 = new Car(); // class Car : IFuelable { }
fuelableObj2.FillFuel();

// ** Fueling code does not need know what kind of vehicle it is, so far 
// as it can Fill Fuel**

其他回答

抽象和封装的区别。

抽象:以一种简化的/不同的方式来呈现事物的想法,这种方式要么更容易理解和使用,要么更切合实际。

考虑一个发送电子邮件的类……它使用抽象来向你展示自己作为某种信使,所以你可以调用emailSender。发送(邮件,收件人)。它的实际功能——选择POP3 / SMTP、调用服务器、MIME转换等等,都被抽象了出来。你只看到你的信使。

封装:保护和隐藏对象私有的数据和方法的思想。它更多的是让某样东西独立且万无一失。

以我为例。我把我的心率从世界其他地方压缩起来。因为我不希望其他人改变这个变量,我也不需要其他人来设置它来让我运行。它对我来说至关重要,但你不需要知道它是什么,而且你可能根本不在乎。

环顾四周,您会发现几乎所有您接触到的东西都是抽象和封装的例子。例如,你的手机向你展示了一种抽象的功能,它能把你说的话传达给别人——掩盖了GSM、处理器架构、无线电频率以及其他无数你不理解或不关心的东西。它还封装了来自您的某些数据,如序列号、ID号、频率等。

这一切都使世界成为一个更美好的居住地

封装是抽象的一个例子。封装的全部意义在于抽象函数内部发生的事情,将所有的复杂性简化为一个符号(函数的引用或名称),将函数变成一个黑盒。

在编程中,“抽象”一词是一个命令。当一个类继承了一个抽象类(或接口)时,您将被命令创建一个抽象。

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

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

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