Interface returning function returns his childs

Здесь последняя функция имеет возвращаемый тип Person(наш интерфейс), но возвращает новый инстанс наследника - User или Subscriber.
 avatar
Pablo
kotlin
2 years ago
1.0 kB
2
Indexable
Never
interface Person {
    var name: String;
    var id: Int;
    fun getUserId(name: String): Int;
}

fun getId(name: String): Int {
    return 777;
}

class User: Person(val name: String, id: Int, getUserId: ((String) => Int))

class Subscriber: Person(val name: String, id: Int, youtubeName, canalName, bastyonName: String) {
    fun getUserId(): Int => id;
    fun getYouTubeCanalName(ytName: String): String => if (ytName == youtubeName) canalName;
    fun getBastyon(uid: Int): String => if (uid == id) bastyonName;
    fun getAllCanals(uname: String, uid: Int): List<> => if (uname == name && uid == id) listOf(youtubeName, canalName, bastyonName);
}

fun newUser(name: String): Person {
    return User(name, getId(name), getId)
}

fun newSubscriber(name, canal, youtube, bastyon: String): Person {
    return Subscriber(name, getId, getId, youtube, canal, bastyon);
}

fun newPerson(user: Boolean, name, canal, youtube, bastyon: String): Person {
    return if (user) newUser(name) else newSubscriber(name, canal, youtube, bastyon);
}