2017-06-20 41 views
1

一般我们使用。对象为。这种符号是什么? 我们如何能够从中获得正确的结果?1. +(2)在斯卡拉给出了正确的答案。怎么样?

+0

在Scala中,'+'是方法名,而不是操作符。 – Shankar

+0

无法理解你的问题.... 1这里是一个Integer,一个Object,'+'Integer的一个方法,以及什么? –

+0

@JohnZeng我读过Scala为每个对象提供丰富的包装库.Rich Int为Int,Rich Float为float等等。那么在这里,究竟发生了什么? – NeoWelkin

回答

2

在Scala中,1例如是对一个对象键入Int。该对象具有方法+,它接受另一个Int作为参数。 此外,Scala有一些语法糖,让你写1.+(2)1 + 2,一样的,你可以写foo.map(bar)foo map bar

+0

得到你的解释我的问题。你能解释一下foo example.i不是来自Java背景。 – NeoWelkin

+0

'foo.map(bar)'意味着这个语法糖的一个通用的例子或泛化。如果你有一个带有'map'方法的对象'Foo',它带有一个参数,例如'bar',然后你可以把它写成'foo.map(bar)'或'foo map bar'。就像'+'一样:'1 + 2'被scala编译器内部转换为'1。+(2)'。 – EmilioMg

10

我复制并从“在Scala编程”,这说粘贴文本:

斯卡拉技术上不具有运算符重载,因为它实际上并没有运营商在传统意义上的。相反,可以在方法名称中使用+, - ,*和/等字符。因此,当您在步骤1中将1 + 2键入Scala解释器时,实际上是在Int对象1上调用一个名为+的方法,并将2作为参数传入。如图3.1所示,可以可选地已经使用传统的方法调用的语法写入1 + 2(1)。+(2)

enter image description here

+3

这是一种方法,而不是一种功能。它们是有区别的。看到这里:https://stackoverflow.com/questions/155609/difference-between-a-method-and-a-function – EmilioMg

+0

@Ramesh Maharjan是点运算符调用该函数? – NeoWelkin

+1

@NewWelkin我有更新我的答案。感谢EmillMg。 :) –

2

在Scala中,也那些已知在Java“的基本类型”是对象,并且延伸AnyVal。 在斯卡拉,每个值都是支持某些方法的对象,具体取决于它的类。 数值类型如IntDouble等具有之和的方法+(方法名称不限于字母数字字符)。现在,问题是,斯卡拉提供syntactic sugar,让这些方法调用显示为operationsoperator syntax),因此1+2 == 1.+(2)

0

DocumentationIntSource File,您可以为+方法得到的描述,它的重载版本:

/** Returns the sum of this value and `x`. */ 
def +(x: Byte): Int 
/** Returns the sum of this value and `x`. */ 
def +(x: Short): Int 
/** Returns the sum of this value and `x`. */ 
def +(x: Char): Int 
/** Returns the sum of this value and `x`. */ 
def +(x: Int): Int 
/** Returns the sum of this value and `x`. */ 
def +(x: Long): Long 
/** Returns the sum of this value and `x`. */ 
def +(x: Float): Float 
/** Returns the sum of this value and `x`. */ 
def +(x: Double): Double 

正如你在这里看到的,+看起来像一个方法,没有更多。理解符号1.+(2)就足够了。

然而,这并不是全部。有关更多信息,请参阅here

1

1 + 21.+(2)的简写在这里,+是该方法的名称。斯卡拉对方法名称中的非字母数字字符没有愚蠢的偏见。您可以使用任何符号名称来定义方法。

一般来说,你可以写a method b作为a.method(b)的简写,其中method是一个带有两个参数(一个隐式,一个显式)的方法。例如,而不是1.to(10)您可以编写1 to 10

使用任何您认为更易于阅读的内容。开始Scala程序员倾向于坚持Java语法,这就好了。当然,即使是最强硬的Java程序员似乎更喜欢a + b而不是a.+(b)

0

下面的代码是自我解释,对象int,我想应用这些方法。所以我按了。这给了我一组方法,其中+是其中的一部分,它将int作为参数并返回Int。所以它解释了一切。

的主要原因是

斯卡拉实际上并没有运营商在传统意义上的,但它的方法。

scala> val x=2; 
x: Int = 2 

scala> x. 
!= - == ^   compareTo  intValue  isPosInfinity isValidShort round  toBinaryString toFloat   toRadians underlying 
% / >  abs   doubleValue isInfinite  isValidByte  isWhole  self   toByte   toHexString  toShort  until 
& < >= byteValue floatValue isInfinity  isValidChar  longValue  shortValue toChar   toInt   unary_+  | 
* << >> ceil  floor   isNaN   isValidInt  max   signum  toDegrees  toLong   unary_- 
+ <= >>> compare  getClass  isNegInfinity isValidLong  min   to   toDouble   toOctalString unary_~ 

scala> x.+ 
         def +(x: Long): Long def +(x: Float): Float  def +(x: Int): Int def +(x: Double): Double 
def +(x: Short): Int def +(x: Char): Int def +(x: String): String def +(x: Byte): Int 

scala> x.+ 
         def +(x: Double): Double def +(x: Char): Int def +(x: Short): Int def +(x: String): String 
def +(x: Long): Long def +(x: Float): Float  def +(x: Int): Int def +(x: Byte): Int 

scala> x.+(3) 
res1: Int = 5 
相关问题