[译]OOSE第5章:面向对象的程序设计 5.6 An example & 5.7 Summary

5.6 An example

We shall here consider how we can use the classes from Chapter 3 to construct a whole game, where people interact with each other. The people involved are a father with his two sons, a woman and an anonymous person. We shall begin by looking at the class Game:

下面我们将介绍根据第三章所描述的案例中所涉及到的类来构建一个完整的游戏,在游戏中不同的人会互相联系。其中涉及到的人有一个父亲和他的两个儿子,一个女人,和一个未知的人。我们首先从Game这个类开始。

   

The class is created as a direct descendant of the existing class GeneralGame. The instance variables of the game are declared. Game has two operations: new, which creates and initiates a new game instance, and startGame, which starts a game. To create an instance of the class Game, the following expression is executed:

currentGame : = Game.new

   Game这个类是一个已知的类GeneralGame的直接后裔类。关于类GeneralGame的实例变量都已经明确的声明。Game有两个操作,一个是new, 用于创建新的Game实例变量,另外一个是startGame,用于激活一个Game。 为了创建一个类Game的实例,下面的表达式将会被执行:

currentGame : = Game.new

The operation new, in the class Game, is now executed and an instance of Game is created. The instance's environment will be as follows:

在类Game中的操作new 会被执行,这样一来Game中的一个实例对象会被激活。这个实例的运行环境将会是如下所显示:

We can see that the instance contains the instance variables declared in the class. The instance variables have not yet received any values and therefore have a value of nil (no reference). In order to create new people which the instance variables can reference, we require classes for them. We therefore use the class structure developed in Chapter 3, with a class Person, two classes Male and Female which inherit Person, and two classes oldMale and youngMale which inherit Male. The class Person is created as a descendant to the class Object, and is as follows:

我们可以发现这个实例包含了所有在类中声明的实例变量。由于实例变量还没有接受到任何的赋值,所以就有一个NULL的数值( 没有参考索引)。为了能够创建新的人可以供实例变量来索引到,我们需要和人有关系的类。所以我们使用在第三章中设计的类结构,这个类结构中有一个类人Person,两个类男人Male和女人Female是人Person的后裔类,而两个类oldMale 和youngMale是男人Male的后裔类。而类人Person是类Object的后裔类,看起来会是这样的结构

Note that the operation new creates new instances of Head and Legs, but not of Body. This is due to males and females having different bodies and this difference is specified in classes Male and Female. These two classes are direct descendants of Person and implement individually their differences from Person. When new is performed on a class, it thus must execute in both the actual class and all its parent classes.

注意在这里的操作new仅仅创建了新实例的头和手,但是没有创建身体。这是因为男人和女人拥有不同的身体,同时这种区别在类男人和女人中进行定义。这两个类都是类Person的直接后裔,同时他们将各自独立的实现和类Person有差别的部分。当在后裔类中执行new操作,那么就必须同时执行确切的类和他所有的父亲类所包含的操作和实例变量。

Note that mybody is a polymorphic reference as it can refer to instances of both classes MaleBody and FemaleBody. These classes are not shown, and their creation is an exercise for the reader. The classes oldMale and youngMale contain two variations of the operation dance and their declaration may also be exercises for the reader.

注意mybody是一个具有多态特性的参考变量,因为他可以索引到类MaleBody and FemaleBody的不同的实例当中。这些类还没有显示,关于这些类的创建过程可以作为读者的一个练习。而类oldMale and youngMale中也包含了两个不同的操作dance,关于着两个操作的声明也留给读者作为练习。

In order to start the game, we perform the operation startGame on the instance currentGame. Thus we execute the following statement:

为了开始相关的游戏,我们会在currentGame的实例中执行操作startGame。这样一来我们就执行下面的表达式。

currentGame.startGame

When this is executed, instances will be created as specified in the operation startGame. We show here only the instance woman after execution of this operation:

当这个操作被执行,根据操作startGame定义的要求,各个实例将会被创建。在这里,我们仅仅显示女人这个实例在操作执行以后的取值

5.7 Summary

Object-oriented programming is a style of programming where the core concepts of object-orientation are used. In this chapter we have looked at mechanisms for these concepts.

面向对象的程序设计是一个编写程序的模式,在这种编程模式中应用了面向对象设计的核心概念。在这个章节中,我们已经讨论了这些概念的运作机制。

The things to program in an object-oriented programming language are the classes. In the class, the programmer defines the variables and the operations associated with the class and instances of the class. From these classes, instances are dynamically created during execution of the program.

在面向对象的编程语言中最核心的物件是类。程序设计者在类当时定义和类相关的变量和操作,以及和类相关的实例。根据这些类的定义,在程序的运行其间,实例可以动态的进行创建。

Inheritance provides a mechanism for creating new classes as modifications to existing classes. However, since an operation can be implemented in an ancestor class, an operation look-up alghorithm must be executed to find the correct operation. Inheritance is not a prerequisite for reuse. The construction of a new class can very well benefit from existing t lasses without inheriting them.

继承这种核心概念提供了一种对已知的类进行修改的机制。然而,由于操作能够在层次化继承结构的祖先类an ancestor class中进行定义,所以一个操作查找算法必须被执行来寻找合适的操作。而继承并非重新用的前提条件。面向对象的设计中还提供了其他的机制用于重用已知的类来实现新类的设计过程。

Polymorphism can be implemented using dynamic binding. This means that the actual binding of a stimulus to a specific operation is not done until the stimulus is actually sent during run¬time. This often means that the operation look-up must be done dynamically during run-time. However, many language implementations have solved this efficiently so that the look-up overhead cost is minimized.

   多态这种机制可以利用动态绑定的技术来实现。这样就意味着一个激励和一个特定操作的绑定操作会一直延迟,直到激励在运行时刻确切的发送。这也意味着操作查找算法必须在执行的时刻动态的运行。然而很多程序设计语言实现已经解决了执行效率的问题,这样一来操作查找算法过载的开销被很好的控制。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值