29.11.2014 Views

Smalltalk and Object Orientation: an Introduction - Free

Smalltalk and Object Orientation: an Introduction - Free

Smalltalk and Object Orientation: an Introduction - Free

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Figure 12.3: Defining the initialize method in the Org<strong>an</strong>izer class<br />

12.5 A class side method<br />

We shall now do something we have not yet done. We shall define a class method. To do this, first<br />

select the class radio button below the class list window. You will n ow be on the class side of the<br />

Org<strong>an</strong>izer class. You should see that the code view window has ch<strong>an</strong>ged <strong><strong>an</strong>d</strong> now indicates the class<br />

inst<strong>an</strong>ce definition. You will also see that your initialize-release protocol <strong><strong>an</strong>d</strong> initialize message<br />

have disappeared. This is b ecause you are now on the class side <strong><strong>an</strong>d</strong> they are part of the inst<strong>an</strong>ce side<br />

definitions.<br />

We are now going to define a class message protocol. Just as before, to do this you must first define<br />

the protocol name. You do this using the right button menu in the protocol window. Call the new<br />

protocol “inst<strong>an</strong>ce creation ”. Now define a method in the code view window called new. Follow the<br />

example illustrated below exactly.<br />

new<br />

^super new initialize<br />

Note you must make sure th at you include the return symbol “^”, otherwise you will get the class<br />

Org<strong>an</strong>izer returned in response to the message new, rather th<strong>an</strong> the new inst<strong>an</strong>ce you w<strong>an</strong>ted. The<br />

main purpose of this method is to make sure that the inst<strong>an</strong>ce method initialize is sent to the new<br />

inst<strong>an</strong>ce of <strong>an</strong> Org<strong>an</strong>izer, before <strong>an</strong>ything else happens. This is a common use for redefining new, i.e.<br />

doing something before <strong>an</strong>ything else c<strong>an</strong> get its h<strong><strong>an</strong>d</strong>s on it. The System Full Browser (available with<br />

the adv<strong>an</strong>ced toolkit) is illustrated in Figure 12.4. This figure presents the state of the system after the<br />

class side method has been defined.<br />

Figure 12.4: Defining the class side new method<br />

106

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!