Kotlin-31.オペレータ/演算子再負荷(operatooverload)

3559 ワード

公式文書:http://kotlinlang.org/docs/reference/operator-overloading.html
1.オペレータ再負荷(Operator overloading)
Kotlin                 !
             ( + - * /),      precedence
        member function     extension function
            operator     
2.一元操作子(Uniary operations)
1.     (Unary prefix operators)
                  
    +a          a.unaryPlus()
    -a          a.unaryMinus()
    !a          a.not()

     +a   ,         :
          a   ,   T;
            T   operator       unaryPlus(), T          ;
                   ,     ;
                     R,    +a     R

      :           (Basic types),           !

         "-"   :
        data class Point(val x: Int, val y: Int)
        operator fun Point.unaryMinus() = Point(-x, -y)

        val point = Point(10, 20)
        println(-point)  //  “(-10, -20)”

2.        (Increments and decrements)
                  
    a++         a.inc()
    a--         a.dec()

    a++  ++a   ,        :
          a   ,   T;
            T   operator       inc();
                 T  ;

    a++        :
         a          a0 ,
         a.inc()     a,
         a0         .

    ++a        :
         a.inc()     a,
         a           .
3.二元操作子(Binary operations)
1.     /   (Arithmetic operators)
                  
    a + b       a.plus(b)
    a - b       a.minus(b)
    a * b       a.times(b)
    a / b       a.div(b)
    a % b       a.rem(b), a.mod(b)(   )
    a..b        a.rangeTo(b)

     Kotlin 1.1   rem   , mod   (Kotlin 1.0)   

        :
    // Counter   "+"   
    data class Counter(val dayIndex: Int) {
        operator fun plus(increment: Int): Counter {
            return Counter(dayIndex + increment)
        }
    }

2.in   ('In' operator)
               
    a in b    b.contains(a)
    a !in b  !b.contains(a)

3.       (Indexed access operator)
                              
    a[i]                    a.get(i)
    a[i, j]                 a.get(i, j)
    a[i_1, ..., i_n]        a.get(i_1, ..., i_n)
    a[i] = b                a.set(i, b)
    a[i, j] = b             a.set(i, j, b)
    a[i_1, ..., i_n] = b    a.set(i_1, ..., i_n, b)

4.     (Invoke operator)
                          
    a()                 a.invoke()
    a(i)                a.invoke(i)
    a(i, j)             a.invoke(i, j)
    a(i_1, ..., i_n)    a.invoke(i_1, ..., i_n)

5.    (Augmented assignment)
                   
    a += b       a.plusAssign(b)
    a -= b       a.minusAssign(b)
    a *= b       a.timesAssign(b)
    a /= b       a.divAssign(b)
    a %= b       a.modAssign(b)

      a += b,          :
                 
                     plus()   ,    (  );
                    Unit,    ;
              a.plusAssign(b)   
              a = a + b    (    a + b   a    )        
      :  Kotlin        !

6.        (Equality and inequality operators)
                   
    a == b      a?.equals(b) ?: (b === null)
    a != b      !(a?.equals(b) ?: (b === null))

      : === !==(    /     )    ,       !
    null == null   true;     x, x == null   false

7.     (Comparison operators)
                   (  Int)
    a > b       a.compareTo(b) > 0
    a < b       a.compareTo(b) < 0
    a >= b      a.compareTo(b) >= 0
    a <= b      a.compareTo(b) <= 0
:http://www.jianshu.com/p/d0f5036fa076 CSDNブログ:http://blog.csdn.net/qq_32115349/articale/details/74364679 GitHubブログ:http://lioil.win/2017/07/04/Kotlin-operator.html Codingブログ:http://c.lioil.win/2017/07/04/Kotlin-operator.html