方法内部的自我意义何在? [英] What's the significance of self inside of a method?

查看:97
本文介绍了方法内部的自我意义何在?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在七周内通读七种编程语言,其中一个问题指出:

I'm reading through Seven Programming Languages in Seven Weeks, and one of the problems states:

如果分母为零,如何更改/以返回0?

How would you change / to return 0 if the denominator is zero?

我首先尝试定义自己的/并将其实现代理到原始的/方法,如下所示:

I first tried defining my own / and proxying its implementation to the original / method like this:

Number oldSlash := Number getSlot("/")
Number / = method(x, Number oldSlash(x))

但是那对我不起作用.做完谷歌搜索后,我发现了一段类似的代码.我发现该方法的实现中使用的是self代码.因此,我尝试使用self,它似乎可以正常工作:

However that was not working for me. After doing some Googling I found a similar piece of code. The code I found was using self in the implementation of the method. So, I tried using self and it seemed to work just fine:

Number oldSlash := Number getSlot("/")
Number / = method(x, self oldSlash(x))

我的问题是:为什么在使用关键字self时此方法起作用,为什么在使用Number时却不起作用?

My question is: Why does this work when the keyword self is used, and why does it not work when Number is used instead?

推荐答案

简短版本:

Number是数字的基类";不是实际的数值.您不能将其用于数学运算. self代表调用了您的方法的对象,它实际上是您要用作除法中分子的数字.

Number is a "base class" for numbers; not an actual numerical value. You can't use it for mathematical operations. self represents the object your method was invoked on, which turns out to be the number you want to use as numerator in your division.

较长版本:

首先,有一些背景知识:众所周知,除法有两个论点.您定义的方法仅接受一个参数(x).另一个参数是隐式的,它是您要调用除法的数字.为了清晰起见,在编写a / b时,在对象a上调用了方法/,并传递了值b作为参数.用更像C的语言,您会说a.divide(b)之类的话.您没有将a作为参数传递,但是无论如何都可以通过函数将其作为self进行访问.

First, some background: Division as you know takes two arguments. The method you're defining only takes one argument (the x). The other argument is implicit and it's the Number you're invoking the division on. To make it crystal clear, when you're writing a / b the method / is invoked on the object a and it gets passed the value b as parameter. In a more C-like language, you'd say something like a.divide(b). You don't pass a as a parameter, but it is accessible from the function anyway, as self.

因此,使用从上获得的知识,编写self oldSlash(x)时将self作为分子,而将x作为分母进行除法. self的值是在调用"newSlash"方法时设置的,再次隐式地指向您正在调用该方法的对象.如果您熟悉JavaScript,则self是Io的this名称.

So, using what we know from above, writing self oldSlash(x) performs division using self as numerator and x as denominator. The value of self is set when your "newSlash" method is called, once again implicitly to the object you're calling the method on. If you're familiar with JavaScript, self is Io's name for this.

相反,当您编写Number oldSlash(x)时,您将对象Number用作除法中的分子. Number不是实际数字,而是所有数字的基类".它没有值.因此,您无法对其执行数学运算.

When instead you write Number oldSlash(x) you use the object Number as the numerator in the division. Number is not an actual number, but rather the "base class" for all numbers. It does not have a value. Hence you cannot perform mathematical operations on it.

这篇关于方法内部的自我意义何在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆