扩展功能可以在“静态"域中调用吗?道路? [英] Can extension functions be called in a "static" way?

查看:72
本文介绍了扩展功能可以在“静态"域中调用吗?道路?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以创建扩展功能并将其命名为,就好像它是静态的一样?

Is it possible to create an extension function and call it as if it were static?

fun System.sayByeAndExit() {
    println("Goodbye!")
    System.exit()
}

fun main(args: Array<String>) {
    System.sayByeAndExit() // I'd like to be able to call this
}

我知道代码示例不起作用...

  • 我了解kotlin的扩展功能是静态解析的,如 Kotlin参考(扩展功能) ,但这并不意味着它们可以被当作类中的静态函数来调用(从Java的角度来看).

    I know that the code sample doesn't work...

    • I understand that kotlin's extension functions are resolved statically, as mentioned in the Kotlin Reference (Extension Functions), but this does not mean they can be called as if they were static functions within a class (in a Java sense).

      我也理解该代码将不起作用,因为没有没有实例传递给编译器将生成的方法;因此它将无法编译.

      I also understand that this code will not work because there is no instance of System to pass into the method that the compiler will generate; therefore it won't compile.

      你们中的有些人可能想知道为什么这种行为是可取的.我能理解您为什么会认为不是,所以这里有一些原因:

      Some of you might be wondering why this behaviour is desirable. I can understand why you would think that is isn't, so here are some reasons:

      1. 它具有标准扩展功能提供的所有优点.
      2. 不需要创建类的实例 只需即可访问其他功能.
      3. 可以从应用程序范围的上下文(如果该类可见)访问这些功能.
      1. It has all of the benefits that standard extension functions give.
      2. An instance of the class doesn't need to be created just to access the extra functionality.
      3. The functions can be accessed from an application-wide context (provided the class is visible).

      总结一下...

      Kotlin是否有办法将静态函数钩"到类上?我很想知道.

      推荐答案

      您真正要的是用于类引用的扩展函数"或将静态方法添加到现有类",这在这里被另一个问题覆盖:如何在Kotlin中向Java类添加静态方法包含在功能请求中 KT-11968

      You are really asking for "extension functions for a Class reference" or "adding static methods to existing classes" which was covered by another question here: How can one add static methods to Java classes in Kotlin which is covered by a feature request KT-11968

      扩展功能不能添加到没有实例的任何东西中.对类的引用不是实例,因此您不能扩展诸如java.lang.System之类的内容.但是,您可以扩展现有类的伴侣对象.例如:

      Extension functions cannot be added to anything that does not have an instance. A reference to a Class is not an instance and therefore you cannot extend something like java.lang.System. You can however extend a companion object of an existing class. For example:

      class LibraryThing {
          companion object { /* ... */ }
      }
      

      允许您扩展LibraryThing.Companion,因此,当您实际上在扩展伴侣对象的单例实例时,调用某些新的myExtension()方法就好像您是在扩展Class引用本身一样:

      Allows you to extend LibraryThing.Companion and therefore calling some new myExtension() method would look like you are extending the Class reference itself, when really you are extending the singleton instance of the companion object:

      fun LibraryThing.Companion.myExtension() = "foo"
      
      LibraryThing.Companion.myExtension() // results in "foo"
      LibraryThing.myExtension() // results in "foo"
      

      因此,您可能会发现一些Kotlin库仅在这种情况下添加了空的伴随对象.其他人则不然,对于那些人,您倒霉".由于Java没有伴随对象,因此您也不能对Java执行相同的操作.

      Therefore you might find some Kotlin libraries add empty companion objects just for this case. Others do not, and for those you are "out of luck." Since Java does not have companion objects, you cannot do the same for Java either.

      另一个通常要求的功能是采用现有的Java静态方法,该方法将类的实例作为第一个参数,并使其充当扩展函数.问题 KT-5261 KT-732 KT-3487 甚至可能其他功能请求.

      The other commonly requested feature is to take an existing Java static method that accepts an instance of a class as the first parameter, and make it behave as an extension function. This is tracked by issues KT-5261, KT-2844, KT-732, KT-3487 and probably other feature requests.

      这篇关于扩展功能可以在“静态"域中调用吗?道路?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆