Smalltalk and Object Orientation: an Introduction - Free
Smalltalk and Object Orientation: an Introduction - Free
Smalltalk and Object Orientation: an Introduction - Free
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
The result of sending this method is illustrated in Figure 6.4 along with the structure of a class. The<br />
class Person receives the message new which causes the cl ass method new to execute which<br />
generates a new inst<strong>an</strong>ce of the class, with its own copy of the inst<strong>an</strong>ce variables age <strong><strong>an</strong>d</strong> name.<br />
Class: MyClass<br />
Inst<strong>an</strong>ce Variables<br />
age, name<br />
Class variables<br />
numberOfInst<strong>an</strong>ces<br />
new<br />
MyClass new.<br />
Inst<strong>an</strong>ce methods<br />
birthday<br />
Class methods<br />
new<br />
<strong>an</strong>Inst<strong>an</strong>ce<br />
age: 31<br />
name: 'John'<br />
Figure 6.4: Inst<strong>an</strong>ce creation<br />
The issue of classes having methods, some of which are intended for <strong>an</strong> inst<strong>an</strong>ce of the class, <strong><strong>an</strong>d</strong><br />
some of which are intended for the class, is not as complicated as it may at first seem. Not least because<br />
the tools used with <strong>Smalltalk</strong> tend to keep the two sides of the classes pretty well distinct. In <strong>an</strong> a ttempt<br />
to make it clearer here are some definitions:<br />
• Inst<strong>an</strong>ce variables: Defined by the class, but a copy is maintained in each inst<strong>an</strong>ce which has its<br />
own value for that inst<strong>an</strong>ce variable.<br />
• Class variables: Defined in the class with a single copy in the cl ass accessible by inst<strong>an</strong>ces of<br />
the class.<br />
• Class inst<strong>an</strong>ce variables: Defined in the class with a single copy in the class <strong><strong>an</strong>d</strong> only accessible<br />
by the class.<br />
• Inst<strong>an</strong>ce methods: Defined in the class with a single copy maintained in the class but executed<br />
within the context of <strong>an</strong> object.<br />
• Class methods: Defined in the class with a single copy maintained in the class <strong><strong>an</strong>d</strong> executed<br />
within the context of the class.<br />
Some of these concepts will be considered in greater detail later.<br />
6.3.6 Classes in <strong>Smalltalk</strong><br />
There are very m<strong>an</strong>y classes in <strong>an</strong>y <strong>Smalltalk</strong> system, e.g. in VisualWorks there over 1,000 classes.<br />
However, you will only need to become familiar with a very few of them. The remaining classes<br />
provide facilities that you use without even realizing it.<br />
6.4 Method definitions<br />
Methods provide a way of defining the behavior of <strong>an</strong> object i.e. what the object does. For example, a<br />
method may ch<strong>an</strong>ge the state of the object or it may retrieve some information. A method is the<br />
equivalent of a procedure in most other l<strong>an</strong>guages. A method c<strong>an</strong> only be defined within the scope of <strong>an</strong><br />
object (<strong><strong>an</strong>d</strong> there is no concept such as the main method as there is in C). It has a specific structure:<br />
messagePattern arguments<br />
"comment"<br />
| temporaries |<br />
statements<br />
where the messagePattern represents the name of the method <strong><strong>an</strong>d</strong> the arguments represent the names<br />
of arguments. These arguments are accessible within the method.<br />
The “comment” field is a comment describing the operation performed by the method a nd <strong>an</strong>y<br />
other useful information. Note that comments c<strong>an</strong>not be nested in <strong>Smalltalk</strong>. This c<strong>an</strong> be awkward if<br />
you wish to comment out some code for later. For example, consider the following piece of <strong>Smalltalk</strong><br />
which I have just commented out:<br />
“<br />
x := 12 * 4.<br />
66