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.

1. inst<strong>an</strong>ce variables - discussed above<br />

2. class variables - will be discussed later in the book<br />

3. class inst<strong>an</strong>ce variables - will also be discussed later in the book<br />

4. temporary variables - see below<br />

5. global variables- see below<br />

7.5.1.1 Temporary variables<br />

These exist only for the duration of some activity (e.g. the execution of a method). They are denoted by<br />

being placed between two bars, e.g. | x y z | indicates that the variables x, y <strong><strong>an</strong>d</strong> z are temporary<br />

variables. Try typing the following into the Workspace window:<br />

| x y z|<br />

x := 5.<br />

y := 6.<br />

z := x + y.<br />

Tr<strong>an</strong>script show: z printString.<br />

Now select all the text you typed in. Now use the right mouse button (middle if you have a three<br />

button mouse) <strong><strong>an</strong>d</strong> select the do it option on the menu. You should now see the value 11 printed in your<br />

Tr<strong>an</strong>script window.<br />

7.5.1.2 Global variables<br />

These are shared by all inst<strong>an</strong>ces of all classes. These c<strong>an</strong> be useful during testing if you w<strong>an</strong>t to keep<br />

hold of a particular object. They always start with a capital letter <strong><strong>an</strong>d</strong> c<strong>an</strong> be deleted by looking at the<br />

<strong>Smalltalk</strong> system dictionary. This c<strong>an</strong> be accessed from the global variable <strong>Smalltalk</strong> (you c<strong>an</strong> use the<br />

inspect message to examine the contents of the variable <strong>Smalltalk</strong>). To delete a global variable from the<br />

current system, use:<br />

<strong>Smalltalk</strong> removeKey: #.<br />

It is interesting to note that class names start with a capital letter <strong><strong>an</strong>d</strong> are therefore actually global<br />

variables!<br />

7.5.2 Pseudo variables<br />

There are a number of pseudo variable s in <strong>Smalltalk</strong> which c<strong>an</strong> be referenced within a method. A<br />

pseudo variable is a special variable, whose value is ch<strong>an</strong>ged by the system but whi ch c<strong>an</strong>not be<br />

ch<strong>an</strong>ged by the programmer. That is, the value of these variables is determined by the current context<br />

within which the code around it is executing. There are two such pseudo variables; they are self <strong><strong>an</strong>d</strong><br />

super:<br />

self This refers to the receiver of a message itself. When a message is received, the search for the<br />

corresponding method starts in the class of the receiver. It is the way in which a method in one<br />

object c<strong>an</strong> initiate the execution of <strong>an</strong>other method in that same object.<br />

super Also refers to the message receiver, but the method search starts in the superclass of the class in<br />

which super is used. This is often used if the functionality of a method is to be extended rather<br />

th<strong>an</strong> overwritten. For example:<br />

myMethod: <strong>an</strong><strong>Object</strong><br />

new code before super.<br />

super myMethod.<br />

new code after super.<br />

Don’t worry about the syntax or the me<strong>an</strong>ing of this at the moment just make sure you get the idea<br />

of things.<br />

72

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

Saved successfully!

Ooh no, something went wrong!