抽象在编程中意味着什么? [英] What does abstraction mean in programming?

查看:28
本文介绍了抽象在编程中意味着什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习 Python,但我不确定是否理解以下语句:函数(包括它的名称)可以捕获我们对问题的心理组块或抽象."

I'm learning python and I'm not sure of understanding the following statement : "The function (including its name) can capture our mental chunking, or abstraction, of the problem."

粗体部分是我不明白编程方面的含义.引用来自 http://www.openbookproject.net/thinkcs/python/english3e/functions.html

It's the part that is in bold that I don't understand the meaning in terms of programming. The quote comes from http://www.openbookproject.net/thinkcs/python/english3e/functions.html

如何像计算机科学家一样思考,第 3 版.

How to think like a computer scientist, 3 edition.

谢谢!

推荐答案

抽象是所有计算机科学的核心概念.如果没有抽象,我们仍然会使用机器代码进行编程,或者更糟的是,一开始就没有计算机.所以恕我直言,这是一个非常好的问题.

Abstraction is a core concept in all of computer science. Without abstraction, we would still be programming in machine code or worse not have computers in the first place. So IMHO that's a really good question.

什么是抽象

抽象某物意味着给事物命名,这样名称就能捕捉到一个函数或整个程序的核心功能.

Abstracting something means to give names to things, so that the name captures the core of what a function or a whole program does.

你参考的书中给出了一个例子,它说

One example is given in the book you reference, where it says

假设我们正在处理海龟,我们需要的一个常见操作是画正方形.画一个正方形"是一个抽象概念,或者一个心理块,一些较小的步骤.所以让我们写一个函数来捕获这个积木"的模式:

Suppose we’re working with turtles, and a common operation we need is to draw squares. "Draw a square" is an abstraction, or a mental chunk, of a number of smaller steps. So let’s write a function to capture the pattern of this "building block":

暂时忘记海龟,只想画一个正方形.如果我告诉你(在纸上)画一个正方形,你会立即知道该怎么做:

Forget about the turtles for a moment and just think of drawing a square. If I tell you to draw a square (on paper), you immediately know what to do:

  • 画一个正方形 => 画一个四边都等长的长方形.

您无需再问就可以做到这一点,因为您已经牢记平方是什么,而无需我一步一步地告诉您.在这里,square这个词是绘制一个所有边长相同的矩形"的抽象.

You can do this without further questions because you know by heart what a square is, without me telling you step by step. Here, the word square is the abstraction of "draw a rectangle with all sides of the same length".

抽象很深

等等,你怎么知道矩形是什么?好吧,这是以下内容的另一个抽象:

But wait, how do you know what a rectangle is? Well, that's another abstraction for the following:

  • 矩形 => 画两条彼此平行、长度相同的线,然后添加另外两条与另外两条线垂直的平行线,同样长度相同但长度可能不同比前两个.
  • rectangle => draw two lines parallel to each other, of the same length, and then add another two parallel lines perpendicular to the other two lines, again of the same length but possibly of different length than the first two.

当然它一直在继续——线平行垂直连接都是抽象知名概念.

Of course it goes on and on - lines, parallel, perpendicular, connecting are all abstractions of well-known concepts.

现在,想象一下每次要绘制矩形或正方形时,您都必须给出矩形的完整定义,或者解释直线、平行线、垂直线和连接线——这将花费太长时间这样做.

Now, imagine each time you want a rectangle or a square to be drawn you have to give the full definition of a rectangle, or explain lines, parallel lines, perpendicular lines and connecting lines -- it would take far too long to do so.

抽象的真正力量

这是抽象的第一个力量:它们使谈话和完成事情变得更加容易.

That's the first power of abstractions: they make talking and getting things done much easier.

抽象的第二个力量来自于可组合性的良好属性:一旦定义了抽象,就可以组合两个或多个抽象以形成一个新的、更大的抽象:说你厌倦了画正方形,但你真的想画一个房子.假设我们已经定义了三角形,那么我们可以定义:

The second power of abstractions comes from the nice property of composability: once you have defined abstractions, you can compose two or more abstractions to form a new, larger abstraction: say you are tired of drawing squares, but you really want to draw a house. Assume we have already defined the triangle, so then we can define:

  • house => 画一个正方形,上面有一个三角形
  • house => draw a square with a triangle on top of it

接下来,你想要一个村庄:

Next, you want a village:

  • 村庄 =>绘制多个房屋并排在一起
  • village => draw multiple houses next to each other

哦,等等,我们想要一座城市——而且我们有一个新概念街道:

Oh wait, we want a city -- and we have a new concept street:

  • city => 让许多村庄彼此靠近,用更多房屋填满空地,但为街道留出空间
  • street =>(街道的一些定义)
  • city => draw many villages close to each other, fill empty spaces with more houses, but leave room for streets
  • street => (some definition of street)

等等...

这一切如何应用于编程?

如果在计划程序的过程中(称为分析和设计的过程),您发现对要解决的问题有很好的抽象,您的程序会变得更短,因此更容易编写而且——也许更重要的是——更容易阅读.这样做的方法是尝试掌握定义您的问题的主要概念——如在绘制房子的(简化)示例中,这是正方形三角形,画一个村庄就是房子.

If in the course of planning your program (a process known as analysis and design), you find good abstractions to the problem you are trying to solve, your programs become shorter, hence easier to write and - maybe more importantly - easier to read. The way to do this is to try and grasp the major concepts that define your problems -- as in the (simplified) example of drawing a house, this was squares and triangles, to draw a village it was houses.

在编程中,我们将抽象定义为函数(以及其他一些构造,如类和模块,但现在让我们关注函数).函数本质上命名一组单个语句,因此函数本质上是一种抽象——有关详细信息,请参阅您书中的示例.

In programming, we define abstractions as functions (and some other constructs like classes and modules, but let's focus on functions for now). A function essentially names a set of single statements, so a function essentially is an abstraction -- see the examples in your book for details.

这一切的美好

在编程中,抽象可以决定生产力的成败.这就是为什么很多时候,常用的函数被收集到中,供其他人重用.这意味着您不必担心细节,您只需要了解如何使用现成的抽象.显然,这会让您的工作变得更轻松,这样您就可以更快地工作,从而提高工作效率:

In programming, abstractions can make or break productivity. That's why often times, commonly used functions are collected into libraries which can be reused by others. This means you don't have to worry about the details, you only need to understand how to use the ready-made abstractions. Obviously that should make things easier for you, so you can work faster and thus be more productive:

示例:

想象一下,有一个名为nicepic"的图形库,其中包含用于上述所有抽象的预定义函数:矩形、正方形、三角形、房屋、村庄.

Imagine there is a graphics library called "nicepic" that contains pre-defined functions for all abstractions discussed above: rectangles, squares, triangles, house, village.

假设您想基于上述抽象创建一个程序来绘制一幅漂亮的房子图片,您只需编写以下代码:

Say you want to create a program based on the above abstractions that paints a nice picture of a house, all you have to write is this:

import nicepic
draw_house()

所以这只是两行代码来获得更复杂的东西.这不是很好吗?

So that's just two lines of code to get something much more elaborate. Isn't that just wonderful?

希望这会有所帮助.

这篇关于抽象在编程中意味着什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆