【问题标题】:Kotlin class does not implement interface methods but abstract class does implement methodsKotlin 类不实现接口方法,但抽象类实现方法
【发布时间】:2020-02-04 19:15:13
【问题描述】:

我正在使用spring boot 版本2.1.9spring-data。我已经定义了一个存储库

interface TokenRepository : CrudRepository<Token, Long> {

    fun findBySubject(subject: String): Token?
}

出于测试目的,我创建了 CrudRepository 的抽象假冒,它实现了 CrudRepository 中的所有方法:

abstract class RepositoryFake<T> : CrudRepository<T, Long> {

    ...

    override fun existsById(id: Long): Boolean {
        ...
    }

    override fun deleteById(id: Long) {
        ...
    }

    override fun findById(id: Long): Optional<T> {
        ...
    }

    ...

}

现在,当我创建扩展 RepositoryFakeTokenRepository 的实现时

class TokenRepositoryFake : RepositoryFake<Token>(), TokenRepository {

    override fun findBySubject(subject: String): Token? {
        ...
    }
}

编译器抱怨TokenRepositoryFake 不是抽象的,并且它没有实现方法existsByIddeleteByIdfindById,尽管这些方法在RepositoryFake 中实现。它不会抱怨CrudRepository 中定义的其他方法。为什么编译器抱怨缺少方法而不是所有其他方法?

错误信息示例: Class 'TokenRepositoryFake' is not abstract and does not implement abstract member public abstract fun deleteById(p0: Long): Unit defined in my.repositories.TokenRepository

【问题讨论】:

    标签: kotlin spring-data


    【解决方案1】:

    我想我找到了问题所在。如果我将RepositoryFake 的类型更改为

    abstract class RepositoryFake<T, ID> : CrudRepository<T, ID>
    

    然后将TokenRepositoryFake改为

    class HDChainRepositoryFake : RepositoryFake<HDChain, Long>(), HDChainRepository
    

    一切都会编译。我假设它与 kotlin 处理泛型的方式有关,但不完全知道为什么。

    【讨论】:

      猜你喜欢
      • 2011-11-20
      • 1970-01-01
      • 2018-01-21
      • 2012-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-08
      • 1970-01-01
      相关资源
      最近更新 更多