|
Object Creation - creating new objects
|
|
Calling Sequence
|
|
module NewObject() option object; ... end module;
Object( obj )
Object( obj, ... )
|
|
|
Declaring a New Class
|
|
|
•
|
To create a new class of objects, use the module declaration syntax and add option object. As with modules, names are declared in local and export statements. An object's local names can only be accessed from the methods in the class declaration, whereas export names can be accessed from anywhere.
|
|
•
|
In objects names can also have a second modifier, static. The static modifier affects how the value assigned to a name is shared among multiple objects of the same class. A name without the static modifier has a value that is tied to the object. Thus each object stores its own value for the name. A name with the static modifier has a value that is tied to the class, thus each object of the same class shares the same value for that name.
|
|
•
|
static is typically used for an object's methods, as the methods are the same for each object. The object's data members, those names which maintain the state in the object, generally do not have the static modifier. However there are cases where it makes sense to have a static data member (perhaps a class-wide counter to keep track of the number of objects created).
|
|
•
|
The result of executing a module declaration with option object is a new object of the declared class. This object is often treated as a special prototype object that is primarily used to create new objects of the same class. The module syntax allows for a name to be specified. This name will have the prototype object assigned to it and it will be protected.
|
|
|
Creating More Objects
|
|
|
•
|
Once a prototype object exists it can be used to create new objects using the Object routine. By default the new object's names are assigned the same values as the prototype's names. Objects can define a ModuleCopy method to change how the new object is initialized when the Object function is called. The Object routine can accept additional arguments which will be passed into the ModuleCopy method.
|
|
•
|
One nice way of handling new object creation is to use the ModuleApply method. By having ModuleApply call Object, you can create new objects by applying the prototype object's name.
|
|
|
|
|
Examples
|
|
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.
|
>
|
|
|
| (1) |
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.
|
>
|
|
|
| (3) |
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.
|
>
|
|
|
Error, module `module Dollar () local value; local total::static; export amount::static, in_circulation::static, ModuleApply::static, ModuleCopy::static; option object; value := 0; total := 6; amount := proc (self::Dollar, $) return self:-value end proc; in_circulation := proc (self::Dollar, $) Dollar:-total end proc; ModuleApply := proc () Object(Dollar,args) end proc; ModuleCopy := proc (new::Dollar, proto::Dollar, v::numeric, $) new:-value := v; Dollar:-total := Dollar:-total+new:-value end proc; end module` does not export `value`
| |
Using the amount method, the value of a Dollar object can be returned.
|
>
|
|
|
| (5) |
|
>
|
|
|
| (6) |
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.
|
>
|
|
|
| (7) |
|
>
|
|
|
| (8) |
|
|
|
See Also
|
|
module, ModuleApply, ModuleCopy, Object,builtin, Object,function, Object,methods, Object,operator, Objects, procedure
|
|