forked from studoverse/Kotlift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
20_propertyGetterSetter.swift
71 lines (58 loc) · 1.26 KB
/
20_propertyGetterSetter.swift
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
public class FunkyClass {
var internalString = ""
var wrappedProperty: String {
get { return "My string is \(internalString)" }
set(value) {
internalString = "\(value) - previous=\"\(internalString)\""
}
}
init() {
}
}
var computedProperty1: Int32 {
get {
var a = 0
a++
return a
}
}
var computedProperty2: Int32 {
get { return 2 }
}
/*val computedProperty3: Int
get() = 3*/
var _backingProperty: Int32 = 0
var computedProperty4: Int32 {
get {
return 4 + _backingProperty
}
set(value) {
_backingProperty = value
}
}
var computedProperty5: Int32 {
get { return 5 + _backingProperty }
set(value) { _backingProperty = value }
}
/*var computedProperty6: Int
get() = 6 + _backingProperty
set(value) { _backingProperty = value }*/
func main(args: [String]) {
let x = FunkyClass()
print(x.wrappedProperty)
x.wrappedProperty = "abc"
print(x.wrappedProperty)
x.wrappedProperty = "123"
print(x.wrappedProperty)
print(computedProperty1)
print(computedProperty2)
//print(computedProperty3)
print(computedProperty4)
print(computedProperty5)
//print(computedProperty6)
computedProperty4 = 1000
print(computedProperty4)
print(computedProperty5)
//print(computedProperty6)
}
main([])