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


当前回答

从这个

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.

其他回答

封装隐藏了实现细节,这些细节可能是通用的,也可能不是专门的行为。

抽象提供了一种泛化(例如,在一组行为之上)。

这里有一个很好的阅读:抽象、封装和信息隐藏,作者是Object Agency的Edward V. Berard。

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

我将尝试以一种简单的方式演示封装。让我们看看. .

将数据和函数包装成一个单元(称为 类)被称为封装。封装、包含和隐藏 关于对象的信息,如内部数据结构和 代码。

封装是-

隐藏的复杂性, 将数据和函数绑定在一起, 使复杂方法私有, 使实例变量为私有, 对最终用户隐藏不必要的数据和函数。

封装实现了抽象。

抽象就是——

显示什么是必要的, 数据需要从最终用户,

让我们看一个例子

下图显示了“将客户详细信息添加到数据库”的图形用户界面。

通过查看图像,我们可以说我们需要一个客户类。

步骤- 1:我的客户类需要什么?

i.e.

2个变量来存储客户代码和客户名称。 1添加客户代码和客户名称到数据库功能。 名称空间CustomerContent { 公共类客户 { CustomerCode = ""; CustomerName = ""; 公共无效ADD() { //我的DB代码会在这里 }

现在只有ADD方法不会在这里单独工作。

步骤-2:验证将如何工作,ADD函数的行为?

我们将需要数据库连接代码和验证代码(额外的方法)。

public bool Validate()
{
    //Granular Customer Code and Name
    return true;
}

public bool CreateDBObject()
{
    //DB Connection Code
    return true;
}


class Program
{
static void main(String[] args)
{
CustomerComponent.Customer obj = new CustomerComponent.Customer;

obj.CustomerCode = "s001";
obj.CustomerName = "Mac";

obj.Validate();
obj.CreateDBObject();

obj.ADD();
}
}

现在不需要显示额外的方法(Validate();CreateDBObject()[复杂和额外的方法])给最终用户。最终用户只需要看到和知道客户代码,客户名称和ADD按钮,这将添加记录。最终用户不关心如何将数据添加到数据库?

步骤-3:私有不涉及最终用户交互的额外和复杂的方法。

所以使那些复杂的和额外的方法为私有而不是公共(i。e隐藏这些方法),并删除obj.Validate();obj.CreateDBObject ();从主类程序中实现封装。

换句话说,将接口简化为终端用户就是封装。

现在代码如下所示

namespace CustomerContent
{
public class Customer
{
public string CustomerCode = "";
public string CustomerName = "";
public void ADD()
{
   //my DB code will go here
}

private bool Validate()
{
    //Granular Customer Code and Name
    return true;
}

private bool CreateDBObject()
{
    //DB Connection Code
    return true;
}


class Program
{
static void main(String[] args)
{
CustomerComponent.Customer obj = new CustomerComponent.Customer;

obj.CustomerCode = "s001";

obj.CustomerName = "Mac";

obj.ADD();
}
}

简介:

步骤-1:我的客户类需要什么?是抽象的。

步骤-3:封装不涉及最终用户交互的额外和复杂的方法。

附注:上面的代码是硬而快速的。

抽象让您关注对象做了什么,而不是它是如何做的 封装意味着隐藏对象如何做某事的内部细节或机制。

就像你开车时,你知道油门踏板的作用,但你可能不知道它背后的过程,因为它是封装的。

让我用c#举个例子。假设你有一个整数:

int Number = 5;
string aStrNumber = Number.ToString();

你可以使用像number . tostring()这样的方法,它返回数字5的字符表示,并将其存储在字符串对象中。该方法告诉您它做了什么,而不是如何做。

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

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

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