Download presentation
Presentation is loading. Please wait.
Published byKathryn Clarke Modified over 9 years ago
1
Object-oriented programming and design 1 Smalltalk in a Nutshell Objects & classes Messages & methods Inheritance & metaclasses
2
Object-oriented programming and design 2 Smalltalk: Everything is an Object l Application objects: customer, inventory l GUI objects: button, text editor l Foundation: string, set, numbers, booleans l Tools: browser, debugger, compiler, GUI builder l Language implementation: class, method, context,
3
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.
4
Object-oriented programming and design 4 Three Kinds of Message Syntax Unary messages aSalaryChange date Keyword messages earned at: date add: money Binary messages (worked - 40) max: 0
5
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.
6
Object-oriented programming and design 6 Smalltalk in a nutshell l Classes have methods and variables. l Methods are a sequence of messages, assignments, returns. l Variables, literals, pseudovariables l Blocks l Metaclasses - classes have classes
7
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
8
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
9
Object-oriented programming and design 9 Smalltalk Method Syntax Returns ^socialSecurity + federalTaxes + stateTaxes
10
Object-oriented programming and design 10 Variables in Smalltalk l blockArguments and temporaries l methodArguments and temporaries l instanceVariables Can be accessed only by the object's methods. l Globals, class variables Any method in the scope can access it Variable is object of class Association
11
Pseudovariables l nil l true, false l self l super l thisContext (in Squeak and VisualWorks, but not standard Smalltalk) Object-oriented programming and design 11
12
Summary l Classes are objects that define methods and variables of their instances. l Method is a sequence of message-sends, assignments, and return statements l Three kinds of syntax for messages, but one way of implementing them l Literals. Variables. Blocks. Object-oriented programming and design 12
13
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
14
EmployeeTransactiondate Paycheck check107/09/95 aPaycheck date
15
Object-oriented programming and design 15 Using Variables printOnCheckStream: aStream aStream cr; cr. aStream next: 40 put: (Character space). DateFormat print: date on: aStream. aStream cr....
16
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'.
17
Object-oriented programming and design 17 (continued) ralph changeSalaryFor: day1 to: 20. payroll addEmployee: ralph. self employee: ralph hours: self aLittleOvertime starting: day1. ^payroll
18
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.
19
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
20
Object-oriented programming and design 20 Instance initializing method For class Date day: dayInteger year: yearInteger day := dayInteger. year := yearInteger
21
Object-oriented programming and design 21 Creating a Date Date newDay: 3 year: 1995 Date new day: 3 year: 1995 day: 3 year: 1995 3 1995 day year
22
Object-oriented programming and design 22 Complete Smalltalk Expressions (method definition) message, assignment, sequence, block, return Variables, literals Classes, inheritance Class methods / instance methods Pseudovariables nil, true, false self, super, thisContext
23
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
24
Object-oriented programming and design 24 Implications class library = language extension => must know class library must standardize class library merging class libraries is like merging language extensions hard to make class libraries
25
Object-oriented programming and design 25 Applications l No programs, just objects l No “main” routine l Most applications create new classes, reuse a lot of existing ones
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.