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.

“Now calculate y “<br />

y := x * 23.<br />

“<br />

The <strong>Smalltalk</strong> compiler would read this as a comment , followed by the code Now calculate y ,<br />

followed by <strong>an</strong>other comment. This is (almost) certainly going to cause <strong>an</strong> error.<br />

The | temporaries | format is used to define variables which are local to the method. They must be<br />

declared at the beginning of the method (just after the message pattern) <strong><strong>an</strong>d</strong> are initially nil.<br />

The statements represents <strong>an</strong>y le gal set of <strong>Smalltalk</strong> statements. These statements are used to<br />

implement the behavior of the method.<br />

One of the uses of methods is in providing <strong>an</strong> interface between <strong>an</strong> object’s internal data <strong><strong>an</strong>d</strong> the<br />

outside world. Such methods are often termed accessor methods. Such a method retrieves the value of<br />

<strong>an</strong> inst<strong>an</strong>ce variable <strong><strong>an</strong>d</strong> makes it available to other objects. For example, the class Employee has two<br />

inst<strong>an</strong>ce variables age <strong><strong>an</strong>d</strong> name. A method implemented in Employee returns the age of <strong>an</strong><br />

employee. Thus in response to the message age, this method would be executed <strong><strong>an</strong>d</strong> the value of the<br />

employee’s age returned to the object sending the message.<br />

In this situation the employee’s age is held explicitly. An equally valid internal representation for <strong>an</strong><br />

Employee would be to have <strong>an</strong> inst<strong>an</strong>ce variable dateOfBirth. The method age, would now need<br />

to take the date of birth away from the current date, in order to obtain the employees' age.<br />

Note that this would be a ch<strong>an</strong>ge to the implementation of Employee, but there would be no visible<br />

ch<strong>an</strong>ge as far as <strong>an</strong>y other object in the system is concerned. This illustrates the encapsulation possible<br />

within <strong>Smalltalk</strong> (<strong><strong>an</strong>d</strong> other OOP l<strong>an</strong>guages).<br />

6.4.1 The ^ (or return) operator<br />

Once a method has finished executing, <strong>an</strong> <strong>an</strong>swer is ret urned to the sender of the message. By default<br />

the object returned is the receiver itself (i.e. self). However, other objects c<strong>an</strong> be returned by use of a<br />

return expression - <strong>an</strong> expression preceded by <strong>an</strong> up arrow (^). The return expression must be the last<br />

expression executed in a method. This does not me<strong>an</strong> that it must be the last expression in the method,<br />

merely that it is the last executed. For example:<br />

C Version<br />

<strong>Smalltalk</strong><br />

if (x == y) (x = y)<br />

return x;<br />

ifTrue: [^x]<br />

else<br />

return y;<br />

ifFalse: [^y].<br />

In this case, either the value of x or y will be returned depending upon whether x <strong><strong>an</strong>d</strong> y are equal or<br />

not.<br />

6.4.2 An example method<br />

Let us compare a procedure definition in a l<strong>an</strong>guage such as C with the <strong>Smalltalk</strong> equivalent. We will<br />

assume that we wish to define a procedure to take in a number, add 10 to it <strong><strong>an</strong>d</strong> return the result.<br />

int myAdd (int x)<br />

myAdd: aNumber<br />

{ int result; | result |<br />

result = x + 10; result := aNumber + 10.<br />

return result;<br />

^result.<br />

}<br />

From this example you will see that although the format is different you should soon be able to get<br />

used to it. Let us look at some of the constituent parts of the method definition. The method name (<strong><strong>an</strong>d</strong><br />

its message selector) is myAdd: . Note that because this meth od takes a parameter, the method name<br />

must have a trailing colon. It has one parameter called aNumber. Just as in <strong>an</strong>y other l<strong>an</strong>guage, this<br />

parameter variable is limited to the scope of this method. The method also defines a temporary variable<br />

(result) which is also limited to the scope of this method.<br />

Variable names are identifiers containing only letters <strong><strong>an</strong>d</strong> numbers which must start with a letter.<br />

Some examples are:<br />

67

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

Saved successfully!

Ooh no, something went wrong!