06.06.2013 Views

Computer Programming with GNU Smalltalk - Free

Computer Programming with GNU Smalltalk - Free

Computer Programming with GNU Smalltalk - Free

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

The general syntax for creating classes is below:<br />

SuperclassName subclass: SubclassName [<br />

| instanceVariable1 instanceVariable2 |<br />

]<br />

classVariable1 := anObject.<br />

classVariable2 := anotherObject.<br />

<br />

SubclassName class >> aClassMethod: aParameter [<br />

"Comment to describe this class method"<br />

<br />

]<br />

| localVariable1 localVariable2 |<br />

...<br />

^objectToReturn<br />

anInstanceMethod [<br />

"Comment to describe this instance method"<br />

<br />

]<br />

| localVariable1 localVariable2 |<br />

...<br />

^objectToReturn<br />

Objects, Messages and Classes: Part II 59<br />

Above pseudo-code shows how to create a class, add class and instance variables, add class and instance<br />

methods to it. We will explain it line by line but before that, we want to mention about some general<br />

concepts. Most of the things above are optional, for example you don't need to have class variables or<br />

methods. Or you don't need to write comments for anything or define the category of a method. But<br />

these are good software engineering habits and we recommend you to always write comments and<br />

define categories for methods.<br />

Also another interesting thing in above code is the usage of white spaces. Some lines are indented by<br />

tabs or spaces, but why is that? It is just because for polishing the code for human eye, and there is no<br />

rule for how to use them. Mostly, we will indent a body of some new structure. For example, when we<br />

are writing the body of a class then we will indent it one level compared to the context. Interpretation of<br />

one level depends to the programmer. A programmer may choose as many spaces or tabs to insert as<br />

she wants for indenting a line but the important thing is to be consistent throughout the program so that

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!