简体   繁体   English

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

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

Suppose we have oneInstance and secondInstance , one of SomeClass and one OtherClass with the example Class hierarchy below: 假设我们有一个oneInstancesecondInstanceSomeClass和一个OtherClass之一,下面是示例类层次结构:

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

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

Is it possible, at runtime, to change oneInstance so that it's "super" message sends reach secondInstance. 是否可以在运行时更改oneInstance,以便它的“超级”消息发送到达secondInstance。

oneInstance and secondInstance merge essentially making oneInstance work as if they are one object and the structure appear as if they were instantiated from something like and: oneInstance和secondInstance 合并本质上使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)

The simplest would be if I could assign super := secondInstance on oneInstance for a bit and then change it back :D 最简单的是如果我可以在一个super := secondInstance上分配super := secondInstance ,然后再将其改回:D

PS. PS。 Essentially we are reclassing oneInstance by having secondInstance as it's "super" they are now one object with the state of both presuming oneInstance is subclassed from Object with no other state but it's own. 本质上,我们通过使secondInstance为“ super”来对其进行重分类,因为它们现在是一个具有两个状态的对象,假定oneInstance是从Object继承而来的,而没有其他状态,而是它自己的。 Mostly a hack to use the inheritance chain's default method lookup to my advantage. 多数情况下,利用继承链的默认方法查找对我有利。 Closest thing I could find is Object Slicing https://en.wikipedia.org/wiki/Object_slicing 我能找到的最接近的东西是对象切片https://en.wikipedia.org/wiki/Object_slicing

Another way to look at it would be this: 另一种查看方式是:

secondInstance is receiving messages, it's an instance of OtherClass, all is well. secondInstance正在接收消息,它是OtherClass的一个实例,一切都很好。 Some of the messages it receives are not in OtherClass so method lookup goes up the inheritance chain to SomethingClass and then to Object, ProtoObject, etc. and finally they should be forwarded to the other instance. 它接收到的某些消息不在OtherClass中,因此方法查找沿继承链上升到SomethingClass,然后到达Object,ProtoObject等,最后应将它们转发到另一个实例。 This process should be entirely transparent. 这个过程应该是完全透明的。

First of all, in Pharo and Squeak (and most Smalltalks) you are always at runtime. 首先,在Pharo和Squeak(以及大多数Smalltalks)中,您总是在运行时。 So obviously, if its possible to do something, its possible to do it at "runtime". 显然,如果有可能做某事,就可以在“运行时”做某事。 :) :)

Generally messing around with meta capabilities for "regular code" leads to really deceptive code that is hard to debug, both for you and especially others. 通常,搞乱“常规代码”的元功能会导致欺骗性很强的代码,无论是对您还是其他人,都很难调试。 So to implement #doesNotUnderstand: and using #respondsTo: etc, is normally "bad style" unless you REALLY have to do these things. 因此,除非确实需要执行这些操作,否则实现#doesNotUnderstand:并使用#respondsTo:等通常是“不良样式”。

An obvious example for transparent forwarder objects is proxies for OODBS - but there really aren't many good examples. 透明转发器对象的一个​​明显示例是OODBS的代理-但实际上并没有很多好的示例。

But to answer more precisely - in your implementation of #doesNotUnderstand: - just query if self respondsTo: aMessage selector (or similar) and make your decision to delegate or not based on that. 但是要更精确地回答-在您实现#doesNotUnderstand的过程中:-仅查询self respondsTo: aMessage selector (或类似的self respondsTo: aMessage selector ),然后根据该决定来决定是否委托。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM