Kotlin: Unterschied zwischen den Versionen
Zur Navigation springen
Zur Suche springen
(→Typen) |
|||
Zeile 140: | Zeile 140: | ||
} | } | ||
</pre> | </pre> | ||
== sealed class == | |||
<pre>sealed class Entity { | |||
data class Easy(val id: String, val name: String): Entity | |||
data class Hard(val id: String, val name: String, multiplyer:Float): Entity | |||
} | |||
</pre> | |||
== Companion object == | == Companion object == | ||
<pre> class Entity private constructor(val id: String) { | <pre> class Entity private constructor(val id: String) { |
Version vom 17. September 2019, 10:58 Uhr
Kategorie:Sprache
Funktionen
fun main() { println(getGreetings()) } # Statt void: Unit fun log(message?: String): Unit { if message != null { println(message) } } fun getGreetings(): String { return "Hi" } fun short() = "Hello" // "named parameters": fun log(prefix: String, message: String) = println("$prefix$message") fun showNamesParams() { log(prefix:"!", message:"Hi") log(message="Hi", prefix="")
Varargs
fun join(prefix: String, vararg items:String) {
var rc = prefix items.foreach(items) { rc += it } return rc
}
Values/Variables
- Top-Level-Variable (außerhalb Klassen/Funktionen)
- val name: String = "default" // readonly
- var name: String
- Typinferenz (automatischer Typ): var name = "Adam"
Nullable Types
- var name?: String = null
- String-Variables, die mit Typinferenz definiert sind, sind Nullable.
Statements
if isTrue(condition) { doIt() } else { warn() } when (message) { null -> println("OK") else -> println(message) } for thing in arrayOf("a", "b") { } // Gleich: things.forEach { println(it) // "it" ist aktueller Wert } // Gleich: things.forEach { thing -> // "thing" ist aktueller Wert println(thing) } things.forIndexedEach { no, thing -> println(no, thing) }
Expressions
name = lastName if (firstName == null) else "$firstName $lastName" name = lastName when(firstName) { null -> lastName else -> "$firstName $lastName" } // Ersatz, wenn null name += lastName :? ""
Typen
- if item is Person { ... }
- if item !is Person { ... }
- Cast: (infoProvider as SessionInfoProvider).getSessionId()
String
- Konkatenation: firstName + ! " + lastName
- Templates: "$firstName $lastName"
Enums
enum class EntityType { EASY, MEDIUM, HARD: fun formated() = name.toLowerCase() } when type { EntityType.EASY -> type.name // Metainfo else -> "not easy" }
Container
- Normalerweise: nicht änderbar ("immutable")
- List<String>
- MutableList<Int>
val things = arrayOf("box", "books") assertTrue(things[0] == things.get(0)) val map = mapOf(1 to "a", 2 to "b", 3 to "c") map.forEach { key, value -> println("$key -> $value") } val map = mutableMapOf(1 to "a", 2 to "b", 3 to "c") map.
Klassen
- Klassen: private / internal
- Members: private / protected
- Klassen + vals: open: kann abgeleitet werden
- in abgeleiteter Klasse: override
class Person constructor() class Person(_firstName: String, lastName: String){ val age: Int? = null val firstName: String val lastName: String init { firstName = _firstName lastName = _lastName } } // Kürzer: class Person(val firstName: String, val lastName: String){ // 2.ter Konstruktor: constructor(): this("Peter", "Parker") var nickName: String? = null // expliziter Setter: set(value){ println("nickname is set") field = value } get() { println("access to nickname") return field } } open class BasicInfoProvider { } class FancyInfoProvider : BasicInfoProvider { }
sealed class
sealed class Entity { data class Easy(val id: String, val name: String): Entity data class Hard(val id: String, val name: String, multiplyer:Float): Entity }
Companion object
class Entity private constructor(val id: String) { compianion Factory { const val id = "id" fun create() = Entitiy(id) } } fun main() { val entity = Entity.Factory.create() }
Interfaces
interface personInfoProvider { fun printInfo(person: Person) fun debug(person: Person) { person.debug() } val providerInfo: String } class BasicInfoProvider: PersonInfoProvider { override val providerInfo = "!" override fun printInfo(person: Person) { super.printInfo(person) } }