mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2024-12-27 01:03:19 +03:00
Merge pull request #3593 from ashraf-patel/feature/ashraf/kotlin-enum
[Kotlin/en] Kotlin enum
This commit is contained in:
commit
87ea429179
@ -350,11 +350,22 @@ fun helloWorld(val name : String) {
|
||||
|
||||
// Enum classes are similar to Java enum types.
|
||||
enum class EnumExample {
|
||||
A, B, C
|
||||
A, B, C // Enum constants are separated with commas.
|
||||
}
|
||||
|
||||
fun printEnum() = println(EnumExample.A) // => A
|
||||
|
||||
// Since each enum is an instance of the enum class, they can be initialized as:
|
||||
enum class EnumExample(val value: Int) {
|
||||
A(value = 1),
|
||||
B(value = 2),
|
||||
C(value = 3)
|
||||
}
|
||||
fun printProperty() = println(EnumExample.A.value) // => 1
|
||||
|
||||
// Every enum has properties to obtain its name and ordinal(position) in the enum class declaration:
|
||||
fun printName() = println(EnumExample.A.name) // => A
|
||||
fun printPosition() = println(EnumExample.A.ordinal) // => 0
|
||||
|
||||
/*
|
||||
The "object" keyword can be used to create singleton objects.
|
||||
We cannot instantiate it but we can refer to its unique instance by its name.
|
||||
|
Loading…
Reference in New Issue
Block a user