I have an interface called UserManager
interface UserManager {
var user:User
/* ... */
}
and a class called UserManagerImpl
, that implements the UserManager
class UserManagerImpl : UserManager {
override var user: User // = must provide an User object
/* ... */
}
Here's my problem:
How to allow another class to set an User
in the UserManager()
at any time ( i.e don't provide an initial User
object alongside the property declaration and let another class create and provide an User
instance) ?
Take in count that
User
to be a non-null value, so no nullable property ( User?
)setUser(User)
and getUser()
method in the interface It is true that "interfaces cannot have lateinit properties" but that doesn't prevent implementing classes from using it:
interface User
interface UserManager {
var user: User
}
class UserManagerImpl : UserManager {
lateinit override var user: User
}
fun main(args: Array<String>) {
val userManager: UserManager = UserManagerImpl()
userManager.user = object : User {}
println(userManager.user)
}
Prints something like LateinitKt$main$1@4b1210ee
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With