You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Pharo中创建方法?Counter类count: setter方法实现求助

Pharo Smalltalk Counter类count: setter方法实现方案

首先确保你的Counter类已经定义了实例变量count,之后按以下步骤操作:

  • 打开Counter类的实例方法编辑界面,注意不要切换到类方法编辑侧
  • 输入如下setter方法代码,保存即可:
count: aNumber
    count := aNumber

补充说明

你给出的示例中还调用了c count来读取值,所以需要提前实现对应的getter方法,代码如下:

count
    ^ count

完成两个方法的编写后,运行你给出的测试代码,最终会返回预期的7结果。

Smalltalk中setter方法默认返回self(即当前对象实例),所以你可以直接在Counter new后链式调用count: 7完成初始化赋值,符合你给出的示例写法。

内容的提问来源于stack exchange,提问作者DNess007

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.03 08:27:05