3.13. Kotlin extension

发布时间 :2023-10-12 23:00:04 UTC      

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

The output of the instance execution is as follows:

null

Principles, Technologies, and Methods of Geographic Information Systems  102

In recent years, Geographic Information Systems (GIS) have undergone rapid development in both theoretical and practical dimensions. GIS has been widely applied for modeling and decision-making support across various fields such as urban management, regional planning, and environmental remediation, establishing geographic information as a vital component of the information era. The introduction of the “Digital Earth” concept has further accelerated the advancement of GIS, which serves as its technical foundation. Concurrently, scholars have been dedicated to theoretical research in areas like spatial cognition, spatial data uncertainty, and the formalization of spatial relationships. This reflects the dual nature of GIS as both an applied technology and an academic discipline, with the two aspects forming a mutually reinforcing cycle of progress.