Changes

Jump to navigation Jump to search
New stub
{{stub}}

The following code is taken from a tutorial that unfortunately does not include every step the beginner needs to have explained.

[http://static.squeak.org/tutorials/BankAccount.html BankAccount tutorial]

Object subclass: #NameOfClass

instanceVariableNames: 'instVarName1 instVarName2'

classVariableNames: 'ClassVarName1 ClassVarName2'

poolDictionaries: ''

category: 'My Stuff'

Object subclass: #BankAccount

instanceVariableNames: 'balance'

classVariableNames: ''

poolDictionaries: ''

category: 'My Stuff'

b _ BankAccount new.
b inspect
b balance.



message selector and argument names
"comment stating purpose of message"

| temporary variable names |
statements


balance

"Return the balance"

^ balance


initialize

balance _ 0.


deposit: amount

balance _ balance + amount.


withdraw: amount

amount > balance ifTrue: [

^ self inform: 'sorry, not enough funds'].

balance _ balance - amount.


Object subclass: #BankAccount

instanceVariableNames: 'balance history '

classVariableNames: ''

poolDictionaries: ''

category: 'My Stuff'


initialize

balance _ 0.

history _ OrderedCollection new.


balance: newBalance

balance _ newBalance.

history addLast: newBalance.


withdraw: amount

amount > balance ifTrue: [

^ self inform: 'sorry, not enough funds'].

self balance: balance - amount.

See also [http://web.cecs.pdx.edu/~black/OOP/Tutorial/SqueakLanguageRef.html Squeak Smalltalk: Language Reference]
1,009

edits

Navigation menu