25
Object-oriented programming and design 1 Smalltalk in a Nutshell Objects & classes Messages & methods Inheritance & metaclasses

Object-oriented programming and design 1 Smalltalk in a Nutshell Objects & classes Messages & methods Inheritance & metaclasses

Embed Size (px)

Citation preview

Page 1: Object-oriented programming and design 1 Smalltalk in a Nutshell Objects & classes Messages & methods Inheritance & metaclasses

Object-oriented programming and design

1

Smalltalk in a Nutshell

Objects & classes

Messages & methods

Inheritance & metaclasses

Page 2: 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,

Page 3: Object-oriented programming and design 1 Smalltalk in a Nutshell Objects & classes Messages & methods Inheritance & metaclasses

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.

Page 4: Object-oriented programming and design 1 Smalltalk in a Nutshell Objects & classes Messages & methods Inheritance & metaclasses

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

Page 5: Object-oriented programming and design 1 Smalltalk in a Nutshell Objects & classes Messages & methods Inheritance & metaclasses

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.

Page 6: Object-oriented programming and design 1 Smalltalk in a Nutshell Objects & classes Messages & methods Inheritance & metaclasses

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

Page 7: Object-oriented programming and design 1 Smalltalk in a Nutshell Objects & classes Messages & methods Inheritance & metaclasses

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

Page 8: Object-oriented programming and design 1 Smalltalk in a Nutshell Objects & classes Messages & methods Inheritance & metaclasses

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

Page 9: Object-oriented programming and design 1 Smalltalk in a Nutshell Objects & classes Messages & methods Inheritance & metaclasses

Object-oriented programming and design

9

Smalltalk Method Syntax

Returns

^socialSecurity + federalTaxes + stateTaxes

Page 10: Object-oriented programming and design 1 Smalltalk in a Nutshell Objects & classes Messages & methods Inheritance & metaclasses

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

Page 11: Object-oriented programming and design 1 Smalltalk in a Nutshell Objects & classes Messages & methods Inheritance & metaclasses

Pseudovariables

nil true, false self super thisContext (in Squeak and VisualWorks,

but not standard Smalltalk)

Object-oriented programming and design

11

Page 12: Object-oriented programming and design 1 Smalltalk in a Nutshell Objects & classes Messages & methods Inheritance & metaclasses

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

Page 13: Object-oriented programming and design 1 Smalltalk in a Nutshell Objects & classes Messages & methods Inheritance & metaclasses

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

Page 14: Object-oriented programming and design 1 Smalltalk in a Nutshell Objects & classes Messages & methods Inheritance & metaclasses

EmployeeTransactionEmployeeTransactiondatedate

PaycheckPaycheck check1check107/09/9507/09/95

aPaycheck dateaPaycheck date

Page 15: Object-oriented programming and design 1 Smalltalk in a Nutshell Objects & classes Messages & methods Inheritance & metaclasses

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.

......

Page 16: Object-oriented programming and design 1 Smalltalk in a Nutshell Objects & classes Messages & methods Inheritance & metaclasses

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'.

Page 17: Object-oriented programming and design 1 Smalltalk in a Nutshell Objects & classes Messages & methods Inheritance & metaclasses

Object-oriented programming and design

17

(continued)

ralph changeSalaryFor: day1 to: 20.

payroll addEmployee: ralph.

self

employee: ralph

hours: self aLittleOvertime

starting: day1.

^payroll

Page 18: Object-oriented programming and design 1 Smalltalk in a Nutshell Objects & classes Messages & methods Inheritance & metaclasses

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.

Page 19: Object-oriented programming and design 1 Smalltalk in a Nutshell Objects & classes Messages & methods Inheritance & metaclasses

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

Page 20: Object-oriented programming and design 1 Smalltalk in a Nutshell Objects & classes Messages & methods Inheritance & metaclasses

Object-oriented programming and design

20

Instance initializing method

For class Date

day: dayInteger year: yearInteger

day := dayInteger.

year := yearInteger

Page 21: Object-oriented programming and design 1 Smalltalk in a Nutshell Objects & classes Messages & methods Inheritance & metaclasses

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

Page 22: Object-oriented programming and design 1 Smalltalk in a Nutshell Objects & classes Messages & methods Inheritance & metaclasses

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

Page 23: Object-oriented programming and design 1 Smalltalk in a Nutshell Objects & classes Messages & methods Inheritance & metaclasses

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

Page 24: Object-oriented programming and design 1 Smalltalk in a Nutshell Objects & classes Messages & methods Inheritance & metaclasses

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

Page 25: Object-oriented programming and design 1 Smalltalk in a Nutshell Objects & classes Messages & methods Inheritance & metaclasses

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