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.

printString, ' years old.'; cr.<br />

]<br />

]<br />

> aHuman [<br />

^age > aHuman getAge<br />

]<br />

< aHuman [<br />

^age < aHuman getAge<br />

]<br />

= aHuman [<br />

^age = aHuman getAge<br />

]<br />

| me myBrother |<br />

me := Human new.<br />

me setName: 'Canol Gökel'.<br />

me setAge: 24.<br />

myBrother := Human new.<br />

myBrother setName: 'Gürol Gökel'.<br />

myBrother setAge: 27.<br />

me introduceYourself.<br />

myBrother introduceYourself.<br />

(me < myBrother) printNl.<br />

Hello, my name is Canol Gökel and I'm 24 years old.<br />

Hello, my name is Gürol Gökel and I'm 27 years old.<br />

true<br />

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

Our Human class is pretty simple. It has two instance variables named name and age. After defining the<br />

accessors (setName:, getName etc.) we defined a method named introduceYourself. When a<br />

Human object is sent this message, it introduces itself by telling some information about itself. We have<br />

also written methods to compare two Human's age. If a comparison is wanted to be done among two<br />

Human objects, you can use >, < or = binary messages. Notice that we have used the instance variable<br />

age for the receiver and sent the getAge message to the argument object to reach the ages of them<br />

which are integer numbers. The comparison methods are already defined for Integer objects so we<br />

don't have to go deeper.<br />

This implementation is pretty simple. Suppose that we needed to add a few more details to this class.<br />

We can modify the above code directly but there will be times that we don't want to modify the<br />

original code. For example, maybe more than one program is sharing the code and only one of them is

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

Saved successfully!

Ooh no, something went wrong!