A Brief History of Object-Oriented Programming: 面向对象简史

SIMULA was the first object language. As its name suggests it was used to create simulations. Alan Kay, who was at the University of Utah at the time, liked what he saw in the SIMULA language. He had a vision of a personal computer that would provide graphics-oriented applications and he felt that a language like SIMULA would provide a good way for non-specialists to create these applications. He sold his vision to Xerox Parc and in the early 1970s, a team headed by Alan Kay at Xerox Parc created the first personal computer called the Dynabook. Smalltalk was the object-oriented language developed for programming the Dynabook. It was a simulation and graphics-oriented programming language. Smalltalk exists to this day although it is not widely used commercially.

SIMULA是第一个面向对象语言。名字就说明这是用于创造。Alan Kay还在犹他大学读书的时候,就很喜欢这类语言。他有一台装有图像对象程序的个人电脑,那时他就觉得应该用像SIMULA这样完美范式的编程语言来编写程序。在20世纪70年代他把他的想法带到了施乐公司,并且带领了团队创造了一个叫DynaBook的个人电脑,里面包含SmallTalk这种面向对象的编程语言环境,SmallTalk是带有图形化界面得编程语言,很可惜的是,SmallTalk并没有得到很广泛的应用和商用。

The idea of object-oriented programming gained momentum in the 1970s and in the early 1980s Bjorn Stroustrup integrated object-oriented programming into the C language. The resulting language was called C++ and it became the first object-oriented language to be widely used commercially.

SmallTalk面向对象的设计在20世纪70年代掀起了浪潮,20世纪80年代初期Bjorn Stroustrup把面向对象集成在了C语言,因此产生了叫C++,并且得到广泛的应用和商用。

In the early 1990s a group at Sun led by James Gosling developed a simpler version of C++ called Java that was meant to be a programming language for video-on-demand applications. This project was going nowhere until the group re-oriented its focus and marketed Java as a language for programming Internet applications. The language has gained widespread popularity as the Internet has boomed, although its market penetration has been limited by its inefficiency.

在20世纪90年代初期,在Sun公司的James Gosling开发出一个叫JAVA的简单c++版本,用于控制音像应用的项目。直到SUN重新把JAVA作为互联网程序商用的时候,这个项目早就被废弃了。即使JAVA的效率比较低,但是在互联网依然产生了巨大的影响并且得到很广泛的应用。

ObjectsObject-oriented programming is first and foremost about objects. Initially object-oriented languages were geared toward modeling real world objects so the objects in a program corresponded to real world objects. Examples might include:
1. Simulations of a factory floor–objects represent machines and raw materials
2. Simulations of a planetary system–objects represent celestial bodies such as planets, stars, asteroids, and gas clouds
3. A PC desktop–objects represent windows, documents, programs, and folders
4. An operating system–objects represent system resources such as the CPU, memory, disks, tapes, mice, and other I/O devices

对象面向对象编程是第一个且主要针对对象的语言,首先面向对象语言是对现实世界对象的建模,然后在程序中的对象反映现实世界。可以从以下4个例子可以看出:
1、工厂车间–机器和原材料
2、行星系–大行星,星星,小行星,云集(尘埃?)
3、个人电脑–windows操作系统、文件系统、程序集、文件夹
4、操作系统-CPU、内存、硬盘、磁盘、麦克风、外设IO设备

The idea with an object is that it advertises the types of data that it will store and the types of operations that it allow to manipulate that data. However, it hides its implementation from the user. For a real world analogy, think of a radio. The purpose of a radio is to play the program content of radio stations (actually translate broadcast signals into sounds that humans can understand). A radio has various dials that allow you to control functions such as the station you are tuned to, the volume, the tone, the bass, the power, and so on. These dials represent the operations that you can use to manipulate the radio. The implementation of the radio is hidden from you. It could be implemented using vacuum tubes or solid state transistors, or some other technology. The point is you do not need to know. The fact that the implementation is hidden from you allows radio manufacturers to upgrade the technology within radios without requiring you to relearn how to use a radio.

这些对象可以保存对应的数据,也可以操作这些数据,对象如何实现的话对于用户来说是不透明的。拿现实世界的收音机来说,它是用来播放一些音频音带音乐的(实际是广播一些人们能听懂的信号),收音机上有多个功能按钮可以操作,例如可以换频道、调声音、调音质、电源开关等等,这些都是你能操作控制收音机的功能,例如用了哪些二极管、晶体管或者其他技术,这些功能如何实现的用户不需要知道。事实上,哪怕收音机进行了技术升级,你也不需要重新学习如何使用它。

The idea is the same with objects. An object advertises the set of functions it will perform for you but does not reveal its implementation. Think of it as a black box. For example, think of the objects on a desktop. For simplicity think of two types of objects, a document and a program. A document contains data and a program contains executable statements. The desktop needs some functions to manipulate these objects. For example, it needs to be able to copy, cut, and paste these objects. It does not care how these objects are implemented. It just needs them to perform those three functions. Consequently both documents and programs provide functions that allow them to be copied, cut, and pasted.

对于对象也是一样,对象有一系列的功能对于你来说透明的,对于使用的人是不透明的。例如说,现在这个对象就是个人电脑,个人电脑有两个对象,文件和程序,文件包括数据,程序包括可执行的指令,电脑需要一些功能可以控制这些对象,对于文件来说,你可能想可以复制、剪切、粘贴这三个功能,我们并不关心这些功能是如何实现的,我们只需要用他们来实现这三个功能即可,所以文件和程序都应该提供这三个功能。

As another example, consider a stack. A stack provides a set of operations such as push, pop, isempty, and top. If the stack is implemented as an object, its implementation will be hidden from the program. It may be implemented as an array, a queue, or some other data structure. The program does not need to know how the array is implemented. Its only concern is that the stack provide the specified operations and that the operations provide the desired behavior.

另外一个例子,栈,提供压栈、退栈、空栈、栈顶等一系列的操作,如果栈是一个对象,那么对于程序调用方来说是不透明的。当然,他可以被数组、队列或其他的数据结构实现这些功能,程序不需要知道数组是如何实现这些功能的,他只需要栈提供了这些指定的功能并且产生你所期望的结果。

The set of operations provided by an object is called its interface. The interface defines both the names of the operations and the behavior of these operations. In essence the interface is a contract between the object and the program that uses it. The object guarantees that it will provide the advertised set of operations and that they will behave in a specified fashion. Any object that adheres to this contract can be used interchangeably by the program. Hence the implementation of an object can be changed without affecting the behavior of a program. For example, we can replace a stack object that is implemented as an array with a stack object that is implemented as a queue without affecting the behavior of the program.

对象提供一系列的功能称为它的接口,它定义了功能的名称和功能的作用。从本质上来说,接口就是程序和对象之间的约定,对象保证了它提供这些功能会产生期望的结果。任何遵循此约定的对象都可以被程序切换地调用,程序切换调用实现了这些功能的对象不会对结果产生任何影响。例如,我们可以把实现了栈的数组换成队列,也不会对程序结果产生任何影响。

ClassesAn object is not much good if each one must be custom crafted. For example, radios would not be nearly as prevalent if each one was handcrafted. What is needed is a way to provide a blueprint for an object and a way for a “factory” to use this blueprint to mass produce objects. Classes provide this mechanism in object-oriented programming. A class is a factory that is able to mass produce objects. The programmer provides a class with a blueprint of the desired type of object. A “blueprint” is actually composed of:
1. A declaration of a set of variables that the object will possess,
2. A declaration of the set of operations that the object will provide, and
3. A set of function definitions that implements each of these operations.

The set of variables possessed by each object are called instance variables. The set of operations that the object provides are called methods. For most practical purposes, a method is like a function.

如果一个对象都是由每个人去自定义的,那么这个对象就不能够高度统一。如果每一个收音机都是手工制作的,这样的话收音机没办法做到很流行,那么久需要针对这个对象绘制蓝图(模具)给到工厂去大规模的量化生产。程序员提供了描述这个对象的蓝图的类,那么蓝图应当有以下3方面组成:
1、声明对象的一系列的变量
2、声明对象提供的一系列的功能
3、定义一系列的功能对象的变量称为实例变量,对象的功能称为方法,对于绝大部分的实例来说,方法就是功能。

When a program wants a new instance of an object, it asks the appropriate class to create a new object for it. The class allocates memory to hold the object’s instance variables and returns the object to the program. Each object knows which class created it so that when an operation is requested for that object, it can look up in the class the function that implements that operation and call that function.

如果程序想要实例化对象,那么可以新建一个合适的类去实例化。类通过分配内存去保存对象的实例变量和返回对象给程序,每一个实例化对象,当想要执行这个对象的功能时,都可以通过查找类方法,并且调用它。

InheritanceTo motivate inheritance, think of a radio alarm clock. A radio alarm clock has all of the functions of a radio plus additional functions to handle the alarm clock. If we adopt the radio’s interface for the radio alarm clock, then someone who knows how to operate a radio will also know how to operate the radio portion of the radio alarm clock. Hence, rather than designing the radio alarm clock from scratch, we can extend or inherit the interface defined by the radio. Of course, we can also use the existing implementation for a radio and extend it to handle the alarm clock functions.

要说到继承,可以想想收音机和闹钟的关系。收音机闹钟需要在收音机所有的功能上额外添加的功能。如果我们想收音机的接口上添加闹钟的功能,那么其他原先已经知道如何使用收音机的人也会知道如何调用闹钟的功能。所以,与其因为闹钟的功能重新设计收银机的接口,我们不如直接用继承实现或者接口实现的方法来实现。当然了,我们也可以从原先已经实现收音机接口的对象中并拓展这个闹钟的功能。

In object-oriented programming, Inheritance means the inheritance of another object’s interface, and possibly its implementation as well. Inheritance is accomplished by stating that a new class is a subclass of an existing class. The class that is inherited from is called the superclass. The subclass always inherits the superclass’s complete interface. It can extend the interface but it cannot delete any operations from the interface. The subclass also inherits the superclass’s implementation, or in other words, the functions that implement the superclass’s operations. However, the subclass is free to define new functions for these operations. This is called overriding the superclass’s implementation. The subclass can selectively pick and choose which functions it overrides. Any functions that are not overridden are inherited.

在面向对象编程中,继承意味着是继承其他对象,也可能本身的实现。继承是通过新建一个子类继承已经存在的类,这个已经存在的称为父类。子类通常要完整实现父类的,可以继承接口但不能删掉接口的任何一个功能,也就是说,要实现父类的所有功能方法。当然,子类可以定义新的方法来实现这些功能,这就是方法的覆盖,子类可以自由选择哪些方法覆盖,哪些方法不被覆盖。没有任何一个方法被覆盖的情况叫做继承。

There is a great deal of debate about how to use inheritance. In particular, the debate swirls about whether inheritance should be used when you want to inherit an interface or whether it should be used when you want to inherit implementation. For example, suppose that you want to define a search object that stores (key, value) pairs and allows values to be looked up by providing their keys. More precisely, let us say that the search object supports the following operations:
* insert: Adds a (key, value) pair to the object.
* delete: Deletes a (key, value) pair from the object.
* lookup: Given a key retrieves the value associated with that key from the object.

Later we decide that we want a new object that allows us to traverse the (key, value) pairs in sorted order. The new object should support the above operations plus two additional operations, rewind that puts us back to the beginning, and next that returns the next (key, value) pair. Since the new object supports all of the operations of the original search object, we can make the new object inherit the original object’s interface. This is an example of interface inheritance.

这是针对如何使用继承的争论,特别是,当你想实现接口的时候是否应该使用继承,或者当你想继承实现的时候是否应该使用继承。例如,你想定义一组key-value形式的搜索对象,并且可以允许通过查找key得到对应的value。更为准确的来说,搜索对象应当支持以下操作:
●新增:Add(key,value),新增数据到对象
● 删除:Delete(key,value),从对象中删除数据
●查找:Retrieve(key),
根据key获取对应的value现在我们又想允许新对象按照排序顺序key-value,可以再原有的基础上新加两个操作,一个是返回对象的第一对数据,另外一个是可以返回当前对象的下一对数据。由于新的对象支持所有的普通搜索对象,所以我们可以说我们继承了普通对象。这就是继承接口的例子。

To give an example of implementation inheritance, suppose that we want to implement the original search object using a binary search tree. The binary search tree probably already has an implementation for these three operations but it may not use these names for the operations. If we wanted to inherit the binary tree’s implementation, we would make the search object be a subclass of the binary tree. We could then make the insert, delete, and lookup operations call the appropriate binary tree operations. Of course, we could also scrap our proposed interface and use the names of the binary tree interface instead.

接下来是继承实现的例子,设想我们想实现二叉树的普通搜索对象,二叉搜索树应该有这三种操作(增删查),尽管这些名称可能不会用在操作上。如果实现二叉搜索树,我们可以新建一个搜索对象作为二叉树的子类。我们可以用新增、删除、查找来调用合适的二叉树操作,当然,我们也可以不用我们之前新增的接口,直接用二叉树的名称代替。

Well, remember that an object is supposed to hide its implementation and that it should be interchangeable with other objects that implement the same interface. We can’t very well scrap the interface and use the binary tree’s interface because that would tie the interface to the binary tree’s interface. So we should hold firm on our originally proposed interface. However, there’s another problem. By making the search object inherit from the binary tree, we’ve also made its implementation dependent on the binary tree.

请记得"对象实现对于使用者不透明,而且在继承同一个接口上,实现的对象可进行内部切换"。我们不能废弃这个接口,因为有对应实现二叉树的子类绑定了,所以我们废弃之前,我们得确认我们定义的新对象。当然,这又是另外一个问题了,我们实现了二叉树接口,又继承了这个二叉树。

Hopefully the above example shows why implementation inheritance may not be a good idea. In general I’ve found that inheritance should be used only when you want to inherit an interface. If it so happens that the implementation you get can be also be used, well and good. There are other ways to re-use implementation and we will discuss those later in the course.

希望以上的例子可以说明继承实现并不是个什么好主意。一般来说,当你想实现接口的时候,继承应该是唯一的途径。当前了,除非你用继承实现用得特别特别好。这是我们在接下来的文章应该讨论的问题"另外的途径使用复用来实现接口"。

Abstract Data TypesObjects provide an ideal mechanism for implementing abstract data types. An abstract data type is:
1. A type of data, and
2. A set of operations for manipulating that data.

Examples of abstract data types include stacks, trees, and hash tables. The reason for the word “abstract” is that an abstract data type defines only the set of operations it supports (i.e., its interface). It does not define an implementation. In order to make the data type concrete one must provide an implementation.An object is a nice implementation vehicle for abstract data types since the data stored by the object can represent the abstract data type’s data and the object’s interface can represents the abstract data type’s set of operations.

抽象数据类型对象提供了以一种理想的机制来实现抽象数据类型,抽象数据类型:
1、一种数据的类型
2、一系列可以操作数据的功能
抽象数据类型的例子包括栈、树和哈希表,抽象这个单词表明只有这种类型才能定义的功能。(如它的接口)。它并不定义实现的内容,使为了使这种数据类型必须提供实现。对于抽象数据类型来说,对象是一种很好的实现例子,因为对象可以保存对应抽象数据类型的数据,实现的子类也可以很好地实现接口所定义的操作。

Execution ModelOne of the original purposes for object languages was to model applications that have multiple objects that may be operating simultaneously. For example, the machines on a factory floor operate simultaneously. In an operating system the various input devices, such as disks, the keyboard, and the mouse, may be operating simultaneously. In a computer game various players, either human or computer-generated, may be operating simultaneously.

面向对象语言众多目的之一就是为了使应用可以同时运行多个对象。例如,工厂可以有多个机器在同时运作,操作系统中多个输入设备(硬盘、鼠标、键盘)可以同时运行,电脑游戏中多个玩家、人、电脑合成可以同时在线。

These applications all have something in a common–they do not have a single thread of control. Instead control is distributed throughout the application. At any given moment multiple objects may want to perform some action. Conventional imperative languages like C have trouble modeling this type of application because they have a single thread of control. Object languages solved this problem by making everything an object and having control reside within each object. That is, at any given moment multiple objects could be executing an operation (at least this is the conceptual model–a computer with a sequential processor might simulate this model by interleaving the execution of the operations). Objects would communicate with one another by passing messages. A message is simply an invocation of an operation in another object. For example, an operating system might queue input events arriving from various input devices. Each input device might be represented as an object and the input event queue might be represented as an object. Each time an input device receives an input event, it would invoke the operation on the event queue that adds an input event to the queue.

这些应用都有共同之处–他们不是单一控制的,他们分布在整个应用的生命周期中控制,可以在任意时间内对象可以执行某些操作。传统过程式的C语言在这类型的应用中就会出现异常,因为它是单进程控制的。面向对象语言通过每个对象都有控制权的方法来解决这个问题,所以在程序执行的任意时间对象都能调用对应的操作。(至少这个是概念模型–顺序执行的计算机可以通过交叉控制多个正在执行的对象)。对象之前的通讯通过传递信息,在对象中信息是被对象的对应的操作所调用的,例如,操作系统可能有来自各种各样的输入设备的输入事件队列,每一个输入设备触发的输入事件都可当做为一个对象,每次输入设备收到输入事件的时候,对象都会把这个操作事件添加到操作系统的输入事件队列中。

Smalltalk and Java are two well known examples of this “everything is an object” concept. They are also well suited for modeling distributed responsibility. Unfortunately, not every application needs to have distributed responsibility and that is where these object-oriented languages run into problems. People try to force this model onto applications that are more naturally modeled using a single thread of control. When this happens, programmers, especially programmers learning object-oriented programming, get confused.

SmallTalk和java是两种知名的"一切皆为对象"理论的案例,它们非常适合做分布式责任建模。很可惜的是,并不是每个应用都需要做分布式建模的,同时这也是面向对象语言的存在问题,这个时候有些人就尝试用单进程进行分布式建模,每每此时,这些程序员,或者是正在学习面向对象语言的程序员就感觉很困惑。

In this course we will use C++ which has a different execution model. It retains the notion of a central thread of control. Objects provide services to the central thread of control. For example, a stack object provides a stack service. We will primarily use objects to implement abstract data types. Using objects in this way will give us the advantages of data encapsulation and code re-use. Data encapsulation means that the implementation of an object is hidden and hence we can interchange objects with different implementations but the same interface. Code re-use means that we can use the same object in different applications, so we don’t have to write the code twice.

在这个课程里面我们将会使用C++的不同的执行模型,它仍然保留着中心进程的概念,并且控制着中心进程。例如,栈对象会提供栈对应的服务,我们主要使用对象来实现抽象数据类型,用这些对象我们具备数据封装和代码复用的优势,数据封装意味着这个对象的实现是不透明的,我们可以通过不同的对象实现同一个接口来内部通信,代码复用意味着我们可以在不同的应用的使用相同的对象,不用重复造轮子。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值