在Kotlin中没有静态关键字。
在Kotlin中表示静态Java方法的最佳方法是什么?
在Kotlin中没有静态关键字。
在Kotlin中表示静态Java方法的最佳方法是什么?
当前回答
尽管这是2年多一点的现在,并有很多伟大的答案,我看到一些其他的方法得到“静态”Kotlin字段的缺失。下面是Kotlin-Java静态互操作的示例指南:
Scenario 1: Creating a static method in Kotlin for Java Kotlin @file:JvmName("KotlinClass") //This provides a name for this file, so it's not defaulted as [KotlinClassKt] in Java package com.frybits class KotlinClass { companion object { //This annotation tells Java classes to treat this method as if it was a static to [KotlinClass] @JvmStatic fun foo(): Int = 1 //Without it, you would have to use [KotlinClass.Companion.bar()] to use this method. fun bar(): Int = 2 } } Java package com.frybits; class JavaClass { void someFunction() { println(KotlinClass.foo()); //Prints "1" println(KotlinClass.Companion.bar()); //Prints "2". This is the only way to use [bar()] in Java. println(KotlinClass.Companion.foo()); //To show that [Companion] is still the holder of the function [foo()] } //Because I'm way to lazy to keep typing [System.out], but I still want this to be compilable. void println(Object o) { System.out.println(o); } }
Michael Anderson的回答提供了更多的深度,在这种情况下绝对应该参考。
下一个场景处理在Kotlin中创建静态字段,这样Java就不必在不需要静态函数的情况下一直调用KotlinClass.foo()。
Scenario 2: Creating a static variable in Kotlin for Java Kotlin @file:JvmName("KotlinClass") //This provides a name for this file, so it's not defaulted as [KotlinClassKt] in Java package com.frybits class KotlinClass { companion object { //This annotation tells Kotlin to not generate the getter/setter functions in Java. Instead, this variable should be accessed directly //Also, this is similar to [@JvmStatic], in which it tells Java to treat this as a static variable to [KotlinClass]. @JvmField var foo: Int = 1 //If you want something akin to [final static], and the value is a primitive or a String, you can use the keyword [const] instead //No annotation is needed to make this a field of [KotlinClass]. If the declaration is a non-primitive/non-String, use @JvmField instead const val dog: Int = 1 //This will be treated as a member of the [Companion] object only. It generates the getter/setters for it. var bar: Int = 2 //We can still use [@JvmStatic] for 'var' variables, but it generates getter/setters as functions of KotlinClass //If we use 'val' instead, it only generates a getter function @JvmStatic var cat: Int = 9 } } Java package com.frybits; class JavaClass { void someFunction() { //Example using @JvmField println(KotlinClass.foo); //Prints "1" KotlinClass.foo = 3; //Example using 'const val' println(KotlinClass.dog); //Prints "1". Notice the lack of a getter function //Example of not using either @JvmField, @JvmStatic, or 'const val' println(KotlinClass.Companion.getBar()); //Prints "2" KotlinClass.Companion.setBar(3); //The setter for [bar] //Example of using @JvmStatic instead of @JvmField println(KotlinClass.getCat()); KotlinClass.setCat(0); } void println(Object o) { System.out.println(o); } }
Kotlin的一个伟大特性是您可以创建顶级函数和变量。这使得创建常量字段和函数的“无类”列表变得非常好,而这些列表又可以用作Java中的静态函数/字段。
Scenario 3: Accessing top level fields and functions in Kotlin from Java Kotlin //In this example, the file name is "KSample.kt". If this annotation wasn't provided, all functions and fields would have to accessed //using the name [KSampleKt.foo()] to utilize them in Java. Make life easier for yourself, and name this something more simple @file:JvmName("KotlinUtils") package com.frybits //This can be called from Java as [KotlinUtils.TAG]. This is a final static variable const val TAG = "You're it!" //Since this is a top level variable and not part of a companion object, there's no need to annotate this as "static" to access in Java. //However, this can only be utilized using getter/setter functions var foo = 1 //This lets us use direct access now @JvmField var bar = 2 //Since this is calculated at runtime, it can't be a constant, but it is still a final static variable. Can't use "const" here. val GENERATED_VAL:Long = "123".toLong() //Again, no need for @JvmStatic, since this is not part of a companion object fun doSomethingAwesome() { println("Everything is awesome!") } Java package com.frybits; class JavaClass { void someFunction() { println(KotlinUtils.TAG); //Example of printing [TAG] //Example of not using @JvmField. println(KotlinUtils.getFoo()); //Prints "1" KotlinUtils.setFoo(3); //Example using @JvmField println(KotlinUtils.bar); //Prints "2". Notice the lack of a getter function KotlinUtils.bar = 3; //Since this is a top level variable, no need for annotations to use this //But it looks awkward without the @JvmField println(KotlinUtils.getGENERATED_VAL()); //This is how accessing a top level function looks like KotlinUtils.doSomethingAwesome(); } void println(Object o) { System.out.println(o); } }
另一个值得注意的可以在Java中作为“静态”字段使用的是Kotlin对象类。这些是在第一次使用时惰性实例化的零参数单例类。更多信息可以在这里找到:https://kotlinlang.org/docs/reference/object-declarations.html#object-declarations
但是,要访问单例对象,需要创建一个特殊的INSTANCE对象,处理它和处理Companion一样麻烦。下面是如何在Java中使用注释来给它一种干净的静态感觉:
Scenario 4: Using object classes Kotlin @file:JvmName("KotlinClass") //This provides a name for this file, so it's not defaulted as [KotlinClassKt] in Java package com.frybits object KotlinClass { //No need for the 'class' keyword here. //Direct access to this variable const val foo: Int = 1 //Tells Java this can be accessed directly from [KotlinClass] @JvmStatic var cat: Int = 9 //Just a function that returns the class name @JvmStatic fun getCustomClassName(): String = this::class.java.simpleName + "boo!" //Getter/Setter access to this variable, but isn't accessible directly from [KotlinClass] var bar: Int = 2 fun someOtherFunction() = "What is 'INSTANCE'?" } Java package com.frybits; class JavaClass { void someFunction() { println(KotlinClass.foo); //Direct read of [foo] in [KotlinClass] singleton println(KotlinClass.getCat()); //Getter of [cat] KotlinClass.setCat(0); //Setter of [cat] println(KotlinClass.getCustomClassName()); //Example of using a function of this 'object' class println(KotlinClass.INSTANCE.getBar()); //This is what the singleton would look like without using annotations KotlinClass.INSTANCE.setBar(23); println(KotlinClass.INSTANCE.someOtherFunction()); //Accessing a function in the object class without using annotations } void println(Object o) { System.out.println(o); } }
其他回答
简单来说,你可以使用“同伴对象”进入Kotlin静态世界,比如:
companion object {
const val TAG = "tHomeFragment"
fun newInstance() = HomeFragment()
}
要创建一个常量字段,请使用代码中的“const val”。 但是尽量避免使用静态类,因为它会给使用Mockito进行单元测试带来困难。
你把函数放在“同伴对象”中。
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中工作)。
静态属性的顶级/伴生对象
顶级
当属性与类有些关联时,在类声明之前将它们定义为顶级属性:
const val MAX_ATTEMPTS = 3
private const val DEFAULT_NAME = "Guest"
private const val MIN_AGE = 16
data class User(val id: String, val name: String = DEFAULT_NAME)
这类似于Java中的静态属性。
当属性完全独立于任何类时,可以将它们定义为不包含类的单独文件中的顶级属性。
伴星
当属性与类密切相关并且只会在该类中使用时,在伴随对象中定义它们:
data class User(val id: String, val name: String = DEFAULT_NAME) {
companion object {
const val DEFAULT_NAME = "Guest"
const val MIN_AGE = 16
}
}
用于静态方法的顶级/伴生对象
顶级
类似于上面的属性,当函数与类有点关联时,将它们定义在类的上方:
fun getAllUsers() { }
fun getProfileFor(userId: String) { }
data class User(val id: String, val name: String)
用法:
val userList = getAllUsers()
伴星
当函数与类密切相关时,在伴随对象中定义它们:
data class User(val id: String, val name: String) {
companion object {
fun getAll() { }
fun profileFor(userId: String) { }
}
}
用法:
val userProfile = User.profileFor("34")
这类似于Java中的静态方法。
顶级函数对于Kotlin来说通常更习惯。在伴生对象中定义函数的一个更好的原因是当您使用接口扩展伴生对象时。在单例部分中展示了一个示例。
静态类的嵌套类
当具有相关功能的类属于一起时,可以通过嵌套将它们分组在一起:
class User(val id: String, val name: String) {
class UserAccess : UserDao {
override fun add(user: User) { }
override fun remove(id: String) { }
}
}
这相当于Java中的静态嵌套类。这里的UserAccess类实现了一个接口UserDao。
用法:
fun main() {
val john = User("34", "John")
val userAccess = User.UserAccess()
userAccess.add(john)
}
用于静态INSTANCE的单例对象
顶级
当您只想要类的单个对象时,您不再需要像在Java中那样在类中创建静态INSTANCE。简单地使用顶级对象声明:
object UserAccess : UserDao {
override fun add(user: User) { }
override fun remove(id: String) { }
}
还要注意在单例中扩展接口或类是多么容易。
上面的代码,在底层,在Java中产生以下静态INSTANCE单例模式(简化):
public final class UserAccess implements UserDao {
public static final UserAccess INSTANCE;
public void add(User user) { }
public void remove(String id) { }
private UserAccess() { }
static { INSTANCE = new UserAccess();}
}
伴星
当单例对象与类密切相关时,使用companion对象:
data class User(val id: String, val name: String) {
companion object : UserDao {
override fun add(user: User) { }
override fun remove(id: String) { }
}
}
这样可以获得更优雅的命名:User.add(john)。另外,您要明确这个单例只用作User类的实用程序。如果需要多个单例对象或函数/属性组,也可以在类中使用不带companion关键字的对象。
静态工厂的伴生对象
Koltin中的工厂函数是使用伴生对象创建的。当您希望提供多种方法来创建对象,而对象构造过程很复杂,或者多个构造函数表达能力不够时,工厂函数非常有用。
例如,下面代码段中的newInstance()工厂函数通过自动生成id来创建一个用户:
class User private constructor(val id: Long, val name: String) {
companion object {
private var currentId = 0L;
fun newInstance(name: String) = User(currentId++, name)
}
}
这相当于Java中的静态工厂方法。
构造函数是私有的,但是伴生对象可以访问构造函数。
在上面的代码中,下一个id生成的一致性得到了保证,因为伴生对象是一个单例对象,只有一个对象会跟踪id,不会有任何重复的id。
还要注意,伴生对象可以具有表示状态的属性(在本例中为currentId)。
用法:
val john = User.newInstance("John")
@JvmStatic用于Java互操作性
Java的静态概念在Kotlin中并不存在。同伴对象是一个称为companion的真实类的实例。因此,当您从Java调用Kotlin代码时,Companion类的对象首先在后台实例化。你需要在Java中使用Companion对象调用该函数:
Profile userProfile = User.Companion.profileFor("34");
为了获得惯用的Java命名和更少的冗长,可以在函数或属性上使用@JvmStatic注释:
companion object {
@JvmStatic
fun profileFor(userId: String): Profile { }
}
@JvmStatic注释创建getProfileFor()函数的一个单独的纯静态副本。现在你可以在Java中使用常规语法:
Profile userProfile = User.profileFor("34");
就是这样!希望这些例子对你的项目有用。
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
您可以通过Companion Objects在Kotlin中实现静态功能
Adding companion to the object declaration allows for adding the static functionality to an object even though the actual static concept does not exist in Kotlin. A companion object can access all members of the class too, including the private constructors. A companion object is initialized when the class is instantiated. A companion object cannot be declared outside the class. class MyClass{ companion object { val staticField = "This is an example of static field Object Decleration" fun getStaticFunction(): String { return "This is example of static function for Object Decleration" } } }
伴侣对象的成员可以通过简单地使用类名作为限定符来调用:
输出:
MyClass.staticField // This is an example of static field Object Decleration
MyClass.getStaticFunction() : // This is an example of static function for Object Decleration