属性类型不是一种重写类型

对不起,没有具体的标题,我真的不知道如何更好地命名。
我有以下例子:

class User interface Repository<T, ID> interface UserRepository : Repository<User, Long> abstract class RepositoryTest<T, ID> { abstract var repository: Repository<T, ID> } class UserRepositoryTest : RepositoryTest<User, Long>() { //Error: Var-property type is "UserRepository", which is not a type of overriden lateinit override var repository: UserRepository } 

我需要这个架构进行数据库测试。 Repository有插入,保存,删除等方法。我想在RepositoryTest使用这个抽象来删除所有条目,并在每次测试之前插入所需的数据。 在UserRepositoryTest我想将资源库指定为UserRepository (它是Repository的子UserRepository ),但是我在示例中提到了一个错误。
为什么它给了我错误? 我以为我可以通过一个亚型的类型。

repository是一个var字段。 由于UserRepositoryTest可以被称为RepositoryTest<User, Long> ,因此您应该可以将Repository<User, Long>分配给Repository<User, Long> repository字段。 但是,它不一定是UserRepository因此是错误。

RepositoryTest类中将版本RepositoryTest更改为val应该可以解决这个问题。