Chapter12 Scala操作符

來源:互聯網
上載者:User

標籤:scala操作符   提取器   scala   update   優先順序   

1. 標識符
  • 變數名、函數名、類名等統稱為標識符,scala可以使用任何字元來作標識符,比如 ()!#%&×+-/:<>[email protected]\^|~ 等。

  • 反引號中可以使用任何字元序列。

    val √ = scala.math.sqrt _    √(2)    val `val` = 42    println(`val`)
2. 中置操作符
  • 操作符要接兩個參數,分別位於操作符兩邊。

  • a 標識符 b 等價於 a.標識符(b) 。

    1 to 10    等價於 1.to(10)    1 -> 10    等價於 1 .->(10) 
3. 一元操作符
  • 只有一個參數的操作符成為一元操作符,操作符的位置可能位於參數的前或後,所有有下面兩種情況。

  • a 標識符 等價於 a.標識符() ,標識符 a 等價於 a.unary_標識符 。

    1 toString    等價於 1.toString()    val a = 42    -a    等價於 a.unary_-
4. 賦值操作符
  • 賦值操作符的形式為 操作符=,運算式是 a 操作符= b ,等價於 a = a 操作符 b 。
    a += 1    等價於 a = a + 1
  • <=、>=、!=、==、===、=/= 等不是賦值操作符,
5. 優先順序
  • 後置操作符優先順序低於中置操作符,a 中置操作符 b 後置操作符 等價於 (a 中置操作符 b) 後置操作符 。

  • 符號優先順序如下表所示:

    操作符 優先順序
    除下面字元以外的操作符 1(最高)
    * / % 2
    + - 3
    : 4
    < > 5
    != 6
    & 7
    ^ 8
    | 9
    賦值操作符 10(最低)
6. 結合性
  • Scala除冒號操作符和賦值操作符是右結合,所有其他動作符都是左結合。
    // 構造列表List    1 :: 2 :: Nil   // :: 右結合    1 :: (2 :: Nil)    (1 :: 2) :: Nil // 錯誤
  • 右結合的的第二個參數就是方法,比如 2::Nil 等價於 Nil.::(2)。
7. apply和update方法
  • 函數或方法位於指派陳述式的等號左側,調用的是update方法,否則調用的apply方法。
    val scores = new scala.collection.mutable.HashMap[String, Int]    scores("Bob") = 100     等價於調用:scores.update("Bob", 100)     val bobsScore = scores("Bob")    等價於調用:scores.apply("Bob")
8. 提取器unapply
  • 提取器:是一個帶有unapply方法的對象,可以當做是伴生對象apply的方法的反操作。

  • unapply接受一個對象,從中提取值。

    class Fraction(n: Int, d: Int) {      private val num: Int = if (d == 0) 1 else n;      private val den: Int = if (d == 0) 0 else d;      def *(other: Fraction) = new Fraction(num * other.num, den * other.den)    }    // unapply返回的就是構造該對象的兩個值    object Fraction {      def apply(n: Int, d: Int) = new Fraction(n, d)      def unapply(input: Fraction) =        if (input.den == 0) None else Some((input.num, input.den))    }    object Main extends App {        var Fraction(a, b) = Fraction(3, 4) * Fraction(2, 5)      println(a)      println(b)    }
  • 每個範例類都會自動具備apply方法和unapply方法,範例類後面章節會講解,這裡只要瞭解就可以。

  • 提取器可以只測試輸入而不真正將其值提取出來,只返回一個Boolean。

9. unapplySeq方法
  • 使用unapplySeq方法可以提取任意長度的值序列,它返回一個Option[Seq[A]],A是被提取的類型。
    // Name 提取器可以產生所有組成部分的序列    // 關於 Option,Some,None後面章節會詳細講解,這裡只需瞭解upapplySeq的作用就可以。    object Name {      def unapplySeq(input: String): Option[Seq[String]] =        if (input.trim == "") None else Some(input.trim.split("\\s+"))    }

【待續】

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

Chapter12 Scala操作符

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.