I tried to run the example of GNU Smalltalk in the documentation but ran into an issue.
Object subclass: Account [
| balance |
new [
| r |
r := super new.
r init.
^r
]
init [
'initialize account' printNl.
balance := 0
]
get [
^balance
]
]
In the new
method the init
message is never sent to the Account
method.
Heres my output:
st> Account new get
nil
st> Account new init get
'initialize account'
0
I took this example from the GNU Smalltalk Documentation.
Can somebody help me spot the error? I assumed that perhaps the init
method of super is called, but Object
does not have a init
method. Furthermore should super
create a instance of the current class?
Thanks Benjamin for the answer
So my problem was that i did not distinguish between class functions (new
) and Object functions
Fixed Code
Object subclass: Account [
| balance |
init [ balance := 0 ]
get [ ^balance ]
]
Account class extend [
new [ ^ (super new init) ]
]开发者_StackOverflow
st> Account new get
0
There is a slight change you've made to the code that means it won't work.
In Smalltalk, methods can be attached in one of two places: they can apply to instances of a class, which in this case would be account objects (like the one you get back from running Account new
), or they can be class methods, which means they apply to the actual class itself, Account
.
Account init
is an instance method. It sets some sane defaults for the instance of the Account
class you've made. You wrote that correctly, and it's behaving as it should.
On the other hand, you've changed Account new
from a class method to an instance method. When it's a class method, it replaces Account
s existing new
method with one that calls init
. When it's an instance method, it doesn't really do anything, since you're not going to be calling new
on the instance.
The fix is easy enough: tell GNU Smalltalk that the new
method is a class method. To do that, you just put the method on Account class >> new
instead of loose in the Account
class.
The first new
should be Account class >> new
. Thanks!
An other possibility is:
Object subclass: Account [
| balance |
Account class [
new [
...
]
otherClassMethod [
...
]
]
instanceMethod [
...
]
]
精彩评论