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.

7.5.3 true, false <strong><strong>an</strong>d</strong> nil variables<br />

These variables represent the two boole<strong>an</strong> states, true <strong><strong>an</strong>d</strong> false <strong><strong>an</strong>d</strong> a null value (referred to as nil). This<br />

should not be confused with the null pointer in l<strong>an</strong>guages such as C. It really me<strong>an</strong>s nothing or no value.<br />

The three variables are:<br />

• nil The single inst<strong>an</strong>ce of class Undefined <strong>Object</strong> (the non-value).<br />

• true Represents truth. It is the only inst<strong>an</strong>ce of class True.<br />

• false represents falsehood. Which is the only inst<strong>an</strong>ce of the class False.<br />

True <strong><strong>an</strong>d</strong> False are subclasses of<br />

structures.<br />

Boole<strong>an</strong> which implements boole<strong>an</strong> algebra <strong><strong>an</strong>d</strong> control<br />

7.6 Messages <strong><strong>an</strong>d</strong> message selectors<br />

7.6.1 Message expressions<br />

Message express ions describe messages to receivers. They are composed of a receiving object (the<br />

receiver), the message selector (which indicates which method to execute) <strong><strong>an</strong>d</strong> zero or more parameters.<br />

Figure 7.2 illustrates the main components of a message expression.<br />

A message expression<br />

Parameters<br />

<strong>an</strong>AddressBook addAddress: 'C47, UWA' for: 'John Hunt'.<br />

Receiver<br />

Message selector<br />

Figure 7.2: The components of a message expression<br />

The value of the expression is determined by the method it invokes. For example, in the f ollowing<br />

example, the result returned by the method marries: is saved into the variable newStatus.<br />

7.6.2 Message types<br />

newStatus := thisPerson marries: thatPerson.<br />

There are three different forms which a message c<strong>an</strong> take. From the programmers point of view they are<br />

essentially the same. They are only distinguished by the format they take <strong><strong>an</strong>d</strong> by the precedence they<br />

have. The following description explains each of the three types of message <strong><strong>an</strong>d</strong> states their precedence.<br />

The only exception to this is that parenthesized expressions take precedence over all message<br />

expressions.<br />

Messages without <strong>an</strong>y arguments are termed Unary Messages, for example:<br />

#($a $b 23 'john') size.<br />

13 odd.<br />

Time now.<br />

Messages with a single argument, where the selector is composed of one or two non -alph<strong>an</strong>umeric<br />

symbols are termed Binary Messages. They have a higher precedence th<strong>an</strong> keyword message s, but a<br />

lower precedence th<strong>an</strong> unary messages. Examples of binary messages include:<br />

a >= b. 24 * 7. recordCount + 1.<br />

The second character in a binary message selector c<strong>an</strong>not be a minus sign.<br />

73

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

Saved successfully!

Ooh no, something went wrong!