一般我們使用。對象爲。這種符號是什麼? 我們如何能夠從中獲得正確的結果?1. +(2)在斯卡拉給出了正確的答案。怎麼樣?
回答
在Scala中,1例如是對一個對象鍵入Int。該對象具有方法+,它接受另一個Int作爲參數。 此外,Scala有一些語法糖,讓你寫1.+(2)
爲1 + 2
,一樣的,你可以寫foo.map(bar)
爲foo map bar
。
我複製並從「在Scala編程」,這說粘貼文本:
斯卡拉技術上不具有運算符重載,因爲它實際上並沒有運營商在傳統意義上的。相反,可以在方法名稱中使用+, - ,*和/等字符。因此,當您在步驟1中將1 + 2鍵入Scala解釋器時,實際上是在Int對象1上調用一個名爲+的方法,並將2作爲參數傳入。如圖3.1所示,可以可選地已經使用傳統的方法調用的語法寫入1 + 2(1)。+(2)
在Scala中,也那些已知在Java「的基本類型」是對象,並且延伸AnyVal
。 在斯卡拉,每個值都是支持某些方法的對象,具體取決於它的類。 數值類型如Int
,Double
等具有之和的方法+
(方法名稱不限於字母數字字符)。現在,問題是,斯卡拉提供syntactic sugar
,讓這些方法調用顯示爲operations
(operator syntax
),因此1+2 == 1.+(2)
從Documentation和Int
類Source 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 + 2
是1.+(2)
的簡寫在這裏,+
是該方法的名稱。斯卡拉對方法名稱中的非字母數字字符沒有愚蠢的偏見。您可以使用任何符號名稱來定義方法。
一般來說,你可以寫a method b
作爲a.method(b)
的簡寫,其中method
是一個帶有兩個參數(一個隱式,一個顯式)的方法。例如,而不是1.to(10)
您可以編寫1 to 10
使用任何您認爲更易於閱讀的內容。開始Scala程序員傾向於堅持Java語法,這就好了。當然,即使是最強硬的Java程序員似乎更喜歡a + b
而不是a.+(b)
。
下面的代碼是自我解釋,對象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
- 1. C代碼給出了錯誤的答案,但java代碼給出了正確的答案spoj
- 2. -1%4的正確答案是什麼?
- 3. C++ * =沒有給出正確的答案
- 4. 歐拉項目#2;代碼未能輸出正確的答案
- 5. 如何根據測驗中的正確答案給出答案
- 6. ADODB Recordset.RecordCount給出了不正確的答案
- 7. JavaScript輸出「1」 - - 「1」是2怎麼樣?
- 8. 給出一個測驗2正確的答案
- 9. bestappr函數給出了pari/gp中的不正確答案
- 10. 爲什麼scipy的稀疏求解器給出了不正確的答案?
- 11. 我怎樣才能得到正確答案在正確答案的位置顯示
- 12. 答案是:6÷2(1 + 2)=?
- 13. 程序在寫入時給出錯誤的答案,但在複製時給出正確的答案
- 14. 單數2斯卡拉解決方案
- 15. pmin給出了錯誤的答案
- 16. 要求回答,直到給出正確答案? Java Eclipse
- 17. 使用蒙特卡羅方法計算PI給出了不準確的答案
- 18. 仙功能不給正確的答案
- 19. 在斯卡拉你怎麼加入2 RDD
- 20. 如果沒有給出正確的答案,請重複案例
- 21. SUMIF公式沒有給出正確的答案在excel vba
- 22. 浮點給出了錯誤的答案
- 23. Matlab給出了錯誤的答案
- 24. sort_values與排序給出不同的答案,但sort_values是正確的答案
- 25. 給出的答案
- 26. Date.AddDays沒有給予正確的答案
- 27. 斯卡拉-java.lang.ArrayIndexOutOfBoundsException:1
- 28. 斯卡拉怎麼寫,如果其他
- 29. 打印兩個雙打答案給出了答案0.00
- 30. 的Javascript:一個循環後打開URL給出了正確的答案
在Scala中,'+'是方法名,而不是操作符。 – Shankar
無法理解你的問題.... 1這裏是一個Integer,一個Object,'+'Integer的一個方法,以及什麼? –
@JohnZeng我讀過Scala爲每個對象提供豐富的包裝庫.Rich Int爲Int,Rich Float爲float等等。那麼在這裏,究竟發生了什麼? – NeoWelkin