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

Mpl 9 oop
Mpl 9 oopMpl 9 oop
Mpl 9 oop
AHHAAH
Ā 
C# Delegates and Event Handling
C# Delegates and Event HandlingC# Delegates and Event Handling
C# Delegates and Event Handling
Jussi 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)

Stoop 414-smalltalk elementsofdesign
Stoop 414-smalltalk elementsofdesignStoop 414-smalltalk elementsofdesign
Stoop 414-smalltalk elementsofdesign
The World of Smalltalk
Ā 
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
Ā 
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)
The World of Smalltalk
Ā 
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.pdf
kakarthik685
Ā 
Stoop 300-block optimizationinvw
Stoop 300-block optimizationinvwStoop 300-block optimizationinvw
Stoop 300-block optimizationinvw
The World of Smalltalk
Ā 

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

1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
Ā 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
KarakKing
Ā 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
Ā 

Recently uploaded (20)

Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
Ā 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
Ā 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
Ā 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
Ā 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
Ā 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
Ā 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
Ā 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
Ā 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
Ā 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
Ā 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
Ā 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
Ā 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
Ā 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
Ā 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
Ā 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
Ā 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
Ā 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Ā 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
Ā 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
Ā 

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