Kotlin: Unterschied zwischen den Versionen
Zur Navigation springen
Zur Suche springen
(→Typen) |
|||
Zeile 97: | Zeile 97: | ||
* Klassen: private / internal | * Klassen: private / internal | ||
* Members: private / protected | * Members: private / protected | ||
* Klassen + vals: open: kann abgeleitet werden | |||
** in abgeleiteter Klasse: override | |||
<pre>class Person constructor() | <pre>class Person constructor() | ||
class Person(_firstName: String, lastName: String){ | class Person(_firstName: String, lastName: String){ | ||
Zeile 121: | Zeile 123: | ||
return field | return field | ||
} | } | ||
} | |||
open class BasicInfoProvider { | |||
} | |||
class FancyInfoProvider : BasicInfoProvider { | |||
} | |||
</pre> | |||
== Companion object == | |||
<pre> class Entity private constructor(val id: String) { | |||
compianion Factory { | |||
const val id = "id" | |||
fun create() = Entitiy(id) | |||
} | |||
} | |||
fun main() { | |||
val entity = Entity.Factory.create() | |||
} | } | ||
</pre> | </pre> | ||
= Interfaces = | = Interfaces = | ||
<pre>interface personInfoProvider { | <pre>interface personInfoProvider { |
Version vom 17. September 2019, 10:40 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"
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 { }
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) } }