Next: Instance methods, Previous: Documenting the class, Up: Creating classes
We have created a class, but it isn’t ready to do any work for us—we have to define some messages which the class can process first. We’ll start at the beginning by defining methods for instance creation:
Account class extend [ new [ | r | <category: 'instance creation'> r := super new. r init. ^r ] ]
The important points about this are:
Account class
means that we are defining messages which are
to be sent to the Account class itself.
<category: 'instance creation'>
is more documentation support; it says that the methods
we are defining supports creating objects of type
Account.
new [
and ending with ]
defined what action to take for the message new
.
When you enter this definition, GNU Smalltalk will simply
give you another prompt, but your method has been compiled in
and is ready for use. GNU Smalltalk is pretty quiet on successful
method definitions—but you’ll get plenty of error
messages if there’s a problem!
If you’re familiar with other Smalltalks, note that the body of the method is always in brackets.
The best way to describe how this method works is to step through it. Imagine we sent a message to the new class Account with the command line:
Account new
Account
receives the message new
and looks up
how to process this message. It finds our new definition, and
starts running it. The first line, | r |
, creates a local
variable named r
which can be used as a placeholder for
the objects we create. r
will go away as soon as the message
is done being processed; note the parallel with balance
, which
goes away as soon as the object is not used anymore. And note that
here you have to declare local variables explicitly, unlike what
you did in previous examples.
The first real step is to actually create the object.
The line r := super new
does this using a fancy trick.
The word super
stands for the same object that the message
new
was originally sent to (remember? it’s Account
),
except that when Smalltalk goes to search for the methods,
it starts one level higher up in the hierarchy than the current
level. So for a method in the Account class, this is
the Object class (because the class Account inherits from is
Object—go back and look at how we created the Account
class), and the Object class’ methods then execute some code
in response to the #new
message. As it turns out, Object
will do the actual creation of the object when sent a #new
message.
One more time in slow motion: the Account method #new
wants to do some fiddling about when new objects are created,
but he also wants to let his parent do some work with
a method of the same name. By saying r := super new
he
is letting his parent create the object, and then he is attaching
it to the variable r
. So after this line of code executes,
we have a brand new object of type Account, and r
is bound to it. You will understand this better as time
goes on, but for now scratch your head once, accept it as a
recipe, and keep going.
We have the new object, but we haven’t set it up correctly.
Remember the hidden variable balance
which we saw
in the beginning of this chapter? super new
gives us the
object with the balance
field containing nothing, but we want
our balance field to start at 0. 26
So what we need to do is ask the object to set itself up.
By saying r init
, we are sending the init
message to our new Account. We’ll define
this method in the next section—for now just assume that
sending the init
message will get our Account set up.
Finally, we say ^r
. In English, this is return what
r is attached to. This means that whoever sent to Account
the new
message will get back this brand new account. At
the same time, our temporary variable r
ceases to exist.
And unlike C, Smalltalk
draws a distinction between 0
and nil
. nil
is the nothing object, and you will receive an error if you
try to do, say, math on it. It really does matter that we
initialize our instance variable to the number 0 if we wish
to do math on it in the future.
Next: Instance methods, Previous: Documenting the class, Up: Creating classes