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. The extension function can add new methods to the existing class without modifying the original class, and extend the definition form of the function: The following example extends The output of the instance execution is as follows: The following code is The output of the instance execution is as follows: 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: The output of the instance execution is as follows: If the extension function is consistent with the member function, the memberfunction takes precedence when using the function. The output of the instance execution is as follows: Within the extension function, you can use the The output of the instance execution is as follows: 3.13.1. Extended function #
fun receiverType.functionName(params){
body
}
receiverType
represents the receiver of the function, that is, the object of the function extension
functionName
:The name of the extension function
params
: arguments to the extension function, which can be
NULL
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()
}
user name Runoob
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())
}
[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). 3.13.2. Extension functions are statically parsed #
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())
}
c
class C {
fun foo() { println("member function") }
}
fun C.foo() { println("spread function") }
fun main(arg:Array<String>){
var c = C()
c.foo()
}
member function
3.13.3. Extend an empty object #
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())
}
null