Kotlin: How can I create a “static” inheritable function?

后端 未结 3 1057
野的像风
野的像风 2020-12-06 04:43

For example, I want to have a function example() on a type Child that extends Parent so that I can use the function on both.



        
3条回答
  •  [愿得一人]
    2020-12-06 05:06

    The purpose is to create an "alternative operator" to construction

    In Java you should use a Factory instead of a static method when used as a "constructor".

    On the Kotlin side you could use a top-level-function instead of a Factory.

    Or if you really want to have a "static method" in your class, I'd create a companion object and add static extension methods.

提交回复
热议问题