Some Basic Points on Classes

Slides:



Advertisements
Similar presentations
Advanced JS The World's Most Misunderstood Programming Language ) Douglas Crockford( Shimon Dahan
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:
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 
S.Ducasse Stéphane Ducasse 1 A Taste of Smalltalk.
Stéphane Ducasse 1 Syntax and Messages.
9. Understanding Classes and Metaclasses. Birds-eye view © Oscar Nierstrasz ST — Introduction 1.2 Reify your metamodel — A fully reflective system models.
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.
CS 598 Scripting Languages Design and Implementation 7. Smalltalk 80.
S.Ducasse Stéphane Ducasse 1 Classes and Metaclasses - an Analysis.
S.Ducasse Stéphane Ducasse 1 Design Heuristics.
Object-Oriented Application Development Using VB.NET 1 Chapter 6 Writing a Problem Domain Class Definition.
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.
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.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
S.Ducasse Stéphane Ducasse 1 Common Mistakes and Debugging in VW.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
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.
(c) University of Washington06-1 CSC 143 Java Inheritance Tidbits.
Stéphane Ducasse 1 Some Points on Classes.
S.Ducasse Stéphane Ducasse 1 Smalltalk in a Nutshell.
S.Ducasse Stéphane Ducasse savoie.fr e/ e/ 1 Smalltalk in a Nutshell.
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.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
Programming Languages Dan Grossman 2013 Classes and Objects.
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.
Design Points - Law of Demeter
2.3 Collaboration Contracts
Java Primer 1: Types, Classes and Operators
Can perform actions and provide communication
Can perform actions and provide communication
Can perform actions and provide communication
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:

Some Basic Points on Classes Class definition Method definition Basic class instantiation

Class Definition: The Class Packet A template is proposed by the browser: NameOfSuperclass subclass: #NameOfClass instanceVariableNames: 'instVarName1 instVarName2' classVariableNames: 'ClassVarName1 ClassVarName2' poolDictionaries: '' category: 'CategoryName’ Just fill this Template in: Object subclass: #Packet instanceVariableNames: 'contents addressee originator ' classVariableNames: '' 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

Named Instance Variables NameOfSuperclass subclass: #NameOfClass instanceVariableNames: 'instVarName1 instVarName2' ... Object subclass: #Packet 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 methods) Instance variables cannot be accessed by class methods. A client cannot directly access instance variables. The clients must use accessor methods to access an instance variable.

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

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 ^ self originator = aNode Design Hint: Do not directly access instance variables of a superclass from the subclass methods. This way classes will not be strongly linked at the structure level.

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 "Having received the packet, send it on. This is the default behavior." self send: thePacket This is equivalent to: self send: thePacket. ^self

Methods always return a value If we want to return the value returned by #send: Node>>accept: thePacket "Having received the packet, send it on. This is the default behavior." ^self send: thePacket. Use ^ self to notify the reader that something abnormal is arriving MyClass>>foo … ^ self

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

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 isNil, isAddressedTo:, isSentBy: For converting methods prefix the method with as asString

Object Instantiation Objects can be created by: (basic)new/new: Direct Instance creation: (basic)new/new: Messages to instances that create other objects Class specific instantiation messages

Instance Creation aClass new/basicNew returns a newly and UNINITIALIZED instance OrderedCollection new -> OrderedCollection () Packet new -> aPacket Instance variable values = nil 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)

Opening the Box 1 to: 6 -> 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 1 printString -> aString 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

Instance Creation 1@2 -> aPoint 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

Class-specific Instantiation 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

New and basicNew #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