2017-06-20 93 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 
相關問題