- 重載算術(shù)運(yùn)算符
data class Point(val x: Int,val y:Int){
operator fun plus(other: Point): Point { // operator關(guān)鍵字修飾plus函數(shù):a + b = a.plus(b)
return Point(x + other.x, y + other.y)
}
}
operator fun Point.plus(other: Point) : Point{...}//也可用擴(kuò)展函數(shù)定義
Point(10,20) + Point(30,40) == Point(40,60)
對于一元運(yùn)算符
operator fun Point.unaryMinus(): Point { return Point(-x,-y) } // -a
表達(dá)式 |
函數(shù)名 |
a * b |
times |
a / b |
div |
a % b |
mod |
a + b |
plus |
a - b |
minus |
+a |
unaryPlus |
-a |
unaryMinus |
!a |
not |
++a |
inc |
--a |
dec |
//不同類型之間使用約定運(yùn)算符,類型不能調(diào)換位置
operator fun Point.times(scale: Double) : Point { .... } // Point(10,20) * 1.5 == Point(15,30)
operator fun Double.times(p: Point) : Point { .... } // 1.5 * Point(10,20) == Point(15,30)
//返回值不同
operator fun Char.times(count: Int) : String { .... } // 'a' * 3 == "aaa"
Kotlin沒有提供位運(yùn)算符,用下面方式代替
中綴運(yùn)算符 |
作用 |
shl |
帶符號左移 |
shr |
帶符號右移 |
ushr |
無符號右移 |
and |
按位與 |
or |
按位或 |
xor |
按位異或 |
inv |
按位取反 |
0x0F and 0xF0 == 0
0x0F or 0xF0 == 255
- 重載復(fù)合賦值運(yùn)算符
通常定義了plus、times ,+=福铅、*=也生效
val numbers = ArrayList<Int>()
numbers += 42 // numbers[0] == 42
//實現(xiàn)上面的功能需要用到plusAssign函數(shù)弟胀,類似的也有timesAssign、minusAssign
operator fun <T> MutableCollection<T>.plusAssign(element:T) { this.add(element) }
- 重載比較運(yùn)算符
- == 等于Java的equals眉厨, === 等于Java的 ==
- override fun equal(obj: Any? : Boolean { ... } //就是重寫 ==,沒有用operator 關(guān)鍵字
- data類型編譯器會默認(rèn)實現(xiàn) equals
- compareTo 和Java一樣
class Person(val firstName: Stirng,lastName: String) : Comparable<Person> {
override fun compareTo(other: Person) : Int {
//使用kotlin內(nèi)置函數(shù)compareValuesBy可以很方便進(jìn)行比較
return compareValuesBy(this,other,Person::lastName,Person::firstName)
}
}
- 集合與區(qū)間的約定
operator fun Point.get(index: Int) : Int{
return when(index){
0 -> x
1 -> y
else -> throw IndexOutOfBoundsException("...")
}
}
operator fun Point.set(index: Int,value: Int) {
return when(index){
0 -> x = value
1 -> y = value
else -> throw IndexOutOfBoundsException("...")
}
}
val p = Point(10,20) // p[1] == 20
p[0] = 20 // p[0] == 20
//支持多維
operator fun get(rowIndex: Int,colIndex: Int)
matrix[row,col]
- a in c 相當(dāng)于 c.contains(a) :
operator fun Obj.contains(p: Type) : Boolean
- start..end 相當(dāng)于 start.rangeTo(end) ! ..優(yōu)先級低于算術(shù)運(yùn)算符 ! :
operator fun <T: Comparable<T>> T.rangeTo(that : T) : ClosedRange<T>
- for循環(huán) 對應(yīng) iterator(和Java相同)
operator fun CharSequece.iterator() : CharIterator
- 結(jié)構(gòu)聲明和組件函數(shù)
val p = Point(10,20)
val (x,y) = p // x == 10 y == 20
class Point(val x: Int,val y: Int) { //componentN:N<=5 只能到前五個
operator fun component1() = x
operator fun component2() = y
}
- 對于元組Kotlin有Pair锌奴、Triple類
- 委托屬性
class Delegate {
operator fun getValue(...) {..}
operator fun setValue(...,value: Tyep) {..}
}
class Foo{
var p: Type by Delegate() //通過 Delegat來對p操作
}
- kotlin有個懶加載 by lazy()就是使用委托
class Person(val name: String){
val emails by lazy { loadEmails(this) }
}
- 實現(xiàn)委托屬性(原理) P198 7.5.3 代碼清單 7.19-24
- 一個操作數(shù)據(jù)庫框架exposed就是通過委托實現(xiàn)