29.11.2014 Views

Smalltalk and Object Orientation: an Introduction - Free

Smalltalk and Object Orientation: an Introduction - Free

Smalltalk and Object Orientation: an Introduction - 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.

8. An Example <strong>Smalltalk</strong> Class<br />

8.1 <strong>Introduction</strong><br />

You should now be ready to write some <strong>Smalltalk</strong> code. So this chapter will take you through a worked<br />

example. It is a very simple example, in which you will create a new class, define some inst<strong>an</strong>ce<br />

variables <strong><strong>an</strong>d</strong> write a couple of methods.<br />

8.2 The class Person<br />

The Person class will provide a very basic set of features. It will record a person’s name <strong><strong>an</strong>d</strong> their age.<br />

It will also allow a person to have a birthday (<strong><strong>an</strong>d</strong> thus increment their age).<br />

8.2.1 Creating the class<br />

The first thing you should do is to create a new class category to put your class into. This is done in the<br />

class category w indow of the System Browser. Use the right button menu (middle on a three button<br />

mouse) <strong><strong>an</strong>d</strong> select the add option. This option will prompt for a category name. It is best if you use a<br />

me<strong>an</strong>ingful category name. I have used the category name “Example Class”.<br />

Once you have provided a category name it will be immediately created. However, note that it will<br />

add the category at the bottom of the list of categories or, if you have selected (highlighted) <strong>an</strong> existing<br />

category, immediately above that category. This me<strong>an</strong>s that if you have accidentally selected a category<br />

in the middle of the list, your new category will be added in the middle of the category list. It is easier to<br />

find your own categories if they are not mixed up with other categories. It is therefore advisable to<br />

make sure that no categories are selected when you create a new one.<br />

Next define a new class. You c<strong>an</strong> do this by filling out the template in the bottom window<br />

(commonly called the code view) of the System Browser to mirror the following:<br />

<strong>Object</strong> subclass: #Person<br />

inst<strong>an</strong>ceVariableNames: ' name age '<br />

classVariableNames: ''<br />

poolDictionaries: ''<br />

category: 'Example Class'<br />

That is, define the class Person as a subclass of <strong>Object</strong> <strong><strong>an</strong>d</strong> give it two inst<strong>an</strong>ce variables name<br />

<strong><strong>an</strong>d</strong> age. (Note that if you have called your class category something other th<strong>an</strong> “ Example Class”<br />

the category field will be different).<br />

Notice in Visualworks, that the template fills in the superclass, the class name <strong><strong>an</strong>d</strong> the variable fields<br />

with default values, e.g.:<br />

NameOfSuperclass subclass: #NameOfClass<br />

inst<strong>an</strong>ceVariableNames: 'instVarName1 instVarName2'<br />

classVariableNames: 'ClassVarName1 ClassVarName2'<br />

poolDictionaries: ''<br />

If you do not delete these fields, they will be included in the compiled class. For example, it is easy<br />

to find that you have four inst<strong>an</strong>ce variables <strong><strong>an</strong>d</strong> two class variable s. The additional variables having<br />

names such as instVarName1 <strong><strong>an</strong>d</strong> ClassVarName1.<br />

If you have defined the new class correctly, then at this point your browser should l ook like that in<br />

Figure 8.1. Do not worry if the list of class categories in the right most window is different, the<br />

import<strong>an</strong>t points are that the class definitions match <strong><strong>an</strong>d</strong> that the highlighted text is the same.<br />

76

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

Saved successfully!

Ooh no, something went wrong!