博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Kotlin中的Java静态方法等效于什么?
阅读量:2288 次
发布时间:2019-05-09

本文共 4670 字,大约阅读时间需要 15 分钟。

本文翻译自:

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楼

参考:


#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 : 从 :

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楼

recommends to solve most of the needs for static functions with package-level functions . 建议使用包级功能解决静态功能的大多数需求。 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 foofun 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 xxxfun 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                      }              }    }

转载地址:http://ricnb.baihongyu.com/

你可能感兴趣的文章
5年Java程序员,五面蚂蚁险拿offer定级P7,大厂面试不过如此?
查看>>
大厂面试必问!HashMap 怎样解决hash冲突?
查看>>
面试屡屡碰壁,痛定思痛闭关修炼!半年后4面阿里成功拿offer
查看>>
最全的大厂最新面试249题与笔记总结:多线程+JVM +Spring+微服务+分布式+Redis+MySQL
查看>>
吊!设计模式全解:6大设计原则+23种设计模式+设计模式PK+设计模式混编
查看>>
服!看完阿里大牛手写的Java异步编程实战笔记,我惊呆了
查看>>
Java程序员跳槽,三面之后被拒,原因竟是“招不起”
查看>>
想要彻底搞懂微服务架构?必先学:SpringBoot+SpringCloud+docker
查看>>
6天面试10家,已经拿到offer,Java程序员的面试总结分享
查看>>
渣本的逆袭之路!备战3个月,三面蚂蚁金服成功斩获Offer
查看>>
10月末美团、滴滴、蘑菇街9次面试总结(Java岗)
查看>>
热气腾腾的腾讯后台开发面经(总共五面)
查看>>
深入理解设计模式(设计原则+种设计模式+设计模式PK+设计模式混编)
查看>>
谷歌大佬回国发展,吊打各大厂面试官!吐血总结大厂面试高频点及笔记解析
查看>>
面试复盘:面完字节、美团、阿里等大厂,今年面试到底问什么?
查看>>
从0到1,决战Spring Boot《Spring Boot 2实战之旅》
查看>>
5面终于拿到字节跳动offer!忍不住和大家分享一波
查看>>
拿到阿里、字节offer后。我总结了一线大厂Java面试重难点:Java基础+并发+JVM+算法+框架+分布式+架构设计
查看>>
金九银十已过 成功入职美团,面试回顾及个人总结:算法+框架+Redis+分布式+JVM
查看>>
香!阿里P8手写3份满级“并发编程”笔记,原理→精通→实战
查看>>