Unformatted text preview:

1Object-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 25Smalltalk in a NutshellObjects & classesMessages & methodsInheritance & metaclassesObject-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 26Smalltalk: Everything is an Object■ Application objects: customer, inventory■ GUI objects: button, text editor■ Foundation: string, set, numbers, booleans■ Tools: browser, debugger, compiler, GUIbuilder■ Language implementation: class, method,context,2Object-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 27Communicating with an ObjectAll computation is done by objects.The only way to interact with an object is to"send a message" to it.Smalltalk has three kinds of syntax forsending a message.All messages have same implementation.Object-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 28Three Kinds of Message SyntaxUnary messagesaSalaryChange dateKeyword messagesearned at: date add: moneyBinary messages(worked - 40) max: 03Object-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 29Sending a MessageObject responds to message by looking inits class for a method with the sameselector.If it doesn’t find the method, it looks in itssuperclass.Repeat until it finds the method. If itnever does, there is an error.Object-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 30Message Lookup and InheritanceEmployeeTransaction has subclassesPaycheck, SalaryChange, and Timecard.EmployeeTransaction defines the instancevariable date and the method:date^date4EmployeeTransactiondatePaycheckcheck107/09/95aPaycheck dateObject-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 32Smalltalk Expression SyntaxConstants3.675, 14, ’hello’, #weight, $d,#( #foo ’bar’ 92)Assignments and variablesv := v + 1Messages5Object-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 33Smalltalk Expression SyntaxSequences. Blocks.x < y ifTrue: [z := x] ifFalse: [z := y].paychecks do: [:each | each post]CascadesaSet add: #blue; add: #redObject-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 34Smalltalk Method SyntaxReturns^socialSecurity + federalTaxes +stateTaxes6Object-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 35Variables in SmalltalkblockArguments and temporariesmethodArguments and temporariesinstanceVariablesCan be accessed only by the object’smethods.Object-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 36Variables in SmalltalkClassVariablesShared by all objects in the class or insubclasses.GlobalsShared by all objects.7Object-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 37Variables in SmalltalkEmployeeTransaction subclass: #PaycheckinstanceVariableNames: ’amountPaidtaxes totalPaid totalTaxes ’classVariableNames: ’AmountFormatDateFormat ’poolDictionaries: ’’category: ’Employee’Object-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 38Using VariablesprintOnCheckStream: aStreamaStream cr; cr.aStream next: 40 put: (Character space).DateFormat print: date on: aStream.aStream cr....8Object-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 39More variablestest"PayrollSystem test"| payroll day1 ralph |day1 := Date newDay: 5 year: 1996.payroll := self new.ralph := Employee new name:’Ralph Johnson’.Object-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 40(continued)ralph changeSalaryFor: day1 to: 20.payroll addEmployee: ralph.selfemployee: ralphhours: self aLittleOvertimestarting: day1.^payroll9Object-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 41Initializing an ObjectEvery object needs to be initialized.Uninitialized variables are nil.The initialization method often definesthe type of a variable.Two methods: one class and oneinstance.Object-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 42Class MethodsClass is an object. You can definemethods for it, too.For class Date classday: dayInteger year: yearInteger^self new day: dayInteger year:yearInteger10Object-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 43Instance initializing methodFor class Dateday: dayInteger year: yearIntegerday := dayInteger.year := yearIntegerObject-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 44Creating a DateDate day: 3 year: 1995Date new day: 3 year: 1995day: 3 year: 199531995dayyear11Object-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 45Complete SmalltalkExpressions (method definition)VariablesClasses, inheritanceClass methods / instance methodsPseudovariablesnil, true, falseself, super, thisContextObject-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 46Smalltalk (the language) is trivialComplexity is class library.New language extensions fit in as well asnumbers and control structures.Language extension => core language istrivial12Object-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 47Implicationsclass library = language extension=>must know class librarymust standardize class librarymerging class libraries is likemerging language extensionshard to make class librariesObject-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 48Uses of InheritanceProvide a common implementationProvide a default implementationProvide a parameterize implementation13Object-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 49MagnitudeSuperclass of classes like Number andDate with <, >, <=, >=<= aMagnitude^(self > aMagnitude) not> aMagnitudeself subclassResponsibilityObject-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 50DateInstance variables: day, year< aDate"Answer whether the argument, aDate,precedes the date of the receiver. "year = aDate yearifTrue: [^day < aDate day]ifFalse: [^year < aDate year]14Object-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 51SortingTo sort a list, store it in aSortedCollection.SortedCollection uses < to sort elementsunless told otherwise.SortedCollection newadd: (Date newDay: 3 month: ’March’ year:1955);add: (Date newDay: 4 month: ’November’year: 1960);add: (Date newDay: 22 month: ’June’ year:1983);yourself=> SortedCollection (3 March 1955 4November 1960 22 June 1983 )15Object-oriented Programming and Design - Copyright 1998 by Ralph


View Full Document

UIUC CS 497 - Smalltalk in a Nutshell

Download Smalltalk in a Nutshell
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Smalltalk in a Nutshell and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Smalltalk in a Nutshell 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?