| www.delorie.com/gnu/docs/smalltalk/gst_61.html | search |
![]() Buy GNU books! | |
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
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 methodsFor: 'instance creation'!
new
| r |
r := super new.
r init.
^r
! !
|
Again, programming your editor to do this is recommended. The important points about this are:
Account class means that we are defining messages which are
to be sent to the Account class itself.
methodsFor: 'instance creation'
is more documentation support; it says that all of the methods
defined will be to support 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!
This is also the first example where we've had to use
more than one statement, and thus a good place to present
the statement separator--the . period. Like Pascal, and unlike C,
statements are separated rather than terminated. Thus you
need only use a . when you have finished one statement
and are starting another. This is why our last statement,
^r, does not have a . following. Once again like
Pascal, however, Smalltalk won't complain if your enter a spurious
statement separator after the last statement.
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.
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. (24)
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.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
| webmaster donations bookstore | delorie software privacy |
| Copyright © 2003 by The Free Software Foundation | Updated Jun 2003 |