1,关键字by修饰类,表示类委托 interface Animation{ fun eat() } //动态代理 class Dog:Animation{ override fun eat() { println("dog eat oligarch") } } class DogProxy:Animation by Dog(){ }
2,关键字by修饰变量,实现属性委托
var name:String by NameDelegate() class NameDelegate:ReadWriteProperty<Any?,String>{ override fun getValue(thisRef: Any?, property: KProperty<*>): String { println(property.name) return property.name } override fun setValue(thisRef: Any?, property: KProperty<*>, value: String) { println(value) } }
3,operator修饰方法,表示重写操作符文章来源:https://www.toymoban.com/news/detail-801986.html
lateinit var mList:MyList<Int> mList.plus(1) mList+1 interface MyList<out T>{ fun get(index:Int):T operator fun plus(t: @UnsafeVariance T) operator fun div(t: @UnsafeVariance T) operator fun minus(t: @UnsafeVariance T) }
kotlin所有运算操作符:文章来源地址https://www.toymoban.com/news/detail-801986.html
一元操作符(Unary Operators) | ||
+a | a.unaryPlus() | |
-a | a.unaryMinus() | |
!a | a.not() | |
a++ | a.inc() | |
二元操作符( Binary Operators) | ||
到了这里,关于Kotlin特性学习笔记的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!