Kotlin

Aus Info-Theke
Zur Navigation springen Zur Suche springen

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

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
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
    }
}

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)
  }
}