GNU Smalltalk:教程中示例的问题(对象创建)

发布于 2024-10-31 08:02:39 字数 1021 浏览 3 评论 0原文

我尝试运行文档中的 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

我从 GNU Smalltalk 文档。

有人可以帮我发现错误吗?我假设可能调用了 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

攒一口袋星星 2024-11-07 08:02:39

您对代码做了一些细微的更改,这意味着它不起作用。

在 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 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 Accounts 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.

回首观望 2024-11-07 08:02:39

第一个 new 应该是 Account class >>>新的。谢谢!

The first new should be Account class >> new. Thanks!

日记撕了你也走了 2024-11-07 08:02:39

另一种可能性是:

Object subclass: Account [
    | balance |
    Account class [
        new [
          ...
        ]

        otherClassMethod [
          ...
        ]
    ]

    instanceMethod [
      ...
    ]
]

An other possibility is:

Object subclass: Account [
    | balance |
    Account class [
        new [
          ...
        ]

        otherClassMethod [
          ...
        ]
    ]

    instanceMethod [
      ...
    ]
]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文