24. 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
25. 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
26. 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
27. 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
28. Zugriff auf Properties
Malte Bucksch 29
val human = KotlinHuman()
human.cityAddress = "Hamburg“
val address = human.cityAddress
29. 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
30. 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
33. 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
34. 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)
}
55. When: Java Switch mit Superkräften
fun getDogPluralString(dogCount: Int) = when (dogCount) {
0 -> "Keine Hunde"
1 -> "Ein Hund"
else -> "$dogCount Hunde"
}
Malte Bucksch 59
56. 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
68. Lambda Definition
val dog = Dog("Bruno")
val greetDog: (Dog) -> String = { dog -> "Hey! ${dog.name}" }
println(greetDog(dog))
Parameter type Return type
Malte Bucksch 72
69. Lambda Definition
val dog = Dog("Bruno")
val greetDog: (Dog) -> String = { dog -> "Hey! ${dog.name}" }
println(greetDog(dog))
Parameter name Lambda Body
Malte Bucksch 73
- Vorstellen! Ich bin Malte Bucksch, Mitgründer von QuickBird Studios und Lead Android Developer
Reden heut über kotlin
FRAGE: Wer hat kotlin schon mal benutzt? Wer kennt swift?
Heute praxisnah basics von kotlin lernen
Ich werde nicht alle Features von Kotlin zeigen, aber wichtige und coole features
Was die Vorteile sind
Ich zeige lieber einige wichtige feature und ihr könnt sie direct ausprobieren, als nochmal alle features runter zu rattern. Da gibt es gut ressourcen online
- Even can run on iOS
- Wir machen es mit Android Studio -> weil es
Keine Semicolons
Schlanker
Type wird automatisch erkannt -> das heißt NICHT, dass Kotlin nicht typsicher ist
Wenn möglich sollten variable a
Keine Semicolons
Schlanker
Type wird automatisch erkannt -> das heißt NICHT, dass Kotlin nicht typsicher ist
Wenn möglich sollten variable a
Keine Semicolons
Schlanker
Type wird automatisch erkannt -> das heißt NICHT, dass Kotlin nicht typsicher ist
Wenn möglich sollten variable a
- No semicolons anymore
- No semicolons anymore
- No semicolons anymore
- In kotlin sind es 5 Zeilen code, in Java sind es .. Code TODO NOW
- Show function with 3 int values with standard params and explain why named arguments are needed
- Auf repeat hinweisen
- Kennt man von java
In java typen sind nullable by default. In kotlin nicht
Ohne optionals und null checks ist programmieren ein wenig wie russisches Roulett: es geht moistens gut, aber eines tages!
Programmier is SICHER, dass dort kein Null wert drin ist, ABER manchmal eben doch
In java typen sind nullable by default. In kotlin nicht
Ohne optionals und null checks ist programmieren ein wenig wie russisches Roulett: es geht moistens gut, aber eines tages!
Programmier is SICHER, dass dort kein Null wert drin ist, ABER manchmal eben doch
!! Was unseren hund sehr glücklich macht, weil er jetzt Hallo zu hunden sagen kann, ohne gefahr zu laufen seine ganze Applikation zu crashen
- Es mag fälle geben wo man wirklich eine Exception haben will
- Any ist Kotlin’s Object – Überklasse von allen Klassen
- If/else same as in java
- In kotlin: when breaks after each case
- In kotlin: when breaks after each case
- TODO NOW consider adding this exact code also in java if time to show difference -> don’t return but use println to make difference clear
Lambdas sind Funktions objekte
Man kann Referenzen auf Funktionen haben und sie herumreichen