Answer the question
In order to leave comments, you need to log in
How to make a "static" overridden function inherited from interfaces?
The situation is this. There are 2 interfaces that have the same function, with the same implementation. Therefore, the class needs to implement this function
interface Animal{
fun voice(){
println("звуки природы......")
}
}
interface Cat {
fun voice(){
println("мяу......")
}
}
// здесь мне бы хотелось, чтобы класс Tiger использовал реализацию из интрфейса Cat
// НО ТАКЖЕ МНЕ ХОТЕЛОСЬ БЫ ИСПОЛЬЗОВАТЬ ЭТУ ФУНКЦИЮ БЕЗ СОЗДАНИЯ ОБЪЕКТА
class Tiger : Animal, Cat {
companion object {
override fun voice() {
super<Cat>.voice()
}
}
}
Answer the question
In order to leave comments, you need to log in
I don't know why you want this, it's strange, in my opinion. But you can make it work, just make the companion object implement this interface:
class Tiger {
companion object : Animal, Cat {
override fun voice() {
super<Cat>.voice()
}
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question