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.

You c<strong>an</strong> now define the initialize method. In this case, the method is very simple as it merely sets<br />

the inst<strong>an</strong>ce variable age to 0 <strong><strong>an</strong>d</strong> name to a null string ''. To do this type in the following method<br />

definition into the code view window, replacing the method template displayed there.<br />

initialize<br />

"This method is used to initialize <strong>an</strong>y variables etc."<br />

self name: ''.<br />

self age: 0.<br />

Once you have typed in the method you c<strong>an</strong> accept it. When you do so, the system will inform you that<br />

the two methods age: <strong><strong>an</strong>d</strong> name: are undefined 8 . From the dialogs you should select the proceed<br />

option as we will define these methods later. Figure 8.3 illustrates the System Browser at this point.<br />

Figure 8.2: Defining a class comment<br />

8.3.2 The “accessing” protocol<br />

Now we will define the “accessing” protocol. Again this is a protocol name which is widely used within<br />

the system classes. If yo u adhere to the st<strong><strong>an</strong>d</strong>ard protocol names you will find it easier to follow the<br />

system classes (<strong><strong>an</strong>d</strong> other peoples classes). Define the new protocol as before <strong><strong>an</strong>d</strong> then define input the<br />

following method.<br />

age<br />

"This is <strong>an</strong> example accessor method"<br />

^age<br />

This is <strong>an</strong> example of <strong>an</strong>other feature of <strong>Smalltalk</strong> style, that is, if you write a method to return <strong>an</strong><br />

inst<strong>an</strong>ce variable’s value, you do not call it returnAge, instead you give it the same name as the inst<strong>an</strong>ce<br />

variable; in this case age. The system is able to det ermine whether you me<strong>an</strong> the inst<strong>an</strong>ce variable or<br />

the method from the way you call it. These methods are termed accessor methods.<br />

The next method will be called age: this illustrates <strong>an</strong>other <strong>Smalltalk</strong> style element in this<br />

example. This method updates <strong>an</strong> inst<strong>an</strong>ce variable. The method is therefore given the same name as the<br />

inst<strong>an</strong>ce variable but with a trailing colon. We do not call it setAge: or updateAge:. You may not like<br />

8<br />

Note that if you have objects in the system which define these messages, then VisualWorks will not tell you that they are<br />

undefined, even though they are undefined for Person!<br />

78

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

Saved successfully!

Ooh no, something went wrong!