5.2 OBJECT
5.2 对象
The most important concept that an object-oriented language must support is the object concept. Thus the language must support the definition of a set of operations for the object, namely the object's interface, and an implementation part for the object (which a user of the object should not know about). The object implementation is thus encapsulated and hidden from the user.
作为一种面向对象的编程语言来说首先需要支持的概念就是对象的概念. 具体来说,编程语言需要为一个对象定义一组操作,通常叫做对象的接口,并且能够定义这个对象的实现部分an implementation part(这一部分是对象的实现者所不可见的).对象的实现通过这种方式实现了面向用户的封装和隐藏.
Each object has a well-defined interface which specifies which stimuli the object can receive, that is, which operations can be performed on the object. Each stimulus received causes an operation to be performed, where the stimulus is interpreted by the receiving object. If one tries to send a stimulus to an object which has no corresponding operation (that is, the stimulus is not represented in the object's interface), an error occurs.
每一个对象都拥有一个科学定义的接口用于定义这个对象所能够接收的激励stimuli列表,换句话说激励stimuli列表决定了这个对象所能够执行的操作的集合. 当对象接收到一个激励以后,他会首先解释这个激励,并根据解释的结果引发后续的特定操作.假如有人希望给一个对象发送一个未知的激励, 也就是说这个激励没有对应的操作(换句话说,对象的接口中没有定义的操作), 接收对象将会产生一个错误.
An object is implemented internally as a number of variables which store information and a number of operations, or routines, for the object (see Figure 5.2). In Smalltalk, the only way to affect the internal variables is to perform an operation on the object. In C++ or Eiffel, for instance, it is possible to define whether the user should be able to directly access these variables.
一个对象的内部实现主要两部分组成,一部分是一组的变量,这些变量可以用来存储对象的信息结构,另外一部分是对象包含的一组操作,操作中有时也包含循环的操作,图5-2清晰的揭示了这种结构.在Smalltalk中,改变对象内部变量的唯一方法是通过接口对对象施加操作;而在C++或者Eiffel中,可以定义是否允许外部用户直接修改这些变量directly access these variables.
Each object is able to receive a specified number of stimuli. The object interprets a stimulus and performs an operation or, perhaps, directly accesses a variable. In Smalltalk, this stimulus is called a message and the operation executed as a result of receiving a message is called a method. To be able to match stimulus and operation, they must have the same name. In Eiffel, the stimulus is not made explicit; instead the terminology is that an object performs an operation on another object. If we wanted to send a stimulus (for example, jump) to an object Tom, it may look as follows:
每一个对象都能够接收一组特定的激励stimuli. 对象负责解释一个激励,并执行一个操作,或者直接接入一个变量. 在Smalltalk中,这一种激励被叫做一个消息message, 作为消息接收以后后续引发的一个操作被叫做一个方法.为了能够准确的匹配激励stimulus和操作operation,他们必须拥有相同的命名.在Eiffel中,激励的定义方式是隐含的, 代替的实现技术是一个对象直接对另外一个对象施加操作.假如我们希望对对象TOM发送一个跳的操作,他可能看起来是这样一种方式.
We can see from the above example that the syntax in the different languages reflects the approach adopted by the program language designer. In Smalltalk, C++ and Eiffel, the object Tom is central and the operation is performed on ToM. However, in Ada it is the operation which is central and operates on the object. We shall not describe how the object is internally structured in this section, but shall discuss this when we discuss class and instance, in the next section.
从上面的四个案例中,我们可以根据不同编程语言的语法结构特点来理解不同的程序语言设计者所采用的实现方式. 在 Smalltalk, C++ and Eiffel中,对象 TOM是中心, 而相关的操作是直接由TOM负责施加的;而在ADA中,操作是核心, 他直接施加在对象之上. 我们在这个章节中将不会详细的讨论对象的内部是如何实现的,我们将在类和实例的章节中讨论对象的内部是如何实现的.