属性类型不是一种重写类型
对不起,没有具体的标题,我真的不知道如何更好地命名。
我有以下例子:
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
应该可以解决这个问题。