Is there a built-in method in Kotlin to do this?
open class Base {
var data: Int = 0
}
class Derived(arg: Base) : Base() {
init {
copyAllProperties(from = arg, to = this)
}
}
You can write it yourself:
open class Base() {
var data: Int = 0
}
class Derived(arg: Base) : Base() {
init {
super.data = arg.data
}
}
Or use implementation by delegation[1]:
interface Base {
var data: Int
}
class BaseImpl : Base {
override var data: Int = 0
}
class Derived(b: Base) : Base by b
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With