The progress of abstraction (抽象过程)

来源:互联网 发布:js 人民币大写在线 编辑:程序博客网 时间:2024/05/29 17:32

All programming languages provide abstractions .

所有编程语言都提供抽象机制。

It can be argued that the complexity of the problems you're able to solve is directly related to the kind and quality of abstraction.

一种较有争议的说法是,人们所能够解决的问题的复杂程度直接取决于抽象机制的类型和质量。

By "kind" I mean, "What is it that you are abstracting?"

所谓的“类型”,是指“你对什么进行抽象?”

Assembly language is a small abstraction of the underlying machine.

汇编语言是对底层机器的轻微抽象。

Many so-called "imperative" language that followed (such as FORTRAN, BASIC, and C) were abstractions of assembly language.

接着出现的许多所谓“命令式”语言(如FORTRAN, BASIC, C等)都是对汇编语言的抽象。

These language are big improvements over assembly language, but their primary abstraction still requires you to think in terms of the structure of the computer rather than the structure of th problem you are trying to solve.

这些语言在汇编语言基础上有了大幅的改进,但是它们所作的主要抽象仍要求在解决问题时要基于计算机的结构,而不是基于所要解决的问题的结构来考虑。

The programmer must establish the association between the machine model (in the "solution space," which is the place where you're implementing that solution, such as a computer) and the model of the problem that is actually being solved (in the “problem space,” which is the problem exists, such as a business).

在机器模型(位于“方案空间”)与实际解决问题的模型(位于“问题空间“)之间,程序员必须建立起一种联系。

The effort required  to perform this mapping, and the fact that it is extrinsic to the programming language, produces programs that are difficult to write and expensive to maintain and as a side effect created the entire "programming methods" industry.

这个过程要求人们付出较大的精力,由于它脱离了编程语言本身的范围,使得代码难以编写,并且维护成本高,同时作为副作用,它创造了完全的编程方法产业。


The alternative to modeling the machine is to model the problem you're trying to solve.

为机器建模的另一个方法是为你正要解决的问题建模。

Early language such as LISP and APL chose particular views of the world("All problems are ultimately lists" or "All problem are algorithmic," respectively).

对于一些早期语言来说,像LISP和APL,它们选择世界特定的角度--所有问题都归纳为清单或所有问题都归纳为算法。

Prolog casts all problems into chains of decisions.

Prolog则是将所有问题都归纳为决策链。

Languages have been created for constraint-based programming and for programming exclusively by manipulating graphical symbols.

我们认为这些语言的创建,一方面基于强制编程,一方面专为处理图形符号而设计。

(The latter proved to be too restrictive.)

(后来这个被证实国语限制。)

Each of these approaches may be a good solution to the particular class of problem they're designed to solve, but when you step outside of that domain they become awkward.

以上每一种方法都可以成为它们要解决的问题的很好的方法,但是当你跳出这些特定区域的时候,就会发现它们变得笨拙。


The object-oriented approach goes a step further by providing tools for the programmer to represent elements in the problem space.

面向对象的程序设计在此基础上则跨出了一大步,程序员可以通过它提供的工具来展现问题空间的元素。

This representation is general enough that the programmer is not constrained to any particular type of problem.
这种展现足够全面,因此程序员不用局限于任何特定类型的问题。

We refer to the elements in the problem space and their representations in the solution space as "objects."(You will also need other objects that don't have problem-space analogs.)

我们将问题空间的元素 以及他们在方案空间表示成为“对象”。(同时你也会需要一些其他的对象,这些对象在问题空间没有相似物。)

The idea is that the program is allowed to adapt itself to the lingo of the problem by adding new types of objects, so when you read the code describing the solution, you're reading words that also express the problem.

程序允许添加新的对象类型来灵活调整,所以当你阅读描述解决方案的代码时,同时也是在阅读问题的表述。

This is more flexible and powerful language abstraction than what we've had before.

与我们以前见过的相比,这无疑是一种更加灵活更加强大的语言抽象机制。

Thus, OOP allows you to describe the problem in terms of the problem, rather than in terms of the computer where the solution will run.

因此,面向对象编程允许你根据问题来描述问题,而不是根据你方案运行的电脑。

There's still a connection back to the computer:

仍然有一个途径回到计算机:

Each object looks quite a bit like a little computer -- it has a state, and it has operations that you can ask it to perform.

每个对象看起来都十分像一个小型计算机 -- 它们有状态,而且可要求它们进行特定的操作。

However, this doesn't seem like such a bad analogy to objects in the real world -- they all have characteristics and behaviors.

然而,与真实世界的对象相比它们也是不错的类似物,它们都有自己的特征和行为。

Alan Kay summarized five basic characteristics of Smalltalk, the first successful object-oriented language and one of the language upon which Java is based.

Alan Kay 总结了Smalltalk的五个基本特征,这是第一种成功的面向对象的编程语言,也是java的基础语言之一。

These characteristics represent a pure approach to object-oriented programming:这些特点展示了纯粹的面向对象编程:

1. Everything is an object.万物皆对象。

Think of an object as a fancy variable; 将对象想象成一个特殊的变量;

it stores data, but you can "make requests" to that object, asking it to perform operations on itself. 它存储数据,并且你可以通过给这个对象发送请求,让它对自身进行操作。

In theory, you can take any conceptual component in the problem you're trying to solve(dogs, buildings, services, etc.) and represent it as an object in your program. 理论上,你可以提取出任何你正在解决的问题的组件,并且在你的程序中以对象的形式展现。

2. A program is a bunch of objects telling each other what to do by spending messages. 程序就是一大堆对象,通过发送消息相互告知要做什么。

To make a request of an object, you "send a message" to that object. 你可以给一个对象发送消息来给这个对象发送请求。

More concretely, you can think of a message as a request to call a method that belongs to a particular object.  更具体地讲,你可以将消息看做调用特定对象的方法的请求。

3. Each object has its own memory made up of other objects. 每一个对象都有自己的存储空间存放其他对象。

Put another way, you create a new kind of object by making a package containing existing objects. 或者说,通过封装现有对象,可以制作出新型对象。

Thus, you can build complexity into a program while hiding it behind the simplicity of objects.因此,你可以在程序中将复杂隐藏在简单的对象之后。

4. Every object has a type. 每一个对象都有类型。

Using the parlance, each object is an instance of a class, in which "class" is synonymous with "type."  根据语法,每一个对象都是一个类(class)的实例,其中,“类(class)”是类型(type)的同义词。

The most important distinguishing characteristic of a class is "What messages can you send to it?" 一个类的最重要特征就是能将什么消息发送给它。

5. All objects of a particular type can receive the same messages. 同一个类的所有实例都可以接收相同的消息。

This is actually a loaded statement, as you will see later. 这实际上是一个加载生命,正如你将看到的。

Because an object of type "circle" is also an object of type "shape," a circle is guaranteed to accept shape messages. 因为类型为圆的一个对象也属于类型为形状的一个对象,所以一个圆完全能接受形状消息。

This means you can write code that talks to shapes and automatically handle anything that fits the description of a shape. 这意味着你可以写出代码统一指挥形状,令其自动控制所有符合形状描述的对象,其中自然包括圆。

This substitutability is one of the powerful concepts in OOP. 这一特性称为对象的“可替换性”, 是OOP最重要的概念之一。



0 0
原创粉丝点击