Skip to content

Commit 5217ea2

Browse files
committed
[Kotlin] added data class, enum, sealed class
working on generics
1 parent 8193a05 commit 5217ea2

File tree

8 files changed

+217
-33
lines changed

8 files changed

+217
-33
lines changed

KotlinFromScratch/.idea/workspace.xml

+96-31
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

KotlinFromScratch/src/DataClass.kt

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class DataClass {
2+
fun test() {
3+
println("Properties declared in the primary constructor")
4+
val scout = User("Scout", 15, "F")
5+
val (name, age) = scout
6+
println("$name, $age years of age")
7+
8+
val jack = User(age = 12, name = "Jack")
9+
val olderJack = jack.copy(age = 20)
10+
println("$jack.name, $jack.age years of age")
11+
println("$olderJack.name, $olderJack.age years of age")
12+
13+
14+
println("")
15+
println("Properties declared in the class body")
16+
val employee = Employee("Michael")
17+
employee.age = 18
18+
}
19+
}

KotlinFromScratch/src/Employee.kt

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
data class Employee(val name: String) {
2+
var age: Int = 0
3+
var gender: String = "M"
4+
}

KotlinFromScratch/src/Generics.kt

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Generics {
2+
interface Source<out T> {
3+
fun nextT(): T
4+
}
5+
6+
private fun demo(source: Source<String>) {
7+
// Declaration-site variance
8+
val objects: Source<Any> = source
9+
println(objects.nextT())
10+
}
11+
12+
fun test() {
13+
println("Declaration-site variance")
14+
// Declaration-site variance -> In Java, we can't declare type String to type Object
15+
demo(object : Source<String> {
16+
override fun nextT(): String {
17+
return "next"
18+
}
19+
})
20+
21+
22+
println("")
23+
println("Type projections")
24+
}
25+
}

KotlinFromScratch/src/Main.kt

+11-2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,15 @@ fun main(args: Array<String>) {
1414
// val propertiesAndFields = PropertiesAndFields()
1515
// propertiesAndFields.test()
1616

17-
val baseClass = BaseClass()
18-
baseClass.test()
17+
// val baseClass = BaseClass()
18+
// baseClass.test()
19+
20+
// val dataClass = DataClass()
21+
// dataClass.test()
22+
23+
// val sealedClass = SealedClass()
24+
// sealedClass.test()
25+
26+
val generics = Generics()
27+
generics.test()
1928
}

KotlinFromScratch/src/SealedClass.kt

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
class SealedClass {
2+
fun test() {
3+
4+
println("enum")
5+
val state = EnumState.PLAYING
6+
println(getStateDescription(state))
7+
8+
println("")
9+
println("Replaced with sealed class")
10+
val playerA = SealedState.Play("A")
11+
println(getSealedStateDescription(playerA))
12+
val defaultPlayer = SealedState.PLAYING_
13+
println(getSealedStateDescription(defaultPlayer))
14+
}
15+
16+
private fun getStateDescription(state: EnumState) = when (state) {
17+
EnumState.PAUSE -> "Player is paused"
18+
EnumState.IDLE -> "Player is idle"
19+
EnumState.PLAYING -> "Player is playing"
20+
EnumState.STOP -> "Player is stopped"
21+
}
22+
23+
private fun getSealedStateDescription(state: SealedState) = when (state) {
24+
SealedState.PAUSE_ -> "Player is paused"
25+
SealedState.IDLE_ -> "Player is idle"
26+
SealedState.PLAYING_ -> "Player is playing"
27+
SealedState.STOP_ -> "Player is stopped"
28+
is SealedState.Pause -> "Player ${state.name} is paused"
29+
is SealedState.Play -> "Player ${state.name} is playing"
30+
is SealedState.Idle -> "Player ${state.name} is idle"
31+
is SealedState.Stop -> "Player ${state.name} is stopped"
32+
}
33+
}
34+
35+
enum class EnumState {
36+
PAUSE, PLAYING, IDLE, STOP
37+
}
38+
39+
sealed class SealedState {
40+
data class Pause(val name: String) : SealedState()
41+
data class Play(val name: String) : SealedState()
42+
data class Idle(val name: String) : SealedState()
43+
data class Stop(val name: String) : SealedState()
44+
object PAUSE_ : SealedState()
45+
object PLAYING_ : SealedState()
46+
object IDLE_ : SealedState()
47+
object STOP_ : SealedState()
48+
}

KotlinFromScratch/src/User.kt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
data class User(val name: String, val age: Int, val gender: String = "M")

README.md

+13
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,15 @@ The exam is only available in Java at this time (4/1/2019)
232232
- Companion objects (which is similar to ```static```)
233233
- Call extension functions of the base class declared other class
234234
- Call functions both declared in the base class and self class inside extension functions (check [BaseClassExtensions])
235+
8. [Data Class], [User], [Employee]
236+
- Properties declared in the primary constructor or class body
237+
- Copying
238+
9. [enum and sealed class]
239+
- enum vs sealed class
240+
10. [Generics]
241+
- Declaration-site variance
242+
- Type projections
243+
235244

236245

237246

@@ -260,6 +269,10 @@ The exam is only available in Java at this time (4/1/2019)
260269
[Visibility Modifiers]:<https://github.com/Catherine22/AAD-Preparation/blob/master/KotlinFromScratch/src/package1/VisibilityModifiers.kt>
261270
[BaseClass]:<https://github.com/Catherine22/AAD-Preparation/blob/master/KotlinFromScratch/src/BaseClass.kt>
262271
[BaseClassExtensions]:<https://github.com/Catherine22/AAD-Preparation/blob/master/KotlinFromScratch/src/BaseClassExtensions.kt>
272+
[Data Class]:<https://github.com/Catherine22/AAD-Preparation/blob/master/KotlinFromScratch/src/DataClass.kt>
273+
[User]:<https://github.com/Catherine22/AAD-Preparation/blob/master/KotlinFromScratch/src/User.kt>
274+
[Employee]:<https://github.com/Catherine22/AAD-Preparation/blob/master/KotlinFromScratch/src/Employee.kt>
275+
[enum and sealed class]:<https://github.com/Catherine22/AAD-Preparation/blob/master/KotlinFromScratch/src/SealedClass.kt>
263276

264277
[CallLogProvider]:<https://android.googlesource.com/platform/packages/providers/ContactsProvider/+/refs/tags/android-9.0.0_r34/src/com/android/providers/contacts/CallLogProvider.java>
265278

0 commit comments

Comments
 (0)