Tag: 界面

如何在Kotlin中编写下面的代码来实现回调

我如何在Java中写入Kotlin? Callback callback= new Callback() { @Override public void getCallback(ServerResponse serverResponse) { } }

Kotlin VS Scala:使用主构造函数参数实现方法

在斯卡拉你可以写这样的代码。 trait List[T] { def isEmpty() :Boolean def head() : T def tail() : List[T] } class Cons[T](val head: T, val tail: List[T]) :List[T] { def isEmpty = false } 你不需要覆盖它们已经定义的尾部和头部,但是在Kotlin中,我必须对它进行编码。 interface List<T> { fun isEmpty() :Boolean fun head() : T fun tail() : List<T> } class Cons<T>(val head: T, val tail: List<T>) :List<T> { […]

接口与函数调用的Kotlin性能

在Kotlin中,因为我们可以使用函数作为变量,所以我倾向于用这样的函数调用替换接口: class A { private var listener: AA? = null var callThis: (() -> Unit) ? = null) fun somethingHere() { callThis?.invoke() listener?.callThis2() } fun attachListener(listener: AA) { this.listener = listener } interface AA { fun callThis2() } } class B { init { val objectA = A() objectA.callThis = {} objectA.attachListener(object : A.AA { […]

Java与Kotlin接口声明

说我有这个Java和Kotlin接口: public interface JavaInterface { void onTest(); } interface KotlinInterface { fun onTest() } 为什么不能在没有构造函数的情况下创建Kotlin接口的实例? // this is okay val javaInterface: JavaInterface = JavaInterface { } // compile-time exception: interface does not have constructor val kotlinInterface1: KotlinInterface = KotlinInterface { } // this is okay val kotlinInterface2: KotlinInterface = object : KotlinInterface { override fun […]