-
Notifications
You must be signed in to change notification settings - Fork 0
/
StandardDelegation.kt
47 lines (34 loc) · 1 KB
/
StandardDelegation.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import kotlin.properties.Delegates
// predefined delegates
class studentheavy{
init{
println("Student heavy ")
}
}
class student{
// when we want lazy load to instance we use lazy keyword
val heavy by lazy { studentheavy() }
// when i want to observe change in variable i will use Delegates.observable
var marks : Int by Delegates.observable(50){ property,oldValue,newValue ->
println("old value $oldValue")
println("new value $newValue")
}
// if i want to intersecpt the assignment then i will use vetoable
var age : Int by Delegates.vetoable(14) { property,oldValue,newValue ->
println("old value $oldValue")
println("new value $newValue")
newValue>=14
}
}
fun main() {
val student = student()
// student.heavy
// student.marks = 70
// student.marks= 100
student.age =13
println(student.age)
student.age =14
println(student.age)
student.age =15
println(student.age)
}