Kotlin - How to "delay" var redefined interface?

I have an interface called UserManager

interface UserManager {

    var user:User

    /* ... */
}

and a class called UserManagerImplthat implementsUserManager

class UserManagerImpl : UserManager {

    override var user: User // = must provide an User object

    /* ... */
}

Here is my problem:

How to allow another class to be set Userto UserManager()at any time (i.e., not to provide an initial object Usernext to the property declaration and allow another class to create and provide an instance User)?

Keep in mind that

  • Interfaces cannot have lateinit properties .
  • I want it to Userbe a non-empty value, so the nullable ( User?) property
  • I want to use access to the field instead of ads and use the method setUser(User)and getUser()interface
+4
1

, " lateinit", -:

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)
}

- LateinitKt$main$1@4b1210ee.

+7

All Articles