Static extension methods in Kotlin

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

    You can create a static method with using Companion object like:

    class Foo {
        // ...
        companion object {
            public fun bar() {
                // do anything
            }
        }
    }
    

    and then you can call it like:

    class Baz {
        // ...
        private fun callBar() {
            Foo.bar()
        }
    }
    

提交回复
热议问题