一元操作符

表达式 对应的函数
+a a.unaryPlus()
-a a.unaryMinus()
!a a.not()
a++ a.inc()
a-- a.dec()

 

fun main(arg: Array<String>) {
    var a = "a"
    println(a++)
    println(a)
    println(++a)
}
operator fun String.inc():String{//重载String的++
    return this +this
}

二元操作符

表达式 对应的函数
a+b a.plus(b)
a-b a.minus(b)
a*b a.tims(b)
a/b a.div(b)
a%b a.mod(b)
a.b a.rangeTo(b)
a in b b.contains(a)
a !in b !b.contains(a)

 

fun main(arg: Array<String>) {
   var s= "hello"
    print(s*10)

}
//重载乘法操作符
operator fun String.times(other:Any?):String{
    var  result = ""
    if (other!= null){
        if (other is Int)
            for (i in 1..other){
                result+=this
            }
    }
    return result
}

方括号操作符重载

表达式 对应的函数
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..b a.rangeTo(b)
a[i_1,...,i_n]=b a.set(i_1,..i_n,b)

 

fun main(arg: Array<String>) {
    val arr1 :Array<Any> = arrayOf("3", "2")
    val arr2 :Array<Any> = arrayOf("9", "8")

    println("arr1[1] = ${arr1[1]}")
    println("arr2[1] = ${arr2[1]}")

    val arr3 = arrayOf(arr1, arr2)
    println("arr3[0][0] = ${arr3[0][0]}")


}
//重载一维数组[i]操作符
operator fun Array<Any>.get(i:Int):Int{
    if (this[i] is Int){
        return this[i] as Int
    }else if (this[i] is String){
        try {
            return this[i].toString().toInt()
        }catch (e:Exception){

        }
    }
    return 0
}
//重载二维数组[i][j]操作符
operator fun Array<Array<Any>>.get(i:Int,j:Int):Int{
    if (this[i][j] is Int){
        return this[i][j] as Int
    }else if (this[i][j] is String){
        try {
            return this[i][j].toString().toInt()
        }catch (e:Exception){

        }
    }
    return 0
}


赋值操作符重载

表达式  对应的函数
a+=b  a.pulsAssign(b)
a-=b a.minusAssign(b)
a*=b a.timesAssign(b)
a/=b a.divAssign(b)
a%=b a.modAssign(b)

 

 

fun main(arg: Array<String>) {
    val myString = myString()
    myString.value="sss"
    myString*=3
    print(myString)


}
class myString{
    var value :String = "hello"
    override fun toString(): String {
        return "myString(value='$value')"
    }

}

operator fun myString.timesAssign(n:Int):Unit{
    val v =this.value
    for (i in 1..n-1)
        this.value+=v
}

 

逻辑操作符重载

 

表达式 对应的函数
a>b a.compaerTo(b)>0
a<b a.compaerTo(b)<0
a>=b a.compaerTo(b)>=0
a<=b a.compaerTo(b)<=0
a==b a?.equals(b)?(b==null)
a!=b !a?.equals(b)?(b==null)

 

fun main(arg: Array<String>) {

    val myClass1 = myClass(1, 2)
    val myClass2 = myClass(1, 2)
    val myClass3 = myClass(3, 4)
    println(myClass1 == myClass2)
    println(myClass3 == myClass2)
}

class myClass(a: Int, b: Int) {
    private var a: Int = a
    private var b: Int = b
    operator fun compareTo(c: myClass): Int {
        var m = this.a + this.b
        var n = c.a + c.b
        return m - n

    }


    override fun equals(other: Any?): Boolean {
        if (this === other) return true
        if (javaClass != other?.javaClass) return false

        other as myClass

        if (a != other.a) return false
        if (b != other.b) return false

        return true
    }

    override fun hashCode(): Int {
        var result = a
        result = 31 * result + b
        return result
    }


}