SlideShare a Scribd company logo
1 of 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 (VW)
S.Ducasse 5
Fill the Template (VW)
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: (Sq)
A template is proposed by the browser:
NameOfSuperclass subclass: #NameOfClass
instanceVariableNames: 'instVarName1
instVarName2'
classVariableNames: 'ClassVarName1
ClassVarName2'
poolDictionaries: ''
category: 'CategoryName’
S.Ducasse 7
Filling the Template (Sq)
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

More Related Content

What's hot

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
 
2013 lecture-02-syntax shortnewcut
2013 lecture-02-syntax shortnewcut2013 lecture-02-syntax shortnewcut
2013 lecture-02-syntax shortnewcutPharo
 
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
 
C# Delegates and Event Handling
C# Delegates and Event HandlingC# Delegates and Event Handling
C# Delegates and Event HandlingJussi Pohjolainen
 

What's hot (13)

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
 
02 basics
02 basics02 basics
02 basics
 
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...
 
04 threads
04 threads04 threads
04 threads
 
2013 lecture-02-syntax shortnewcut
2013 lecture-02-syntax shortnewcut2013 lecture-02-syntax shortnewcut
2013 lecture-02-syntax shortnewcut
 
Inheritance
InheritanceInheritance
Inheritance
 
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
 
C# Delegates and Event Handling
C# Delegates and Event HandlingC# Delegates and Event Handling
C# Delegates and Event Handling
 
Net
NetNet
Net
 

Similar to 9 - OOP - Smalltalk Classes (a)

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
 

Similar to 9 - OOP - Smalltalk Classes (a) (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 432-singleton
Stoop 432-singletonStoop 432-singleton
Stoop 432-singleton
 
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)
 
8 - OOP - Syntax & Messages
8 - OOP - Syntax & Messages8 - OOP - Syntax & Messages
8 - OOP - Syntax & Messages
 
Test beautycleanness
Test beautycleannessTest beautycleanness
Test beautycleanness
 
10 - OOP - Inheritance (a)
10 - OOP - Inheritance (a)10 - OOP - Inheritance (a)
10 - OOP - Inheritance (a)
 
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)
 
4 - OOP - Taste of Smalltalk (Tamagoshi)
4 - OOP - Taste of Smalltalk (Tamagoshi)4 - OOP - Taste of Smalltalk (Tamagoshi)
4 - OOP - Taste of Smalltalk (Tamagoshi)
 
Stoop ed-lod
Stoop ed-lodStoop ed-lod
Stoop ed-lod
 
Stoop 303-advanced blocks
Stoop 303-advanced blocksStoop 303-advanced blocks
Stoop 303-advanced blocks
 
06 inheritance
06 inheritance06 inheritance
06 inheritance
 
10 - OOP - Inheritance (b)
10 - OOP - Inheritance (b)10 - OOP - Inheritance (b)
10 - OOP - Inheritance (b)
 
Stoop 423-some designpatterns
Stoop 423-some designpatternsStoop 423-some designpatterns
Stoop 423-some designpatterns
 
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 ed-unit ofreuse
Stoop ed-unit ofreuseStoop ed-unit ofreuse
Stoop ed-unit ofreuse
 
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
 
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
 

More from The World of Smalltalk (20)

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
 
09 metaclasses
09 metaclasses09 metaclasses
09 metaclasses
 
08 refactoring
08 refactoring08 refactoring
08 refactoring
 
06 debugging
06 debugging06 debugging
06 debugging
 
05 seaside
05 seaside05 seaside
05 seaside
 
03 standardclasses
03 standardclasses03 standardclasses
03 standardclasses
 
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 sed-class initialization
Stoop sed-class initializationStoop sed-class initialization
Stoop sed-class initialization
 
Stoop metaclasses
Stoop metaclassesStoop metaclasses
Stoop metaclasses
 
Stoop ed-subtyping subclassing
Stoop ed-subtyping subclassingStoop ed-subtyping subclassing
Stoop ed-subtyping subclassing
 
Stoop ed-some principles
Stoop ed-some principlesStoop ed-some principles
Stoop ed-some principles
 
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
 

Recently uploaded

Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Celine George
 
Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)cama23
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfJemuel Francisco
 
Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4JOYLYNSAMANIEGO
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designMIPLM
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Mark Reed
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...JhezDiaz1
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxAshokKarra1
 
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...JojoEDelaCruz
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxlancelewisportillo
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptxmary850239
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfTechSoup
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...Postal Advocate Inc.
 
4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptxmary850239
 
Activity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translationActivity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translationRosabel UA
 
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfVirtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfErwinPantujan2
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxCarlos105
 

Recently uploaded (20)

Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
 
Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
 
Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-design
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptx
 
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
 
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptxLEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
 
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptxYOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
 
4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx
 
Activity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translationActivity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translation
 
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfVirtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
 

9 - OOP - Smalltalk Classes (a)

  • 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 (VW)
  • 5. S.Ducasse 5 Fill the Template (VW) 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: (Sq) 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 Template (Sq) 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