SlideShare ist ein Scribd-Unternehmen logo
1 von 26
S.Ducasse 1
QuickTime™ and aTIFF (Uncompressed) decompressorare needed to see this picture.
Stéphane Ducasse
Stephane.Ducasse@univ-savoie.fr
http://www.listic.univ-savoie.fr/~ducasse/
Some Points on Classes
S.Ducasse 2
License: CC-Attribution-ShareAlike 2.0
http://creativecommons.org/licenses/by-sa/2.0/
S.Ducasse 3
Outline
• Class definition
• Method definition
• Basic class instantiation
S.Ducasse 4
A template is proposed by the browser:
Smalltalk defineClass: #NameOfClass
superclass: #{NameOfSuperclass}
indexedType: #none
private: false
instanceVariableNames: 'instVarName1
instVarName2'
classInstanceVariableNames: ''
imports: ''
category: ''
Class Definition:The Class Packet
S.Ducasse 5
Fill the Template
Smalltalk defineClass: #Packet
superclass: #{Object}
indexedType: #none
private: false
instanceVariableNames: 'contents addressee
originator'
classInstanceVariableNames: ''
imports: ''
category: 'LAN'
Automatically a class named “Packet class” is created. Packet
is the unique instance of “Packet class”.To see it, click on the
class button in the browser
S.Ducasse 6
Class Definition
A template is proposed by the browser:
NameOfSuperclass subclass: #NameOfClass
instanceVariableNames: 'instVarName1 instVarName2'
classVariableNames: 'ClassVarName1 ClassVarName2'
poolDictionaries: ''
category: 'CategoryName’
S.Ducasse 7
Filling the Templates
Just fill this Template in:
Object subclass: #Packet
instanceVariableNames: 'contents addressee
originator '
classVariableNames: ''
poolDictionaries: ''
category: 'LAN-Simulation’
Automatically a class named “Packet class” is created.
Packet is the unique instance of Packet class.To see it,
click on the class button in the browser
S.Ducasse 8
Named InstanceVariables
instanceVariableNames: 'instVarName1 instVarName2'
...
instanceVariableNames: 'contents addressee originator '
...
•Begins with a lowercase letter
•Explicitly declared: a list of instance variables
•Name should be unique in the inheritance chain
•Default value of instance variable is nil
•Private to the instance: instance based (vs. C++ class-based)
•Can be accessed by all the methods of the class and its
subclasses
•Instance variables cannot be accessed by class methods.
•A client cannot directly access instance variables.
•The clients must use accessors to access an instance variable.
S.Ducasse 9
Roadmap
• Class definition
• Method definition
• Basic class instantiation
S.Ducasse 10
Method Definition
• Fill in the template. For example:
Packet>>defaultContents
“returns the default contents of a Packet”
^ ‘contents no specified’
Workstation>>originate: aPacket
aPacket originator: self.
self send: aPacket
• How to invoke a method on the same object? Send the message
to self
Packet>>isAddressedTo: aNode
“returns true if I’m addressed to the node aNode”
^ self addressee = aNode name
S.Ducasse 11
Accessing InstanceVariables
Using direct access for the methods of the class
Packet>>isSentBy: aNode
^ originator = aNode
is equivalent to use accessors
Packet>>originator
^ originator
Packet>>isSentBy: aNode
^ self originator = aNode
Design Hint: Do not directly access instance variables of
a superclass from subclass methods.This way classes are
not strongly linked.
S.Ducasse 12
Methods always return aValue
• Message = effect + return value
• By default, a method returns self
• In a method body, the ^ expression returns the value of the
expression as the result of the method execution.
Node>>accept: thePacket
self send: thePacket
This is equivalent to:
Node>>accept: thePacket
self send: thePacket.
^self
12
S.Ducasse 13
Methods always return a value
• If we want to return the value returned by #send:
Node>>accept: thePacket
^self send: thePacket.
• Use ^ self to notify the reader that something abnormal is
arriving
MyClass>>foo
…
^ self
S.Ducasse 14
Some Naming Conventions
• Shared variables begin with an upper case letter
• Private variables begin with a lower case letter
• For accessors, use the same name as the instance
variable accessed:
Packet>>addressee
^ addressee
Packet>>addressee: aSymbol
addressee := aSymbol
S.Ducasse 15
Some Naming Conventions
• Use imperative verbs for methods performing an action
like #openOn:, #close, #sleep
• For predicate methods (returning a boolean) prefix the
method with is or has
• Ex: isNil, isAddressedTo:, isSentBy:
• For converting methods prefix the method with as
• Ex: asString
S.Ducasse 16
Roadmap
• Class definition
• Method definition
• Basic class instantiation
S.Ducasse 17
Object Instantiation
Objects can be created by:
- Direct Instance creation: new/new:
- Messages to instances that create other objects
- Class specific instantiation messages
S.Ducasse 18
Object Creation
-When a class creates an object =
allocating memory + marking it to
be instance of that class
S.Ducasse 19
Instance Creation with new
aClass new
returns a newly and UNINITIALIZED instance
OrderedCollection new -> OrderedCollection ()
Packet new -> aPacket
Default instance variable values are nil
nil is an instance of UndefinedObject and only
understands a limited set of messages
S.Ducasse 20
Messages to Instances
Messages to Instances that create Objects
1 to: 6 (an
interval)
1@2 (a point)
(0@0) extent: (100@100) (a rectangle)
#lulu asString (a string)
1 printString (a
string)
3 asFloat (a float)
#(23 2 3 4) asSortedCollection
(a
sortedCollection)
S.Ducasse 21
Opening the Box
1 to: 6
creates an interval
Number>>to: stop
"Answer an Interval from the receiver up to the argument,
stop, with each next element computed by incrementing the
previous one by 1."
^Interval from: self to: stop by: 1
S.Ducasse 22
Strings...
1 printString
Object>>printString
"Answer a String whose characters are a description
of the receiver."
| aStream |
aStream := WriteStream on: (String new: 16).
self printOn: aStream.
^ aStream contents
S.Ducasse 23
Instance Creation
1@2
creates a point
Number>>@ y
"Answer a new Point whose x value is the receiver and
whose y value is the argument."
<primitive: 18>
^ Point x: self y: y
S.Ducasse 24
Class-specific Messages
Array with: 1 with: 'lulu'
OrderedCollection with: 1 with: 2 with: 3
Rectangle fromUser -> 179@95 corner: 409@219
Browser browseAllImplementorsOf: #at:put:
Packet send:‘Hello mac’ to: #mac
Workstation withName: #mac
S.Ducasse 25
new and new:
• new:/basicNew: is used to specify the size of the
created instance
Array new: 4 -> #(nil nil nil nil)
• new/new: can be specialized to define customized
creation
• basicNew/basicNew: should never be overridden
• #new/basicNew and new:/basicNew: are class methods
S.Ducasse 26
Summary
How to define a class?
What are instance variables?
How to define a method?
Instances creation methods

Weitere ähnliche Inhalte

Was ist angesagt?

Class notes(week 6) on inheritance and multiple inheritance
Class notes(week 6) on inheritance and multiple inheritanceClass notes(week 6) on inheritance and multiple inheritance
Class notes(week 6) on inheritance and multiple inheritanceKuntal Bhowmick
 
Mpl 9 oop
Mpl 9 oopMpl 9 oop
Mpl 9 oopAHHAAH
 
Using Combine, SwiftUI and callAsFunction to build an experimental localizati...
Using Combine, SwiftUI and callAsFunction to build an experimental localizati...Using Combine, SwiftUI and callAsFunction to build an experimental localizati...
Using Combine, SwiftUI and callAsFunction to build an experimental localizati...Donny Wals
 
Scaling modern JVM applications with Akka toolkit
Scaling modern JVM applications with Akka toolkitScaling modern JVM applications with Akka toolkit
Scaling modern JVM applications with Akka toolkitBojan Babic
 
2013 lecture-02-syntax shortnewcut
2013 lecture-02-syntax shortnewcut2013 lecture-02-syntax shortnewcut
2013 lecture-02-syntax shortnewcutPharo
 
C# Delegates and Event Handling
C# Delegates and Event HandlingC# Delegates and Event Handling
C# Delegates and Event HandlingJussi Pohjolainen
 

Was ist angesagt? (13)

Stoop 432-singleton
Stoop 432-singletonStoop 432-singleton
Stoop 432-singleton
 
Chapter ii(oop)
Chapter ii(oop)Chapter ii(oop)
Chapter ii(oop)
 
04 idioms
04 idioms04 idioms
04 idioms
 
Class notes(week 6) on inheritance and multiple inheritance
Class notes(week 6) on inheritance and multiple inheritanceClass notes(week 6) on inheritance and multiple inheritance
Class notes(week 6) on inheritance and multiple inheritance
 
07 bestpractice
07 bestpractice07 bestpractice
07 bestpractice
 
Mpl 9 oop
Mpl 9 oopMpl 9 oop
Mpl 9 oop
 
Using Combine, SwiftUI and callAsFunction to build an experimental localizati...
Using Combine, SwiftUI and callAsFunction to build an experimental localizati...Using Combine, SwiftUI and callAsFunction to build an experimental localizati...
Using Combine, SwiftUI and callAsFunction to build an experimental localizati...
 
Stoop 416-lsp
Stoop 416-lspStoop 416-lsp
Stoop 416-lsp
 
02 basics
02 basics02 basics
02 basics
 
Scaling modern JVM applications with Akka toolkit
Scaling modern JVM applications with Akka toolkitScaling modern JVM applications with Akka toolkit
Scaling modern JVM applications with Akka toolkit
 
Inheritance
InheritanceInheritance
Inheritance
 
2013 lecture-02-syntax shortnewcut
2013 lecture-02-syntax shortnewcut2013 lecture-02-syntax shortnewcut
2013 lecture-02-syntax shortnewcut
 
C# Delegates and Event Handling
C# Delegates and Event HandlingC# Delegates and Event Handling
C# Delegates and Event Handling
 

Andere mochten auch (20)

03 standardclasses
03 standardclasses03 standardclasses
03 standardclasses
 
Stoop sed-class initialization
Stoop sed-class initializationStoop sed-class initialization
Stoop sed-class initialization
 
Stoop ed-lod
Stoop ed-lodStoop ed-lod
Stoop ed-lod
 
Double Dispatch
Double DispatchDouble Dispatch
Double Dispatch
 
Stoop metaclasses
Stoop metaclassesStoop metaclasses
Stoop metaclasses
 
Debugging VisualWorks
Debugging VisualWorksDebugging VisualWorks
Debugging VisualWorks
 
10 - OOP - Inheritance (b)
10 - OOP - Inheritance (b)10 - OOP - Inheritance (b)
10 - OOP - Inheritance (b)
 
Stoop 413-abstract classes
Stoop 413-abstract classesStoop 413-abstract classes
Stoop 413-abstract classes
 
Stoop ed-dual interface
Stoop ed-dual interfaceStoop ed-dual interface
Stoop ed-dual interface
 
15 - Streams
15 - Streams15 - Streams
15 - Streams
 
Stoop 400 o-metaclassonly
Stoop 400 o-metaclassonlyStoop 400 o-metaclassonly
Stoop 400 o-metaclassonly
 
Stoop 415-design points
Stoop 415-design pointsStoop 415-design points
Stoop 415-design points
 
Stoop 400-metaclass only
Stoop 400-metaclass onlyStoop 400-metaclass only
Stoop 400-metaclass only
 
Stoop 421-design heuristics
Stoop 421-design heuristicsStoop 421-design heuristics
Stoop 421-design heuristics
 
Stoop ed-unit ofreuse
Stoop ed-unit ofreuseStoop ed-unit ofreuse
Stoop ed-unit ofreuse
 
Stoop 423-some designpatterns
Stoop 423-some designpatternsStoop 423-some designpatterns
Stoop 423-some designpatterns
 
09 metaclasses
09 metaclasses09 metaclasses
09 metaclasses
 
8 - OOP - Smalltalk Model
8 - OOP - Smalltalk Model8 - OOP - Smalltalk Model
8 - OOP - Smalltalk Model
 
Stoop 301-internal objectstructureinvw
Stoop 301-internal objectstructureinvwStoop 301-internal objectstructureinvw
Stoop 301-internal objectstructureinvw
 
Stoop ed-some principles
Stoop ed-some principlesStoop ed-some principles
Stoop ed-some principles
 

Ähnlich wie 9 - OOP - Smalltalk Classes (b)

4 - OOP - Taste of Smalltalk (VisualWorks)
4 - OOP - Taste of Smalltalk (VisualWorks)4 - OOP - Taste of Smalltalk (VisualWorks)
4 - OOP - Taste of Smalltalk (VisualWorks)The World of Smalltalk
 
Test beautycleanness
Test beautycleannessTest beautycleanness
Test beautycleannessbergel
 
4 - OOP - Taste of Smalltalk (Tamagoshi)
4 - OOP - Taste of Smalltalk (Tamagoshi)4 - OOP - Taste of Smalltalk (Tamagoshi)
4 - OOP - Taste of Smalltalk (Tamagoshi)The World of Smalltalk
 
Java-Module 3-PPT-TPS.pdf.Java-Module 3-PPT-TPS.pdf
Java-Module 3-PPT-TPS.pdf.Java-Module 3-PPT-TPS.pdfJava-Module 3-PPT-TPS.pdf.Java-Module 3-PPT-TPS.pdf
Java-Module 3-PPT-TPS.pdf.Java-Module 3-PPT-TPS.pdfkakarthik685
 
ITT 2015 - Ash Furrow - Lessons from Production Swift
ITT 2015 - Ash Furrow - Lessons from Production SwiftITT 2015 - Ash Furrow - Lessons from Production Swift
ITT 2015 - Ash Furrow - Lessons from Production SwiftIstanbul Tech Talks
 
Class object method constructors in java
Class object method constructors in javaClass object method constructors in java
Class object method constructors in javaRaja Sekhar
 

Ähnlich wie 9 - OOP - Smalltalk Classes (b) (20)

Stoop 414-smalltalk elementsofdesign
Stoop 414-smalltalk elementsofdesignStoop 414-smalltalk elementsofdesign
Stoop 414-smalltalk elementsofdesign
 
Stoop 304-metaclasses
Stoop 304-metaclassesStoop 304-metaclasses
Stoop 304-metaclasses
 
Stoop 423-smalltalk idioms
Stoop 423-smalltalk idiomsStoop 423-smalltalk idioms
Stoop 423-smalltalk idioms
 
4 - OOP - Taste of Smalltalk (VisualWorks)
4 - OOP - Taste of Smalltalk (VisualWorks)4 - OOP - Taste of Smalltalk (VisualWorks)
4 - OOP - Taste of Smalltalk (VisualWorks)
 
10 - OOP - Inheritance (a)
10 - OOP - Inheritance (a)10 - OOP - Inheritance (a)
10 - OOP - Inheritance (a)
 
8 - OOP - Syntax & Messages
8 - OOP - Syntax & Messages8 - OOP - Syntax & Messages
8 - OOP - Syntax & Messages
 
5 - OOP - Smalltalk in a Nutshell (b)
5 - OOP - Smalltalk in a Nutshell (b)5 - OOP - Smalltalk in a Nutshell (b)
5 - OOP - Smalltalk in a Nutshell (b)
 
Test beautycleanness
Test beautycleannessTest beautycleanness
Test beautycleanness
 
4 - OOP - Taste of Smalltalk (Tamagoshi)
4 - OOP - Taste of Smalltalk (Tamagoshi)4 - OOP - Taste of Smalltalk (Tamagoshi)
4 - OOP - Taste of Smalltalk (Tamagoshi)
 
06 inheritance
06 inheritance06 inheritance
06 inheritance
 
Stoop 303-advanced blocks
Stoop 303-advanced blocksStoop 303-advanced blocks
Stoop 303-advanced blocks
 
Java-Module 3-PPT-TPS.pdf.Java-Module 3-PPT-TPS.pdf
Java-Module 3-PPT-TPS.pdf.Java-Module 3-PPT-TPS.pdfJava-Module 3-PPT-TPS.pdf.Java-Module 3-PPT-TPS.pdf
Java-Module 3-PPT-TPS.pdf.Java-Module 3-PPT-TPS.pdf
 
Stoop 433-chain
Stoop 433-chainStoop 433-chain
Stoop 433-chain
 
6 - OOP - LAN Example
6 - OOP - LAN Example6 - OOP - LAN Example
6 - OOP - LAN Example
 
Stoop 300-block optimizationinvw
Stoop 300-block optimizationinvwStoop 300-block optimizationinvw
Stoop 300-block optimizationinvw
 
java classes
java classesjava classes
java classes
 
ITT 2015 - Ash Furrow - Lessons from Production Swift
ITT 2015 - Ash Furrow - Lessons from Production SwiftITT 2015 - Ash Furrow - Lessons from Production Swift
ITT 2015 - Ash Furrow - Lessons from Production Swift
 
Class object method constructors in java
Class object method constructors in javaClass object method constructors in java
Class object method constructors in java
 
d.ppt
d.pptd.ppt
d.ppt
 
d.ppt
d.pptd.ppt
d.ppt
 

Mehr von The World of Smalltalk (17)

05 seaside canvas
05 seaside canvas05 seaside canvas
05 seaside canvas
 
99 questions
99 questions99 questions
99 questions
 
13 traits
13 traits13 traits
13 traits
 
12 virtualmachine
12 virtualmachine12 virtualmachine
12 virtualmachine
 
11 bytecode
11 bytecode11 bytecode
11 bytecode
 
10 reflection
10 reflection10 reflection
10 reflection
 
08 refactoring
08 refactoring08 refactoring
08 refactoring
 
06 debugging
06 debugging06 debugging
06 debugging
 
05 seaside
05 seaside05 seaside
05 seaside
 
01 intro
01 intro01 intro
01 intro
 
Stoop sed-smells
Stoop sed-smellsStoop sed-smells
Stoop sed-smells
 
Stoop sed-class initialization
Stoop sed-class initializationStoop sed-class initialization
Stoop sed-class initialization
 
Stoop ed-subtyping subclassing
Stoop ed-subtyping subclassingStoop ed-subtyping subclassing
Stoop ed-subtyping subclassing
 
Stoop ed-inheritance composition
Stoop ed-inheritance compositionStoop ed-inheritance composition
Stoop ed-inheritance composition
 
Stoop ed-frameworks
Stoop ed-frameworksStoop ed-frameworks
Stoop ed-frameworks
 
Stoop 450-s unit
Stoop 450-s unitStoop 450-s unit
Stoop 450-s unit
 
Stoop 440-adaptor
Stoop 440-adaptorStoop 440-adaptor
Stoop 440-adaptor
 

Kürzlich hochgeladen

Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhikauryashika82
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfchloefrazer622
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfAyushMahapatra5
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...fonyou31
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 

Kürzlich hochgeladen (20)

Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 

9 - OOP - Smalltalk Classes (b)

  • 1. S.Ducasse 1 QuickTime™ and aTIFF (Uncompressed) decompressorare needed to see this picture. Stéphane Ducasse Stephane.Ducasse@univ-savoie.fr http://www.listic.univ-savoie.fr/~ducasse/ Some Points on Classes
  • 2. S.Ducasse 2 License: CC-Attribution-ShareAlike 2.0 http://creativecommons.org/licenses/by-sa/2.0/
  • 3. S.Ducasse 3 Outline • Class definition • Method definition • Basic class instantiation
  • 4. S.Ducasse 4 A template is proposed by the browser: Smalltalk defineClass: #NameOfClass superclass: #{NameOfSuperclass} indexedType: #none private: false instanceVariableNames: 'instVarName1 instVarName2' classInstanceVariableNames: '' imports: '' category: '' Class Definition:The Class Packet
  • 5. S.Ducasse 5 Fill the Template Smalltalk defineClass: #Packet superclass: #{Object} indexedType: #none private: false instanceVariableNames: 'contents addressee originator' classInstanceVariableNames: '' imports: '' category: 'LAN' Automatically a class named “Packet class” is created. Packet is the unique instance of “Packet class”.To see it, click on the class button in the browser
  • 6. S.Ducasse 6 Class Definition A template is proposed by the browser: NameOfSuperclass subclass: #NameOfClass instanceVariableNames: 'instVarName1 instVarName2' classVariableNames: 'ClassVarName1 ClassVarName2' poolDictionaries: '' category: 'CategoryName’
  • 7. S.Ducasse 7 Filling the Templates Just fill this Template in: Object subclass: #Packet instanceVariableNames: 'contents addressee originator ' classVariableNames: '' poolDictionaries: '' category: 'LAN-Simulation’ Automatically a class named “Packet class” is created. Packet is the unique instance of Packet class.To see it, click on the class button in the browser
  • 8. S.Ducasse 8 Named InstanceVariables instanceVariableNames: 'instVarName1 instVarName2' ... instanceVariableNames: 'contents addressee originator ' ... •Begins with a lowercase letter •Explicitly declared: a list of instance variables •Name should be unique in the inheritance chain •Default value of instance variable is nil •Private to the instance: instance based (vs. C++ class-based) •Can be accessed by all the methods of the class and its subclasses •Instance variables cannot be accessed by class methods. •A client cannot directly access instance variables. •The clients must use accessors to access an instance variable.
  • 9. S.Ducasse 9 Roadmap • Class definition • Method definition • Basic class instantiation
  • 10. S.Ducasse 10 Method Definition • Fill in the template. For example: Packet>>defaultContents “returns the default contents of a Packet” ^ ‘contents no specified’ Workstation>>originate: aPacket aPacket originator: self. self send: aPacket • How to invoke a method on the same object? Send the message to self Packet>>isAddressedTo: aNode “returns true if I’m addressed to the node aNode” ^ self addressee = aNode name
  • 11. S.Ducasse 11 Accessing InstanceVariables Using direct access for the methods of the class Packet>>isSentBy: aNode ^ originator = aNode is equivalent to use accessors Packet>>originator ^ originator Packet>>isSentBy: aNode ^ self originator = aNode Design Hint: Do not directly access instance variables of a superclass from subclass methods.This way classes are not strongly linked.
  • 12. S.Ducasse 12 Methods always return aValue • Message = effect + return value • By default, a method returns self • In a method body, the ^ expression returns the value of the expression as the result of the method execution. Node>>accept: thePacket self send: thePacket This is equivalent to: Node>>accept: thePacket self send: thePacket. ^self 12
  • 13. S.Ducasse 13 Methods always return a value • If we want to return the value returned by #send: Node>>accept: thePacket ^self send: thePacket. • Use ^ self to notify the reader that something abnormal is arriving MyClass>>foo … ^ self
  • 14. S.Ducasse 14 Some Naming Conventions • Shared variables begin with an upper case letter • Private variables begin with a lower case letter • For accessors, use the same name as the instance variable accessed: Packet>>addressee ^ addressee Packet>>addressee: aSymbol addressee := aSymbol
  • 15. S.Ducasse 15 Some Naming Conventions • Use imperative verbs for methods performing an action like #openOn:, #close, #sleep • For predicate methods (returning a boolean) prefix the method with is or has • Ex: isNil, isAddressedTo:, isSentBy: • For converting methods prefix the method with as • Ex: asString
  • 16. S.Ducasse 16 Roadmap • Class definition • Method definition • Basic class instantiation
  • 17. S.Ducasse 17 Object Instantiation Objects can be created by: - Direct Instance creation: new/new: - Messages to instances that create other objects - Class specific instantiation messages
  • 18. S.Ducasse 18 Object Creation -When a class creates an object = allocating memory + marking it to be instance of that class
  • 19. S.Ducasse 19 Instance Creation with new aClass new returns a newly and UNINITIALIZED instance OrderedCollection new -> OrderedCollection () Packet new -> aPacket Default instance variable values are nil nil is an instance of UndefinedObject and only understands a limited set of messages
  • 20. S.Ducasse 20 Messages to Instances Messages to Instances that create Objects 1 to: 6 (an interval) 1@2 (a point) (0@0) extent: (100@100) (a rectangle) #lulu asString (a string) 1 printString (a string) 3 asFloat (a float) #(23 2 3 4) asSortedCollection (a sortedCollection)
  • 21. S.Ducasse 21 Opening the Box 1 to: 6 creates an interval Number>>to: stop "Answer an Interval from the receiver up to the argument, stop, with each next element computed by incrementing the previous one by 1." ^Interval from: self to: stop by: 1
  • 22. S.Ducasse 22 Strings... 1 printString Object>>printString "Answer a String whose characters are a description of the receiver." | aStream | aStream := WriteStream on: (String new: 16). self printOn: aStream. ^ aStream contents
  • 23. S.Ducasse 23 Instance Creation 1@2 creates a point Number>>@ y "Answer a new Point whose x value is the receiver and whose y value is the argument." <primitive: 18> ^ Point x: self y: y
  • 24. S.Ducasse 24 Class-specific Messages Array with: 1 with: 'lulu' OrderedCollection with: 1 with: 2 with: 3 Rectangle fromUser -> 179@95 corner: 409@219 Browser browseAllImplementorsOf: #at:put: Packet send:‘Hello mac’ to: #mac Workstation withName: #mac
  • 25. S.Ducasse 25 new and new: • new:/basicNew: is used to specify the size of the created instance Array new: 4 -> #(nil nil nil nil) • new/new: can be specialized to define customized creation • basicNew/basicNew: should never be overridden • #new/basicNew and new:/basicNew: are class methods
  • 26. S.Ducasse 26 Summary How to define a class? What are instance variables? How to define a method? Instances creation methods