大道至简

  C++博客 :: 首页 :: 联系 :: 聚合  :: 管理
  0 Posts :: 7 Stories :: 0 Comments :: 0 Trackbacks

常用链接

留言簿

我参与的团队

搜索

  •  

最新评论

 

1: Introduction to Objects

The genesis of the computer revolution was in a machine. The genesis of our programming languages thus tends to look like that machine.

计算机革命源于这机器(电脑),因此程序设计语言看起来趋向于也来自这台机器。

(事物不是留于形势的,正所谓形者实之宾也。很多时候是因为环境把某些东西造成了某些样子,而它的实质则与它的外在是无关的,就像编程思想,它远远不应该像是被禁锢在计算机中那样,它本应该更伟大。还是Bruce说的,这句话我觉得充满禅机,这使得Bruce看起来要远远比一个程序员伟大的多。这句话是 The genesis of the computer revolution was in a machine. The genesis of our programming languages thus tends to look like that machine. 程序不应该只是我们在计算机中写下的可编译的代码,它应该是我们的 bicycles for the mind。)

But computers are not so much machines as they are mind amplification tools (“bicycles for the mind,” as Steve Jobs is fond of saying) and a different kind of expressive medium. As a result, the tools are beginning to look less like machines and more like parts of our minds, and also like other expressive mediums such as writing, painting, sculpture, animation, and filmmaking. Object-oriented programming is part of this movement toward using the computer as an expressive medium.

但是计算机并不像其他在人脑中描述的工具(...)一样,是一种不同的表现媒体。最后,这个工具越来越不像机器,开始变得更像我们脑中思想的一部分,更像是一种像写作,绘画,雕刻,动画,电影制作一样的表达媒介。而面向对象编程也正是这种趋势(电脑作为表达媒介)的一部分。

This chapter will introduce you to the basic concepts of object-oriented programming (OOP), including an overview of OOP development methods. This chapter, and this book, assume that you have had experience in a procedural programming language, although not necessarily C. If you think you need more preparation in programming and the syntax of C before tackling this book, you should work through the “Thinking in C: Foundations for C++ and Java” training CD ROM, bound in with this book and also available at www.BruceEckel.com
 

This chapter is background and supplementary material. Many people do not feel comfortable wading into (精神饱满得投入)object-oriented programming without understanding the big picture first. Thus, there are many concepts that are introduced here to give you a solid overview of OOP. However, many other people don’t get the big picture concepts until they’ve seen some of the mechanics first; these people may become bogged down and lost without some code to get their hands on. If you’re part of this latter group and are eager to get to the specifics of the language, feel free to jump past this chapter – skipping it at this point will not prevent you from writing programs or learning the language. However, you will want to come back here eventually to fill in your knowledge so you can understand why objects are important and how to design with them.

 

The progress of abstraction

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” languages that followed (such as Fortran, BASIC, and C) were abstractions of assembly language. These languages 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 the 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 modeling that problem, such as a computer) and the model of the problem that is actually being solved (in the “problem space,” which is the place where the problem exists). 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 languages such as LISP and APL chose particular views of the world (“All problems are ultimately lists” or “All problems are algorithmic”). PROLOG casts all problems into chains of decisions. 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 is 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 farther 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.” (Of course, 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 a 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, though. 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.


程序员通过面向对象接触到问题空间的最底层,在程序设计语言上迈出了一大步。因为它并不拘泥于任何特定类型的问题,所以这种表示法在一般情况是下足够的。我们将提到问题空间的元素(本元)表示为解空间的“对象”。(当然,你也需要一些其他的对象,这些对象不需要类似的问题空间)重要的是,程序允许通过添加新类别的对象去自适应问题的术语(暗语),所以,当你阅读代码,你读的就像用普通语言表达的问题(按照问题的方式)。相比之前的语言,这是一种灵活和强大的语言。因此,OOP允许你描述问题按照问题的方式,优于按照电脑运行解决方案的方式来描述问题。但,OOP仍然和计算机有着联系,每个对象看起来很想一个小型的电脑;对象都有一个状态,它有自己能执行的操作。现实世界中的对象用这样一个类比来表示并不坏,无论如何,他们都有自己的特性和行为。

Some language designers have decided that object-oriented programming by itself is not adequate to easily solve all programming problems, and advocate the combination of various approaches into multiparadigm programming languages.
[4]

posted on 2010-07-21 23:30 Tony_ice 阅读(377) 评论(0)  编辑 收藏 引用

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理