封装和抽象之间的确切区别是什么?
当前回答
两者之间存在差异
ABSTRAACTION
and
封装
1. First difference between Abstraction and Encapsulation is that, Abstraction is implemented in Java using interface and abstract class while Encapsulation is implemented using private, package-private and protected access modifier.
2. Data abstraction simply means generalizing something to hide the complex logic that goes underneath where Encapsulation is DATA HIDING.
3. Encapsulation is combining related logic data (variables and methods) where as Abstraction is hiding internal implementation details and expose only relevant details to the user. In a way you can Abstraction is achieved by Encapsulation.
其他回答
封装是抽象的一个例子。封装的全部意义在于抽象函数内部发生的事情,将所有的复杂性简化为一个符号(函数的引用或名称),将函数变成一个黑盒。
在编程中,“抽象”一词是一个命令。当一个类继承了一个抽象类(或接口)时,您将被命令创建一个抽象。
封装意味着隐藏数据,比如使用getter和setter等。
抽象意味着-隐藏实现使用抽象类和接口等。
另一个例子:
假设我创建了一个不可变的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_;
}
现在很明显,我已经封装了宽度和高度(访问受到某种限制),但我没有抽象任何东西(好吧,也许我忽略了矩形在坐标空间中的位置,但这是示例的缺陷)。
好的抽象通常意味着好的封装。
一个好的抽象例子是通用数据库连接类。它的公共接口与数据库无关,非常简单,但允许我对连接做我想做的事情。你看到了吗?这里还有封装,因为类内部必须有所有低级句柄和调用。
我将尝试以一种简单的方式演示封装。让我们看看. .
将数据和函数包装成一个单元(称为 类)被称为封装。封装、包含和隐藏 关于对象的信息,如内部数据结构和 代码。
封装是-
隐藏的复杂性, 将数据和函数绑定在一起, 使复杂方法私有, 使实例变量为私有, 对最终用户隐藏不必要的数据和函数。
封装实现了抽象。
抽象就是——
显示什么是必要的, 数据需要从最终用户,
让我们看一个例子
下图显示了“将客户详细信息添加到数据库”的图形用户界面。
通过查看图像,我们可以说我们需要一个客户类。
步骤- 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:封装不涉及最终用户交互的额外和复杂的方法。
附注:上面的代码是硬而快速的。
封装是将复杂性包裹在一个胶囊中,即类&因此封装… 抽象是一个对象区别于其他对象的特征。
抽象可以通过使具有一个或多个抽象方法的类抽象来实现。它只是一个特性,应该由扩展它的类来实现。 例如,当你发明/设计一辆汽车时,你定义了一个特征,比如汽车应该有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…..
两者都很强大;但是使用抽象需要比封装更多的技能,没有抽象,更大的应用程序/产品就无法生存。