-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclasses_and_objects.kt
More file actions
267 lines (181 loc) · 3.88 KB
/
Copy pathclasses_and_objects.kt
File metadata and controls
267 lines (181 loc) · 3.88 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/*
====================================
KOTLIN CLASSES & OBJECTS — COMPLETE NOTES
====================================
Class = Blueprint
Object = Real instance created from class
Analogy:
Class → Cookie cutter
Object → Cookie created from cutter
A class defines:
- Properties
- Variables
- Functions
- Behavior
*/
fun main() {
/*
====================================
1. Creating Object
====================================
*/
val worker1 = Worker(
"Priya",
"Maharashtra"
)
println("Worker Name: " + worker1.name)
println("Worker Region: " + worker1.region)
/*
====================================
2. Calling Class Function
====================================
*/
worker1.showInfo()
/*
====================================
3. Updating Mutable Property
====================================
*/
worker1.totalXp += 50
println("Updated XP: " + worker1.totalXp)
/*
====================================
4. Data Class
====================================
*/
val points = WorkerPoints(
workerId = "W001",
totalXp = 150,
streak = 5
)
println(points)
/*
====================================
5. Copy Function
====================================
Creates new object with changes.
*/
val updatedPoints = points.copy(
totalXp = 250
)
println(updatedPoints)
/*
====================================
6. Equality Check
====================================
*/
val p1 = WorkerPoints(
"W001",
100,
3
)
val p2 = WorkerPoints(
"W001",
100,
3
)
println("Equal Objects: " + (p1 == p2))
/*
====================================
7. Enum Class
====================================
*/
val event = GamificationEvent.ANC_VISIT
println("Event XP: " + event.xpValue)
/*
====================================
8. Singleton Object
====================================
*/
println("Registration XP: " + XpConfig.REGISTRATION)
/*
====================================
9. Companion Object
====================================
*/
println("Max XP: " + Worker.MAX_XP)
}
/*
====================================
Regular Class
====================================
*/
class Worker(
val name: String,
val region: String,
var totalXp: Int = 0
) {
/*
init block runs when object created.
*/
init {
println("Worker Created")
}
/*
Class Function
*/
fun showInfo() {
println("Name: " + name)
println("Region: " + region)
println("XP: " + totalXp)
}
/*
Companion Object
*/
companion object {
const val MAX_XP = 1000
}
}
/*
====================================
Data Class
====================================
Used for holding data.
Automatically gives:
- toString()
- equals()
- copy()
- hashCode()
*/
data class WorkerPoints(
val workerId: String,
val totalXp: Int = 0,
val streak: Int = 0
)
/*
====================================
Enum Class
====================================
Fixed set of constants.
*/
enum class GamificationEvent(
val xpValue: Int
) {
REGISTRATION(10),
ANC_VISIT(25),
DELIVERY(50)
}
/*
====================================
Object Singleton
====================================
Only one instance exists.
*/
object XpConfig {
const val REGISTRATION = 10
const val VISIT = 25
const val DELIVERY = 50
}
/*
====================================
SUMMARY
====================================
class → blueprint
object → real instance
data class → stores data
enum class → fixed constants
companion object → static-like values
object → singleton
copy() → clone object
== → value comparison
*/