If we assign a literal to an overridden property, that property's value becomes the value of that literal.
Alternative, if we implement a custom getter function on the overridden property, each access of the property calls that getter. In this case, the getter will return a random number each time.
Tags:
xxxxxxxxxx
1
import kotlin.random.Random
2
3
abstract class Base {
4
abstract val something: Int
5
}
6
7
class SomethingSource : Base() {
8
override val something
9
get() = Random.nextInt(1,100)
10
}
11
12
fun main() {
13
val source = SomethingSource()
14
15
println(source.something)
16
println(source.something)
17
println(source.something)
18
}