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

时间:2015-09-17 13:03:56

标签: smalltalk pharo amber-smalltalk

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

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

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

是否有可能在运行时更改oneInstance以便超级"超级"消息发送到达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,因为它的超级"它们现在是一个具有两个状态的对象,假设oneInstance是从Object继承而没有其他状态但它自己的状态。主要是使用继承链的默认方法查找我的优势。我能找到的最近的东西是对象切片https://en.wikipedia.org/wiki/Object_slicing

另一种看待它的方法是:

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

1 个答案:

答案 0 :(得分:1)

首先,在Pharo和Squeak(以及大多数Smalltalks)中,您在运行时始终。很明显,如果它可以做某事,它可以在"运行时"中进行。 :)

通常会对常规代码"的常规代码进行处理。无论是对你还是其他人来说,都会导致难以调试的欺骗性代码。所以实现#doesNotUnderstand:并使用#respondsTo:等,通常是"糟糕的风格"除非你真的不得不做这些事情。

透明转发器对象的一个​​明显例子是OODBS的代理 - 但实际上并没有很多好的例子。

但要更准确地回答 - 在#doesNotUnderstand的实现中: - 只查询self respondsTo: aMessage selector(或类似)并根据该决定委托或不委托。