这里发生了几件事。简而言之:
- 将常量“1”隐式转换为 Int 实例
- 对采用单个参数的方法利用“空格符号”
在 1 max 2 的情况下,常量 1 被隐式转换为 Int(即 Int 类的实例)。
由于 Int 类定义了一个名为“max”的方法,该方法采用单个参数,您可以使用空格或infix notation。以下都是等价的(在 spark-shell 中运行):
scala> 1 max 2
res8: Int = 2
scala> 1.max(2)
res9: Int = 2
scala> val x = 1 // Note the result type here is Int
x: Int = 1
scala> x.max(2)
res10: Int = 2
scala> x max (2)
res11: Int = 2
scala> 1 // Note the result type here is *also* Int
res12: Int = 1
scala> 1. // Here are all of the methods that are defined for an Int
!= + << >> byteValue ensuring formatted isInfinity isValidByte isWhole notify signum toChar toInt toString until
## - <= >>> ceil eq getClass isInstanceOf isValidChar longValue notifyAll synchronized toDegrees toLong unary_+ wait
% -> == ^ compare equals hashCode isNaN isValidInt max round to toDouble toOctalString unary_- |
& / > abs compareTo floatValue intValue isNegInfinity isValidLong min self toBinaryString toFloat toRadians unary_~ →
* < >= asInstanceOf doubleValue floor isInfinite isPosInfinity isValidShort ne shortValue toByte toHexString toShort underlying
请注意,Int 上有很多可用的方法,例如 max、min、+、- 等等。查看 say, + 的签名,我们可以看到 + 是一个接受单个参数的方法。因此我们可以做到以下几点:
scala> 1 + 2 // No surprises here
res15: Int = 3
scala> 1.+(2) // Huh? This works because + is a method of Int that takes a single parameter.
// This is effectively the same as your max example.
res16: Int = 3
scala> 1.+ // Here are the signatures of the + method.
def +(x: Char): Int
def +(x: Long): Long
def +(x: Float): Float
def +(x: Short): Int
def +(x: Double): Double
def +(x: Byte): Int
def +(x: String): String
def +(x: Int): Int
scala> 1 + 'A' // From the above, we can see that the following is also valid
res17: Int = 66 // because the return type is Int
scala> 1 + "41"
res18: String = 141 // In this case, the + operator is a String concatenation operator Because the return type is String
scala> 1 + "x"
res19: String = 1x // Also because the return is String, but possible more intuitive.
到问题的 pow 部分。
Math.pow 是一种采用 2 个参数的方法。因为它需要 2 个参数,所以您无法使用空格符号。
此外,pow 不是与 Int 关联的方法。它很像 Math 类的静态方法(实际上 Math 是一个对象)。所以,就像你不能说 x.pow(y,z) 你不能说 1.pow(y, z) 另一方面你可以说 Math.pow(x, 2) - 得到 x 平方 -因为这与 pow 方法的签名相匹配。
这是 Math.pow 的签名:
scala> Math.pow
def pow(x$1: Double,x$2: Double): Double
这比 + 稍微不那么令人兴奋,但很明显它需要 2 个 Doubles 并返回一个 Double。即使将整数作为参数提供(当需要双精度时),说 Math.pow(2,2) 的示例仍然有效,因为 Ints 会自动转换为 Double。
我希望这有助于解释您所看到的。我鼓励您在 spark-shell、sbt 或其他一些 scala REPL 中尝试这些示例。