Message Sending:
Sometimes it is useful to send messages to more than one object. Or you may need
to send an object a message but you don't know the name of the object ahead of
time. For instance, you may create a procedure that interacts with an object,
but you don't know the name of the object ahead of time.
Talkto:
The Talkto procedure allows you to talk to a specific object. Similar to
methods, we will use the thisclass procedure to tell the object
what commands we want it to execute. Suppose we have a class called Rectangle
that has four properties: Height, Width, XPos, and YPos. The Rectangle class
also has two methods: Draw, which will draw the rectangle and Erase, that will
erase the rectangle.
? DefineClass "Rectangle
? DefineProperty "Rectangle "Height
? DefineProperty "Rectangle "Width
? DefineProperty "Rectangle "XPos
? DefineProperty "Rectangle "YPos
? DefineMethod "Rectangle "Draw [[] [localmake "Xcor Xcor] [localmake "Ycor
Ycor] [ pu setxy thisRectangle.Xpos thisRectangle.Ypos pd] [repeat 2 [ fd
thisRectangle.Height rt 90 fd thisRectangle.Width rt 90]] [pu setxy :Xcor :Ycor
pd]]
? DefineMethod "Rectangle "Erase [[] [localmake "Xcor Xcor] [localmake "Ycor
Ycor] [ pu setxy thisRectangle.Xpos thisRectangle.Ypos pd pe] [repeat 2 [ fd
thisRectangle.Height rt 90 fd thisRectangle.Width rt 90]] [pu setxy :Xcor :Ycor
pd ppt]]
Now we will create 3 Rectangle Objects: R1, R2, and R3
? newRectangle "R1
? newRectangle "R2
? newRectangle "R3
Next, we will set the properties of the rectangles.
? R1.SetXPos 0
? R1.SetYPos 0
? R1.SetHeight 100
? R1.SetWidth 120
? R2.SetXPos 100
? R2.SetYPos 100
? R2.SetHeight 50
? R2.SetWidth 75
? R3.SetXPos -100
? R3.SetYPos -120
? R3.SetHeight 125
? R3.SetWidth 90
Now we have our rectangle objects created, and properties for them set, let's
use the Talkto procedure to tell each rectangle to draw. The Talkto procedure
set's the global object (:this), so any thisclass convention will
be sent to the global object. (There is also a local object also called (:this)
so our methods and properties will work as expected.) The TalkTo procedure
takes one argument: the object you wish to talk to.
? TalkTo "R1
? thisRectangle.Draw
Draws a rectangle 100 by 120 at Pos 0,0
? print thisRectangle.Height
100
? TalkTo "R2
? thisRectangle.Draw
Draws a rectangle 50 by 75 at Pos 100,100
? print thisRectangle.Height
50
? Talkto "R3
? R3.Draw
Draws a rectangle 125 by 90 at Pos -100,-120
? print thisRectangle.Height
125
Finally, as I mentioned aboved there is a global object which is stored in the
variable :this. We can use :this to see what the current object is.
? print :this
R3
Tell:
The Tell procedure takes two arguments, the name of an object and a list of
commands. The Tell procedure will perform the list of commands with the given
object. The difference between Tell and TalkTo is that Tell does not change the
global object. Tell modifies the local object (also called :this). The
difference between the two is not so much a trick of the Object Oriented
Framework, but the dynamic scoping rules of Logo itself. Again we will use the thisclass
convention to tell the current object what commands to run.
Let's tell R2 to draw itself.
? Tell "R2 [ thisRectangle.Draw ]
Draws a rectangle 50 by 75 at Pos 100,100
? print :this
R3
Notice that the global object (:this) is still R3. To illustrate what is going
on let's enter the following commands.
? TalkTo "R3
? print se [Before Tell :this equals ] :this tell "R2 [ print se [Inside Tell
:this equals] :this] print se [After Tell :this equals] :this
Before Tell :this equals R3
Inside Tell :this equals R2
After Tell :this equals R3
In final section we will look at Getters and Setters.
|