Pharo Smalltalk - 在实例化对象后是否可以将 super 分配给其他实例?

Pharo Smalltalk - Is it possible to assign super to some other instance after the Object has been instantiated?

假设我们有 oneInstancesecondInstance,其中一个 SomeClass 和一个 OtherClass,下面的示例 Class 层次结构:

oneInstance
Object
 - SomeClass (some variables of it's own, nothing major)

secondInstance
Object
 - SomethingClass
 - OtherClass (just about any class in the tree here)

是否可以在运行时更改 oneInstance,以便 "super" 消息发送到 secondInstance。

oneInstance 和 secondInstance merge 本质上使 oneInstance 像一个对象一样工作,并且结构看起来好像它们是从类似的东西实例化的:

secondInstance wraps around oneInstance
    Object
     - SomethingClass
     - OtherClass (just about any class in the tree here)
     - SomeClass (some variables of it's own, nothing major)

最简单的方法是,如果我可以在 oneInstance 上分配 super := secondInstance 一段时间,然后再将其改回 :D

PS。本质上,我们通过让 secondInstance 对 oneInstance 进行重新分类,因为它是 "super" 它们现在是一个具有两者状态的对象,假设 oneInstance 是 Object 的子类,没有其他状态,但它是自己的。主要是使用继承链的默认方法查找对我有利的技巧。我能找到的最接近的东西是对象切片 https://en.wikipedia.org/wiki/Object_slicing

另一种看待它的方式是这样的:

secondInstance 正在接收消息,它是 OtherClass 的实例,一切正常。它接收到的一些消息不在 OtherClass 中,因此方法查找在继承链上上升到 SomethingClass,然后到 Object、ProtoObject 等,最后它们应该被转发到另一个实例。这个过程应该是完全透明的。

首先,在 Pharo 和 Squeak(以及大多数 Smalltalks)中,您在运行时 总是。所以很明显,如果可以做某事,就可以在 "runtime" 做。 :)

通常乱用 "regular code" 的元功能会导致真正难以调试的欺骗性代码,对您和其他人来说都是如此。因此,要实施#doesNotUnderstand: 并使用#respondsTo: 等,通常 "bad style" 除非你真的必须做这些事情。

透明转发器对象的一个​​明显示例是 OODBS 的代理 - 但确实没有太多好的示例。

但要更准确地回答 - 在您执行 #doesNotUnderstand 时: - 只需查询是否 self respondsTo: aMessage selector(或类似)并根据此决定是否委托。