简体   繁体   English

smalltalk中的关键字消息(初级)(Pharo)

[英]Keyword messages in smalltalk (Beginner)(Pharo)

I am trying to create a keyword message style method, but I can't figure out how to access the Receiver from inside the method. 我正在尝试创建一个关键字消息样式方法,但我无法弄清楚如何从方法内部访问Receiver。 I am sure this is simple, however I can't find the answer anywhere. 我确信这很简单,但我无法在任何地方找到答案。 What I am trying to implement is redundant, but I would still like to know how it works. 我想要实现的是多余的,但我仍然想知道它是如何工作的。

subst: i1 by: i2
      ^ self copyReplaceAll: i1 with: i2.

It would be called in the workspace as follows: 它将在工作区中调用如下:

string1 := 'Lemon'.
string2 := 'm'.
string3 := 'ss'.
string1 subst: string2 by: string3.

Error msg: "MessageNotUnderstood: ByteString>>subst:by:" 错误消息:“MessageNotUnderstood:ByteString >> subst:by:”

All the method should do is replace every occurance of "m" in "Lemon" with "ss" to create "Lesson" (which copyReplaceAll already does). 所有方法应该做的是用“ss”替换“Lemon”中每个“m”的出现来创建“Lesson”(copyReplaceAll已经做过)。 I can't figure out how to get string1 into the method. 我无法弄清楚如何将string1放入方法中。 Any help would be greatly appreciated, 任何帮助将不胜感激,

Thanks in advance! 提前致谢!

self is the current object (ie the receiver). self是当前对象(即接收者)。

Please read (or at least skim) a tutorial to get the basics. 请阅读(或至少略读)一个教程以获得基础知识。


"MessageNotUnderstood: ByteString>>subst:by:"

This error means that you have not defined the message on ByteString. 此错误意味着您尚未在ByteString上定义消息。 Either you have failed to actually define it anywhere, or you have defined it on the wrong class. 您可能无法在任何地方实际定义它,或者您已在错误的类上定义它。

In Smalltalk, method s are not freestanding things. 在Smalltalk中, 方法不是独立的东西。

A method is an object's way to respond to a message send . 方法是对象响应消息发送的方式

If the thing that receives the message knows how to respond to the message, it has a method of responding to the message. 如果接收消息的事情知道如何响应该消息,它具有响应消息的方法 The message name is said to be in the object's message protocol . 消息名称被称为对象的消息协议

So - every message has a receiver and a message name . 所以 - 每条消息都有一个接收者和一个消息名称

A keyword message will also have one or more arguments , with one keyword for each argument. 关键字消息还将具有一个或多个参数 ,每个参数都有一个关键字

The flipside of this is that all methods are part of an object . 另一方面, 所有方法都是对象的一部分 They are stored within the object's Class definition , as either a Class method or an instance method . 它们作为Class方法实例方法存储在对象的Class定义中

(The sole exception is in the specific case of the anonymous objects called blocks . In these cases, the method is defined in a block definition ). (唯一的例外是在称为的匿名对象的特定情况下。在这些情况下,该方法在块定义中定义 )。

Here is an example keyword message: Transcript show: 'Hello World!' 这是一个示例关键字消息: Transcript show: 'Hello World!'

It means "send the message show: 'Hello World!' 这意味着“发送消息show: 'Hello World!' to the receiver Transcript ". 到收件人Transcript “。 (Transcript is a window which displays system output. All graphical Smalltalk environments have a Transcript class). (Transcript是一个显示系统输出的窗口。所有图形Smalltalk环境都有一个Transcript类)。

This message has three parts: 消息包含三个部分:

The left-most part is always the receiver . 最左边的部分始终是接收器 In this example, the receiver is Transcript . 在此示例中, 接收器Transcript

The message name is show: which has a single keyword, show: 消息名称为show:其中包含一个关键字show:

The argument is 'Hello World' 争论的焦点是'Hello World'

(An even fuller explanation of Transcript show: 'Hello World!' (对Transcript show: 'Hello World!'更全面的解释Transcript show: 'Hello World!'
can be found at [ http://beginningtosmalltalk.blogspot.com/2015/11/hello-world.html] 可以在[ http://beginningtosmalltalk.blogspot.com/2015/11/hello-world.html]找到

An example keyword message with multiple keywords: 包含多个关键字的示例关键字消息:

aByteString copyReplaceAll: i1 with: i2

The receiver is aByteString , an instance of the Class ByteString 接收器是aByteString ,Class ByteString一个实例

The message name is copyReplaceAll:with: It has two keywords, copyReplaceAll: and with: . 消息名称为copyReplaceAll:with:它有两个关键字copyReplaceAll:with:

The arguments are i1 and i2 . 参数是i1i2

If ByteString Class, (or any Class above it in the Class hierarchy, like String ), contains the method, then the message is in the object's protocol. 如果ByteString类(或类层次结构中的任何类,如String )包含该方法,则消息在对象的协议中。

string1 := 'Lemon' . string1 copyReplaceAll: i1 with: i2

If you want, you can add your code to String or ByteString as an additional method. 如果需要,可以将代码添加到StringByteString作为附加方法。

subst: i1 by: i2
"Substitute all instances of substring i1 with string i2. Return the receiver"
^ self copyReplaceAll: i1 with: i2

Then it can be called in the workspace as follows: 然后可以在工作区中调用它,如下所示:

string1 := 'Lemon'. string2 := 'm'. string3 := 'ss'. string1 subst: string2 by: string3

But string1 subst: string2 by: string3 is not very different to 但是string1 subst: string2 by: string3它没有太大区别
string1 copyReplaceAll: string2 with: string3

Another style point to note is that each keyword in a keyword message should be as descriptive and unambiguous as possible. 另一个要注意的风格要点是关键字消息中的每个关键字应尽可能具有描述性和明确性。 subst could mean substitute or substring subst可能意味着substitutesubstring

The easiest way to add a method to a Class is by using the System Browser. 向Class添加方法的最简单方法是使用系统浏览器。 Click on the Class in the System Browser, and a pro-forma method definition will appear in the edit pane. 单击系统浏览器中的类,并在编辑窗格中显示形式方法定义。

Overtype it, and Accept it (on my system via right-click on a 2 or 3-button mouse, or the 'Ctrl-s' keyboard shortcut. Although mouse and key mappings can vary on different platforms). 改写它,并Accept it (在我的系统上通过右键单击2或3键鼠标,或'Ctrl-s'键盘快捷键。虽然鼠标和键映射可能在不同的平台上有所不同)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM