at:tutorial:objects
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
at:tutorial:objects [2007/07/27 10:15] – added tvcutsem | at:tutorial:objects [2013/05/17 20:23] (current) – updated tvcutsem | ||
---|---|---|---|
Line 174: | Line 174: | ||
}; | }; | ||
def each: clo { | def each: clo { | ||
- | 1.to: elements.length | + | 1.to: elements.length do: { |i| |
clo(elements[i]); | clo(elements[i]); | ||
}; | }; | ||
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 ===== | ||
Line 284: | Line 301: | ||
</ | </ | ||
- | The rationale is that '' | + | The rationale is that '' |
< | < | ||
Line 291: | Line 308: | ||
</ | </ | ||
- | When explicitly // | + | When explicitly // |
==== UAP and Assignment ==== | ==== UAP and Assignment ==== | ||
Line 317: | Line 334: | ||
// and a mutator method seconds: | // and a mutator method seconds: | ||
- | def minutes() { (seconds / 60) }; | + | def minutes() { seconds / 60 }; |
- | def hours() | + | def hours() |
def minutes: | def minutes: | ||
Line 334: | Line 351: | ||
>> 3600 | >> 3600 | ||
> time.seconds := 180 | > time.seconds := 180 | ||
+ | >> 180 | ||
> time.minutes | > time.minutes | ||
>> 2 | >> 2 |
at/tutorial/objects.1185524132.txt.gz · Last modified: 2007/07/27 16:27 (external edit)