I have a java interface:
public interface TestInterface {
String getName();
}
I have a kotlin class:
class TestImpl(private var name: String = "name"): TestInterface {
override fun getName(): String = name
}
But if I also want the setter method of the name field,
class TestImpl(private var name: String = "name"): TestInterface {
override fun getName(): String = name
// I don't want to write a setter like this
fun setName(n: String) {
this.name = n
}
}
What better way can I do? Thanks.
You could use
class TestImpl(@get:JvmName("getName_") var name: String = "name"): TestInterface {
override fun getName(): String = name
}
Then you access it as x.name
/ x.name = ...
from Kotlin, and x.getName()
/ x.setName(...)
from Java. Unfortunately, you also have getName_()
in Java, but you don't have to use it.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.