help-smalltalk
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Help-smalltalk] Re: Objects and classes


From: Canol Gokel
Subject: [Help-smalltalk] Re: Objects and classes
Date: Tue, 27 May 2008 20:58:12 +0000 (UTC)
User-agent: Loom/3.14 (http://gmane.org/)

Mark Carter <alt.mcarter <at> googlemail.com> writes:

> 
> I'm REALLY new to Smalltalk. In a previous thread, I created a file  
> Hello.st:
> 
> Object subclass: #Hello
>         instanceVariableNames: ''
>         classVariableNames: ''
>         poolDictionaries: ''
>         category: ''!
> 
> "Create a method"
> !Hello methodsFor: 'speaking'!
> publish
>      Transcript show: 'Greetings. '!
> 
> ! !
> 
> To load it, I do
> FileStream fileIn: 'Hello.st' !
> 
> BUT, if I want to "run" Hello, I have to do
> Hello new publish
> 
> My question is: why did I have to `new` Hello, whereas I don't have to  
> `new` FileStream? Presumably for Hello I have to create an object by  
> instantiating a class, whereas with a class like FileStream I don't  
> have to. What's going on? Are there methods which only belong to  
> classes and not objects, and if so, how do I define them?
> 
> Sorry if the question seems really dumb.
> 


Yes you guessed right, there are class methods. You can create them like this:

!Hello class methodsFor: 'speaking'!

Notice that I changed "Hello" into "Hello class". I am also a new Smalltalker so
maybe I'm wrong but as far as I know "class" is a message which returns the
class of an object.

So the code below is the new hello.st and works with both "Hello new publish" or
just "Hello publish".

Object subclass: #Hello
        instanceVariableNames: ''
        classVariableNames: ''
        poolDictionaries: ''
        category: ''!

"Create a class method"
!Hello class methodsFor: 'speaking'!
publish
    Transcript show: 'Greetings. '!

! !

"Create a method"
!Hello methodsFor: 'speaking'!
publish
    Transcript show: 'Greetings. '!

! !

Hello publish!





reply via email to

[Prev in Thread] Current Thread [Next in Thread]