简体   繁体   English

Smalltalk中自我和你自己有什么区别?

[英]What is the difference between self and yourself in Smalltalk?

In Smalltalk, there are two terms often found within a method body: self and yourself . 在Smalltalk中,在方法体中经常会发现两个术语: selfyourself

What is the difference between them? 他们之间有什么区别?

The reserved word self is a pseudo variable (you cannot assign to it) that refers to the current receiver of the method where it is used. 保留字self是一个伪变量(您无法分配给它),它指的是使用它的方法的当前接收者。 On the other side yourself is a message you can send to any object to get that very same object. 在另一边yourself是可以发送到任何物体来获取相同的对象的消息。

The implementation of yourself is yourself的实现是

yourself
    ^self

meaning that the message yourself will behave as I just explained. 即消息yourself的行为,因为我刚才解释。

The reason why yourself exists is to support message cascading, where you put it as the last message to make sure the resulting expression will answer with the receiver: yourself存在的原因是支持消息级联,你把它作为最后一条消息,以确保结果表达式将与接收者一起回答:

^receiver
   msg1;
   msg2;
   yourself

If msg2 might answer with something different from the receiver you can append the yourself message to ignore that answer and return receiver instead. 如果msg2可能会回答与receiver不同的内容,您可以添加yourself消息来忽略该答案并返回receiver Of course you could have achieved the same result by writing: 当然,您可以通过编写来获得相同的结果:

receiver
   msg1;
   msg2.
^receiver

Because of the simplicity of these two examples, it might be hard to understand what the advantage would be. 由于这两个例子的简单性,可能很难理解其优势。 However, consider that receiver is not a variable but a complex expression, something like. 但是,请考虑receiver不是变量而是复杂表达式,例如。

^(self msg: arg1 arg: arg2)
   msg1;
   msg2;
   yourself.

Without using yourself you would have to add a temporary to save the value of the receiver to achieve the same: 在不使用yourself您必须添加一个临时值来保存接收器的值以实现相同的目的:

| answer |
answer := self msg: arg1 arg: arg2.
answer
   msg1;
   msg2.
^answer

which is a little bit more verbose. 这有点多啰嗦。

To summarize, self is a reserved word that refers to the current receiver and yourself is just a regular method that is there just for convenience. 总而言之, self是一个保留字,指的是当前的接收器,而yourself只是为了方便起见的常规方法。

self is a synonym for an object: specifically the receiver of the message that invoked the method. self是对象的同义词:特别是调用该方法的消息的接收者。 It is used within the body of a method. 它在方法体内使用。

yourself is a message that you can send to an object, that returns the receiver of the message. yourself是一个可以发送给对象的消息,它返回消息的接收者。

anObject yourself returns anObject . anObject yourself返回anObject

yourself is often used at the end of a message cascade within a method body. yourself经常在方法体内的消息级联结束时使用。

When you want the return value from the method to be the receiver, but the final message in the cascade returns something else, you could write either: 如果希望方法的返回值为接收者,但级联中的最终消息返回其他内容,则可以编写:

self aMessageReturningTheReceiver;
      aMessageReturningTheArgument: anArgument .
^self

or 要么

self aMessageReturningTheReceiver;
      aMessageReturningTheArgument: anArgument;
      yourself

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

相关问题 Pharo Smalltalk中=和==有什么区别? - What is the difference between = and == in Pharo Smalltalk? Smalltalk中的关联集合和字典之间有什么区别? - What is the difference between a collection of associations and a dictionary in Smalltalk? Self和Smalltalk之间的差异 - Differences between Self and Smalltalk Smalltalk中Array和Literal Array之间有什么区别? - What's the difference between the Array and Literal Array in Smalltalk? Smalltalk中图像生成和图像剥离有什么区别? - What is the difference between image generation and image stripping in Smalltalk? Erlang 中的进程/消息和 Smalltalk 中的对象/消息有什么区别? - What is the difference between processes/messages in Erlang and objects/messages in Smalltalk? Continuation(如Smalltalk)和中断(如汇编程序)之间有什么区别? - What's the difference between a Continuation (as in Smalltalk) and an interrupt (as in an Assembler)? Smalltalk中new和initialize之间的区别? - Difference between new and initialize in Smalltalk? ANSI Smalltalk和Smalltalk-80有什么区别? - What's the difference of ANSI Smalltalk and Smalltalk-80? 这两种方法有什么区别[Smalltalk Best Practice Patterns - Kent Beck]? - What is the difference between those two approaches [Smalltalk Best Practice Patterns - Kent Beck]?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM