GNU Smalltalk:教程中示例的问题(对象创建)
我尝试运行文档中的 GNU Smalltalk 示例,但遇到了问题。
Object subclass: Account [
| balance |
new [
| r |
r := super new.
r init.
^r
]
init [
'initialize account' printNl.
balance := 0
]
get [
^balance
]
]
在 new
方法中,init
消息永远不会发送到 Account
方法。 这是我的输出:
st> Account new get
nil
st> Account new init get
'initialize account'
0
有人可以帮我发现错误吗?我假设可能调用了 super 的 init
方法,但 Object
没有 init
方法。此外,super
应该创建当前类的实例吗?
感谢本杰明的回答
所以我的问题是我没有区分类函数(new
)和对象函数
固定代码
Object subclass: Account [
| balance |
init [ balance := 0 ]
get [ ^balance ]
]
Account class extend [
new [ ^ (super new init) ]
]
st> Account new get
0
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) ]
]
st> Account new get
0
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您对代码做了一些细微的更改,这意味着它不起作用。
在 Smalltalk 中,方法可以附加在两个位置之一:它们可以应用于类的实例,在这种情况下将是帐户对象(就像您从运行
Account 中返回的对象) new
),或者它们可以是类方法,这意味着它们适用于实际的类本身,Account
。Account init
是一个实例方法。它为您创建的Account
类的实例设置一些合理的默认值。你写得正确,而且它的行为也应该如此。另一方面,您已将
Account new
从类方法更改为实例方法。当它是类方法时,它将用调用init
的方法替换Account
现有的new
方法。当它是实例方法时,它实际上不会执行任何操作,因为您不会在实例上调用new
。修复方法很简单:告诉 GNU Smalltalk new 方法是一个类方法。为此,您只需将该方法放在
Account class >>> 上即可。 new
而不是Account
类中的loose。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 theAccount
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 replacesAccount
s existingnew
method with one that callsinit
. When it's an instance method, it doesn't really do anything, since you're not going to be callingnew
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 onAccount class >> new
instead of loose in theAccount
class.第一个
new
应该是Account class >>>新的。谢谢!
The first
new
should beAccount class >> new
. Thanks!另一种可能性是:
An other possibility is: