This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision Last revision Both sides next revision | ||
at:tutorial:objects [2007/07/27 16:31] tvcutsem * |
at:tutorial:objects [2008/08/01 14:29] tvcutsem * |
||
---|---|---|---|
Line 184: | Line 184: | ||
< | < | ||
- | > Array.add(1).add(2).add(3) | + | Array.add(1).add(2).add(3) |
- | >> < | + | def c := Array.collect: |
- | > def c := Array.collect: | + | c.each: { |v| system.print(v)} |
- | >> < | + | |
- | > c.each: { |v| system.print(v)} | + | |
- | 234 | + | |
- | >>nil | + | |
</ | </ | ||
Line 233: | Line 229: | ||
> makeBankAccount(100).balance; | > makeBankAccount(100).balance; | ||
>> | >> | ||
- | <object:5068254> | + | <obj:{super, |
</ | </ | ||
+ | |||
+ | This pattern of creating objects by means of " | ||
+ | |||
+ | < | ||
+ | def b := makeBankAccount(100); | ||
+ | def b2 := b.new(); // shares its balance field with b! | ||
+ | b.deposit(10); | ||
+ | </ | ||
+ | |||
+ | In order to prevent this kind of errors, it is considered best practice to override '' | ||
+ | |||
+ | < | ||
+ | def makeBankAccount(balance) { | ||
+ | object: { | ||
+ | def new(@args) { makeBankAccount(@args) }; | ||
+ | def deposit(amnt) { /* as before */ }; | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | By overriding '' | ||
===== Uniform Access ===== | ===== Uniform Access ===== |