Homework due on Friday: Exercises 1 and 5 ppg. 73-74.
Homework due following Friday (9 February):
Exercises 4 and 6 page 101.
Creation and Initialization
Message Passing Syntax
Budd (and many others) consider message passing syntax
to be central to OOP. Others believe this is just one of
the choices of how to broker behavior among objects.
Object Pascal Message Passing
Message selector is dot.
Pass a message with arguments: x.moveTo (25, 37);
Pass a message with no arguments: x.flip;
Procedure methods can be used as statements,
function methods must be used in a context in
which a value of the returned type can be employed.
Pseudo variable self refers to the object
to whom the message has been passed:
if (self.suit = Heart) or (self.suit = Diamond) then ...
C++ Message Passing
Passing a message is usually referred to as
invoking a member function. This terminology
is indicative of the non-traditional OO status of
C++.
Dot is used as member data and member function (method)
selector.
Pass a message with arguments: x.moveTo (25, 37);
Pass a message with no arguments: x.flip ();
Pseudo-variable associated with the message receiver:
this pointer points to the object.
int Card::color ()
{
return (this->suit () % 2);
}
Use of member function of same class inside body of a
member function is equivalent to invocation upon the
same object
int Card::color ()
{
return (suit () % 2);
}
Smalltalk Message Passing
Juxtaposition is used to indicate message passing:
x flip
Message passing with Arguments uses colon terminated
message and parameter names: x moveTo: 25 and: 37
All Smalltalk operations and control structures are messages.
Thus, binary operations like + and *,
etc., are treated as special messages names passing a
single argument to the receiver object. Evaluation
of 3 + 4 sends the message `+'
with argument 4 to object 3.
Pseudo-variable self refers to the message recipient.
Objective-C Message Passing
Follows the smalltalk model, but encloses message
expressions within square brackets [ ... ]
Message passing with no arguments: [ x flip ]
Message passing with arguments:
[ x moveTo: 25 and: 36 ]
Only the message expression is enclosed in brackets:
y = [ x copy ];
Message expressions can be used in any program context
in which a value of the returned type can be employed.
Dylan, Message Passing, and Generic Functions
Dylan does not specifically support message passing.
Dylan uses generic functions to broker
behavior among objects.
In the traditional view, the object itself does not
embody the behavior provided by the method, the
method does. The object does, however, collect together
the methods that apply to it. This encapsulates
the behavior inside the object.
Dylan instead uses the module program unit
to encapsulate all the things that belong to a class or
group of classes, but uses generic functions
to associate behavior with specific objects.
A generic function is a description of the name and
parameter list of a family of functions.
define generic \+ (n1 :: , n2 :: ) => value :: ;
This states that the function named + (symbol
operators must be escaped with a \ in contexts where
they are not to be evaluated) always takes two
arguments and returns one
result.
Particular definitions of + can be defined for
more specific argument classes, but the arguments must
always be members of subclasses of <number>.
Consider this sequence of definitions:
define generic double (o ::
This sequence defines a generic function named double.
If it is applied to a number, it yields the result of
adding the number to itself. If it is appled to a
string it yields the result of concatenating the string
with itself.
When any call of the generic function is seen,
the most specific applicable method is used
to make the call. There is a single rule for determining
the most specific method. It involves sorting the
available methods accordint to the types of arguments
in the call. The functions sorted-applicable-methods
can be used to find out what method will be employed for
any call.
sorted-applicable-methods (double, 1);
sorted-applicable-methods (double, "abc");
Adding another method to double can show how this sorting
plays a role in determining behavior. Adding the following
method causes two possible methods to apply to an
integer, but only one method to apply to a real number.
define method double (i :: ) => ;
i + 1;
end method double;