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

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,操作是核心他直接施加在对象之上我们在这个章节中将不会详细的讨论对象的内部是如何实现的,我们将在类和实例的章节中讨论对象的内部是如何实现的.

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值