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.

4.5.2.2 System Tr<strong>an</strong>script<br />

The System Tr<strong>an</strong>script is a text window which has the additional property of supporting display<br />

operations from expressions as they are executed. It c<strong>an</strong> be referred to using the name Tr<strong>an</strong>script. In<br />

effect, it is the output window of the VisualWorks system. It is where the system itself reports<br />

import<strong>an</strong>t information such as when the image was last saved etc.<br />

It is also very useful as a quick way of outputting information, for example by placin g trace<br />

statements within code to see exactly what is happening or for displaying the results of computations<br />

which do not require sophisticated presentation. For example:<br />

Tr<strong>an</strong>script show: 'John'.<br />

You c<strong>an</strong> do this <strong>an</strong>ywhere within <strong>an</strong>y part of your code because Tr<strong>an</strong>script is actually a global<br />

variable <strong><strong>an</strong>d</strong> <strong>an</strong> inst<strong>an</strong>ce of a class called TextCollector. The Tr<strong>an</strong>script (<strong><strong>an</strong>d</strong> other<br />

TextCollectors) respond to the message:<br />

show aString<br />

5<br />

Other useful messages understood by the Tr<strong>an</strong>script include: cr ( starts a new line in the<br />

Tr<strong>an</strong>script.), space (puts a single space in the Tr<strong>an</strong>script) <strong><strong>an</strong>d</strong> tab (puts a ‘tab’ in the Tr<strong>an</strong>script).<br />

The Tr<strong>an</strong>script only knows how to print strings. Therefore, to make it easy to print <strong>an</strong>ything in the<br />

Tr<strong>an</strong>script, you c<strong>an</strong> use a me ssage called printString. All objects underst<strong><strong>an</strong>d</strong> this message (as it is<br />

implemented in the class <strong>Object</strong>). When sent to <strong>an</strong> object, the result is a printable string which<br />

represents the object. The result will be different depending on th e class of the object, but you are<br />

guar<strong>an</strong>teed something you c<strong>an</strong> print. The way to use printString is:<br />

Tr<strong>an</strong>script show: some<strong>Object</strong> printString<br />

Type the following into the Workspace (this is the window at the bottom right of Figure 4.3, more<br />

information on this window is provided in the next chapter). Once you have typed it in, select it with the<br />

6<br />

left mouse button. Now use the right mouse button to bring up the window menu . This menu has a<br />

number of options on it, half way down you will see <strong>an</strong> option called do it. Select this option. The code<br />

will then be executed <strong><strong>an</strong>d</strong> the results will be printed in the Tr<strong>an</strong>script.<br />

Tr<strong>an</strong>script show: 'Hello World'.<br />

Tr<strong>an</strong>script show: (3 + 4) printString.<br />

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

You have now written your first piece of <strong>Smalltalk</strong>. This illustrates <strong>an</strong> import<strong>an</strong>t point, that with<br />

<strong>Smalltalk</strong> you will get the most out of <strong>an</strong>y book or course by trying things out. So try things out;<br />

explore, be adventurous; it is the quickest way that you will learn.<br />

4.5.3 The System Browser<br />

The System Browser allows the user to inspect the definition of <strong>an</strong>y object in the system, <strong><strong>an</strong>d</strong> to modify<br />

it if required. More th<strong>an</strong> one browser c<strong>an</strong> be displayed simult<strong>an</strong>eously (indeed it is oft en useful to have<br />

a number of browsers open at the same time so that different classes c<strong>an</strong> be considered at the same<br />

time).<br />

The System Browser is made up of five subviews (or windows) <strong><strong>an</strong>d</strong> two complementary items<br />

marked inst<strong>an</strong>ce <strong><strong>an</strong>d</strong> class. By default, the inst<strong>an</strong>ce item is selected; this me<strong>an</strong>s that the messages<br />

displayed are the ones sent to inst<strong>an</strong>ces of a class rather th<strong>an</strong> to the class itself. Note that each of the<br />

subviews has <strong>an</strong> independent scroll bar. The System Browser is illustrated in the middle of Figure 4.3.<br />

In the figure, the object class has been selected (this is indicated in the second window across the top).<br />

From left to right, the top four p<strong>an</strong>es in the System Browser are :<br />

5<br />

This is <strong>an</strong> example of <strong>Smalltalk</strong> terminology. Rather th<strong>an</strong> say that some procedure has been defined for a n object, <strong>Smalltalk</strong>ers<br />

say that it underst<strong><strong>an</strong>d</strong>s it.<br />

6<br />

This assumes that you are using a two button mouse, for example on a PC. If your mouse has only one button please refer to<br />

the VisualWorks system m<strong>an</strong>uals to find out what the appropriate key sequence is to mimic the second <strong><strong>an</strong>d</strong> / or third mouse<br />

button. If you have a three button mouse then the middle button is the equivalent of the right button on a two button mouse.<br />

50

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

Saved successfully!

Ooh no, something went wrong!