overridding without subclassing examples

Matthew Fulmer tapplek at gmail.com
Wed Sep 19 00:17:41 UTC 2007


On Tue, Sep 18, 2007 at 03:14:15PM -0700, Brad Fuller wrote:
> Is there a way to override an instance method w/o subclassing?
> For instance, class Morph has a method that returns true, but I want the that 
> method to return false when instancing some types of Morphs and others not. 
> (there is no setter/getter for this method.) Is there a way to do that w/o 
> adding a setter/getter to Morph or subclassing the different Morphs?

Your question is pretty vague

I imagine you see something like

isMorph
    ^ true

in class morph, but you want some (or all?) morphs to instead
use

isMorph
    ^ self isSomethingWeirdHappening not

First you can redefine the method in the appropriate class(es). If you
want to put this change in a MC package, move it to category

*MyPackage-override

and this method will be stored in the package, and will override
the existing method when loaded.

Package-level overrides are not recommended, but they are available

-- 
Matthew Fulmer -- http://mtfulmer.wordpress.com/
Help improve Squeak Documentation: http://wiki.squeak.org/squeak/808



More information about the Squeak-dev mailing list