Examples
deposit:50
withdraw:20
|
Examples of method invocation:
deposit: 50 and withdraw: 20
Sending deposit: 50 to the bank account causes a lookup for a slot named deposit:.
This locates the method of the same name. Execution of the method begins by
binding the formal argument, d, to the actual argument, 50, and by binding the
name self to the bank account object.
The deposit: method can be read as follows (we will explain the syntax and
semantics of Self in detail later):
- Send the message balance to self (i.e., the counter), to get the value of the
balance slot. Note that when a message is sent to self, we need not state the
receiver explicitly (unlike Smalltalk).
- Get the value of the argument, d (50).
- To the result of balance, send the message + with the result of d as argument.
This will return their sum.
- Send the message balance: to self, passing the result of balance+d as an
argument. This assigns the new value to balance. The result of the assignment
is self (i.e., the bank account object), not the assigned value.
If we made everything in deposit: as explicit as possible, it would look like this:
self balance: ((self balance) + d)
Sending withdraw: 20 is similar, except that before the final balance: message is sent,
the result of balance-w is passed as an argument to the max: message, which is sent
to 0. Here, the receiver of the message is explicit. Max: sent to a number with
another number as argument returns the larger of the numbers.
Fully parenthesized with explicit messages to self, withdraw: would be like this:
self balance: (0 max: ((self balance) - w))
|