java - 在java代码中定义Kotlin扩展函数

标签 java kotlin extension-function

我想要一个用于字符串的 Kotlin 扩展函数,该函数只能在 MyParent 类( protected 函数)的继承者中访问。 MyParent 类是用 java 编写的,无法转换为 Kotlin。是否可以在java代码中定义一个可以用作Kotlin代码中的扩展函数的方法?

我想我期望某种正确的签名,或者一些神奇的@ThisIsExtenstionFunction注释。

最佳答案

事实上,你可以。不是开箱即用的,但这是可能的;)

让我们考虑这个例子。

首先,在 Java 中创建一个类 MyParent

public class MyParent {
}

并且,在该接口(interface)中定义扩展方法:

interface MyParentExt {
    fun String.ext(importantParam: Int)
}

所以,让MyParent实现这个接口(interface)。

public class MyParent implements MyParentExt {

    @NonNull
    @Override
    public String ext(@NonNull String $receiver, int importantParam) {
        // do cool stuff
        return $receiver;
    }

    // For testing let's add other public method:
    public String ext(int importantParam) {
        return "Kotlin > Java";
    }

}

那么,让我们检查一下我们可以对 child 做些什么:

// Java child:
public class MyJavaChild extends MyParent {
    MyJavaChild() {
        "Java xD".ext(0); // Invalid! There is no such a method!
        ext("Java xD", 0); // Valid call for extension function
        ext(0); // Just Valid ;)
    }
}

// Kotlin class:
class MyGreatKotlinChild : MyParent() {
    init {
        "Kotlin is cool".ext(0) // Valid extension function!
        ext("Kotlin is cool", 0) // Invalid call. There is no method with such a signature!
        ext(0) // Valid.
    }
}

因此,我们可以使用 lang 指定的表示法在 Java 和 Kotlin 中访问我们的扩展方法。

作为您的要求之一,您希望此方法受到保护。 不幸的是,接口(interface)方法的可见性始终是公共(public)的。因此,您仍然应该能够在成员上调用此函数。

但是...让我们检查一下它实际上是如何工作的;)

// Java
public class Test {
    public static void main(String[] args) {
        MyChild child = new MyChild();

        // Both methods are valid
        child.ext("Java...", 0);
        child.ext(0);
    }
}

// Kotlin
fun main() {
    child.ext(0) // Normal function, valid call.
    child.ext("It actually works...", 0) // Hah, no way ;)

    // Just to be sure:
    "String".ext(0) // Nope, this ext method is visible for only class scope.
}

此外,您的父类只能扩展您可以在其中定义扩展方法的 ParentOfMyParent 类:

// Kotlin
abstract class ParentOfMyParent {
    protected abstract fun String.ext()
}

// Java
public class Parent extends ParentOfMyParent {
    @Override
    protected void ext(@NotNull String $receiver) {
    }
}

// Kotlin-child
class Child : Parent() {
    init {
        "".ext()
    }
}

// Java-child
public class ChildJava extends Parent {
    ChildJava() {
        ext("");
    }
}

这种方法具有所需的可见性。

关于java - 在java代码中定义Kotlin扩展函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56987823/

相关文章:

Kotlin闭包解决逻辑和操作

intellij-idea - 有没有办法在 Intellij IDE 中显示给定 Kotlin 类的所有扩展功能?

kotlin - 私有(private)顶级扩展函数和类内私有(private)扩展函数的区别

java - 如何在 Play Framework 中使用 Ebean 比较日期?

java - 为什么在使用数据库时更喜欢 Java 8 Stream API 而不是直接的 hibernate/sql 查询

java - 未经检查的异常或运行时异常之间的区别

android - Flow LifeCycle 是否可以识别为 LiveData?

rx-java - Kotlin通配符方法

kotlin - 如何将注释 @IntRange 用于 Integer 的 Kotlin 扩展函数

java - 使用 FileOutputStream 将数据结构写入文件?