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


当前回答

抽象:抽象的意思是显示功能的哪一部分。

封装:封装意味着隐藏功能的How部分。

让我们举一个非常简单的例子

/// <summary>
/// We have an Employee class having two properties EmployeeName and EmployeeCode
/// </summary>
public class Employee
{
    public string EmplpyeeName { get; set; }
    public string EmployeeCode { get; set; }

    // Add new employee to DB is the main functionality, so are making it public so that we can expose it to external environment
    // This is ABSTRACTION
    public void AddEmployee(Employee obj)
    {
        // "Creation of DB connection" and "To check if employee exists" are internal details which we have hide from external environment
        // You can see that these methods are private, external environment just need "What" part only
        CreateDBConnection();
        CheckIfEmployeeExists();
    }


    // ENCAPLUSATION using private keyword
    private bool CheckIfEmployeeExists()
    {
        // Here we can validate if the employee already exists
        return true;
    }

    // ENCAPLUSATION using private keyword
    private void CreateDBConnection()
    {
        // Create DB connection code
    }
}

控制台应用程序程序类

class Program
{
    static void Main(string[] args)
    {
        Employee obj = new Employee();
        obj.EmplpyeeName = "001";
        obj.EmployeeCode = "Raj";

        // We have exposed only what part of the functionality
        obj.AddEmployee(obj);
    }
}

其他回答

另一个例子:

假设我创建了一个不可变的Rectangle类,如下所示:

class Rectangle {
 public:
  Rectangle(int width, int height) : width_(width), height_(height) {}
  int width() const { return width_; }
  int height() const { return height_; }

 private:
  int width_;
  int height_;
}

现在很明显,我已经封装了宽度和高度(访问受到某种限制),但我没有抽象任何东西(好吧,也许我忽略了矩形在坐标空间中的位置,但这是示例的缺陷)。

好的抽象通常意味着好的封装。

一个好的抽象例子是通用数据库连接类。它的公共接口与数据库无关,非常简单,但允许我对连接做我想做的事情。你看到了吗?这里还有封装,因为类内部必须有所有低级句柄和调用。

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

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

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

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

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

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

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

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

简而言之:

抽象是一种帮助我们识别哪些特定信息是必要的,哪些信息应该隐藏的技术。

因此,封装是一种以隐藏对象的细节和实现细节的方式封装信息的技术。

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

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

封装是-

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

封装实现了抽象。

抽象就是——

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

让我们看一个例子

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

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

步骤- 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:封装不涉及最终用户交互的额外和复杂的方法。

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