Static extension methods in Kotlin

后端 未结 8 2378
生来不讨喜
生来不讨喜 2020-12-05 03:55

How do you define a static extension method in Kotlin? Is this even possible? I currently have an extension method as shown below.

public fun Uber.doMagic(co         


        
8条回答
  •  日久生厌
    2020-12-05 04:20

    To create an extension method in kotlin you have to create a kotlin file(not a class) then declare your method in the file Eg:

    public fun String.toLowercase(){
        // **this** is the string object
    }
    

    Import the function in the class or file you are working on and use it.

提交回复
热议问题