Messages to "super"
Ian Piumarta
ian.piumarta at inria.fr
Fri Aug 29 18:49:47 UTC 2003
On 29 Aug 2003, Martin Drautzburg wrote:
> I thought I knew it all, but ...
>
> I created the following class
>
> Object subclass: #Foo ...
> bar
> ^ self
>
> When I create aFoo and inspect it, it answers true to both
> self respondsTo: #bar and
> super respondsTo: #bar
"self" and "super" both refer to the receiver (so both refer to the _same
object_). Since the receiver responds to #bar, both of these anwser
correctly. The only difference between "self" and "super" is _where_ the
method _lookup_ begins. (Sends to self are lookuped up starting in the
class of the receiver -- even if that's below the method that's doing the
send because of inheritance; sends to super are looked up starting in the
_superclass_ of the class in which the _method_ [the one in which the send
to super is being performed] is _defined_, regardless of the actual
class of the receiver.)
It might help to add this method to the instance side of Foo
respondsTo: aSelector
^42
and then try the "inspector experiment" again. After 2 seconds' thought,
it'll "click".
> also I get Foo no matter if I ask
> self class or
> super class
>
> Can someone explain ?
"self" and "super" are the same object.
> The reason why I was doing this, was in a #mouseDown method I thoght
> I'd invoke mouseDown on super if it came from a mouse button I didn't
> handle. But since I didn't know if the superclass has a #mouseDown
> method at all I tried to figure that out by sending a #respondsTo:
If you know your superclass then you can just send it "canUnderstand:
aSymbol" directly.
Foo canUnderstand: #bar => true
Object canUnderstand: #bar => false
Hint: select the text "respondsTo:" and browse its implementors
(Command-M).
Ian
More information about the Squeak-dev
mailing list
|