[译]OOSE第3章:面向对象知识入门 3.4Polymorphism

本文深入探讨了面向对象编程中的多态性概念,解释了如何通过不同类的实例间交互实现多态性,以及多态性如何帮助构建更灵活、易于扩展的软件系统。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

3.4 Polymorphism

Instances, created from classes, will together provide us with the dynamic behavior that we wish to model. It is when these instances start to communicate with each other that the system's behavior is performed. An instance may know of other instances to which stimuli can be sent. If an instance sends a stimulus to another instance, but does not have to be aware of which class the receiving instance belongs to, we say that we have polymorphism. Polymorphism means, at least in object-oriented contexts, that the sending instance does not need to know the receiving instance's class and that this class can be any class.

通过类创造出来的一些实例,也会互相通信,这种实例之间的动态关系也是面向对象系统中需要建模的方面. 只有当不同的实例开始互相通讯与协作,整个面向对象的系统才开始工作和行为.一个实例发送激励(stimuli)给其他接收实例,发送实例也许在发送激励(stimuli)前就已经确切知道了接收实例的标识.假如一个实例发送激励stimuli给另外一个接收实例时,他不需要关心接收实例确切的属于哪一个类,那么我们认为这种面向对象的系统拥有多态polymorphism的特性.至少在面向对象系统的语意上下文中,多态意味着发送实例不需要了解接收实例所属于的类,任何类都有可能成为激励的接收者.

“Polymorphism means that the sender of a stimulus does not need to know the receiving instance's class. The receiving instance can belong to an arbitrary class.”

多态Polymorphism意味着发送实例不需要了解接收实例所属于的类,激励(stimuli)的接收实例可以属于任意的类.

All people have a best friend. We can see this in the information structure of the classes Male and Female; both have information on a friend. We have not specified any special conditions for this friend, but we can assume that it is another person. A person's friend must, therefore, be either of the class Male or Female. We therefore have polymorphism in this model, as friend can refer to an instance of one of two classes. Mary's friend is either Tom or John and as these are both associated with the same class, Male, we have no use for the polymorphism characteristic. However, Tom's friend is either Mary or John and, as they are from different classes, class Male will not know with which class the friend will be associated. Here, the reference friend must be polymorphic. Polymorphism means that different instances can be associated, and that these instances can belong to different classes. It is in the latter case, as with Tom, that we have use of the polymorphic characteristic.

每个人都有一个最好的朋友,我们在男人和女人这两个类当中都会看到这个信息结构,他们都会有一个信息字段标识朋友.我们没有针对朋友这个字段标识任何特殊的限定条件,因为从客观世界来分析朋友必定会是另外一个人。换句话说,一个人的朋友必须是属于男人和女人这两个类当中的一个。因此我们在这个模型中拥有了多态的特性,因为朋友必然是涉及到两个类中的一个实例。Mary's friend is either Tom or John and as these are both associated with the same class, Male, we have no use for the polymorphism characteristic. However, Tom's friend is either Mary or John and, as they are from different classes, class Male will not know with which class the friend will be associated. Here, the reference friend must be polymorphic. Polymorphism means that different instances can be associated, and that these instances can belong to different classes. It is in the latter case, as with Tom, that we have use of the polymorphic characteristic. Mary's的朋友是Tom或者是John,因为他们都是关联到男人这个类,在这种场合下我们并没有应用多态这个特性。然而,Tom的朋友可能是Mary or John,因为他们是属于不同的类;注意男人这个类是不可能知道朋友这个信息结构关联到那一个具体的类,所以,在这种情况下,涉及到的朋友必须是具有多态的特性。多态(Polymorphism意味着不同的实例(instances可以互相关联,并且这些实例是可以属于不同的类。这就是在和TOM相关的场景中,我们应用了对象多态的特性。

A stimulus can be interpreted in different ways, depending on the receiver's class. It is, therefore, the instance which receives the stimulus that determines its interpretation, and not the transmitting instance. Often polymorphism is said to mean that one operation can be implemented in different ways in different classes. This is actually only a consequence of what is said above and not polymorphism in itself. (Polymorphism and dynamic binding are also often confused. Dynamic binding means that the stimulus is not bound to a certain operation in the receiving instance's class until it is sent. We shall discuss dynamic binding in more detail in the chapter on object-oriented programming.)

同一个激励stimulus可以用不同的方式来解释和执行,具体的执行方式完全依赖接收实例所归属的类。所以在多态的场景下,是由接收激励stimulus的实例来决定相关的解释方式,而不是发送激励stimulus的实例。所以实质上说,多态这种特性意味着一个操作operation在不同的类中可以采用不同的实现方式。上面描述的是多态对面向对象系统动态行为的影响,但并不是多态这一概念本身(多态和动态绑定这两个概念通常很容易混淆。动态绑定这种概念指的是激励和接收实例的一个特定操作并不具备绑定关系,而只有当激励发送的时刻,这种关联关系才会明确,在后面面向对象的程序设计部分我们还会详细的讨论和动态绑定更多的细节。)

 

As we have not specified any conditions on the class, we should, in theory, be able to associate an instance of any class to friend. It is therefore possible to associate an arm with friend. To avoid errors of this kind, it is often necessary to restrict the receivers of a stimulus. If we know, in advance, the receiver's class,  then we don't need polymorphism, but if we allow the receiver to be of varying class, within limits, we must specify this restriction in the polymorphism characteristic. Thus, we need to restrict the instances which can be associated. This is often called limited polymorphism. We require only instances of the classes Male and Female to be friends. This restriction can be made in different ways. Normally, inheritance hierarchy is used. We shall soon return to this problem.

因为我们没有对接收激励的实例(instance)所归属的类(class)提出任何限定条件,在理论上说,我们应该科可以把任何一个类所归属的实例和朋友关联起来,那么我们甚至有可能把一个对象手和朋友关联起来。为了避免这种错误,在面向对象的建模时必须限定一个激励的接收类的范围。假如我们事先知道接收实例归属的类(class),我们就不需要多态这种特性;假如我们允许接受实例归属于不同的类(class),而这些类有具有一定的限定范围,我们就必须针对多态特性增加限定条件。在这种情况下我们必须限定那些实例可以被关联,这被称为是受限的关联。在刚才讨论的问题中,我们仅仅允许属于男人和女人这两个类当中的实例被关联为朋友。有很多种方法来实现这种限定条件,通常继承这种方法可以被使用来实现这个目的,后面我们很快会涉及到这个问题。

Polymorphism is a very important characteristic for our models. It is the receiver of a stimulus that determines how a stimulus will be interpreted, not the transmitter. The transmitter need only know that another instance can perform a certain behavior, not which class the instance belongs to and thus not which operation actually performs the behavior. This is an extremely strong tool for allowing us to develop flexible systems. We have, in this way, only specified what shall occur and not how it shall occur. Through delegating what shall occur in this way, a flexible and modification-resistant system is obtained. If we wish to add an object of a new class, this modification will affect only the new object, and not those sending stimuli to it.

多态这种特性在我们利用OOSE方法建模的系统中是非常重要的一个特性。由激励的接收者来决定一个激励应该如何解释和实现,而不是激励的发送者。发送者仅仅需要知道另外一个实例可以执行特定的操作,但是不清楚这一个实例所归属的类,以及那相关操作的确切的实现模式这种特性是一个非常强大的建模方法允许我们构建功能灵活的系统(flexible systems)。通过这种方式,我们仅仅需要定义操作行为的结果,而不需要定义操作行为的模式(only specified what shall occur and not how it shall occur.。通过授权“什么操作需要发生”这种多态的模式,我们获得了一个灵活并且容易调整变更的系统。假如我们希望增加一个新的对象属于一个新的类,这种调整仅仅会影响新的对象,而不会影响发送激励的对象。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值