Upload
bergel
View
2.056
Download
2
Embed Size (px)
DESCRIPTION
After a short introduction to object orientation, this lecture gives an overview of the Smalltalk programming language. This lecture was given on November 10, 2008, during the summer school on programming languages (http://pleiad.dcc.uchile.cl/pl2008/)
Citation preview
!"#$Object-oriented programming in
SmalltalkAlexandre Bergel
RMoD team, INRIA,Lille, France
• Gain elementary notions of object orientation
• Learn what is a pure object oriented language
• Increase the programming language section on your CV
Goal of this lecture
Outline
1. Essence of class-based object orientation
2. Object oriented programming with Smalltalk
3. Smalltalk in Action
Outline
1. Essence of class-based object orientation
2. Object oriented programming with Smalltalk
3. Smalltalk in Action
Procedural programming
(Flatt 98)
color = data shape = behavior
Procedural programming
(Flatt 98)
Expressing combination
!"#$
Example in C
struct Point { const void * class; int x, y;
};
void draw (void * self) {switch(self[0]) {
case POINT_CLASS: ...case CIRCLE_CLASS: ...
}}
struct Circle { const void * class; int x, y, rad;
};
void move (void * self) {switch(self[0]) {
case POINT_CLASS: ...case CIRCLE_CLASS: ...
}}
Expressing extension
Expressing extension
Situation with some difficulties
Object-oriented programming
• Simula 67 addressed the combinational explosion of data and behavior
• a class is an object factory
• class = superclass + attributes + methods
• object reactive to messages
Object as programming entities
name=Billcourses={c1, c2}
a student
printDescription()
Object as programming entities
name=Billcourses={c1, c2}
instance
a student
printDescription()
description()printDescription()
agegender
Person
description()courses
Student
Transcript show: self description
^ 'My name is ', name
super description, ' and I follows ', courses
Object as programming entities
name=Billcourses={c1, c2}
instance
a student
printDescription()
description()printDescription()
agegender
Person
description()courses
Student
Transcript show: self description
^ 'My name is ', name
super description, ' and I follows ', courses
1
Object as programming entities
name=Billcourses={c1, c2}
instance
a student
description()
description()printDescription()
agegender
Person
description()courses
Student
Transcript show: self description
^ 'My name is ', name
super description, ' and I follows ', courses
1
2
Object as programming entities
name=Billcourses={c1, c2}
instance
a student
superdescription()
description()printDescription()
agegender
Person
description()courses
Student
Transcript show: self description
^ 'My name is ', name
super description, ' and I follows ', courses
1
2
3
Object as programming entities
name=Billcourses={c1, c2}
instance
a student
superdescription()
description()printDescription()
agegender
Person
description()courses
Student
Transcript show: self description
^ 'My name is ', name
super description, ' and I follows ', courses
1
2
3
Object-oriented programming
Encapsulation
Composition
Distribution ofResponsibility
Message Passing
Inheritance
Abstraction & Information Hiding
Nested Objects
Separation of concerns (e.g., HTML, CSS)
Delegating responsibility
Conceptual hierarchy, polymorphism and reuse
Outline
1. Essence of class-based object orientation
2. Object oriented programming with Smalltalk
3. Smalltalk in Action
Smalltalk
The origin of Smalltalk
Alan Kay’s Dynabook project (1968)
Alto — Xerox PARC (1973)
Smalltalk today
Smalltalk today
Smalltalk today
Smalltalk today
Smalltalk today
Squeak resources
Free download — Print-on-demand
SqueakByExample.org
www.squeak.org
Downloads and links
www.seaside.st
One-click image
Getting started
Everything is an object
Everything happens by sending messages
Running Squeak
Do it, print it
You can evaluate any expression anywhere
in Smalltalk
Standard development tools
Debuggers, Inspectors, Explorers
Syntax in a Nutshell
Three kind of messages
5 factorialTranscript cr
3 + 4
3 raisedTo: 10 modulo: 5
Transcript show: 'hello world'
Unary messages
Binary messages
Keyword messages
A typical method in the class Point
<= aPoint "Answer whether the receiver is neither below nor to the right of aPoint."
^ x <= aPoint x and: [y <= aPoint y]
(2@3) <= (5@6) true
Method name Argument Comment
Return Binary messageKeyword messageInstance variable
Block
Statements and cascades
| p pen |p := [email protected] := Pen new.pen up.pen goto: p; down; goto: p+p
Temporary variablesStatement
Cascade
Control structures
max: aNumber ^ self < aNumber ifTrue: [aNumber] ifFalse: [self]
4 timesRepeat: [Beeper beep]
Every control structure is realized by message sends
Control structures
max: aNumber ^ self < aNumber ifTrue: [aNumber] ifFalse: [self]
4 timesRepeat: [Beeper beep]
Every control structure is realized by message sends
ifTrue:ifFalse:
Boolean
ifTrue:ifFalse:
True
ifTrue:ifFalse:
False
ifTrue: t ifFalse: f
^ t value
ifTrue: t ifFalse: f
^ f value
Creating classes
Number subclass: #Complex instanceVariableNames: 'real imaginary' classVariableNames: '' poolDictionaries: '' category: 'ComplexNumbers'
Send a message to a class (!)
Outline
1. Essence of class-based object orientation
2. Object oriented programming with Smalltalk
3. Smalltalk in Action