Tag: 接口

“接口中的代码”Kotlin,他们如何避免“致命的钻石死亡”?

我正在阅读这篇文章,它说你可以在Kotlin接口中编写代码。 Java不允许在接口中编写代码来避免钻石问题。 如果Kotlin允许在接口中使用代码,并且可以在一个类中实现多个接口,那么不会再一次创建“钻石问题”了吗?

kotlin在课堂授权中调用一个变异函数

我是Kotlin新手,我不知道如何解释,但这个例子应该足够了 interface Walker{ var position:Pair<Int,Int> fun walk(to:Pair<Int,Int>){ position = to; } fun trip() } class People(override var position: Pair<Int, Int>):Walker{ override fun trip() { Log.v("TP","Ouch!!") } } class Superman(override var position: Pair<Int, Int>): Walker by People(position){ } 当我打电话 val sm = Superman(Pair(0,0)) sm.walk(Pair(2,2)) Log.v("TP","${sm.position}" //It give (0,0) instead of (2,2) 有没有一种可能的方式来解决这个问题,同时仍然使用委托

Kotlin类实现Java接口错误

我有一个Java界面 public interface SampleInterface extends Serializable { Long getId(); void setId(Long id); } 和一个应该实现它的Kotlin类 open class ClazzImpl() : SampleInterface private val id: Unit? = null fun getId(): Long? { return null } fun setId(id: Long?) { } 但是我得到一个编译错误: 类ClazzImpl不是抽象的,也不实现抽象成员public abstract fun setId(id:Long!):在com中定义的单元… SampleInterface 任何想法是什么错误?

而不是Kotlin的意外覆盖

我有java界面 public interface LifecycleRegistryOwner extends LifecycleOwner { @Override LifecycleRegistry getLifecycle(); } 和实现这个接口的Kotlin类,刚刚才会超级好 val lifecycle by lazy { val result = LifecycleRegistry(this) result } 但编译器说这是一个意外覆盖。 有没有办法告诉它是由意向? 我目前的实施 class PartnerSettingsActivity :AppCompatActivity(),LifecycleRegistryOwner { private val registry = LifecycleRegistry(this) //<< hate this class-wide val override fun getLifecycle(): LifecycleRegistry = registry

在Kotlin中传递接口作为参数

我想通过一个接口作为这样的参数: class Test { fun main() { test({}) // how can I pass here? } fun test(handler: Handler) { // do something } interface Handler { fun onCompleted() } } 在Java中,我可以使用匿名函数如test(new Handler() { ………. }) ,但是我不能在Kotlin中这样做。 任何人都知道如何做到这一点?