Kotlin中的Java静态方法等效于什么?

本文翻译自:What is the equivalent of Java static methods in Kotlin?

There is no static keyword in Kotlin. Kotlin中没有static关键字。

What is the best way to represent a static Java method in Kotlin? 用Kotlin表示static Java方法的最佳方法是什么?


#1楼

参考:https://stackoom.com/question/2jJZk/Kotlin中的Java静态方法等效于什么


#2楼

You place the function in the "companion object". 您将功能放置在“伴侣对象”中。

So the java code like this: 所以像这样的java代码:

class Foo {
  public static int a() { return 1; }
}

will become 会变成

class Foo {
  companion object {
     fun a() : Int = 1
  }
}

You can then use it from inside Kotlin code as 然后,您可以从Kotlin代码内部使用它,如下所示:

Foo.a();

But from within Java code, you would need to call it as 但是从Java代码中,您需要将其称为

Foo.Companion.a();

(Which also works from within Kotlin.) (这也来自Kotlin。)

If you don't like having to specify the Companion bit you can either add a @JvmStatic annotation or name your companion class. 如果您不想指定Companion位,则可以添加@JvmStatic批注或命名您的伴侣类。

From the docs : 文档

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. 但是,在JVM上,如果使用@JvmStatic批注,则可以将伴随对象的成员生成为实际的静态方法和字段。 See the Java interoperability section for more details. 有关更多详细信息,请参见Java互操作性部分。

Adding the @JvmStatic annotation looks like this 添加@JvmStatic注释如下所示

class Foo {
  companion object {
    @JvmStatic
    fun a() : Int = 1;
  }
}

and then it will exist as a real Java static function, accessible from both Java and Kotlin as Foo.a() . 然后它将作为一个真正的Java静态函数存在,可以从Java和Kotlin中以Foo.a()

If it is just disliked for the Companion name, then you can also provide an explicit name for the companion object looks like this: 如果只是不喜欢Companion名称,那么您还可以为伴随对象提供一个明确的名称,如下所示:

class Foo {
  companion object Blah {
    fun a() : Int = 1;
  }
}

which will let you call it from Kotlin in the same way, but from java like Foo.Blah.a() (which will also work in Kotlin). 它可以让您以相同的方式从Kotlin调用它,但可以从Foo.Blah.a()类的Java中调用它(在Kotlin中也可以使用)。


#3楼

A. Old Java Way : A.旧的Java方式:

  1. Declare a companion object to enclose a static method / variable 声明一个companion object以包含一个静态方法/变量

     class Foo{ companion object { fun foo() = println("Foo") val bar ="bar" } } 
  2. Use : 采用 :

     Foo.foo() // Outputs Foo println(Foo.bar) // Outputs bar 


B. New Kotlin way B.新科特林方式

  1. Declare directly on file without class on a .kt file. 直接声明文件, 而不声明.kt文件的

     fun foo() = println("Foo") val bar ="bar" 
  2. Use the methods/variables with their names . methods/variables与它们的名称一起使用 ( After importing them ) 导入后

    Use : 采用 :

     foo() // Outputs Foo println(bar) // Outputs bar 


#4楼

Docs recommends to solve most of the needs for static functions with package-level functions . Docs建议使用包级功能解决静态功能的大多数需求。 They are simply declared outside a class in a source code file. 它们只是在源代码文件中的类外部声明。 The package of a file can be specified at the beginning of a file with the package keyword. 可以使用package关键字在文件的开头指定文件的软件包。

Declaration 宣言

package foo

fun bar() = {}

Usage 用法

import foo.bar

Alternatively 或者

import foo.*

You can now call the function with: 您现在可以使用以下命令调用该函数:

bar()

or if you do not use the import keyword: 或者,如果您不使用import关键字:

foo.bar()

If you do not specify the package the function will be accessible from the root. 如果不指定软件包,则可以从根目录访问该功能。

If you only have experience with java, this might seem a little strange. 如果您只有Java经验,这似乎有些奇怪。 The reason is that kotlin is not a strictly object-oriented language. 原因是kotlin不是严格的面向对象语言。 You could say it supports methods outside of classes. 您可以说它支持类之外的方法。


#5楼

Write them directly to files. 将它们直接写入文件。

In Java (ugly): 在Java中(丑陋):

package xxx;
class XxxUtils {
  public static final Yyy xxx(Xxx xxx) { return xxx.xxx(); }
}

In Kotlin: 在科特林:

@file:JvmName("XxxUtils")
package xxx
fun xxx(xxx: Xxx): Yyy = xxx.xxx()

Those two pieces of codes are equaled after compilation (even the compiled file name, the file:JvmName is used to control the compiled file name, which should be put just before the package name declaration). 编译后,这两段代码相等(甚至是已编译的文件名, file:JvmName用于控制已编译的文件名,应将其放在包名声明之前)。


#6楼

You need to pass companion object for static method because kotlin don't have static keyword - Members of the companion object can be called by using simply the class name as the qualifier: 您需要为静态方法传递伴随对象,因为kotlin没有static关键字-可以通过仅使用类名作为限定符来调用伴随对象的成员:

package xxx
    class ClassName {
              companion object {
                       fun helloWord(str: String): String {
                            return stringValue
                      }
              }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值