Objects are instances of a particular class. The messages that an object can respond to is defined in the protocol of its class. How messages are executed or implemented is defined in the class methods. Methods give the implementation details for the messages and represent a class behavior.
Before going any further, let's take a look on how objects and messages are named.
A message such as "give me your name" could be "giveMeYourName". This however, is too difficult to work with. It would be easier to shorten it to just "name".
A message that passes information or parameters, such as "set the name to this name", is handled similarly, except that the method name must end with a colon (:). So, The message name becomes "name:". These types of messages are called keyword messages. Messages that have no information to pass are unary messages.
A message can contain multiple arguments. There has to be a keyword for every argument in the message. For example, it is possible to set both the name and the address of a student with one message. This requires a message name with two keywords such as "name:address:".
There are three types of messages: unary, binary and keyword .
x sin "return the result of sin(x)" Date tomorrow "answer a new instance of class Date" 5 factorial "return the factorial of 5" `hi' outputToPrinter "Send the string `hi' to the printer"In these examples `x', `Date', `5', and `hi' are the objects and `sin', `tomorror', `factorial', and `outputToPrinter' are the message names.
+ / \ * ~ < > = @ % | & ? ! ,The following are examples of Smalltalk expressions using binary messages:
a + b "returns the result of sum a and b" a | b "returns the result of "ORing" a with b" a >= b "compares to see if a is greater than or equal to b and returns either true or false"The first example can also read as "the message `+' is sent to the object `a' with a parameter `b'."
AnObject aMessage: parameterThe colon is a required part of the message name.
When there is more than one parameter, a message name must appear for each parameter.
For example:
AnObject aName1: parameter1 aName2: parameter2Here:
Array new: 20 "The message new: is sent to the object Array with parameter 20" TheDate month: currentMonth year: currentYear "Set the private data for object TheDate to month = currentMonth and year = currentYear" Student name: `Steve' address: `Raleigh, "Set the appropriate variables in the Student object according to the parameters"
For example:
AnObject aMessageFor messages that need to pass arguments to the receiving object, such as a keyword message, the receving object's name is followed by the message name and its argument. The arguments are separated by a blank space.
AnObject aNumber: 1 aName: `John'
A variable in Smalltalk contains the identifier of the object, not the object itself. In other words, a variable points to an object. When the receiver of a message is a variable, Smalltalk uses the contents of the variable to identify the object. For example, assume that there is a global variable called MyStudent that points to a Student object. The global variable, MyStudent can now be the receiver of the following messages:
MyStudent name: `Steve' MyStudent name
For example:
Student new
Every class automatically supports the new message. The new message then returns a pointer to the new instance.
In the following example, the global variable MyStudent points to a new Student object:
MyStudent := Customer new
Once the statement above has been executed, messages can be sent to MyStudent, which points to an instantiation of a Student object.
For example:
MyStudent name: `Steve' MyStudent name
MyStudent name: `Steve' address: `Raleigh, NC'.The statement sends the message name:address: to the Student object pointed to by the variable MyStudent. Rather than duplicating the code that already exists in the name: and address: methods, the method name:address: is implemented to have name: and address: perform the task. Since name: and address: are methods of the same object as name:address:, there must be a way for a method to refer to the object in which it exists.
That is the purpose of self. A method can access other methods in its own object by specifying self as the receiver of the message.
Let's look at the code for all three of these methods:
name: aName name := aName address: anAddress address := anAddress name: aName address: anAddress self name: aName. self address: anAddressThe use of self causes Smalltalk to send the name and address messages to the current instance of Student, the same instance that received the name:address: message.
In the example, the methods name: and address: defined their temporary variables with the same identities that are found in name:address:. There is no relationship between these names and they do not have to be the same. The scope of a method's temporary variables are local only to that method. For example, the name: mehtod could also look like this:
name: studentName name := studentNameThe name:address: method could choose to directly set the variables instead of using the lower-level methods of name: and address:. This would appear as follows:
name: aName address: anAddress name := aName address := anAddressIt is better, however, to centralize the setting of a variable inside a single method. The same is true for getting the contents of a variable.
Go to Chapter 3: Smalltalk Statements
Return to Chapter 1: Introduction to IBM Smalltalk Programming