Jump to content

Add a variable to a class instance at runtime: Difference between revisions

(-java,haskell)
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}}
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.