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.

34.3 Classes <strong><strong>an</strong>d</strong> methods<br />

34.3.1 Defining a class<br />

The structure of a class definition is:<br />

Where<br />

NameOfSuperclass subclass: #NameOfClass<br />

inst<strong>an</strong>ceVariableNames: 'instVarName1 instVarName2'<br />

classVariableNames: 'ClassVarName1 ClassVarName2'<br />

poolDictionaries: ''<br />

category: 'Visual Org<strong>an</strong>iser'<br />

NameOfSuperclass is the parent class of the class to be defined.<br />

NameOfClass is the name of the class being defined. (Note that at this stage it does not exist <strong><strong>an</strong>d</strong><br />

must therefore be preceded by a #).<br />

inst<strong>an</strong>ceVariableNames is used to list the inst<strong>an</strong>ce variable to be used with this class.<br />

classVariableNames is used to define the class variable to be used with this class.<br />

poolDictionaries defines the pool variables to be used with this class.<br />

category defines the category in which this class will be placed.<br />

Note that this definition may not define all the variables associated wi th this class, for example, it may<br />

have inherited inst<strong>an</strong>ce variables from its parent.<br />

34.3.2 Methods<br />

The structure of a method is:<br />

Where<br />

messagePattern: argument1 additionalPattern: argument2 ....<br />

"comment"<br />

| temporaries |<br />

statements<br />

messagePattern <strong><strong>an</strong>d</strong> additionalPattern: represents the name of the method. Notice that<br />

the method name is divided up amongst the arguments <strong><strong>an</strong>d</strong> that a part of the message name<br />

which precedes <strong>an</strong> argument has a trailing colon ‘:’.<br />

arguments the names of arguments in the message pattern are accessible within the method.<br />

“comment” is a comment describing the operation performed by the method <strong><strong>an</strong>d</strong> <strong>an</strong>y other useful<br />

information.<br />

| temporaries | 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 />

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

implement the behaviour of the method.<br />

34.3.3 Message expressions<br />

Message expressions describe messages to receivers. The value of the expression is determined by the<br />

method it invokes. For example, the following is a message expression:<br />

newStatus := thisPerson marries: thatPerson<br />

This expression is made up of <strong>an</strong> assignment <strong><strong>an</strong>d</strong> a message exp ression. In turn the message expression<br />

is made up of a receiver, a message selector <strong><strong>an</strong>d</strong> <strong>an</strong> argument:<br />

• thisPerson is the receiver<br />

287

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

Saved successfully!

Ooh no, something went wrong!