SMALLTALK: FUNDAMENTALS OF THE LANGUAGE - PART 1
Before speaking of Smalltalk fundamentals, we will present two resources of the Squeak-Etoys that we will frequently use: The Workspace and the Transcript. They are two windows that we place in the screen using the window “World” and clicking Open.
Later it is alone to click in its names. In the Workspace we can type a program, for example:
(To use a keyboard, in the emulator, the vertical bar is the small letter of the keyboard key close-clasps/close-keys. Two-points cê-cedilha is the capital letter of the keyboard key.)
After writting the program, drags the cursor on all the lines (so that they are green) and press Alt+s with the cursor inside of the window. To execute press Alt+d .
The output of the program appears in the Transcript.
It is our assumption that you know some programming language: Java or JavaScript or VisualBasic or etc. Paraphrasing that phrase: the first language people never forgets. Certainly you love the language that learned and hate all the others. It certainly goes to say that “Smalltalk is a drug, good language exactly is… (to place here the name of its first language)…” I, that I started to work with computers in 1969 in IBM, find that good exactly it is Assembler (later is COBOL) but… we go to try to learn Smalltalk to program for the boys of the OLPC.
Some concepts are common to some languages. A Class is a group of methods (functions, subrotinas, messages) that they can be executed using an object (an instance, a object-reference, a prefix, pointer) of this Classroom.
To create an object of Classroom X, for example, we write statement:
The symbol “:=” he is the assignment operator in Smalltalk.
All statements finish with a “period”. This can be left out in the last statement of the program (script).
To execute the method theMet1 of Classroom X we would write then:
A method can have a parameter and the syntax will be something as:
A parameter can be an object of another Classroom. If you to quizer to create a method that receives three parameters and returns the addition from them the syntax it would be:
Clearly that the name does not need to be in English but the other words yes. Therefore, an idea is for everything in English, not to be very maluco. Programmers must get used to use lingua English.
He repairs the signal of “return”.
The users of Smalltalk like to say esoteric things such as: “Object x receives the message theMet1” meaning that the method theMet1 of Classroom X was executed using object x.
All Classes have a “construction method” that is executed when the Class “is initialized” - when an object of it is created. The name of this method is: initialize.
In the creation of statements of a new method we can use the execution of method of another Classroom or some already defined of the same one. To use a method of the same classroom we use the prefix/object self. (You remember of that we use this object in script of our first exercise)
Some methods contain 0 variable that they at the beginning need to be defined of the method, between bars. For example:
These 0 variable are local to the method.
The definition of properties in Smalltalk is not common. We use methods to simulate this. We can have, for example, the method: color: c.
If we use:
object x has color c. The accepted Smalltalk inheritance. This means that we can crar one subClass Y for Classroom X. This subClass inherits all the methods of the other (the superclassroom). We will see as to create subclasses future. In the reality all the Classrooms are subclasses of the Object Classroom and what we create they are always subclasses. We can modify a method of a superclassroom writing a method, in our Classroom, with the same name. If he says that this new “overraida” method (overrides) the other. If “we do not overraidamos” the method “initialize” of the superclassroom, this will be executed quendo if instancializa subClass. One determined group of Classroom of the Smalltalk is called image. Different images of Smalltalk can be didtribuidas by different company, groups, entities etc. For example have the image of the group “Squeak-Croquet” that is different of the image of the “Squeak-Etoys” etc. etc. If you create new subClass or new methods for an existing Classroom in the Squeak-Etoys of its micron OLPC (we go to see as if she makes this) and give to a “general Save” you you create ITS image that is different of the original that came installed in its micron OLPC. You can distribute the modifications that made (we go to see as if it makes this) for a friend, if want that it executes projects that you created using these elements “not standard” (let us say thus). If you only create projects using the Classrooms “standard” of the Squeak-Etoys (as in the first exercise) you you only need to distribute the project - she is more easy. In the majority of the compiled languages, C++ type, we have the cycle “compiles-link-run” and a compiled application is independent of a change that we make in a Classroom of the API. In the Smalltalk it is different. You can change a method of the “image” and all the applications that use this method will be modified therefore what we have is a species of virtual machine that makes a compilation of script “just in teams”. Care then when to change a Classroom of its image. If preference does not make this: subClass creates one. It is usual that the name of a Classroom starts with capital letter. Names of methods start with small letter and can have capital letters in the way. If a parameter of a method is one string, is placed between apostrophes st Student new. st name: “Carlos Silva”. Attention because we do not have properties in the Smalltalk (we have 0 variable, clearly) and would be incorrect to use; st name: = “Carlos Silva”. In the case “name” is not an 0 variable. It repairs that she is being “called” for a prefix/object.
Page Information
|
Wiki Information |
Recent PBwiki Blog Posts |