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


当前回答

通过使用单个通用示例进行抽象和封装

------------------------------------------------------------------------------------------------------------------------------------

我们都用计算器来计算复杂的问题!

其他回答

抽象是对用户隐藏无用的数据 封装是将数据绑定到一个胶囊(类)中。 我认为封装是我们实现抽象的方式。

这里的大多数答案都关注于OOP,但封装开始得更早:

Every function is an encapsulation; in pseudocode: point x = { 1, 4 } point y = { 23, 42 } numeric d = distance(x, y) Here, distance encapsulates the calculation of the (Euclidean) distance between two points in a plane: it hides implementation details. This is encapsulation, pure and simple. Abstraction is the process of generalisation: taking a concrete implementation and making it applicable to different, albeit somewhat related, types of data. The classical example of abstraction is C’s qsort function to sort data: The thing about qsort is that it doesn't care about the data it sorts — in fact, it doesn’t know what data it sorts. Rather, its input type is a typeless pointer (void*) which is just C’s way of saying “I don't care about the type of data” (this is also called type erasure). The important point is that the implementation of qsort always stays the same, regardless of data type. The only thing that has to change is the compare function, which differs from data type to data type. qsort therefore expects the user to provide said compare function as a function argument.

封装和抽象是密切相关的,因此您可以认为它们确实是不可分割的。就实际而言,这可能是对的;也就是说,这里有一个不太抽象的封装:

class point {
    numeric x
    numeric y
}

我们封装了点的坐标,但是我们没有实质性地将它们抽象出来,只是在逻辑上对它们进行分组。

这里有一个抽象的例子,它不是封装:

T pi<T> = 3.1415926535

这是一个具有给定值(π)的泛型变量pi,声明并不关心变量的确切类型。诚然,我很难在真实的代码中找到这样的东西:抽象实际上总是使用封装。然而,上面的内容在c++(14)中确实存在,通过变量模板(=变量的通用模板);使用稍微复杂一点的语法,例如:

template <typename T> constexpr T pi = T{3.1415926535};

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

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

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

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

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

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