Extended properties and extended companion objects for Kotlin programming

Source: Internet
Author: User
Tags extend

Kotlin Programming related knowledge points : Kotlin programming using IntelliJ IED and understanding Source files (. kt) Kotlin programming and other properties Kotlin programming methods such as using Kotlin programming Kotlin programming of the parent class and the inherited parent class Kotlin programming interface and implementation interface Kotlin programming of the associated object, abstract class, sealed class Kotlin programming nested class, Inner class, Anonymous inner class Kotlin programming object expression and declaration Kotlin programming extension Method Extended Properties

In Kotlin programming, extension of the functions of the class is also supported.

Package com.xingen.kotlin.day2017527 Fun

Main (args:array<string>) {
    var  list=listof ("1", 2, "3")
    println (list.lastindex)
}

/**
 * Property Extension
 * Here, a lastindex property is extended to the list class, but not really as a member property.
 *  /val <T> list<t>.lastindex:int get () {
    //equals to Get=size-1, where the last element's Corner label
    is returned return  this.size-1
}

Output Result:

2

The extension does not actually add a member property to the class, so there is no way to have the extended property have a backup field. This is why the initialization function does not allow extended attributes. Extended properties can only be defined by explicitly providing getter and setter methods. extension of the associated object

In Kotlin programming, a class defines the associated object, and it can also extend the properties and functions of the associated object.

Package com.xingen.kotlin.day2017527 Fun

Main (args:array<string>) {

    day2017527.test ()
}
/**
 * Define Companion Object
 *
/Class day2017527{
    companion object{
    }
}
/**
 * Accompanying object extension:
 * Here to extend a method
 *
/Fun  Day2017527.Companion.test () {
    println ("Extension of accompanying object")
}

Output Result:

Extension of accompanying object

If a property extension or function extension is defined for the associated object, it can be called directly from the class name. scope of the extension

Write the following code in TEST.KT:

Package com.xingen.kotlin.day2017527

/**
 *  for function expansion:
 *   Specify a recipient in front of the function, where the recipient is mutablist< E> collection.
 *   expand a swap function for it
 *  the this for extension function points to the recipient object.
 *
 *  /Fun <T> Mutablelist<t>.swap (x:int,y:int) {
    //Use an intermediate variable to swap the value of the specified position.
    Val temp=this[x]
    this[x]=this[y]
    this[y]=temp
}

Write the following code in DAY2017527.KT:

Package com.xingen.kotlin.day2017527

//import needs to use the packet with the extension
function  import

com.xingen.kotlin.day2017527.*/**
 * Created by ${New root} on 2017/5/28 0028.
 * Blog: Http://blog.csdn.net/hexingen * * Fun

Main (args:array<string>) {
    /**
     * Call the extension method of the Mutabllistof class swap ()
     */
    var  mutablelist=mutablelistof ("1", 2,3)
    println (mutablelist)
    //using the custom extension function
    Mutablelist.swap (0,2)
    println (mutablelist)

}

Output Result:

[1, 2, 3]
[3, 2, 1]

Know:
In one source file, you write a package-level extension function that you can use in another source file.

The import method is as follows: Import the corresponding package, here is the import com.xingen.kotlin.day2017527.*. Of course, you can also import specific paths, packages. Extension function name, here is import com.xingen.kotlin.day2017527.swap. Benefits of Scaling

Through function extension, property extension, associated object extension of the contact, aspects of customizing some common function code. In Java programming, this is usually put into the Util class. In Kotlin programming, the role of tool classes can be achieved through extensions.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.