Computer Programming with GNU Smalltalk - Free
Computer Programming with GNU Smalltalk - Free
Computer Programming with GNU Smalltalk - Free
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
60 <strong>Computer</strong> <strong>Programming</strong> <strong>with</strong> <strong>GNU</strong> <strong>Smalltalk</strong><br />
it does not look messed up.<br />
Other than the indentation, also most of the white spaces we used were programmer-dependent. You<br />
can choose not to place or even place more white spaces around anything. If it is a wrong usage<br />
(whenever it causes an ambiguity) then the interpreter will give you an error. For example, you cannot<br />
join the variable name and a keyword selector because interpreter cannot detect where the variable<br />
name ends and where the keyword selector begins. So you should put at least one space there.<br />
Let's look at the code above line by line. The first thing we do is to define a class <strong>with</strong> the code piece<br />
below:<br />
SuperclassName subclass: SubclassName [<br />
...<br />
]<br />
The three dots in our code always mean that there are more expressions there but we cut it short to<br />
emphasize the really important part. So you should not put three dots in your code. With the part<br />
above, we told SuperclassName class to create a subclass named SubclassName. We did this to<br />
inherit the properties and behavior of the superclass so we don't have to create them again. After doing<br />
that, we opened a bracket to specify the class variables, instance variables, class methods and instance<br />
methods.<br />
The part up to the opened bracket is called the header of the class. The part between the square<br />
brackets is called the body of the class.<br />
The names we put between pipes are instance variables of our class:<br />
| instanceVariable1 instanceVariable2 |<br />
Instance variable names are separated by white spaces. We have two instance variable here named<br />
instanceVariable1 and instanceVariable2.<br />
Then we define class variables by using assignment syntax:<br />
classVariable1 := anObject.<br />
classVariable2 := anotherObject.<br />
:= is another message which makes the receiver an object referring to the argument object. So<br />
whenever we use classVariable1 in our code from now on, the interpreter will understand that we<br />
are actually referring to the anObject object. We will use this assignment message also for changing<br />
the instance variables.<br />
Mentioning about a variable for the first time is called declaring a variable. We should first declare a<br />
variable before being able to use it.<br />
Next expression adds a comment to the class: