Java调用Kotlin特性

Java调用Kotlin特性

Java调用Kotlin属性和方法

Kotlin类:

class User {
    var name = "无名"
    val age: Int
        get() = 18

    fun eat() {
        println("$name 在吃饭")
    }
}

在Java中调用:

User user = new User();
user.setName("小明");
user.getAge();
user.eat();

Java调用Kotlin静态属性和方法

Kotlin类:

class User {
    companion object {
        var name = "无名"

        @JvmField
        var age = 0

        fun sayHello() {
            println("$name 说hello")
        }

        @JvmStatic
        fun sayInfo() {
            println("姓名:$name,年龄:$age")
        }
    }
}

在Java中调用:

User.Companion.setName("小明");
User.age = 18;
User.Companion.sayHello();
User.sayInfo();

Java调用Kotlin的@JvmOverloads

Kotlin代码:

class User @JvmOverloads constructor(
    val name: String = "",
    val age: Int = 0,
    val address: String = ""
) {
    @JvmOverloads
    fun setInfo(name: String, age: Int = 0, address: String = "") {
    }
}

在Java中调用:

User user = new User();
user.setInfo("小明");
user.setInfo("小明", 18);
user.setInfo("小明", 18, "北京市");

User user2 = new User("小明");
User user3 = new User("小明", 18);
User user4 = new User("小明", 18, "北京市");

Java调用Kotln顶层函数和扩展函数

Kotlin代码:

StringExt.kt

fun String.lastElement(): Char? {
    if (this.isEmpty()) {
        return null
    }
    return this[length - 1]
}

val String.firstElement
    get():Char? {
        return if (isEmpty()) {
            null
        } else {
            get(0)
        }
    }

在Java中调用:

Character lastElement = StringExtKt.lastElement("hello");
System.out.println(lastElement); //0
Character firstElement = StringExtKt.getFirstElement("hello");
System.out.println(firstElement); //h

Java调用Kotlin高阶函数

Kotlin代码:

class User {
    fun showInfo(name: String, age: Int, block: (String, Int) -> Unit, action: (Int) -> Int) {
        block(name, age)
        val count = action(10)
        println("count:$count")
    }
}

在Java中调用:

new User().showInfo("小明", 18, new Function2<String, Integer, Unit>() {
    @Override
    public Unit invoke(String name, Integer age) {
        System.out.println("name:" + name + ", age:" + age);
        return null;
    }
}, new Function1<Integer, Integer>() {
    @Override
    public Integer invoke(Integer integer) {
        return 100 + integer;
    }
});

Java间接Kotlin协程

在Java中是无法使用Kotlin协程的,我们可以间接的使用协程。

Kotlin代码:

class CoroutineDemo {
    fun test1(block: CoroutineScope.() -> Unit) {
        GlobalScope.launch(Dispatchers.IO) {
            block()
        }
    }

    fun test2(block: suspend CoroutineScope.() -> Unit) {
        GlobalScope.launch(Dispatchers.IO) {
            block()
        }
    }
}

在Java中的使用:

demo.test1(new Function1<CoroutineScope, Unit>() {
    @Override
    public Unit invoke(CoroutineScope coroutineScope) {
        System.out.println("1在协程中执行:" + Thread.currentThread().getName());
        //1在协程中执行:DefaultDispatcher-worker-1 @coroutine#1
        return null;
    }
});

demo.test2(new Function2<CoroutineScope, Continuation<? super Unit>, Object>() {
    @Override
    public Object invoke(CoroutineScope coroutineScope, Continuation<? super Unit> continuation) {
        System.out.println("2在协程中执行:" + Thread.currentThread().getName());
        //2在协程中执行:DefaultDispatcher-worker-3 @coroutine#2
        return null;
    }
});
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值