"Redo: Package, Classes and Methods"
Quentin Ducasse.
-Hello, everyone.
In this first redo,
we are going to look at how to create a package,
how to create a class within the package,
and how to define some methods
and see if everything is working correctly.
First, you can open the Pharo menu.
Then you go to the Tools,
and System Browser.
There, you can see all the packages
that are loaded within your image,
so there are quite a lot.
Since we want to create a new one, we can right-click on any of them
and just go to New package,
say "MyCounter", press "OK".
Our package has been created there
but since we do not really want to see all the other ones,
we can just filter them out to "MyCounter".
There, our package is there, it is quite empty
and we have a template to create a class
so it will be a subclass of the class "Object"
and we can name it there.
Let's name it "Counter"
and we can attribute an instance variable to it
so let's go with "count".
Then, we can see that there is this orange triangle
in the top right corner.
This means that your method has not been compiled
and in order to do so, you can right-click and go to "Accept"
or press the shortcut Cmd+S.
Let's just accept.
There it is, our class has been created
but there is a red exclamation mark on the right side.
This means that there is no comment.
As you can see there, there is a comment,
but it is simply recommendations on how to comment your class.
You are encouraged to do it but we are not going to do it right now.
We are just going to do the right method,
so let's just go this tab right here.
Let's double-click
and write some accessors to our instance variable.
So, let's go to "count".
This will be the getter.
Let's just write the "count" instance variable.
The orange triangle is there, so press Cmd+S.
Our method is there.
Now, if we want to make the setter,
let's just count and pass it an integer.
And the "count" variable should now
get the value of this integer
so we can just write this like that.
Compile it again, Cmd+S.
We have our two accessors.
They have been put
into the "accessing" protocol automatically
and this is correct.
Now, let's see if we can make all those work.
Let's go to another tool called the Playground.
Let's just go to the Pharo menu, to Tools,
then Playground.
And from there, we can create a temporary variable that we call c.
We can make it a counter
so "c = Counter new".
And now, we want the count instance variable to be 7.
And in order to see if this chain has common effects,
we can just call the getter "c count".
In order to see everything,
we can double-click at the end so it selects everything
and then press Cmd+P or just right-click and go to "Print it".
We can see that this is 7,
this is the value we attributed to the instance variable so it is correct.
In this video,
we saw how to create a package, a class,
some accessor methods
and how we can visualize it in the Playground,
but this behavior right here,
we might want to put this into test
and we will see that in the next part of the redo.