[译]OOSE第5章:面向对象的程序设计 5.1 介绍

来源:互联网 发布:mac怎么设置新浪邮箱 编辑:程序博客网 时间:2024/05/17 03:40

5.1 Introduction

5.1 介绍

Object-oriented programming essentially means programming using objects and the other concepts introduced in Chapter 3. In this chapter, we describe how the concepts are used in a programming language and how they can bimplemented. This style of programming has been used, until recently, only on a small scale. The major strengths of object-oriented programming are that it encourages the reuse of code and that it is usually easier to understand and maintain than other types of programming.

面向对象的程序设计从本质上来看就是应用对象objects,以及在前面第三章中介绍的封装,继承,多态等概念进行程序设计的一种方法在这一章中,我们会重点结合程序设计语言讨论这些重要的概念concepts是如何应用,以及这些概念如何通过编写程序来实现直到最近几年以来,面向对象的程序设计在业界才刚刚开始小规模的应用IT项目中应用面向对象的编程方法的主要优势在于能够非常方便的对代码进行重用,此外相比较其他的编程方法,基于面向对象的编程的项目通常更加易于理解和维护

Defining what is and what is not object-oriented programming is a difficult task. Is it a style of programming or is it language dependent (one can write pure C programs in C++)? We believe it to be, essentially, a style of programming; but to optimize the use of this style of programming, a thorough understanding of the core concepts is required. One also requires a lot of knowledge to be able to use these techniques efficiently. The question of what a good object-oriented programming style is has been discussed in the literature. We shall not enter into this discussion here, but refer to other sources, such as Rochat (1986), Johnson and Foote (1988) and Lieberherr and Holland (1989).

假如我们想准确判定什么是面向对象的程序设计,什么不是面向对象的程序设计,这是一个很困难的任务。很多人可能会有疑问:“面向对象的编程到底是一种编程模式,还是一种和编程语言相关的设计方法呢?(有人可以用C++这种编程语言写出纯粹的C语言的程序). 我们相信面向对象的程序设计从本质上来看是一种程序设计的模式,但是如果程序设计师希望科学的应用这种程序设计模式,就首先需要对面向对象程序设计的核心思想进行掌握程序设计师还必须掌握很多专业知识以便能够充分的应用这种程序设计的思路.目前已经有一些文章在分析和讨论什么是好的面向对象的程序设计模式;在此我们不做进一步的讨论,但是我们推荐一些参考资料,比如Rochat (1986), Johnson and Foote (1988) and Lieberherr and Holland (1989).

We have previously introduced the essential concepts in object-orientation. An object-oriented language should, at least, support these concepts, so that the programmer can utilize them to the full. Hence an object-oriented language must support the following:

Encapsulated objects

The class and instance concepts

Inheritance between classes

Polymorphism

我们前期已经介绍了面向对象程序设计中的本质概念。一种面向对象的程序设计语言至少能够支持这些基本的概念concepts,这样一来程序设计者可以充分的利用这些特性来完整的实现面向对象的IT设计框架。因此,一种面向对象的程序设计语言必须支持以下的特性:

可以封装的对象Encapsulated objects

类和实例The class and instance concepts

之间的层次化继承关系Inheritance between classes

多态Polymorphism

 

We shall  describe how an object-oriented language operates by means of  lifting its lid and studying its main machinery. The aim is to provide an understanding of the different parts of an object-oriented language. We will not present an exact picture, but will content ourselves with providing an outline of the principles. A more precise description of the details can be found in any book that describes a language, for example Goldberg and Robson (1983). We shall not dedicate ourselves to any specific programming language, but will mostly refer to an abstract syntax in our examples and only sometimes to an existing language, usually C++, Eiffel or Smalltalk. Different programming languages have sometimes chosen different solutions; we will discuss some of these when they are of extra interest. Further reading about the languages that we discuss here can be obtained as follows: for Smalltalk, see Goldberg and Robson (1983) or LaLonde and Pugh (1990, 1991a); for Eiffel, see Meyer (1988); for C++, see Lippman (1991) or Ellis and Stroustrup (1990); for Ada, see Barnes (1982, 1984) or Booch (1987b); for Simula, see Birtwistle et al. (1979) or Eriksson and Holm (1984); and for Objective-C, see Cox (1986). Peter Wegner (1987) has made a generally accepted classification of different programming languages in the object-based world. He states in this classification that Ada is object-based because it supports the object concept, although not the class concept. Simula, Eiffel, C++, Smalltalk and Objective-C are examples of object-oriented languages according to his classification since they also support the class and inheritance concepts.

 

我们将通过揭示核心原理(lifting its lid的方式描述一种面向对象编程序语言是如何操作的,并学习这种编程语言的主要运作机制。我们这样介绍的目标是在于帮助读者理解面向对象编程语言的不同部分,以便形成一个整体印象。受限于篇幅的原因,我们将无法呈现一个完整的全貌,而是立足与提供面向对象程序设计相关核心思想的总体轮廓an outline of the principles。如果想更加深入的理解面向对象编程的细节,我们推荐读者参考任何一本介绍编程语言的书籍,举个例子(Goldberg and Robson )。我们将不会专注于任何一种特定编程语言进行介绍而是详细的分析相关案例中的抽象语法结构(an abstract syntax,有些时候我们也会借助一种已知的编程语言来说明相关的案例,本书中选用的编程语言通常是C++Eiffel 或者是Smalltalk 在部分案例中,我们选择不同的程序设计语言来实现不同的解决方案;我们将基于一些特殊的兴趣来讨论其中的一些程序设计语言。如果读者想进一步的了解在本书中应用到的一些编程语言,可以参考一下的书籍,如果想了解Smalltalk,可以参考Goldberg and Robson (1983) or LaLonde and Pugh (1990, 1991a),如果想了解 Eiffel可以参考 Meyer (1988); 如果想了解 C++, 可以参考 Lippman (1991) or Ellis and Stroustrup (1990); 如果想了解 Ada, 可以参考 Barnes (1982, 1984) or Booch (1987b); 如果想了解Simula, 可以参考 Birtwistle et al. (1979) or Eriksson and Holm (1984); 至于想了解Objective-C,可以参考Cox (1986). Peter Wegner (1987) has made a generally accepted classification of different programming languages in the object-based world. Peter Wegner (1987)已经针对当今世界上流行的面向对象的编程语音进行了分类,这种分类方法已经获得了业界广泛的认可。他把Ada划分为面向对象的编程语言,因为ADA支持对象的概念。 Simula, Eiffel, C++, Smalltalk and Objective-C 根据Peter的分类全部都是面向对象的编程语言,因为他们全部都支持类和继承的概念 

To enable us to understand how the object appears, we shall use environments and algorithms to understand how these environments should be interpreted. Environments are used a lot within the LISP world. An environment describes an object's state and behavior. Environments can be illustrated in several ways, such as by a Weizenbaum diagram (Weizenbaum (1968)), or with frames (Abelson and Sussman (1985)). We shall illustrate the environments with diagrams similar to the Weizenbaum diagram. Such diagrams are a description technique in which the majority of programming language computation can be explained. An example is shown in Figure 5.1. By studying these diagrams, we shall examine how the programming language's machinery works.

 为了帮助我们理解对象这个实体是如何产生,我们将使用应用上下文环境(environments)和算法来理解和对象相关的应用上下文环境(environments)应当如何正确的进行解释应用上下文环境(environments)着个概念在LISP编程的世界中被频繁的应用一个应用上下文环境(environments)可以全面的描述对象的状态和行为多个的应用上下文环境可以通过不同的方式来进行解释和描述,比如利用一张Weizenbaum (Weizenbaum (1968)), 或者通过框架 frames (Abelson and Sussman (1985)). 我们将使用类似于Weizenbaum 图的图形来解释应用上下文环境.类似于Weizenbaum 图的图形是一种描述的技术,基于这种描述的技术大多数主流的程序设计语言可以直接进行解释.5-1显示了一个这样的案例.通过学习和研究这些图形,我们可以理解这些不同程序设计语言是如何运作的.

 

 

This chapter's contents follow roughly the contents of the chapter on object-orientation. We begin by discussing the object concept, and see how this can be solved in a programming language. We then go on to discuss the class and instance concepts. Inheritance is discussed before polymorphism, as inheritance is often used to structure polymorphism in object-oriented languages. 

  第五章的内容和第三章的内容<<什么是面向对象>>的编程语言有非常紧密的联系.我们将从讨论对象的概念开始,进一步我们将分析这些对象的概念将如何利用编程语言来解决和实现后续我们会深入的讨论类和实例的概念我们会在多态polymorphism之前先讨论继承Inheritance的概念,因为在实际的面向对象的编程语言中,继承是用来组织实现多态的一种方式.

Polymorphism and the connection to dynamic binding are then discussed, along with their mutual relationship. We shall use the same example that was used in the chapter on object-orientation to help us in our discussion and, because of this, we shall formalize it.

后续我们会讨论多态Polymorphism和动态绑定dynamic binding的关系根据这两个概念之间天然的联系.我们将继续使用第三章所描述的案例来辅助我们进行讨论并将依托编程语言来实现他.