[英]Replication from multiple inheritance in Eiffel
I am struggling to understand the interplay of multiple inheritance with replication and polymorphism. 我在努力理解多重继承与复制和多态性之间的相互作用。 Please consider the following classes forming a classical diamond pattern.
请考虑以下构成经典菱形图案的类。
deferred class A
feature
a deferred end
end
deferred class B
inherit A
rename a as b end
end
deferred class C
inherit A
rename a as c end
end
class D
inherit
B
C
select c end
feature
b do print("b") end
c do print("c") end
end
If I attach an instance of D to an object ob_as_c
of type C, then ob_as_c.c
prints "c" as expected. 如果我将D的实例附加到类型为C的对象
ob_as_c
,则ob_as_c.c
将按预期方式打印“ c”。 However, if attach the instance to an object ob_as_b
of type B, then ob_as_b.b
will print also print "c". 但是,如果将实例附加到类型B的对象
ob_as_b
,则ob_as_b.b
将同时打印“ c”。
Is this intended behaviour? 这是预期的行为吗? Obviously, I would like
ob_as_b.b
to print "b". 显然,我希望
ob_as_b.b
打印“ b”。
Just describing the actual behavior in EiffelStudio which may differ from the actual ECMA specification. 仅描述EiffelStudio中的实际行为,可能与实际的ECMA规范有所不同。
What is happening is that without the select both b and c corresponds to a version of a . 正在发生的事情是,如果没有选择 B和C对应于一个版本的 。 The compiler would complain that you have 2 routines with different name but same version.
编译器会抱怨您有两个名称不同但版本相同的例程。 By using `select' you are fixing the error and telling the compiler that for dynamic binding the version to use is c and this is regardless of the type of the target, it is based on the type of the target at runtime.
通过使用“选择”,您可以修复错误,并告诉编译器对于动态绑定,要使用的版本为c ,而与目标的类型无关,它基于运行时目标的类型。
(Cosmetics: the spelling is "deferred". Otherwise it won't compile!) (化妆品:拼写为“ deferred”。否则将无法编译!)
"select" only affects the semantics of a call whose target is an entity declared with the repeated ancestor type, here A, since in this case we need a disambiguation. “选择”仅影响目标为使用重复祖先类型(此处为A)声明的实体的呼叫的语义,因为在这种情况下,我们需要消除歧义。 For entities of type B, C or D there is no ambiguity, so the normal rules of polymorphism and dynamic binding apply: for the same target object, it does not matter whether the entity (the name in the program) is declared of type B or C.
对于类型B,C或D的实体没有歧义,因此多态性和动态绑定的常规规则适用:对于同一目标对象,是否声明类型B的实体(程序中的名称)都没关系或C。
-- Bertrand Meyer -伯特兰·迈耶(Bertrand Meyer)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.