Is there any way of changing the behavior of particular instances of a class without affecting the rest of the instances (i.e., they still behave according to the behavior of the class)?
For instance, I would like the instance a
to have a different way of printing itself than the instance b
in the following example.
Let's suppose I have a class MyClass
, which responds to name
, with the following printing service:
MyClass>>printOn: aStream
aStream
nextPutAll: self class;
nextPutAll: ' instance named ';
nextPutAll: self name`
Then if I create an instance a
, I have the following:
a := MyClass new name: 'a'.
a printString -> 'MyClass instance named a'
Now I would like an instance b
to print only its name without affecting the way a
prints itself.
b := Object new name: 'b'.
b printString -> 'b'