Upload
kathryn-clarke
View
216
Download
0
Tags:
Embed Size (px)
Citation preview
Object-oriented programming and design
1
Smalltalk in a Nutshell
Objects & classes
Messages & methods
Inheritance & metaclasses
Object-oriented programming and design
2
Smalltalk: Everything is an Object Application objects: customer, inventory GUI objects: button, text editor Foundation: string, set, numbers, booleans Tools: browser, debugger, compiler, GUI builder Language implementation: class, method,
context,
Object-oriented programming and design
3
Communicating with an Object
All computation is done by objects.
The only way to interact with an object is to "send a message" to it.
Smalltalk has three kinds of syntax for sending a message.
All messages have same implementation.
Object-oriented programming and design
4
Three Kinds of Message Syntax
Unary messages
aSalaryChange dateKeyword messages
earned at: date add: moneyBinary messages
(worked - 40) max: 0
Object-oriented programming and design
5
One Way to Send a Message
Object responds to message by looking in its class for a method with the same selector.
If it doesn't find the method, it looks in its superclass.
Repeat until it finds the method. If it never does, there is an error.
Object-oriented programming and design
6
Smalltalk in a nutshell
Classes have methods and variables. Methods are a sequence of messages,
assignments, returns. Variables, literals, pseudovariables Blocks Metaclasses - classes have classes
Object-oriented programming and design
7
Smalltalk Expression Syntax
Literals
3.675, 14, 'hello', #weight, $d,
#( #foo 'bar' 92)
Assignments and variables
v := v + 1
Messages
Object-oriented programming and design
8
Smalltalk Expression Syntax
Sequences. Blocks.
x < y ifTrue: [z := x] ifFalse: [z := y].
paychecks do: [:each | each post]
Cascades
aSet add: #blue; add: #red
Object-oriented programming and design
9
Smalltalk Method Syntax
Returns
^socialSecurity + federalTaxes + stateTaxes
Object-oriented programming and design
10
Variables in Smalltalk
blockArguments and temporaries methodArguments and temporaries instanceVariables
Can be accessed only by the object's methods. Globals, class variables
Any method in the scope can access it
Variable is object of class Association
Pseudovariables
nil true, false self super thisContext (in Squeak and VisualWorks,
but not standard Smalltalk)
Object-oriented programming and design
11
Summary
Classes are objects that define methods and variables of their instances.
Method is a sequence of message-sends, assignments, and return statements
Three kinds of syntax for messages, but one way of implementing them
Literals. Variables. Blocks.
Object-oriented programming and design
12
Object-oriented programming and design
13
Message Lookup and Inheritance
EmployeeTransaction has subclasses Paycheck, SalaryChange, and Timecard.
EmployeeTransaction defines the instance variable date and the method:
date
^date
EmployeeTransactionEmployeeTransactiondatedate
PaycheckPaycheck check1check107/09/9507/09/95
aPaycheck dateaPaycheck date
Object-oriented programming and design
15
Using VariablesprintOnCheckStream: aStream
aStream cr; cr.
aStream next: 40 put: (Character space).
DateFormat print: date on: aStream.
aStream cr.
......
Object-oriented programming and design
16
More variables
test
"PayrollSystem test"
| payroll day1 ralph |
day1 := Date newDay: 5 year: 1996.
payroll := self new.
ralph := Employee new name:
'Ralph Johnson'.
Object-oriented programming and design
17
(continued)
ralph changeSalaryFor: day1 to: 20.
payroll addEmployee: ralph.
self
employee: ralph
hours: self aLittleOvertime
starting: day1.
^payroll
Object-oriented programming and design
18
Initializing an Object
Every object needs to be initialized. Uninitialized variables are nil.
The initialization method often defines the type of a variable.
Two methods: one class and one instance.
Object-oriented programming and design
19
Class Methods
Class is an object. It can have methods, too.
For class Date class
newDay: dayInteger year: yearInteger
^self new day: dayInteger year: yearInteger
Object-oriented programming and design
20
Instance initializing method
For class Date
day: dayInteger year: yearInteger
day := dayInteger.
year := yearInteger
Object-oriented programming and design
21
Creating a Date
Date newDay: 3 year: 1995
Date new day: 3 year: 1995
day: 3 year: 1995
31995
dayyear
Object-oriented programming and design
22
Complete Smalltalk
Expressions (method definition)message, assignment, sequence, block, return
Variables, literalsClasses, inheritanceClass methods / instance methodsPseudovariables
nil, true, falseself, super, thisContext
Object-oriented programming and design
23
Smalltalk (the language) is trivial
Complexity is class library.
New language extensions fit in as well as numbers and control structures.
Language extension => core language is trivial
Object-oriented programming and design
24
Implicationsclass library = language extension
=> must know class librarymust standardize class librarymerging class libraries is like merging language extensionshard to make class libraries
Object-oriented programming and design
25
Applications
No programs, just objects No “main” routine Most applications create new classes, reuse
a lot of existing ones