March 12, 2010, 5:27 pm
M206 Computing: An Object-oriented Approach
Smalltalk Messages
Objects respond to messages in their protocol and, broadly speaking,
can be categorised as follows. N.B. h1 and h2 are two variables that reference
instances of the HoverFrog class.
- A message that changes the state of an object e.g. right
- A message that gets an object to do something but doesn't cause a change
in state e.g. jump
- A message that retrieves some data from an object e.g. colour
- A message that requires collaboration, and hence further message sending,
between objects e.g. sending the message sameColourAs: h2
to h1, results
in h1 sending the message colour
to h2.
h2 returns a
message answer to
h1 e.g. Green. This message answer is a reference
to the object representing
the value of the colour attribute of h2. h1
takes this value and passes
it as the argument to the keyword colour: and sends itself the message
colour: Green
The name of a message is called the message selector or selector e.g.
colour, left or sameColourAs:
There are three kinds of message selector.
- Keyword messages, which require arguments (parameters).
h1 position: 2.
position: is the keyword selector and 2 is the argument
h1 hover: Up by: 4
hover:by: is the keyword selector requiring two arguments, in this case
Up - a reference to the string 'up'- and 4
- Unary messages, which do not take an argument
h2 colour.
colour is the unary selector
- Binary messages, which take one argument and have a message
selector that is non-alphanumeric. Using numbers, strings and Booleans
as an example:
1 + 1.
+ is the binary selector and 1 is the argument
'Hey ' , 'Joe'.
, is the binary selector and 'Joe' is the argument
true | false
| is the binary selector and false is the argument.
All messages sent to objects result in the receiver returning a message
answer. This message answer is a reference to an object and may or may
not be of some use.
Next page » Assignment
Previous page « Smalltalk Variables