【发布时间】:2017-11-26 05:52:15
【问题描述】:
我的目标是让接口方法接受实现的类类型。这是我到目前为止编写的代码:
internal interface Diff<Diff> { //in Java I was using <? extends Diff>
fun lessThan(other: Diff): Boolean
}
private class ADiff(private val value: Int) : Diff<ADiff> {
override fun lessThan(other: ADiff): Boolean {
return value < other.value
}
}
//B can now accept even int types which is not desired
private class BDiff(private val value: Int) : Diff<Int> {
override fun lessThan(other: Int): Boolean {
return value < other
}
}
【问题讨论】: