[Newbies] Re: Instance Creation Howto

Thiyagarajan vtstarin at gmail.com
Mon Aug 14 14:18:37 UTC 2006


hello


aComplex := ComplexClass new.

and create a method "new" which override new

new
    ^ super new initializer

and create a another method "initializer"

initializer
    | aVariable |
    aVariable := ComplexClass new.
    aVariable firstVariable: 22.
    aVariable secondVariable: 23.
    ^aVariable




>
> ------------------------------
>
> Message: 10
> Date: Sun, 13 Aug 2006 23:23:43 +0200
> From: hypolit <hypolit at googlemail.com>
> Subject: [Newbies] Instance Creation Howto
> To: beginners at lists.squeakfoundation.org
> Message-ID:
>         <e4dc659c0608131423pfd5ecbak97309d93234a0261 at mail.gmail.com>
> Content-Type: text/plain; charset="iso-8859-1"
>
> Hello,
>
> I am new to Smalltalk and Squeak in general. After having started to read
> "Inside Smalltalk" and "Smalltalk by Example" i came along two similar
> examples, which sound quite reasonable but don't seem to work in Squeak.
> I want to create an instance of my own class "ComplexClass".
>
> It is defined like this:
> Object subclass: #ComplexClass
>     instanceVariableNames: 'firstVar secondVar'
>     classVariableNames: ''
>     poolDictionaries: ''
>     category: 'TestClass'
>
>
> I can create instances with
> aComplex := ComplexClass new.
> normally.
>
> But what if i wanted to call another method instead of "new" to create an
> instance?
> Take, for example, my method "initializer":
>
> initializer
>     | aVariable |
>     aVariable := ComplexClass new.
>     aVariable firstVariable: 22.
>     aVariable secondVariable: 23.
>     ^aVariable
>
>
> In both books there are methods quite similar to my "initializer" method.
> But if execute:
>
> aComplex2 := ComplexClass initializer.
>
> I get an error message, something like "Undefined Object does not
> understand
> #initializer".
> Do i have to write:
>
> aComplex2 := ComplexClass new.
> aComplex2 := aComplex2 initalizer.
>
> That would work but it is quite ugly.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.squeakfoundation.org/pipermail/beginners/attachments/20060814/241e7ddc/attachment-0001.htm


More information about the Beginners mailing list