44
!"#$ Object-oriented programming in Smalltalk Alexandre Bergel RMoD team, INRIA, Lille, France [email protected]

2008 Sccc Smalltalk

  • 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

Page 1: 2008 Sccc Smalltalk

!"#$Object-oriented programming in

SmalltalkAlexandre Bergel

RMoD team, INRIA,Lille, France

[email protected]

Page 2: 2008 Sccc Smalltalk

• 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

Page 3: 2008 Sccc Smalltalk

Outline

1. Essence of class-based object orientation

2. Object oriented programming with Smalltalk

3. Smalltalk in Action

Page 4: 2008 Sccc Smalltalk

Outline

1. Essence of class-based object orientation

2. Object oriented programming with Smalltalk

3. Smalltalk in Action

Page 5: 2008 Sccc Smalltalk

Procedural programming

(Flatt 98)

color = data shape = behavior

Page 6: 2008 Sccc Smalltalk

Procedural programming

(Flatt 98)

Page 7: 2008 Sccc Smalltalk

Expressing combination

Page 8: 2008 Sccc Smalltalk

!"#$

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

}}

Page 9: 2008 Sccc Smalltalk

Expressing extension

Page 10: 2008 Sccc Smalltalk

Expressing extension

Page 11: 2008 Sccc Smalltalk

Situation with some difficulties

Page 12: 2008 Sccc Smalltalk

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

Page 13: 2008 Sccc Smalltalk

Object as programming entities

name=Billcourses={c1, c2}

a student

printDescription()

Page 14: 2008 Sccc Smalltalk

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

Page 15: 2008 Sccc Smalltalk

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

Page 16: 2008 Sccc Smalltalk

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

Page 17: 2008 Sccc Smalltalk

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

Page 18: 2008 Sccc Smalltalk

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

Page 19: 2008 Sccc Smalltalk

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

Page 20: 2008 Sccc Smalltalk

Outline

1. Essence of class-based object orientation

2. Object oriented programming with Smalltalk

3. Smalltalk in Action

Page 21: 2008 Sccc Smalltalk

Smalltalk

Page 22: 2008 Sccc Smalltalk

The origin of Smalltalk

Alan Kay’s Dynabook project (1968)

Alto — Xerox PARC (1973)

Page 23: 2008 Sccc Smalltalk

Smalltalk today

Page 24: 2008 Sccc Smalltalk

Smalltalk today

Page 25: 2008 Sccc Smalltalk

Smalltalk today

Page 26: 2008 Sccc Smalltalk

Smalltalk today

Page 27: 2008 Sccc Smalltalk

Smalltalk today

Page 28: 2008 Sccc Smalltalk

Squeak resources

Free download — Print-on-demand

SqueakByExample.org

www.squeak.org

Downloads and links

www.seaside.st

One-click image

Page 29: 2008 Sccc Smalltalk

Getting started

Page 30: 2008 Sccc Smalltalk

Everything is an object

Page 31: 2008 Sccc Smalltalk

Everything happens by sending messages

Page 32: 2008 Sccc Smalltalk

Running Squeak

Page 33: 2008 Sccc Smalltalk

Do it, print it

You can evaluate any expression anywhere

in Smalltalk

Page 34: 2008 Sccc Smalltalk

Standard development tools

Page 35: 2008 Sccc Smalltalk

Debuggers, Inspectors, Explorers

Page 36: 2008 Sccc Smalltalk

Syntax in a Nutshell

Page 37: 2008 Sccc Smalltalk

Three kind of messages

5 factorialTranscript cr

3 + 4

3 raisedTo: 10 modulo: 5

Transcript show: 'hello world'

Unary messages

Binary messages

Keyword messages

Page 38: 2008 Sccc Smalltalk

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

Page 39: 2008 Sccc Smalltalk

Statements and cascades

| p pen |p := [email protected] := Pen new.pen up.pen goto: p; down; goto: p+p

Temporary variablesStatement

Cascade

Page 40: 2008 Sccc Smalltalk

Control structures

max: aNumber ^ self < aNumber ifTrue: [aNumber] ifFalse: [self]

4 timesRepeat: [Beeper beep]

Every control structure is realized by message sends

Page 41: 2008 Sccc Smalltalk

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

Page 42: 2008 Sccc Smalltalk

Creating classes

Number subclass: #Complex instanceVariableNames: 'real imaginary' classVariableNames: '' poolDictionaries: '' category: 'ComplexNumbers'

Send a message to a class (!)

Page 43: 2008 Sccc Smalltalk

Outline

1. Essence of class-based object orientation

2. Object oriented programming with Smalltalk

3. Smalltalk in Action

Page 44: 2008 Sccc Smalltalk