在Kotlin中没有静态关键字。
在Kotlin中表示静态Java方法的最佳方法是什么?
在Kotlin中没有静态关键字。
在Kotlin中表示静态Java方法的最佳方法是什么?
当前回答
A.旧Java方式:
声明一个伴随对象来包含一个静态方法/变量 类Foo { 伴随对象{ fun foo() = println(" foo ") Val bar ="bar" } } 使用: Foo. Foo() //输出Foo println(Foo.bar) //输出bar
B.新的Kotlin方式
直接在文件上声明,而不需要在.kt文件上使用类。 fun foo() = println(" foo ") Val bar ="bar" 使用方法/变量的名称。(导入后) 使用: foo() //输出foo println(bar) //输出bar
其他回答
您只需要创建一个伴生对象并将函数放入其中
class UtilClass {
companion object {
// @JvmStatic
fun repeatIt5Times(str: String): String = str.repeat(5)
}
}
从kotlin类中调用该方法:
class KotlinClass{
fun main(args : Array<String>) {
UtilClass.repeatIt5Times("Hello")
}
}
或使用import
import Packagename.UtilClass.Companion.repeatIt5Times
class KotlinClass{
fun main(args : Array<String>) {
repeatIt5Times("Hello")
}
}
从java类中调用该方法:
class JavaClass{
public static void main(String [] args){
UtilClass.Companion.repeatIt5Times("Hello");
}
}
或者在方法中添加@JvmStatic注释
class JavaClass{
public static void main(String [] args){
UtilClass.repeatIt5Times("Hello")
}
}
或者在方法中添加@JvmStatic注释并在java中进行静态导入
import static Packagename.UtilClass.repeatIt5Times
class JavaClass{
public static void main(String [] args){
repeatIt5Times("Hello")
}
}
你把函数放在“同伴对象”中。
java代码是这样的:
class Foo {
public static int a() { return 1; }
}
将成为
class Foo {
companion object {
fun a() : Int = 1
}
}
然后,您可以在Kotlin代码中作为
Foo.a();
但是在Java代码中,您需要将其调用为
Foo.Companion.a();
(也可以在Kotlin内部工作。)
如果您不喜欢指定Companion位,您可以添加@JvmStatic注释或命名您的Companion类。
从文档中可以看出:
Companion Objects An object declaration inside a class can be marked with the companion keyword: class MyClass { companion object Factory { fun create(): MyClass = MyClass() } } Members of the companion object can be called by using simply the class name as the qualifier: val instance = MyClass.create() ... However, on the JVM you can have members of companion objects generated as real static methods and fields, if you use the @JvmStatic annotation. See the Java interoperability section for more details.
添加@JvmStatic注释如下所示
class Foo {
companion object {
@JvmStatic
fun a() : Int = 1;
}
}
然后它将作为一个真正的Java静态函数存在,从 Java和Kotlin的Foo.a()。
如果只是不喜欢Companion名称,那么也可以 为同伴对象提供一个显式的名称,如下所示:
class Foo {
companion object Blah {
fun a() : Int = 1;
}
}
这将让您以同样的方式从Kotlin调用它,但是 来自java,如Foo.Blah.a()(这也可以在Kotlin中工作)。
所有静态成员和函数都应该在伴生块中
companion object {
@JvmStatic
fun main(args: Array<String>) {
}
fun staticMethod() {
}
}
简单来说,你可以使用“同伴对象”进入Kotlin静态世界,比如:
companion object {
const val TAG = "tHomeFragment"
fun newInstance() = HomeFragment()
}
要创建一个常量字段,请使用代码中的“const val”。 但是尽量避免使用静态类,因为它会给使用Mockito进行单元测试带来困难。
很多人提到伴生对象,这是正确的。但是,正如你所知道的,你也可以使用任何类型的对象(使用对象关键字,而不是类),即,
object StringUtils {
fun toUpper(s: String) : String { ... }
}
像使用java中的静态方法一样使用它:
StringUtils.toUpper("foobar")
That sort of pattern is kind of useless in Kotlin though, one of its strengths is that it gets rid of the need for classes filled with static methods. It is more appropriate to utilize global, extension and/or local functions instead, depending on your use case. Where I work we often define global extension functions in a separate, flat file with the naming convention: [className]Extensions.kt i.e., FooExtensions.kt. But more typically we write functions where they are needed inside their operating class or object.