We will talk now about the main concepts of the language used at Squeak-Croquet: Smalltalk.
A Class is a group of methods (functions, subroutines, messages) that can be performed using an object (an instance, an object-reference, a prefix, a pointer) of this Class.
To create an object of the Class X, by example, we need to write the statement:
x := X new.
The symbol ":=" is the operator for assignment in Smalltalk.
All statement finish by a "point".
To execute the method theMet of the Class X, by example, we need to write the statement:
A method can have a parameter and the sintax will be something like:
A parameter can be another object of another Class.
TIP: An example to simplify: if you like to create a method that receives 3 parameters and returns their sum:
sumOfThree:a with:b plus:c
It's usual, in Smalltalk, to say that: " the object x received the message theMet1" meaning that this method was executed.
A Class has a " constructor method" whose name will be: initialize. When a Class is instatialized, its " constructor" will be executed.
In the definition of the statements of a method we can put a call for another method of the same or of another Class (that needs to be instantialized first). To use a method of the same Class we use the prefix: self like the object.
Some methods contains variables that need to be defined at the begining of the method inside bars;
| var1 var2 var3 |
These variables are local to this method.
It's not usual the definition of "properties" in Smaltalk. Methods are used to simulate them. We can have, by example, a method color:c, this means that, if we use:
x color: cthe object x has the color c.
Smaltalk accepts inheritance. This means that we can create a subClass Y for a Class X. This subClass will have all the methods of its superClass.
We will learn how to create subClasses in future lessons. Squeak-Croquet has many Classes that we will "subclass".
If Y is a subClass of the Class X that has the method theMet1 we can do, by example:
y := Y new. y theMet1.and the method theMet1 defined in the Class X will be executed.
We can modify a method of a superClass writing a new method in the subClass having the same name of that of the superClass. We say that the new method "overrides" the other.
If we don't override the "initialize" method of the superClass it will be executed when we instantialize the subClass.
A group of Smalltalk Classes (including methods etc.) is called: an image. Different "images" of Smaltalk can be distributed by different companies, user groups, entities etc. By example: the image distributed by the group "Squeakland" is different of that distributed by the group "Croquet". If YOU create new subClasses and/or methods, you can save your work (we call this a "big save" - we will talk about this soon) creating YOUR "image".
Smalltalk breaks out of the "compile-link-run" cycle. Changes in a method in some Class, will change all the use of this method at all applications running under this "image". Be carefull.
It's usual that Class names begin with a capital letter such as "Student". Method names begin with a lower case letter and can have any combination of letters and numbers without embedded blank spaces. When a method name contains more than one word, the extra words start with a capital letter.
A method name would be, by example: "aMessage" or "myAddress."
It's usual that the name of a method means a message. A message such as "give me your name" could be "giveMeYourName:n". This however, is too difficult to work with. It would be easier to shorten the designation of the method to just: "name:n".
If a parameter is a string it's placed inside 'apostrophes'. By example:
st Student new. st name:'Peter Brow'.
Pay attention because here "name" is not a property, and it would be wrong to write:
st name := 'Peter Brow'.
In Smalltalk, the last line of a group of statements doesn't need to finish with "point", but we recomend not use this facility.
Smalltalk has a series of execution-time rules for evaluating a Smalltalk statement. There are three important rules for evaluating code:
PREVIOUS LESSON NEXT LESSON T. CONTENTS HOMEPAGE