Squeak noob question - initialize
Ned Konz
ned at squeakland.org
Tue Jun 29 02:17:46 UTC 2004
Welcome to Squeak!
On Monday 28 June 2004 6:38 pm, Steve Greenberg wrote:
> I'm trying to do the simplest thing, but there is clearly some basic
> concept that I'm missing here. I'm running Squeak 3.6.
Note that Squeak 3.7 has an important change: there is a default call to
#initialize in the definition of #new up in Behavior.
So you will not often want to write this kind of code any more (as it's
redundant):
MyClass class >> new
^super basicNew initialize
> In short, I have ClassA and ClassB, which is a subclass of Class A.
> Each class has one variable, and when I create the class I'd like to
> see the initialization methods run on both classes.
Are you talking about class initialization (like when you load it into a new
image) or instance initialization (like when you make a new object of your
class)? We have both flavors, since classes are also objects.
> When I execute:
>
> b _ ClassB new initialize
>
> I expected that both classes would be initialized. What actually
> happens is that only ClassB is initialized.
I hope that your new object ("b") is what's getting initialized, *not* the
class.
What should happen, given your code, is:
ClassB class >> new
ClassA class >> new
Object class >> new
Object class >> basicNew
ClassB>> initialize
ClassB>> initialize
That is, ClassB>>initialize gets called twice, and the ClassA>>initialize gets
called not at all.
> Object subclass: #ClassA
> instanceVariableNames: 'anIntA '
> classVariableNames: ''
> poolDictionaries: ''
> category: 'SEG'
>
> initialize
> anIntA _ 1.
>
> new
> ^ super new initialize
This isn't really a safe scheme; if you later make a new superclass for ClassA
that *itself* calls initialize, you'll get it called again. Better probably
to say
^super basicNew initialize
and to make your initialize method call *its* superclass:
initialize
super initialize.
anIntA _ 1.
> -------
>
> ClassA subclass: #ClassB
> instanceVariableNames: 'anIntB '
> classVariableNames: ''
> poolDictionaries: ''
> category: 'SEG'
>
> initialize
> anIntB _ 1
If you want the superclass initialize method to be called, you should also do
it here:
initialize
super initialize.
anIntB _ 1
> new
> ^ super new initialize
ClassB class>>new isn't needed (it's a duplicate of its parent's definition).
And in fact it's causing ClassB>>initialize to be called twice.
--
Ned Konz
http://bike-nomad.com/squeak/
More information about the Squeak-dev
mailing list
|