Unformatted text preview:

Smalltalk in a NutshellSmalltalk: Everything is an ObjectCommunicating with an ObjectThree Kinds of Message SyntaxSending a MessageMessage Lookup and InheritanceSlide 7Smalltalk Expression SyntaxSlide 9Smalltalk Method SyntaxVariables in SmalltalkSlide 12Slide 13Using VariablesMore variables(continued)Initializing an ObjectClass MethodsInstance initializing methodCreating a DateComplete SmalltalkSmalltalk (the language) is trivialImplicationsUses of InheritanceMagnitudeDateSortingSlide 28Abstract ClassesA Payroll SystemObject Model for PayrollEmployeeEmployee CommentSlide 34Using EmployeeSlide 36Initializing an EmployeeComplete Creation MethodTimecardInitializing a TimecardSlide 41Using a TimecardInitialization PatternsProcessing a TransactionProcessing a TimecardSlide 46Employee Action ProtocolSmalltalk in a NutshellObjects & classesMessages & methodsInheritance & metaclassesSmalltalk: Everything is an ObjectApplication objects: customer, inventoryGUI objects: button, text editorFoundation: string, set, numbers, booleansTools: browser, debugger, compiler, GUI builderLanguage implementation: class, method, context,Communicating 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 for sending a message.All messages have same implementation.Three Kinds of Message SyntaxUnary messagesaSalaryChange dateKeyword messagesearned at: date add: moneyBinary messages(worked - 40) max: 0Sending a MessageObject responds to message by looking in its class for a method with the same selector.If it doesn't find the method, it looks in its superclass. Repeat until it finds the method. If it never does, there is an error.Message Lookup and InheritanceEmployeeTransaction has subclasses Paycheck, SalaryChange, and Timecard.EmployeeTransaction defines the instance variable date and the method:date^dateEmployeeTransactionEmployeeTransactiondatedatePaycheckPaycheckcheck1check107/09/9507/09/95aPaycheck dateaPaycheck dateSmalltalk Expression SyntaxConstants3.675, 14, 'hello', #weight, $d, #( #foo 'bar' 92)Assignments and variablesv := v + 1MessagesSmalltalk Expression SyntaxSequences. Blocks. x < y ifTrue: [z := x] ifFalse: [z := y].paychecks do: [:each | each post]CascadesaSet add: #blue; add: #redSmalltalk Method SyntaxReturns^socialSecurity + federalTaxes + stateTaxesVariables in SmalltalkblockArguments and temporariesmethodArguments and temporariesinstanceVariablesCan be accessed only by the object's methods.Variables in SmalltalkClassVariablesShared by all objects in the class or in subclasses.GlobalsShared by all objects.Variables in SmalltalkEmployeeTransaction subclass: #PaycheckEmployeeTransaction subclass: #PaycheckinstanceVariableNames: 'amountPaid instanceVariableNames: 'amountPaid taxes totalPaid totalTaxes 'taxes totalPaid totalTaxes 'classVariableNames: 'AmountFormat classVariableNames: 'AmountFormat DateFormat 'DateFormat 'poolDictionaries: ''poolDictionaries: ''category: 'Employee'category: 'Employee'Using VariablesprintOnCheckStream: aStreamprintOnCheckStream: aStreamaStream cr; cr.aStream cr; cr.aStream next: 40 put: (Character space).aStream next: 40 put: (Character space).DateFormat print: date on: aStream.DateFormat print: date on: aStream.aStream cr.aStream cr.......More variablestest"PayrollSystem test"| payroll day1 ralph |day1 := Date newDay: 5 year: 1996.payroll := self new.ralph := Employee new name: 'Ralph Johnson'.(continued)ralph changeSalaryFor: day1 to: 20.payroll addEmployee: ralph.self employee: ralph hours: self aLittleOvertime starting: day1.^payrollInitializing an ObjectEvery object needs to be initialized. Uninitialized variables are nil.The initialization method often defines the type of a variable.Two methods: one class and one instance.Class MethodsClass is an object. You can define methods for it, too.For class Date classday: dayInteger year: yearInteger ^self new day: dayInteger year: yearIntegerInstance initializing methodFor class Dateday: dayInteger year: yearInteger day := dayInteger.year := yearIntegerCreating a DateDate day: 3 year: 1995Date new day: 3 year: 1995day: 3 year: 199531995dayyearComplete SmalltalkExpressions (method definition)VariablesClasses, inheritanceClass methods / instance methodsPseudovariablesnil, true, falseself, super, thisContextSmalltalk (the language) is trivialComplexity is class library.New language extensions fit in as well as numbers and control structures.Language extension => core language is trivialImplicationsclass library = language extension => must know class librarymust standardize class librarymerging class libraries is like merging language extensionshard to make class librariesUses of InheritanceProvide a common implementationProvide a default implementationProvide a parameterize implementationMagnitudeSuperclass of classes like Number and Date with <, >, <=, >=<= aMagnitude^(self > aMagnitude) not> aMagnitudeself subclassResponsibilityDateInstance 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]SortingTo sort a list, store it in a SortedCollection.SortedCollection uses < to sort elements unless 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 4 November 1960 22 June 1983 )Abstract ClassesAbstract class class with no instancesdesigned as a template for subclasses, not for instances.defines standard interfaceA Payroll SystemPayrollSystem keeps track of employees.Employee knows salary, how much has been earned, how much has been payed, how much taxes have been withheld. Employee keeps an audit trail of all the transactions that have occured.Object Model for PayrollEmployeeEmployeeTransactionPayrollSystempost:datePaycheckTimecardSalaryChangesalarypay, taxeshoursWorkedvacationA class with a method #post:A payroll systemhas a set ofemployees.An employee has aset of transactions(and a transactionis for an employee)Timecard, paycheckand salarychangeare kinds of employee transactionspostTo::abstract classabstract classabstract methodabstract methodEmployeeObject subclass: #EmployeeinstanceVariableNames: 'name transactions salary earned paid withholding accruedVacation taxRule


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?