Object subclass: 'Vehicle'
instanceVariables: 'passengers '
passengers
^passengers
addPassenger: aPerson
passengers add: aPerson
removePassenger: aPerson
passengers remove: aPerson
init
passengers := OrderedCollection new.
Vehicle subclass: 'Bus'
instanceVariables: 'route'
route: r
route := r
route
^route
init
super init. "make sure inherited variables are initialized"
self route: 0 "could also just say route := 0"
Instances of the class Bus understand the above three methods, in
addition to those inherited from Vehicle. Note that Bus overrides the
init method. The above example also introduces two important
pseudo variables, self and super. Self is used when an
object wishes to refer to itself, and super is used to refer to the
superclass of the object.
B := Bus new. B init. B addPassenger: P.Remember that the new method asks the class Bus (which is an object) to give us an instance of the Bus class, so B is an instance of Bus. What happens when B is sent the init message? Method lookup in Smalltalk proceeds as follows: When a message is sent, methods in the receiver's class are searched for a matching method. If no match is found, the superclass is searched, and so on up the superclass chain. This means we find the init defined in Bus. We then send super the message init. This directs Smalltalk to begin the lookup in the superclass of the class containing the method in which super is used (this is not always the same as the class of class of the receiver!) This causes the init defined in Vehicle to be executed. Next, we send self the message route:, which means that the receiver (B) is sent the route: message. A message sent to self always causes the method lookup to begin at the instance of the object, regardless of where self is being referenced.
We evaluate "super show". This looks for a show method, starting in the superclass of PanedWindow, namely Window. We find the show method in Window. We evaluate "self drawBorder". This starts the lookup at BorderedPanedWindow again, and we invoke the drawBorder method from BorderedPanedWindow. Then we evaluate "self showTitle". This starts the lookup at BorderedPanedWindow, doesn't find a method, and so finds the showTitle method in Window. Finally we evaluate "self showPanes", which looks in BorderedPanedWindow for a showPanes method, doesn't find it, and then finds the showPanes method in PanedWindow.
| c1 c2 x | x := 0. c1 := [ x := x+1 ]. "c1 is a block" c2 := [ :i | x := x+i ]. "c2 is a block w/ one parm" c1 value. "evaluate c1" c2 value: 20. "evaluate c2, with the argument 20" " now x equals 21... "We use square brackets to define a block. The names before the | in the block are parameters to the block (they must start with a colon). We can send a block the value message to force it to evaluate itself.
True
printOn: stream
stream nextPutAll: 'true'
& b "evaluating and"
^ b
| b "evaluating or"
^ true
not
^ false
and: block "short-circuit and"
^ block value
or: block "short-circuit or"
^ true
Here are the method definitions which implement and, or, and not for
False objects.
False
printOn: stream
stream nextPutAll: false'
& b "evaluating and"
^ false
| b "evaluating or"
^ b
not
^ true
and: block "short-circuit and"
^ false
or: block "short-circuit or"
^ block value
(3=4) & (2>1) (3=4) | (2>1) (3=2) not true & false not (3=4) and: [(1/0) = 8]
True
ifTrue: block
^ block value
ifFalse: block
^ nil
ifTrue: tBlock ifFalse: fBlock
^ tBlock value
False
ifTrue: block
^ nil
ifFalse: block
^ block value
ifTrue: tBlock ifFalse: fBlock
^ fBlock value
3=4 ifTrue: [x := 10]. x=y ifTrue: [x := 8] ifFalse: [x := 9]. x := x=y ifTrue: [8] ifFalse: [9].
a := 1. [a < 10] whileTrue: [Transcript show: a printString. a := a+1]. a := 1. [a > 10] whileFalse: [Transcript show: a printString. a := a+1]. 1 to: 10 do: [:x | Transcript show: x printString].to: is a message understood by numbers. It creates an Interval (which is essentially a collection of numbers), which can then be iterated over, by the general purpose iterator do:. We'll see more examples of do: later, when we talk about the Collection hierarchy.
How to define whileTrue (not really done this way, though):
Block
whileTrue: otherBlock
self value ifTrue: [otherBlock value. self whileTrue: otherBlock].
whileFalse: otherBlock
self value ifFalse: [otherBlock value. self whileTrue: otherBlock].
Collection
add: newObject
"add newObject to the receiver"
addAll: aCollection
"add all objects in collection to the receiver"
remove: oldObject
"remove oldObject from the receiver, report error if not found"
remove: oldObject ifAbsent: aBlock
"remove oldObject from receiver, evaluate aBlock if not found"
removeAll: aCollection
"remove all elements in aCollection from receiver"
isEmpty
"answer whether receiver is empty"
size
"answer the number of elements in the receiver"
Collection
do: aBlock
"evaluate aBlock for each element"
collect: aBlock
"like Lisp mapcar -- make a new collection by applying aBlock to each
element and collecting the results"
select: aBlock
"answer a new collection of the elements that pass the test"
reject: aBlock
"answer a new collection of the elements that fail the test"
inject: startValue into: binaryBlock
"like reduce in Lisp"
detect: aBlock
"like find-if in Lisp, answer the first element that passes the test"
Here are examples of how we might implement some of these methods: (They
are implemented in Collection, which is an abstract class, and inherited by
concrete classes such as OrderedCollection).
collect: aBlock
| newCollection |
"make a new collection of the same class as me"
newCollection := self class new.
self do: [:x | newCollection add: (aBlock value: x)].
^ newCollection
select: aBlock
| newCollection |
"make a new collection of the same class as me"
newCollection := self class new.
self do: [:x | (aBlock value: x) ifTrue: [newCollection add: x]].
^ newCollection
inject: startValue into: binaryBlock
| result |
result := startValue.
self do: [:x | result := binaryBlock value: result value: x].
^ result
a := #(3 4 5 6). "an array literal"
"this is the same as
a := Array new: 4.
a at: 1 put: 3.
a at: 2 put: 4. etc. "
a collect: [:j | j+10] "this returns (13 14 15 16)"
a select: [:j | j>4] "this returns (5 6)"
a inject: 0 into: [:a :b | a+b] "this returns 18"