Stéphane Ducasse 1 Some Points on Classes.

Slides:



Advertisements
Similar presentations
Object Oriented Programming Chapter 7 Programming Languages by Ravi Sethi.
Advertisements

Smalltalk Coding Patterns mostly from Smalltalk Best Practice Patterns Kent Beck Prentice-Hall, 1997.
Stéphane Ducasse«ChapterNr».1 Selected Design Patterns Design Patterns are recurrent solutions to design problems They are pros and cons We already saw:
Classes and Objects: Recap A typical Java program creates many objects which interact with one another by sending messages. Through the objects interactions,
2. Smalltalk Basics. © Oscar Nierstrasz ST — Smalltalk Basics 2.2 Roadmap  Everything is an Object  Syntax on a Postcard  Three Kinds of Messages 
4. Smalltalk Coding Idioms. © Oscar Nierstrasz ST — Smalltalk Coding Idioms 4.2 Roadmap  Snakes and Ladders — Cascade and Yourself  Lots of Little Methods.
Stéphane Ducasse«ChapterNr».1 Arthur Riel Design Heuristics from Object-Oriented Design Heuristics by Arthur Riel Read the Heuristics… –Find reasons why.
7. Understanding Classes and Metaclasses. © Oscar Nierstrasz ST — Understanding Classes and Metaclasses 7.2 Roadmap  Metaclasses in 7 points  Indexed.
Stéphane Ducasse9.1 Inheritance Semantics and Lookup.
Stéphane Ducasse8.1 Syntax and Messages The syntax of Smalltalk is simple and uniform, but it can look strange at first sight! Literals: numbers, strings,
Stéphane Ducasse 1 The Taste of Smalltalk.
PZ06BX Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ06BX - Introduction to Smalltalk Programming Language.
2. Smalltalk Basics. © Oscar Nierstrasz ST — Smalltalk Basics 2.2 Roadmap  Everything is an Object  Syntax on a Postcard  Three Kinds of Messages 
Some Basic Points on Classes
S.Ducasse Stéphane Ducasse 1 A Taste of Smalltalk.
Stéphane Ducasse 1 Syntax and Messages.
Classes and Objects  A typical Java program creates many objects which interact with one another by sending messages. Through the objects interactions,
ASP.NET Programming with C# and SQL Server First Edition
Object-oriented programming and design 1 Smalltalk in a Nutshell Objects & classes Messages & methods Inheritance & metaclasses.
Stéphane Ducasse«ChapterNr».1 Advanced Classes Indexed Classes Classes as Objects Class Instance Variables and Methods Class Variables Pool Dictionaries.
Stéphane Ducasse5.1 Smalltalk in a Nutshell OO Model in a Nutshell Syntax in a Nutshell.
4. Smalltalk Coding Idioms. © Oscar Nierstrasz ST — Smalltalk Coding Idioms 4.2 Roadmap  Snakes and Ladders — Cascade and Yourself  Lots of Little Methods.
Stéphane Ducasse 1 Smalltalk in a Nutshell.
Stéphane Ducasse4.1 A Taste of Smalltalk Two examples: –‘hello world’ –A Tomagoshi To give you an idea of: –the syntax –the elementary objects and classes.
S.Ducasse Stéphane Ducasse 1 The Taste of Smalltalk.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Chapter 5 - Writing a Problem Domain Class Definition1 Chapter 5 Writing a Problem Domain Class Definition.
Chapter 10: Writing Class Definitions Visual Basic.NET Programming: From Problem Analysis to Program Design.
S.Ducasse Stéphane Ducasse 1 Smalltalk Coding Idioms.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Comp 248 Introduction to Programming Chapter 6 Arrays Part B Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University,
S.Ducasse Stéphane Ducasse 1 Classes and Metaclasses - an Analysis.
S.Ducasse Stéphane Ducasse 1 Design Heuristics.
S.Ducasse Stéphane Ducasse savoie.fr e/ e/ 1 Inheritance Semantics and Method Lookup.
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Stéphane Ducasse 1 Elements of Design - Inheritance/Compositio n.
Chapter 2: Variables, Functions, Objects, and Events JavaScript - Introductory.
Computer Science 111 Fundamentals of Computer Programming I Working with our own classes.
Stéphane Ducasse 1 Singleton.
S.Ducasse 1 7. Understanding Metaclasses. S.Ducasse 2 Roadmap Metaclasses in 7 points Indexed Classes Class Instance Variables Class Variables Pool Dictionaries.
S.Ducasse Stéphane Ducasse 1 Common Mistakes and Debugging in VW.
Java Classes Chapter 1. 2 Chapter Contents Objects and Classes Using Methods in a Java Class References and Aliases Arguments and Parameters Defining.
Stéphane Ducasse 1 Some Advanced Points on Classes.
3C-1 Identifiers Variables Literal Objects  Numbers  Characters  Strings Control Structure  Blocks  Conditionals  Loops Messages Defining New Classes.
Stéphane Ducasse7.1 Let’s Play Objects Simulate a LAN physically Set up a context for –future chapters –Exercises Some forward references to intriguate.
3. Standard Classes. © Oscar Nierstrasz ST — Standard Classes 3.2 Roadmap  Object  Numbers, Characters, Strings and Arrays  Variables  Blocks and.
(c) University of Washington06-1 CSC 143 Java Inheritance Tidbits.
S.Ducasse Stéphane Ducasse 1 Smalltalk in a Nutshell.
S.Ducasse Stéphane Ducasse savoie.fr e/ e/ 1 Smalltalk in a Nutshell.
CSE 3302 Programming Languages Chengkai Li Fall 2007 Smalltalk Lecture 14 – Smalltalk, Fall CSE3302 Programming Languages, UT-Arlington ©Chengkai.
Object-Oriented Programming: Inheritance and Polymorphism.
Stéphane Ducasse 1 A Little Journey in the Smalltalk Syntax.
Stéphane Ducasse 1 Elements of Design - Unit of Reuse.
Stéphane Ducasse 1 Abstract Classes.
S.Ducasse Stéphane Ducasse 1 Some Advanced Points on Classes.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
S.Ducasse 1 Instance Initialization How to ensure that an instance is well initialized? Automatic initialize Lazy initialization Proposing the right interface.
Let’s Play Objects.
7.1 What Is An Object Object-oriented program - Description or simulation of application Object-oriented programming is done by adopting or extending an.
Design Points - Law of Demeter
2.3 Collaboration Contracts
Java Primer 1: Types, Classes and Operators
Chapter 2 Squeak: Object-oriented design with multimedia applications
Review Session Biggest discrepancy questions
Object-Oriented Programming: Inheritance and Polymorphism
Introduction to Smalltalk
Notes from Week 5 CSE 115 Spring 2006 February 13, 15 & 17, 2006.
Introduction to Smalltalk
Smalltalk on a Dime.
Presentation transcript:

Stéphane Ducasse 1 Some Points on Classes

S.Ducasse 2 Outline Class definition Method definition Basic class instantiation

S.Ducasse 3 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 4 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 5 Class Definition: (Sq) A template is proposed by the browser: NameOfSuperclass subclass: #NameOfClass instanceVariableNames: 'instVarName1 instVarName2' classVariableNames: 'ClassVarName1 ClassVarName2' poolDictionaries: '' category: 'CategoryName’

S.Ducasse 6 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 7 Named Instance Variables 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 8 Roadmap Class definition Method definition Basic class instantiation

S.Ducasse 9 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 10 Accessing Instance Variables 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 11 Methods always return a Value 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 11

S.Ducasse 12 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 13 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 14 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 15 Roadmap Class definition Method definition Basic class instantiation

S.Ducasse 16 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 17 Object Creation - When a class creates an object = allocating memory + marking it to be instance of that class

S.Ducasse 18 Instance Creation with new aClass new returns a newly and UNINITIALIZED instance OrderedCollection new -> anOrderedCollection () 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 19 Messages to Instances Messages to Instances that create Objects 1 to: 6 (an interval) (a point) extent: (a rectangle) #lulu asString (a string) 1 printString (a string) 3 asFloat (a float) #( ) asSortedCollection (a sortedCollection)

S.Ducasse 20 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 21 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 22 Instance Creation creates a point y "Answer a new Point whose x value is the receiver and whose y value is the argument." ^ Point x: self y: y

S.Ducasse 23 Class-specific Messages Array with: 1 with: 'lulu' OrderedCollection with: 1 with: 2 with: 3 Rectangle fromUser -> corner: Browser browseAllImplementorsOf: #at:put: Packet send: ‘Hello mac’ to: #mac Workstation withName: #mac

S.Ducasse 24 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 25 Summary How to define a class? What are instance variables? How to define a method? Instances creation methods