I have been following the example of making a class in Pharo from the following link: https://ci.inria.fr/pharo-contribution/job/UpdatedPharoByExample/lastSuccessfulBuild/artifact/book-result/PharoObjectModel/PharoObjectModel.html#fig:colorInstanceClassSide
It is the example of making a Dog and a Hyena class. First I created a package called TestC and in the Instance class I have made the following:
For what I know, and correct me if I am wrong, the instance side is where I create the methods that will work when I instantiate an object while the class side it does not need an object to be created to function; just like a static method class in Java.
The first question that I have at this point is why after Accepting the Changes it stills appear the ! symbol in the left part of my classes?
According to the tutorial then I should put the following code:
Dog class
instanceVariableNames: 'count'
Now I did not where to put it, on the instance side or in the class side, I decided to put it on the instance side the following:
and the last methods that I have are the following, I put them on the class side:
enter image description here
I have tested the classes in the Transcript with the following code:
aDog := Dog new.
Dog count.
bDog := Dog new.
Dog count.
and it works, but I would like to know if I made the right decision to put those methods in the class side (second question), and if its right could anybody give me an example of a method to put in the instance side in this example?
Thanks
Creating a class in Pharo is as simple as sending subclass: message to another class object. But most of the classes are created and modified in a system browser (currently Nautilus).
In Smalltalk, everything is an object, and classes act as descriptions of objects. Classes are used to define the set of messages an instance of that class responds to, as well as the variables contained in every instance of that class.
First Question
The exclamation mark !
in front of the class is an invitation to click on it. In this case it will remind you that classes are supposed to have comments (for the sake of documentation).
Digression
Every class defines both the shape (or structure) and the behavior of its instances. The shape is given by the instance variables in the class creation message, such as
Object subclass: #Dog
instanceVariableNames: 'name breed birthdate'
classVariableNames: ''
package: 'TestC'
For the behavior you create methods in the instance side of the class
breed: aString
breed := aString
With this you have a full functional object
dog := Dog new.
dog name: 'Taylor'; breed: 'Great Dane'
However, classes are objects too. So, they may have their own shape. To add an instance variable to a class's shape you send the message:
Dog class instaceVariableNames: 'count'
Note that the receiver of this message is the class of the Dog
class (a Metaclass
), which makes sense because it is classes who shape their instances.
In your example, every time you create a new (instance of) Dog
the count
ivar of the Dog
class will increase by 1. This could serve the propose of keeping track of how many instances of Dog
have been created so far. These instances, however, will remain unaware of this fact.
Second Question
Yes, the ivar count
and the methods to initialize
, increment
and retrieve
it belong in the class side. Why? Because they are intended to keep track of the number of instances your class has created. Whether this is useful or not, is a different question; the fact is that they would have made no sense in the instance side (after all, lucky dogs live exciting lives without ever counting anything.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With