Kotlin extension
Kotlin
can extend the properties and methods of a class without inheriting or using Decorator
mode.
Extension is a static behavior that has no effect on the extended class codeitself.
Extended function
The extension function can add new methods to the existing class without modifying the original class, and extend the definition form of the function:
fun receiverType.functionName(params){
body
}
receiverType
represents the receiver of the function, that is, the object of the function extensionfunctionName
:The name of the extension functionparams
: arguments to the extension function, which can beNULL
The following example extends User
class:
class User(var name:String)
/**spread function**/
fun User.Print(){
print("user name $name")
}
fun main(arg:Array<String>){
var user = User("Runoob")
user.Print()
}
The output of the instance execution is as follows:
user name Runoob
The following code is MutableList
add a swap
function:
// spread function swap,Swapping values from different positions
fun MutableList<Int>.swap(index1: Int, index2: Int) {
val tmp = this[index1] // this Corresponding List
this[index1] = this[index2]
this[index2] = tmp
}
fun main(args: Array<String>) {
val l = mutableListOf(1, 2, 3)
// The values of positions 0 and 2 have been interchanged
l.swap(0, 2) // The 'this' in the 'swap()' function will point to the value of 'l'
println(l.toString())
}
The output of the instance execution is as follows:
[3, 2, 1]
this
keyword refers to the recipient object (receiver object) (that is, the object instance specified before the period when the extension function is called).
Extension functions are statically parsed
The extension function is statically parsed and is not a virtual member of the receiver type. When calling the extension function, which function is called is determined by the object expression of the calling function, not by the dynamic type:
open class C
class D: C()
fun C.foo() = "c" // spread function foo
fun D.foo() = "d" // spread function foo
fun printFoo(c: C) {
println(c.foo()) // Type C
}
fun main(arg:Array<String>){
printFoo(D())
}
The output of the instance execution is as follows:
c
If the extension function is consistent with the member function, the memberfunction takes precedence when using the function.
class C {
fun foo() { println("member function") }
}
fun C.foo() { println("spread function") }
fun main(arg:Array<String>){
var c = C()
c.foo()
}
The output of the instance execution is as follows:
member function
Extend an empty object
Within the extension function, you can use the this
to determine whether the recipient is NULL
in this way, even if the recipient is NULL
you can also call extension functions For example:
fun Any?.toString(): String {
if (this == null) return "null"
// After null detection, 'this' will automatically be converted to a non null type,
so the following toString()
// Resolve to a member function of the Any class
return toString()
}
fun main(arg:Array<String>){
var t = null
println(t.toString())
}
The output of the instance execution is as follows:
null