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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Figure 8.1: Defining a new class<br />

8.2.2 Defining a class comment<br />

The next thing you should do is to define the class comment. It is generally a good idea to do this, but if<br />

you wish to omit it, it will not effect how your class operates. If you do wish to add a comment then<br />

select the comment option off the menu presented from the right mouse button (middle on a three<br />

button mouse) in the class window. The comment I have defined is illustrated in Figure 8.2. Once you<br />

have typed in the text of your comment you must “accept it”. This c<strong>an</strong> be done from the right mouse<br />

button (middle button on a three button mouse).<br />

This is a good example of some common uses of the class comment. Note tha t I have not only<br />

specified the intention of the class, what the inst<strong>an</strong>ce variable of the class are <strong><strong>an</strong>d</strong> what they take, but<br />

also how the class might be used.<br />

8.3 Defining methods<br />

Now you are ready to start defining methods for the class. The first method you s hould define is one<br />

which will be used to initialize the inst<strong>an</strong>ce’s state (i.e. initialize the inst<strong>an</strong>ce variables). Traditionally,<br />

methods such as initialize, which are not intended for general use, are placed in a method protocol<br />

called “initialize-release”. If you look at the methods defined in the system classes you will note that<br />

similar names are used for protocols possessing similar types of methods, “initialize -release” is <strong>an</strong><br />

example of such a protocol name. You should therefore place the method initialize within the<br />

protocol private.<br />

8.3.1 The “initialize-release” protocol<br />

To create the initialize method, you will first have to create the message protocol in which to place the<br />

method definition. To do this, you use the add option off the right button menu (middle if you have a<br />

three button mouse) in the message protocol window. This is the third window along in the System<br />

Browser. This causes a dialog to be displayed requesting the name of the new protocol. This will either<br />

be bl<strong>an</strong>k or contain the name of the last protocol you visited. If it is not initialize-release, then type in<br />

“initialize-release”. Again the new protocol will be either placed at the end of the list of protocols or it<br />

will be placed above the protocol currently selected (if one is selected).<br />

You will then be presented with a new message protocol in the message protocol window. This<br />

protocol will be selected <strong><strong>an</strong>d</strong> the following template will be displayed in the code window:<br />

message selector <strong><strong>an</strong>d</strong> argument names<br />

"comment stating purpose of message"<br />

| temporary variable names |<br />

statements<br />

77

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

Saved successfully!

Ooh no, something went wrong!