如何在Kotlin中向Java类添加静态方法 [英] How can one add static methods to Java classes in Kotlin
问题描述
是否可以向Kotlin
中的java.lang.Math
类添加新的静态方法?通常,由于 Kotlin扩展,这些事情在Kotlin中是可能的.
Is it possible to add a new static method to the java.lang.Math
class in Kotlin
? Usually, such things are possible in Kotlin thanks to Kotlin Extensions.
我已经尝试在我创建的名为Extensions.kt
的文件中执行以下操作:
I already tried doing the following in a file I made called Extensions.kt
:
fun Math.Companion.clamp(value:Double,minValue:Double,maxValue:Double):Double
{
return Math.max(Math.min(value,maxValue),minValue)
}
但是Math.Companion
无法解决...
推荐答案
从Kotlin 1.3开始,这是不可能的.但是,正在考虑在将来的版本中使用它!
As of Kotlin 1.3, this is not possible. However, it's being considered for a future release!
为帮助实现此功能,请就此问题进行投票: https://youtrack.jetbrains .com/issue/KT-11968
To help this feature get implemented, go vote on this issue: https://youtrack.jetbrains.com/issue/KT-11968
这个想法在Kotlin社区中非常流行,所以我敢肯定会很快出现.
This idea is very popular in the Kotlin community, so I bet it'll be in soon enough.
这篇关于如何在Kotlin中向Java类添加静态方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!