The following example illustrates the ideas presented on this page.
>
|
module Dollar()
option object;
local value := 0;
local total::static := 0;
export amount::static := proc( self::Dollar, $ )
return self:-value;
end;
export in_circulation::static := proc( self::Dollar, $ )
total;
end;
export ModuleApply::static := proc( )
Object( Dollar, _passed );
end;
export ModuleCopy::static := proc( new::Dollar, proto::Dollar, v::numeric, $ )
new:-value := v;
total := total + new:-value;
end;
end:
|
This creates a prototype object and assigns it to the name Dollar.
The Object routine can be used to make new instances of Dollar.
>
|
|
| (2) |
Creating a new Dollar object also increases the value of the static variable total.
As Dollar implements a ModuleApply method to create new instances, the following syntax can also be used.
| (4) |
The value local is not accessible from outside Dollar's methods.
Using the amount method, the value of a Dollar object can be returned.
The static local total is shared between all instances of Dollar, so you can pass any Dollar into in_circulation, and get the same result.