From a2a1db06e461ef6a88a8134df4136c4ebc526c7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=BDeljko=20Trogrli=C4=87?= Date: Tue, 11 Jul 2017 21:58:16 +0200 Subject: [PATCH] =?UTF-8?q?Update=20Kotlin=20-=20=C4=8Demu.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "Kotlin-cemu.hr/Kotlin - \304\215emu.md" | 52 ++++++++++++++++++++++-- 1 file changed, 48 insertions(+), 4 deletions(-) diff --git "a/Kotlin-cemu.hr/Kotlin - \304\215emu.md" "b/Kotlin-cemu.hr/Kotlin - \304\215emu.md" index 7186952..50a626e 100644 --- "a/Kotlin-cemu.hr/Kotlin - \304\215emu.md" +++ "b/Kotlin-cemu.hr/Kotlin - \304\215emu.md" @@ -31,12 +31,45 @@ for the JVM, Android and the browser * 2017 Kotlin 1.1, Java 9, Android Java 8 --- # Osnove - klase, funkcije i varijable -## Klasa -## var i val, getteri i setteri --- -# Konstruktori i data klase -## Primarni konstruktor +# Klasa + +```kotlin +class Person(val ime: String, val prezime: String) +``` + +## Sekundarni konstruktori +```kotlin +class Person(val ime: String, val prezime: String?) { + + constructor(ime: String) : this(ime, null) + + constructor(ime: String, log : Boolean) : this(ime, null) { + println("ime = ${ime}") + } +} +``` +--- +# var i val, getteri i setteri +```kotlin +class Person(val ime: String, var prezime: String?, log: Boolean = true) { + init { + if (log) println("Ime ${ime} ${prezime}") + } +} + +fun main(args: Array) { + val person = Person("Pero", "Perić") + person.ime + person.prezime + person.prezime = "Jozić" +} +``` +--- ## Data klase +```kotlin +data class Person(val ime: String, val prezime: String) +``` * equal * hashCode * toString @@ -44,9 +77,20 @@ for the JVM, Android and the browser --- # null ## Tip ili Tip? +```kotlin +``` ## Sigurni pozivi.? +```kotlin +person.prezime?.toUpperCase()?.toLowerCase() +``` ## Elvis, kralj nulla ?: +```kotlin +person.prezime?.toUpperCase()?.toLowerCase() ?: "prazno" +``` ## Hoću exception!! +```kotlin +person.prezime!!.toUpperCase().toLowerCase() +``` ## Java! --- # Zabava s funkcijama