V
V
Vanes Ri_Lax2021-10-25 07:44:52
Kotlin
Vanes Ri_Lax, 2021-10-25 07:44:52

Is it possible to implement this in Kotlin?

Good afternoon!
I have an interface in which I describe a function and in this function I have a variable with a default value.
Next, I created a class that implements this interface with this function, how can I reach this variable in the class?
Here is an example code:

interface MyFun{
    fun testVal(){
        val text: String = "deasdasd"
    }
}

class TestText() : MyFun {
    override fun testVal() {
        println(text) // как мне тут получить это значение которое я определил в интерфейсе?
    }
}

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
Dmitry Roo, 2021-10-25
@xez

You can make a method with a default implementation that will return your string.
https://kotlinlang.org/docs/interfaces.html#proper...

I
illuzor, 2021-10-25
@iLLuzor

No way. This variable in the function scope

J
Jacen11, 2021-10-25
@Jacen11

How can I get this value that I defined in the interface?

You defined it in a function, not in an interface.
this variable belongs to the function, not the class.
obviously you need to move it to a higher level, to the interface, so that the rest can see

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question