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 21:46, 7 December 2020
, 3 years ago→{{header|Smalltalk}}: commented the wrong example (long time passed since and original author did not care)
(→{{header|Smalltalk}}: commented the wrong example (long time passed since and original author did not care)) |
|||
Line 1,661:
p z.
p z:40.
p inspect
"Point class is unaffected:"
p2 := Point x>20 y:30.
p2 z:40. -> error; Point does not implement z:
p2 inspect. "shows 2 slots"
"but we can create another instance of the enhanced point (even though its an anon class)"
p3 := p class new.
p3 x:1 y:2.
p3 z:4.
p3 inspect. "shows 3 slots"
</lang>
<!--
{{incorrect|Smalltalk|It extends the class (adds a new instance var and new method that will exists even in brand new future instances of that class), not only the particular instance. -- The description of the problem must be reworded then, as it
asks for adding variables to the class, not the instance.
CG: commented the bad example; maybe the original author wants to fix it / comment on it.
<lang smalltalk>Object subclass: #Monkey
Line 1,738 ⟶ 1,748:
x: 10
10</pre>
-->
=={{header|Swift}}==
|