3.13. Kotlin extension

发布时间 : 2025-10-25 13:34:29 UTC      

Page Views: 10 views

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.

3.13.1. 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 extension

  • functionName :The name of the extension function

  • params : arguments to the extension function, which can be NULL

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).

3.13.2. 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 

3.13.3. 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){ var t = null println(t.toString()) }    

The output of the instance execution is as follows:

null 
《地理信息系统原理、技术与方法》  97

最近几年来,地理信息系统无论是在理论上还是应用上都处在一个飞速发展的阶段。 GIS被应用于多个领域的建模和决策支持,如城市管理、区划、环境整治等等,地理信息成为信息时代重要的组成部分之一; “数字地球”概念的提出,更进一步推动了作为其技术支撑的GIS的发展。 与此同时,一些学者致力于相关的理论研究,如空间感知、空间数据误差、空间关系的形式化等等。 这恰好说明了地理信息系统作为应用技术和学科的两个方面,并且这两个方面构成了相互促进的发展过程。