C++ “数据抽象”究竟是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3634915/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 13:20:35  来源:igfitidea点击:

What does "data abstraction" exactly mean?

c++abstractiondatabase-abstraction

提问by Naruto

What does data abstraction refer to? Please provide real life examples alongwith.

数据抽象指的是什么?请提供现实生活中的例子。

回答by aaronasterling

Abstraction has two parts:

抽象分为两部分:

  • Hidedetails that don't matter from a certain point of view
  • Identify details that do matter from a certain point of view and consider items to be of the the sameclass if they possess those details.
  • 隐藏从某个角度来看无关紧要的细节
  • 确定从某个角度来看很重要的细节,如果项目拥有这些细节,则将它们视为同一类。

For example, if I am designing a program to deal with inventory, I would like to be able to find out how many items of a certain type the system has in stock. From the perspective of the interface system, I don't care if I am getting this information from a database, a csv file, a remote repository via a SOAP interface or punch cards. I just care that I can can say widget.get_items_in_stock()and know that it will return an integer.

例如,如果我正在设计一个处理库存的程序,我希望能够找出系统库存中有多少某种类型的物品。从接口系统的角度来看,我不在乎我是从数据库、csv 文件、通过 SOAP 接口或穿孔卡从远程存储库获取这些信息。我只是关心我可以说widget.get_items_in_stock()并知道它会返回一个整数。

If I later decide that I want to record that number in some other way, the person designing the interface doesn't need to know, care or worry about it as long as widgetstill has the get_items_in_stock()method. Like wise, the interface doesn't need to care if I subclass the widget class and add a get_square_root_of_items_in_stock()method. I can pass an instance of the new class to it just as well.

如果我后来决定要以其他方式记录该数字,则设计界面的人无需知道、关心或担心它,只要widget仍然拥有该get_items_in_stock()方法即可。同样明智的是,接口不需要关心我是否对小部件类进行子类化并添加get_square_root_of_items_in_stock()方法。我也可以将新类的实例传递给它。

So in this example, we've hiddenthe details of how the data is acquired and decided that anything with a get_items_in_stock()method is an instance of the sameclass (or a subclass thereof) for certain purposes.

所以在这个例子中,我们隐藏了如何获取数据的细节,并决定任何带有get_items_in_stock()方法的东西都是同一类(或其子类)的实例,用于某些目的。

回答by JohnMcG

Data abstraction is any device that allows you to treat data as humans encounter it rather than as it is stored on machine.

数据抽象是任何一种设备,它允许您将数据视为人类遇到的数据,而不是存储在机器上的数据。

At the lowest level, all primitive data types are abstractions -- as programmers, we don't usually have to deal with data at the bit level (which is how it is ultimately stored) but as integers, floating point numbers, characters, etc.

在最低级别,所有原始数据类型都是抽象的——作为程序员,我们通常不必在位级别(这是它最终存储的方式)处理数据,而是将数据处理为整数、浮点数、字符等.

We then add layers onto that abstraction -- maybe two integers represents a Point, or we and enumerations to represent the months of the year, days of the week, etc.

然后我们在这个抽象上添加层——也许两个整数代表 a Point,或者我们和枚举来代表一年中的几个月、一周中的几天等。

With each abstraction layer, we move further from the machine and (hopefully) closer to human understanding of the data. This can extract a performance penalty -- it may not always be the case that points can be most efficiently represented by two integers. This is compensated for by the shorter development (and maintenance) time when abstractions are used.

通过每个抽象层,我们离机器更远,(希望)更接近人类对数据的理解。这可能会导致性能损失——点可以最有效地由两个整数表示的情况可能并不总是如此。这可以通过使用抽象时更短的开发(和维护)时间来弥补。

回答by Asif Alam

The technique of creating new data type that is well suited to an application to be programmed is known as data abstraction.

创建非常适合要编程的应用程序的新数据类型的技术称为数据抽象。

回答by Jossie Calderon

Abstraction is hiding the skeleton from the human body. The skin does a great way of containingit. (See how abstract I'm being there? Pun intended. I digress...)

抽象是隐藏人体的骨架。皮肤可以很好地控制它。(看到我在那里有多抽象了吗?双关语。我离题了......)

If I have a water bottle, then I'm able to drink from it by opening the lid, twisting it until it pops off.

如果我有一个水瓶,那么我就可以通过打开盖子、扭动它直到它弹出来喝水。

bool lid_open = false;
void open_water_bottle_by_twisting() { lid_open = true; }

But water bottles are containers. Containers hold liquids until they become open and they are able to be drunk from (assuming the liquid is drinkable).

但水瓶是容器。容器可以盛放液体,直到它们打开并且可以饮用(假设液体可以饮用)。

class Container 
{ 
    bool lid_open = false;

protected: 
    Container() {}
    void open_by_twisting()
    {
        lid_open = true;
    }
public:
    virtual ~Container();
};

class WaterBottle : public Container
{
    WaterBottle() : Container() {}
public:
    ~WaterBottle();
};

However, not all containers are opened the same way. Some containers, such as the water bottle, have lids that can be twisted off. Others don't have lids, such as exercise bottles - those contain bendy straws that can be bent down for storage or up for drinking.

但是,并非所有容器的打开方式都相同。一些容器(例如水瓶)的盖子可以拧开。其他的则没有盖子,例如运动瓶——它们包含可弯曲的吸管,可以向下弯曲以存放或向上饮用。

class Container 
{ 
    bool lid_open;
    bool straw_open;

protected:
    void TurnLid() { lid_open = true; }
    void BendStraw() { straw_open = true; }
    Container() : lid_open(false), straw_open(false){}

public:
    virtual void open() = 0;
    virtual ~Container();
};

class WaterBottle : public Container
{

public: 
    WaterBottle() : Container() {}
    void open()
    {
        TurnLid();
    }
    ~WaterBottle();
};

class ExerciseBottle : public Container
{
public:
    ExerciseBottle() : Container() {}
    void open()
    {
        BendStraw();
    }
    ~ExerciseBottle();
};

But the client doesn't know what ExerciseBottle's implementation of ExerciseBottle's open()is. It calls BendStraw(), which then sets straw_opento true. But ExerciseBottlesimply calls one function to do all of this work. The client doesn't have to perform several actions that are used in the implementation of open(). The case goes similarly for WaterBottle. And that's what abstraction is: letting the client know that the back-end will do all of the work for it. When the term "separating implementation from interface" is used, this is what is meant.

但是,客户端不知道是什么ExerciseBottle的实施ExerciseBottleopen()是。它调用BendStraw(),然后设置straw_open为 true。但ExerciseBottle只需调用一个函数即可完成所有这些工作。客户端不必执行在open(). 的情况类似WaterBottle这就是抽象让客户知道后端会为它做所有的工作。当使用术语“将实现与接口分离”时,就是这个意思。

回答by Nutan Kaushik

Abstraction means providing only essential information to the outside world and hiding their background details..examp. In ur Computer u can see only monitor, keyboard nd mouse..u don't know anything about internal wiring this is abstraction.

抽象意味着只向外界提供必要的信息并隐藏他们的背景细节......例如。在您的计算机中,您只能看到显示器、键盘和鼠标……您对内部布线一无所知,这是抽象的。

回答by user2296112

Data abstraction seems to be explained as breaking data down as far as you can get it. food would be the abstraction of apple, orange, pizza. animal would be the abstraction of cat, cow, pig. A food object would be something like this pseudo code:

数据抽象似乎被解释为尽可能地分解数据。食物将是苹果、橙子、披萨的抽象。动物将是猫、牛、猪的抽象。食物对象类似于以下伪代码:

class food{

 name;
 calories;
 weight;

 public eat(name);

 }

all foods have a name, calorie amount, and a weight. That's pretty abstract.

所有食物都有名称、热量和重量。这很抽象。

You could then make objects that inherit, which would be a bit less abstract.

然后你可以创建继承的对象,这会不那么抽象。

class pizza inherits food{

  toppings;

  say_toppings();

}

pizza now has toppings, but it inherits name, calories, and weight from food.

比萨现在有浇头,但它继承了食物的名称、卡路里和重量。

basically abstraction has been explained as getting to the lowest level of each item and making classes that extend from them. It makes your code more reusable too... If you've bade your base class of food well enough, and included everything abstract about it anyone working in the food industry could use your class.

基本上抽象已被解释为到达每个项目的最低级别并创建从它们扩展的类。它也使您的代码更具可重用性...如果您已经对食物的基类进行了充分的说明,并且包含了有关它的所有抽象内容,那么任何在食品行业工作的人都可以使用您的类。

回答by Shadab pirjad3

Simply Data Abstraction is nothing but the hiding unnecessary datails from user. Example:Person simply just wants to make a call, he just select or dial no. and click on call button this info. is enough for him.He dont want to know about how connection is made and whatever process behind making call or how voice is transferred.

简单的数据抽象只不过是对用户隐藏不必要的数据。示例:人只是想打电话,他只是选择或拨号码。并单击呼叫按钮此信息。对他来说就足够了。他不想知道连接是如何建立的,呼叫背后的过程或语音是如何传输的。

回答by Shrimant Telgave

Data Abstraction:It is used to provide the necessary information to the user and hide the unnecessary information from the user. It is called data abstraction. It will hide your business logic from outside the world.

数据抽象:用于向用户提供必要的信息,对用户隐藏不必要的信息。它被称为数据抽象。它将向外界隐藏您的业务逻辑。

Technical Example: Console.WriteLine();

技术示例: Console.WriteLine();

Non Technical Example: TV remote, Car Remote.

非技术示例:电视遥控器、汽车遥控器。

More Detail: Data Abstraction with real-time example

详细信息:带有实时示例的数据抽象

回答by Bacteria

I know this question was asked long time ago. But still like to share one real life example which might help others to understand concept of abstractionvery easily.

我知道这个问题很久以前就有人问过了。但还是想分享一个现实生活中的例子,它可能会帮助别人abstraction很容易地理解概念。

A real-world analogy of abstraction might work like this:You (the object) are arranging to meet a blind date and are deciding what to tell them so that they can recognize you in the restaurant. You decide to include the information about where you will be located, your height, hair color, and the color of your Hymanet. This is all data that will help the procedure (your date finding you) work smoothly. You should include all that information. On the other hand, there are a lot of bits of information about you that aren't relevant to this situation: your social security number, your favorite football players all are irrelevant to this particular situation because they won't help your date to find you.

抽象的现实世界类比可能是这样的:您(对象)正在安排相亲,并决定告诉他们什么,以便他们能够在餐厅认出您。您决定包含有关您所在位置、身高、头发颜色和夹克颜色的信息。这是所有有助于程序(您的约会对象)顺利进行的数据。您应该包括所有这些信息。另一方面,有很多关于你的信息与这种情况无关:你的社会安全号码、你最喜欢的足球运动员都与这种特殊情况无关,因为它们不会帮助你的约会对象找到你。

回答by UPENDO JOSEPHAT

Is the complex system that uses data details which are easy to interact or encounter with humans, which differ from the way computer system stores such as in binary number system. Answered by Neema, Rohan and Upendo (The programmers)

是使用易于与人类交互或遇到的数据细节的复杂系统,它不同于计算机系统存储的方式,例如二进制数系统。由 Neema、Rohan 和 Upendo 回答(程序员)