-
Notifications
You must be signed in to change notification settings - Fork 1
/
class.kt
295 lines (240 loc) · 7.28 KB
/
class.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
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
// DECLARATION
class Name {
//some variables
var a;
val b;
fun someFunction(a: String) {
print(a) //parameter of function
print(this.a) //property of class
}
//some more body code
}
class Empty //if class has no body than curly braces are no needed
//class reflection
val classReference = Empty::class //reflection - classReference is instance of KClass in this case KClass<Empty>
// CONSTRUCTORS
//primary constructor can have some parameters
class Primary constructor(text: String) { }
//constructor keyword can be omitted if doesn't have any adnotations or visibility modifiers
class Primary(text: String) { }
//constructor parameters can be also mutable or read-only
class Primary(var text: String, val amount: Int) { }
//constructor keyword can't be ommited because of adnotations or visibility modifiers
class Primary public @Inject constructor(text: String) { }
//primary constructor
class Student(name: String, surname: String) {
var name = name
var surname = surname.toUpperCase()
init {
//do some init staff
print("Student ${name} ${surname} has been created")
}
}
//secondary constructor
//class has only secondary constructor
class Secondary {
constructor(text: String) {
// some code
}
}
//class has primary and secondary constructor
class PrimarySecondary(text: String) {
var text: String = text;
var amount: Int
init {
//This block executes before every secondardy constructor and can be used for example by primary constructor job
amount = 0
}
constructor(text: String, amount: Int) : this(text) {
//Secondardy constructor body
this.amount = amount
}
//more secondary constructors could be here
}
//objects can be created only be declared constructors
var a = PrimarySecondary("text") //okay
var b = PrimarySecondary("text", 10) //okay
var c = PrimarySecondary() //compiler error
//default constructor
class NoConstructor {
//some body
}
//use default primary constructor to create an instance
var obj = NoConstructor()
//constructor reflection
fun constructorReference(x: () -> NoConstructor) {
val obj: NoConstructor = x()
}
constructorReference(::NoConstructor)
// PROPERTIES AND FIELDS
//in Kotlin property is both field and accessors
var name: String = "William"
//equivalent in Java
String name = "William"
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
//properties as arguments
class Weather(val city: String, var temperature: Int) {}
//equivalent of
class Weather(city: String, temperature: Int) {
val city: String = city
var temperature: Int = temperature
}
class Person(name: String, age: Int) {
var name: String = name
get() = field.capitalize()
set(value) {
field = "Mr. " + value
}
var age: Int = age
private set
fun changeAge(age: Int) {
if(this.age < age) {
this.age = age
}
}
}
var person = Person("william", 15)
print("${person.name} is ${person.age} years old") //William is 15 years old
person.name = "Jim"
person.age = 20 //compiler error - setter is private so property can not be directly changed
person.changeAge(20) //change property by specific method
print("${person.name} is ${person.age} years old") Mr. Jim is 20 years old
var person = Person("William", 15)
print("${person.name} is ${person.age} years old")
person.name = 20 //compiler error - the setter is private so property can not be directly changed
person.changeName("Jim") //name property can be changed like this
//properties reflection
var name = person::name //it's okay
name = person.name.get() //compiler error
//DATA CLASSES
data class Product(val name: String) {
var price: Float = 0f
}
var product1 = Product("Jack Daniels")
product1.price = 10f //note that price isn't a part of autogenereated equals, hashCode, toString and copy methods
print(product1.toString()) //Product(name=Jack Daniels)
var product2 = product1.copy()
print(product1.equals(product2)) //true - despite the differenet prices
// NESTED AND INNER CLASS
class Outer {
var amount: Int = 5
val ratio: Int = 2
class Nested {
//nested class doesn't know about Outer class
fun work() = 20
}
inner class Inner {
//inner class knows about Outer class
fun work(): Int {
amount = super@Outer.ratio * amount
return amount
}
}
}
var outerObj = Outer()
var nestedObj = Outer.Nested()
var innerObj = outer.Inner()
print(outerObj.amount) //5
print(nestedObj.work()) //20
print(innerObj.work()) //10
print(outerObj.amount) //10
// SEALED CLASS
sealed class Status {
data class Content(val text: String) : Status() {
//some body
}
object None : Status() {
//some body
}
//some members of Status class
}
class Error(code: Int) : Status() {
//some body
}
//subclasses can be declared outside the sealed class but must be in the same file as sealed class
//get status from network event
val status: Status = Status.Content("content of the page")
when(status) {
is Status.Content -> print(status.text)
is Status.None -> print("No content to show")
is Error -> print("Unexpected error")
}
// INLINE CLASSES
inline class InlineClass(val text: String) : Showable {
val length: Int
get() = text.length
override fun show() {
println("$text has $length length")
}
}
interface Showable {
fun show()
}
// OBJECT EXPRESSION AND DECLARATION
//object expression
var obj = object {
var number = 10
var text = "object expression"
}
print(obj.text) //text
//anonymous inner class
//normal declared class
class OnClickListener {
fun onClick() {
print("click")
}
}
//normal object passed with default action
var listener = OnClickListener()
button.setOnClickListener(listener)
//object expression passed with modify action
button.setOnClickListener(object : OnClickListener() {
override fun onClick() { print("click modified") }
})
//object declaration
object Singleton {
var text = "object declaration"
fun someFun() {}
}
//execution
Singleton.someFun()
print(Singleton.text) //object declaration
//companion object
class SomeClass1 {
companion object SingletonCompanion {
var text = "SingletonCompanion"
fun someFun() {}
}
}
class SomeClass2 {
companion object {
var text = "Companion"
fun someFun() {}
}
}
SomeClass1.SingletonCompanion.someFun() //companion name can be missed
print(SomeClass1.text) //SingletonCompanion
SomeClass2.Companion.someFun() //companion default name is Companion
print(SomeClass2.text) //Companion
// VISIBILITY MODIFIERS
class Visibility {
var number: Int //public by default
private var text: String
public constructor(number: Int, text: String) {
this.number = number
this.text = text
}
internal fun internalFunction() { }
protected fun protectedFunction() { }
}
//execution from the same module but another class
var obj = Visibility(1, "text")
print(obj.number) //1
print(obj.text) //compiler error - it's private
obj.internalFunction() //it's okay because it is the same module
obj.protectedFunction() //compiler error - it's protected so access only in class or in extending class