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!
_______________________________________________
help-smalltalk mailing list
[email protected]
http://lists.gnu.org/mailman/listinfo/help-smalltalk