Advertisement
SepandMeenu

Smalltalk experiment 1

Jul 7th, 2019
1,126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. Object subclass: Account [
  3.     | balance |
  4.     <comment:
  5.         'I represent a place to deposit and withdraw money'>
  6.     Account class >> new [
  7.         <category: 'instance creation'>
  8.         | r |
  9.         r := super new.
  10.         r init.
  11.         ^r
  12.     ]
  13.     init [
  14.         <category: 'initialization'>
  15.         balance := 0.
  16.     'initialized Account' printNl
  17.     ]
  18.  
  19.     say [
  20.         "say hello"
  21.         ('I am account of balance "', balance printString, '".') printNl
  22.     ]
  23.  
  24.     compute: val [
  25.     | tar |
  26.     tar := 3.
  27.     balance := balance + val.
  28.     ]
  29.  
  30.     mutate: array [
  31.     (array at: 2) printNl.
  32.     "array at: 2 put: -9." "NOTE: argument mutation is not allowed"
  33.     ]
  34. ]
  35.  
  36. cl := [:x | |y| y := x + 1. "This is y = "
  37.  ('This is y = ', y printString) printNl].
  38. ('y is ', y printString) printNl.
  39.  
  40. x0 := 1.
  41. acc := Account new.
  42. acc say.
  43. acc compute: x0.
  44. acc say.
  45. acc class printNl.
  46. array := #(1 2 3).
  47. acc mutate: array.
  48. array printNl.
  49. cl value: 1.
  50. ('y is ', y printString) printNl.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement