Dice new vs. self class new
Damien Cassou, Stéphane Ducasse and Luc Fabresse
http://stephane.ducasse.free.fr
From the Exercise
To support
We defined +
as
What Is The Difference...
Between
And
Let us see....
What If We Create A New Subclass
We get a DiceHandle
instance back and not a MemoDiceHandle
instance!!!
Solution 1: Creating a Hook
A subclass may redefine handleClass
Solution 1: Creating a Hook
We get an instance of the subclass!
But We Can Do Better!
Let us see
- In each subclass we should redefine the hook method
handleClass
- This is tedious
Solution 2
self class
always returns the class of the receiver
- We get instances of the same kind of the receiver
Conclusion
If we define a subclass of DiceHandle
,
and send the message +
to an instance
- With
DiceHandle new
, +
does not return an instance of the subclass but of DiceHandle
- With
self class new
, +
returns an instance of the receiver: an instance of a potential subclass
/