在Squeak中,如何包装每个发送的方法? [英] In Squeak, how to wrap every method send?

查看:84
本文介绍了在Squeak中,如何包装每个发送的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个类,在该类中,我有一个方法"sendMessage:to:withArgs:",该方法接收一个对象,一条消息和一组参数. 该方法用于向对象发送消息并执行某种算法. 要使用此方法,我必须创建一个我创建的类的实例x,并执行x sendMessage:'+'到:'7'withArgs:'#(5)'的操作. 将消息"+"发送到带有参数5的对象7的结果,再加上我的算法所做的一些事情.但是我想要的是该算法将在每个方法调用中使用,这意味着7 + 5将调用我的"sendMessage:to:withArgs:". 我怎样才能做到这一点?或者至少,在发送给每个对象的每个方法中都有调用的东西吗?

I created a class, and in that class I have a method 'sendMessage: to: withArgs:' which recieves an object, a message and an array of arguments. The method is used to send messages to object and perform some algorithm. To use this method I have to create an instance x of the class I created and do something like x sendMessage: '+' to: '7' withArgs: '#(5)'. The result of this sending the message '+' to the object 7 with the parameter 5, plus some stuff that my algorithm does. But what I want is that the algorithm will be used in every method call, meaning 7+5 will call my 'sendMessage: to: withArgs:'. How can I do that? Or at least, Is there something called in each method sent to every object?

推荐答案

这很有趣,我们只是在Squeak irc频道中进行了讨论.也许看一下ObjectViewer.

It's kinda funny, we were just discussing that in the Squeak irc channel. Take a peek at ObjectViewer, perhaps.

在您的示例中,您想拦截发送到SmallInteger的消息.有趣的是,ObjectViewer几乎可以与每个类一起使用SmallInteger.

In your example, you want to intercept the message sends to a SmallInteger. Funnily enough, ObjectViewer works with very much every class BUT SmallInteger.

因此,要拦截发送到myObject的消息,请执行此操作.

So, to intercept message sends to myObject, do this.

创建类Intercepter,也许让它继承自ObjectTracer.更改dosNotUnder了解可为您服务的东西:

Create class Intercepter, let it inherit from ObjectTracer, perhaps. Change doesNotUnderstand to something that serves you:

doesNotUnderstand: aMessage
   "do Mojo to aMessage as you describe it"

然后,为了使您的工作顺利进行,请创建您的拦截器:

Then, to get your stuff going, create your Intercepter:

myIntercepter := Intercepter on: myObject.

然后

myObject become: myInterceptor.

这篇关于在Squeak中,如何包装每个发送的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆