如何在Kotlin中向Java类添加静态方法 [英] How can one add static methods to Java classes in Kotlin

查看:406
本文介绍了如何在Kotlin中向Java类添加静态方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以向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屋!

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