Last updated at 4:20 am UTC on 30 December 2006
back selfStudy next
Everything in Squeak is an object. An object contains both data and the procedures to manipulate it's own data. An object's data and how it processes it's data is hidden from the outside world. The behavior of an object is how it is viewed by other objects. The behavior of an object is determined by what messages the object understands.
Objects communicate by sending messages to other objects. A message is a request for the object to do something. If the object understands the message, it will carry out the request. If the object does not understand the message it will complain with an error or walkback window.
An object stores it's data in it's own instance variables. These instance variables are defined in the object's class. An object's instance variables are hidden from other objects.
The actual Squeak code that an object follows to manipulate it's data is called a method. An object's methods are defined for it's Class using the Class browser. A method's name is called a selector.
Objects are created by sending the new message to a Class. A Class should be thought of as a factory which manufactures identical objects. When an object is created, it is said to be an instance of it's Class.
For additional discussion of objects take a look here.
The following is a very brief introduction of how to create an object with instance variables and a method. Hopefully this will make the above description of an Oject a little bit clearer.
First open a System Browser by dragging it out from the Tools flap. In the top left pane click the yellow mouse button to bring up a menu. Select Add a Category from the menu. A text input window will open.
Type in Learning-Experiments and select the Accept button. You will now see the new category in the top left pane of the Browser.
Now we will define a new class called animal in this category. In the bottom pane of the System Browser there is a template to create a new class. At the end of the first line replace the words NameOfSubclass with Animal. On the second line called instanceVariableNames: type in age hungry between the ' '. Now click the yellow mouse button in this pane and select the Accept menu option. You will see your new Animal class in the top class pane.
So now that we have defined a new Animal class, let's create an Animal object. Open a Workspace by dragging it out from the Tools flap. In the Workspace, type in Animal new. We are sending the new message to the Animal class to manufacture an Animal object for us. Click the yellow mouse button in the Workspace and select Inspect from the menu. An Inspector will open on the new Animal object. By selecting the words in the left pane of the Inspector, you can see their values in the right pane of the Inspector.
Now we want to add a method to the Animal Class. In the top 3rd pane from the left of the System Browser, select '– all –'. You will see a method template in the bottom pane of the Browser. This is where you write your Object's methods.
Replace the first line messageSelectorAndArguments with the word speak. The second line is a comment on the method, explaining what it does. Replace the text on the second line with the following: "This is the generic Animal method for speaking. It may be subclassed with more specific kinds of speach". We will not be using any temporary variables, so we can delete the third line. On the fourth line type: Transcript show: 'I am a friendly animal';cr. Now use the yellow mouse button and select Accept on the menu. You will now see the method name in the upper right pane of the System Browser.
Open a Transcript by dragging a Transcript out of the Tools flap. In a Workspace type Animal new speak. Use the yellow mouse button to bring up a menu and select do it. You will see 'I am a friendly animal' in the Transcript. Notice what we did here. 'Animal new' creates an animal object; we then send the message 'speak' to the animal object and it responds by outputting text in the Transcript. –Dave Raftery
part of selfStudy