Static extension methods in Kotlin

后端 未结 8 2371
生来不讨喜
生来不讨喜 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:24

    Recomend you to look at this link. As you can see there, you just should declare method at the top-level of the package (file):

    package strings
    public fun joinToString(...): String { ... }
    

    This is equal to

    package strings;
    
    public class JoinKt {
        public static String joinToString(...) { ... }
    }
    

    With constans everything are the same. This declaration

    val UNIX_LINE_SEPARATOR = "\n"
    

    is equal to

    public static final String UNIX_LINE_SEPARATOR = "\n";
    

提交回复
热议问题