如何在 Pharo 中添加新方法?

How to add a new method in Pharo?

在 Nautilus 系统浏览器 (Pharo 6) 中有一个右键单击命令 Add package... 以及 Add class... 甚至 Add protocol...,但我找不到任何地方Add method... 的方式。

那个命令在哪里?

在 Pharo 中,添加方法不像其他元素那样明确。要添加新方法:

  1. Select 方法的协议,您应该会在编辑器窗格中看到一个模板:

    messageSelectorAndArgumentNames
       "comment stating purpose of message"
    
       | temporary variable names |
       statements
    
  2. 编辑此模板制作新方法,

  3. 保存(右击接受)使用Ctrl-S.

事实上,任何时候您更改方法的定义(例如,messageSelectorAndArgumentNames)并将其保存在编辑器中(右键单击 接受Ctrl-S), 它将创建一个新方法

有关详细信息,请参阅 Developing a simple counter 文档的第 1.3 节(重点是我的):

Create a method

Now let us create the accessor methods for the instance variable count. Start by selecting the class Counter in a browser, and make sure the you are editing the instance side of the class (i.e., we define methods that will be sent to instances) by deselecting the Class side radio button.

Create a new protocol by bringing the menu of methods protocol list. Select the newly created protocol. Then in the bottom pane, the edit field displays a method template laying out the default structure of a method. As a general hint, double click at the end of or beginning of the text and start typing your method. Replace the template with the following method definition:

count
   "return the current value of the value instance variable"
   ^ count

This defines a method called count, taking no arguments, having a method comment and returning the instance variable count. Then choose accept in the menu to compile the method.