Anonymous user
Add a variable to a class instance at runtime: Difference between revisions
Add a variable to a class instance at runtime (view source)
Revision as of 08:23, 6 May 2009
, 15 years ago→{{header|Smalltalk}}: + Tcl
(-java,haskell) |
(→{{header|Smalltalk}}: + Tcl) |
||
Line 266:
x: 10
10</pre>
=={{header|Tcl}}==
The code below uses the fact that each object is implemented as a namespace, to add a ''time'' variable to an instance of ''summation'':
<lang Tcl>
% oo::class create summation {
constructor {} {
variable v 0
}
method add x {
variable v
incr v $x
}
method value {} {
variable v
return $v
}
destructor {
variable v
puts "Ended with value $v"
}
}
::summation
% set s [summation new]
% set ${s}::time now
now
% set ${s}::time
now
%
</lang>
{{Omit From|AWK}}
|