Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Kotlin Workshop

QuickBird
September 27, 2017

Kotlin Workshop

Workshop about the basics of Kotlin. We loved the language from the start and switched all of our projects to Kotlin in 2017.

QuickBird

September 27, 2017
Tweet

More Decks by QuickBird

Other Decks in Programming

Transcript

  1. Agenda I. Einführung II. Setup III. Kotlin 1. Variablen und

    Konstanten 2. Funktionen 3. Klassen 4. Optionals 5. Kontroll-Fluss 6. Extensions 7. Lambdas Malte Bucksch 2
  2. Hacking Führe alle Tests aus und stell sicher, dass alle

    Tests fehlschlagen Run Tests in ‘exercises’ Rechtsklick… Malte Bucksch 11
  3. String myVariable = "Change me"; final String myConstant = "Can't

    change me"; var myVariable: String = "Change me“ val myConstant: String = "Can't change me" 15
  4. String myVariable = "Change me"; final String myConstant = "Can't

    change me"; var myVariable: String = "Change me“ val myConstant: String = "Can't change me" 16
  5. final String myConstant = "Can't change me"; String myVariable =

    "Change me"; val myConstant = "Can't change me" var myVariable = "Change me" 17
  6. public class JavaHuman { private String name; private int age;

    public JavaHuman(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public void sayHello(String otherPersonName){ System.out.println("Hi, nice to see you "+otherPersonName); } public void sayHello(){ sayHello(""); } } class KotlinHuman(var name: String, var age: Int) { fun sayHello(otherPersonName: String = "") { println("Hi, nice to see you $otherPersonName") } } 24
  7. Kotlin Klasse class KotlinHuman(var name: String, var age: Int) {

    fun sayHello(otherPersonName: String = "") { println("Hi, nice to see you $otherPersonName") } } Primary Constructor Malte Bucksch 25
  8. Kotlin Klasse class KotlinHuman(var name: String, var age: Int) {

    fun sayHello(otherPersonName: String = "") { println("Hi, nice to see you $otherPersonName") } } String template Malte Bucksch 26
  9. Getters and setters class KotlinHuman(var name: String, var age: Int)

    { var cityAddress = "Munich" fun sayHello(otherPersonName: String = "") { println("Hi, nice to see you $otherPersonName") } } Malte Bucksch 28
  10. Zugriff auf Properties Malte Bucksch 29 val human = KotlinHuman()

    human.cityAddress = "Hamburg“ val address = human.cityAddress
  11. Getters and setters class KotlinHuman(var name: String, var age: Int)

    { var cityAddress = "Munich" fun sayHello(otherPersonName: String = "") { println("Hi, nice to see you $otherPersonName") } } Malte Bucksch 30
  12. Getters and setters class KotlinHuman(var name: String, var age: Int)

    { var cityAddress = “Munich" get() = "City $field" set(city) { if (city.length < 3) throw IllegalArgumentException("City less than 3 chars") field = city } fun sayHello(otherPersonName: String = "") { println("Hi, nice to see you $otherPersonName") } } Malte Bucksch 31
  13. Syntax-Beispiel class KotlinHuman(var name: String, var age: Int) { fun

    sayHello(otherPersonName: String = "") { println("Hi, nice to see you $otherPersonName") } } return “Hallo".repeat(5) Öffne 1_SimpleDogClass.kt Malte Bucksch 37
  14. Lösung Malte Bucksch 38 private class Dog(var name: String, val

    type: String) { fun bark(times: Int) = println("Wuff".repeat(times)) } @Test fun testMyNewDogClass() { val dog = Dog("Bruno","Labrador") dog.bark(3) }
  15. Aufrufen von Kotlin Optionals val dog: Dog? = findDog() if(dog

    != null){ dog.bark(3) } Malte Bucksch 45
  16. Automatic casting if(dog != null){ dog.bark(3) } val anyObject: Any

    = getAnimal() if(anyObject is Dog){ anyObject.bark() } Malte Bucksch 49
  17. Syntax-Beispiel Öffne 2_Optionals.kt human?.sayHello() val myDog = findDog() ?: buyNewDog()

    // Safe casts val name = (any as? Cat)?.name Malte Bucksch 51
  18. Lösung Malte Bucksch 52 private fun letDogBark(dog: Dog?) { //

    TODO TASK 1 dog?.bark() } private fun getDogName(dog: Dog?): String { // TODO TASK 2 return dog?.name ?: "No dog found" } private fun getNameOf(any: Any): String { // TODO TASK 3 return (any as? Dog)?.name ?: "type unknown" }
  19. For Schleife Malte Bucksch 55 val dogs = getDogs() for

    (index in 0..10){ dogs[index].bark() } Range Operator
  20. When: Java Switch mit Superkräften fun getDogPluralString(dogCount: Int): String {

    when(dogCount){ 0 -> return "Keine Hunde" 1 -> return "Ein Hund" else -> return "$dogCount Hunde" } } Malte Bucksch 56
  21. When: Java Switch mit Superkräften fun getDogPluralString(dogCount: Int): String {

    when(dogCount){ 0 -> return "Keine Hunde" 1 -> return "Ein Hund" else -> return "$dogCount Hunde" } } Malte Bucksch 57
  22. When: Java Switch mit Superkräften fun getDogPluralString(dogCount: Int): String {

    return when(dogCount){ 0 -> "Keine Hunde" 1 -> "Ein Hund" else -> "$dogCount Hunde" } } Malte Bucksch 58
  23. When: Java Switch mit Superkräften fun getDogPluralString(dogCount: Int) = when

    (dogCount) { 0 -> "Keine Hunde" 1 -> "Ein Hund" else -> "$dogCount Hunde" } Malte Bucksch 59
  24. When ohne Argument fun evaluatePassword(password: String): String { return when

    { password.isEmpty() -> "Bitte gib ein Passwort ein" password.length < 5 -> "Passwort ist nicht lang genug" !password.containsNumber() -> "Passwort muss eine Ziffer enthalten" else -> "Das Passwort ist valide" } } Malte Bucksch 60
  25. Syntax-Beispiel Öffne 3_ControlFlow.kt when(dogCount){ 0 -> return "Keine Hunde" 1

    -> return "Ein Hund" else -> return "$dogCount Hunde" } return when (dogCount) { in 0..4 -> "..." 4, 5 -> "..." else -> "..." } Malte Bucksch 62
  26. Lösung Malte Bucksch 63 // TODO TASK 1 fun findDogOwnerName(dog:

    Dog): String? { return when(dog.name){ "Bruno" -> "Hans" "Ignatz" -> "Peter" else -> null } } // TODO TASK 2 fun ageToString(dog: Dog): String { return when(dog.age){ 0,1 -> "Baby Dog" in 2..8 -> "Normal Dog" else -> "Old Dog" } }
  27. Extension Funktion: Zahl gerade oder ungerade fun Int.isEven(): Boolean {

    return this % 2 == 0 } println(1.isEven()) // false println(2.isEven()) // true println(3.isEven()) // false Malte Bucksch 66
  28. Lösung Malte Bucksch 69 // TODO TAKS 1 fun String.scream():

    String { return this.toUpperCase()+"!!!" } // TODO TASK 2 private fun applyAllCapsExtension(text: String): String { return text.scream() } @Test fun testAllCapsDogLanguage() { // TODO TASK 3 val allCapsDogLanguage = "Ich habe ganz viel Hunger".barkify().scream() assertEquals("WUFF WUFF WUFF WUFF WUFF!!!", allCapsDogLanguage) }
  29. Lambda Definition val dog = Dog("Bruno") val greetDog: (Dog) ->

    String = { dog -> "Hey! ${dog.name}" } println(greetDog(dog)) Parameter type Return type Malte Bucksch 72
  30. Lambda Definition val dog = Dog("Bruno") val greetDog: (Dog) ->

    String = { dog -> "Hey! ${dog.name}" } println(greetDog(dog)) Parameter name Lambda Body Malte Bucksch 73
  31. Mapping von Hunden age = 8 age = 2 age

    = 3 age = 6 age = 11 age = 8 age = 2 age = 3 age = 6 age = 11 Malte Bucksch 83
  32. Mapping von Hunden val dogs = listOf(Dog("Bello",age = 8),Dog("Rex",age =

    2), Dog("Lessi",age = 6),Dog("Bruno",age = 3),Dog("Bello",age = 11)) val dogAges = dogs.map { … } Malte Bucksch 84
  33. Mapping von Hunden val dogs = listOf(Dog("Bello",age = 8),Dog("Rex",age =

    2), Dog("Lessi",age = 6),Dog("Bruno",age = 3),Dog("Bello",age = 11)) val dogAges = dogs.map { dog -> dog.age } Malte Bucksch 86
  34. Mapping von Hunden val dogs = listOf(Dog("Bello",age = 8),Dog("Rex",age =

    2), Dog("Lessi",age = 6),Dog("Bruno",age = 3),Dog("Bello",age = 11)) val dogAges = dogs.map { it.age } Malte Bucksch 87
  35. Mapping von Hunden val dogs = listOf(Dog("Bello",age = 8),Dog("Rex",age =

    2), Dog("Lessi",age = 6),Dog("Bruno",age = 3),Dog("Bello",age = 11)) val dogAges = dogs.map { it.age } val average = dogAges.average() (= 6) Malte Bucksch 88
  36. Lösung Malte Bucksch 92 // TODO TASK 1 private fun

    findDogNames(dogs: List<Dog>): List<String> { return dogs.map { it.name } } // TODO TASK 2 private fun findOldDogs(dogs: List<Dog>): List<Dog> { return dogs.filter { it.age > 5 } } // TODO TASK 3 private fun findNamesOfOldDogs(dogs: List<Dog>): List<String> { return dogs.filter { it.age > 5 }.map { it.name } }
  37. Mehr Kotlin Features • Data Klassen (Autogeneriertes Equals, Hashcode &

    Clone) • Mutable und Immutable lists • Destructuring Declarations • Infix Funktionen • Extension Properties • Sealed classes • Sequences • Explicit returns … Malte Bucksch 93