我在Kotlin写了一个小的java.time.Clock
子类。
此版本有效:
class TestClock(
var instant: Instant = Instant.ofEpochSecond(0),
zone: ZoneId = ZoneId.of("UTC")
) : Clock() {
private val zone = zone
override fun getZone(): ZoneId = zone
override fun withZone(zone: ZoneId): Clock =
if (zone == this.zone) this else TestClock(instant, zone)
override fun instant(): Instant = instant
}
但如果我尝试删除zone
属性周围的冗余,请执行以下操作:
class TestClock(
var instant: Instant = Instant.ofEpochSecond(0),
override val zone: ZoneId = ZoneId.of("UTC")
) : Clock() {
override fun withZone(zone: ZoneId): Clock =
if (zone == this.zone) this else TestClock(instant, zone)
override fun instant(): Instant = instant
}
...我收到两条看似矛盾的错误消息:
错误:(7,1)Kotlin:类'TestClock'不是抽象的,也没有实现抽象基类成员public abstract fun getZone():ZoneId!在java.time.Clock中定义的 错误:(9,5)Kotlin:'zone'不会覆盖任何内容
override val zone
是否覆盖java.time.Clock中定义的getZone(): ZoneId!
?