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


当前回答

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

抽象=数据隐藏+封装

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

其他回答

抽象:只显示必要的信息。让我们关注一下在计算机上进行开关的例子。当系统仍在加载时,用户不必知道发生了什么(该信息对用户隐藏)。

让我们再举一个例子,ATM机。客户不需要知道机器如何读取密码并处理交易,他所需要做的就是输入密码,拿现金然后离开。

封装:处理隐藏类的敏感数据,从而私有化类的一部分。这是一种通过不允许外部访问来保持某些信息对其客户端的私密性的方法。

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**

封装是将复杂性包裹在一个胶囊中,即类&因此封装… 抽象是一个对象区别于其他对象的特征。

抽象可以通过使具有一个或多个抽象方法的类抽象来实现。它只是一个特性,应该由扩展它的类来实现。 例如,当你发明/设计一辆汽车时,你定义了一个特征,比如汽车应该有4个门、刹车、方向盘等,所以任何使用这种设计的人都应该包括这些特征。实现并不是抽象的首要部分。它只会定义应该包含的特征。

Encapsulation is achieved keeping data and the behaviour in one capsule that is class & by making use of access modifiers like public, private, protected along with inheritance, aggregation or composition. So you only show only required things, that too, only to the extent you want to show. i.e. public, protected, friendly & private ka funda…… e.g. GM decides to use the abstracted design of car above. But they have various products having the same characteristics & doing almost same functionality. So they write a class which extends the above abstract class. It says how gear box should work, how break should work, how steering wheel should work. Then all the products just use this common functionality. They need not know how the gear box works or break works or steering wheal works. Indivisual product can surely have more features like a/c or auto lock etc…..

两者都很强大;但是使用抽象需要比封装更多的技能,没有抽象,更大的应用程序/产品就无法生存。

许多答案和例子都具有误导性。

封装是将“数据”和“对该数据进行操作的函数”打包到单个组件中,并限制对某些对象组件的访问。 封装意味着对象的内部表示通常隐藏在对象定义之外的视图中。

抽象是一种表示基本特性而不包括实现细节的机制。

封装:——信息隐藏。 抽象:——实现隐藏。

示例(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的实现是隐藏的。——>抽象。

抽象和封装过程都会生成接口。

通过封装生成的接口隐藏了实现细节。

与抽象之前相比,通过抽象生成的接口可以适用于更多的数据类型。