Basic OOP Smalltalk organization Classes and instances Variables and methods Here's what to keep in mind: Instance variables are private to each instance of the class. Class variables are shared by all instances (so if one makes a change, all instances see it). The most important class method(s) is instance creation. Instance methods have direct access to all the instance variables and all the class variables too. Class methods do *not* have access to the instance variables, however. ============================== Using the interface three buttons -- this was covered in a bboard message executing code -- you can (just) do it, do it and print the result, or do it an open an inspector window on the result. ============================== Language components * variables and their names -- no punctuation marks in variables -- use capitalization to separate words -- capitalization of the *first* letter is significant -- Class names are capitalized -- Class variables are capitalized -- Global variables are capitalized -- instance variables are not capitalized -- method names are not capitalized -- formal parameters and temporary variables are not capitalized * symbols #symbol These are case sensitive. #x not equal to #X * comments and strings "comment" 'string' Be sure not to put your strings in "double quotes"! * numbers 3.125 * boolean There is a real Boolean data type (not T and NIL, which are symbols) * pseudo-variables: true false nil These are in effect constants: you can't re-assign their values. True and False are the canonical Boolean values. The nil variable is not Boolean: it stands for the Undefined data value. When you create a new class or an instance, all the class or instance variables are bound to nil. Sending any message to nil (except for isNil) causes an error. ============================== Messages. There are three types of messages: unary binary n-ary 1. Unary messages. The only argument is the "receiver" (the object that receives the message. 3 negated => -3 4 reciprocal. => (1/4) 3 negated reciprocal. => (-1/3) 3/4 => (3/4) 2. Binary messages. These are mostly built-in messages that handle common arithmetic/logical operations: 3 / 4 3 + 7.2 2 + 3/4 => (5/4) 3 + 2 negated. => 1 Be careful here: multiplication does not have higher priority than addition. Evaluation is strictly left to right through the binary operators. 3 + 2 * 7. => 35 But you can always add parens to enforce an evaluation order. 3 + (2 * 7) => 17 Also notice: *all* unary messages are processed before any of the binary messages: 2 + 3 negated. => -1 (2 + 3) negated. => 0-5 This was an interesting example: this is two statements. The first generates the integer 3 (which is then thrown away). The second generates the integer 2, which is the result of the message and is thus printed. The interesting thing is that Smalltalk warned us that the value 3 was not being used. 3. 2 => 2 We discovered that the class Fraction can convert itself to a floating-point number: (3/4) asFloat. 0.75 => 0.75 3. N-ary messages In general, a colon at the end of a message name signifies that an argument is expected. This message is named "at:" #(#symbol 'string' 3.25 $x) at: 2. 'string' This next message is named "at:put:" Notice that the name tells you that there are three objects involved: the receiver and two additional arguments. In this case the receiver is an Array, the second arg is a number, and third is a string: "define the global variable X" X := #(#symbol 'string' 3.25 $x). "side-effect the variable" X at: 2 put: 'new string'. "and notice that its value has been changed" X. #(#symbol 'new string' 3.25 $x ) Message evaluation precedence: All unary messages are processed left-to-right, then All binary messages are processed left-to-right, then All n-ary messages are processed left-to-right. ============================ Defining a class: Temperature We started defining this class: -- instance variables: value and scale -- class variables: none -- instance creation class methods: farenheit: and celsius: