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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

]<br />

]<br />

'Woof!' printNl.<br />

Animal subclass: Cat [<br />

<br />

]<br />

makeNoise [<br />

"An instance method to get the cat's noise."<br />

<br />

]<br />

'Miaow!' printNl.<br />

dog := Dog new.<br />

Animal setAnimalNumber: 1.<br />

dog setName: 'Karabash'.<br />

dog getName printNl.<br />

dog makeNoise.<br />

cat := Cat new.<br />

Animal setAnimalNumber: 2.<br />

cat setName: 'Minnosh'.<br />

cat getName printNl.<br />

cat makeNoise.<br />

'Karabash'<br />

'Woof!'<br />

'Minnosh'<br />

'Miaow!'<br />

2<br />

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

Now we will investigate this program line by line. We first create our class by inheriting an object<br />

named Object which is a special object: ancestor of all classes we will create. It comes <strong>with</strong> some<br />

useful methods predefined for us like new:<br />

Object subclass: Animal [<br />

...<br />

]<br />

Next thing we do is to declare an instance variable named name and a class variable named<br />

animalNumber. We will give a name to our animals and keep the number of the animals we created in<br />

the class variable:<br />

| name |<br />

animalNumber := 0.

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

Saved successfully!

Ooh no, something went wrong!