Upgrade to PRO for Only $50/Year—Limited-Time Offer! 🔥

炎炎夏日學 Android 課程 - Part1: Kotlin 語法介紹

炎炎夏日學 Android 課程 - Part1: Kotlin 語法介紹

炎炎夏日學 Android 課程 - Part1: Kotlin 語法介紹
(Kotlin EveryWhere 活動)

Johnny Sung

June 30, 2019
Tweet

More Decks by Johnny Sung

Other Decks in Programming

Transcript

  1. 環境準備 • 電腦⼀一台 • 安裝 Android Studio & Android SDK

    • Android ⼿手機⼀一⽀支(極建議,非必要) • 夠快的網路路 Wifi • 你最愛的飲料⼀一杯
  2. 純命令列列環境 • 電腦版 IntelliJ 新增 Kotlin JVM 專案 • IntelliJ

    IDEA CE
 https://www.jetbrains.com/idea/ • Kotlin 線上編譯器
 https://play.kotlinlang.org • Java 線上編譯器
 https://www.tutorialspoint.com/compile_java_online.php
  3. • 數字 • String 字串串 (陸譯:字符串串) • Boolean 布林林 (陸譯:布爾)

    • Array 陣列列 (陸譯:數組) • Char 字元 (陸譯:字符) • Double ⾼高精度浮點數 (64 bit) • Float 浮點數 (32 bit) • Long 長整數 (64 bit) 基礎資料型態 https://www.kotlincn.net/docs/reference/basic-types.html • Int 整數 (32 bit) • Short 整數 (16 bit) • Byte 整數 (8 bit)
  4. var number: Int? = null var number2: Int = 1

    變數名稱 型態 值 變數名稱 型態 值 有問號 沒有問號
  5. var myString1: String = "abc" var myString2: String? = null

    val myString3: String = "abc" 宣告⼀一個 myString 的字串串,初始為 "abc" (不為 null,⼀一定要給值) 宣告⼀一個可為 null 的字串串叫 myString,初始為 null 宣告⼀一個不可改的字串串叫 myString,值為 "abc"
  6. if

  7. var isCreature = true if (isCreature) { print("是⽣生物") } else

    { print("不是⽣生物") } (⾃自動判斷為布林林值,省略略型態寫法)
  8. 常⽤用運算元 == 等於 > ⼤大於 >= ⼤大於等於 < ⼩小於 <=

    ⼩小於等於 && AND || OR ! NOT 真值表 A B A && B A || B false false false false false true false true true false false true true true true true
  9. val viewType: Int = 1 when (viewType) { 1, 2

    -> { // Do something with value 1 or 2 } 3 -> { // Do something with value 3 } else -> { // Do something with default } }
  10. switch (viewType) { case 1: case 2: // Do something

    with value 1 or 2 break; case 3: // Do something with value 3 break; default: // Do something with default } val viewType: Int = 1 when (viewType) { 1, 2 -> { // Do something with value 1 or 2 } 3 -> { // Do something with value 3 } else -> { // Do something with default } } Java Kotlin
  11. Java Kotlin if (item instanceof ModelA) { // ... }

    else if (item instanceof ModelB) { // ... } else if (item instanceof ModelC) { // ... } else { // ... } when (item) { is ModelA -> { // ... } is ModelB -> { // ... } is ModelC -> { // ... } else -> { // ... } } 型態判斷 型態判斷
  12. ( 0 到 1 0 ) for (i in 0..10)

    { println(i) } 開始值 結束值 關鍵字的點點 賦予計數的變數名
 (計數器的變數)
  13. Java Kotlin for (i in 10 downTo 0 step 1)

    { println(i) } for (int i = 10; i >= 0; i--) { System.out.println(i); } for (i in 0..10) { println(i) } for (int i = 0; i <= 10; i++) { System.out.println(i); } ( 0 到 10 ) ( 10 到 0 ) ( 0 到 10 ) ( 10 到 0 )
  14. Java Kotlin String[] strings = new String[]{"a", "b"}; 陣列列型態 陣列列型態

    val strings: Array<String> = arrayOf("a", "b") ArrayList<String> strings = new ArrayList<>(); strings.add("a"); strings.add("b"); strings.add("c"); val strings:ArrayList<String> = ArrayList<String>() strings.add("a") strings.add("b") strings.add("c") ArrayList 型態 ArrayList 型態
  15. Java Kotlin val strings = arrayOf("a", "b", "c") for ((index,

    value) in strings.withIndex()) { println("The element at $index is $value") } String[] strings = new String[]{"a", "b", "c"}; for (int index = 0; index < strings.length; index++) { String value = strings[index]; System.out.println("The element at " + index + " is " + value); }
  16. Kotlin 型態省略略 val strings = arrayOf("a", "b") val strings =

    ArrayList<String>() strings.add("a") strings.add("b") strings.add("c") 型態省略略 val num = 1 val str = "hello" 型態省略略 型態省略略 val str:String = "hello" val num:Int = 1
  17. Java Kotlin for (int i = 0; i < strings.length;

    i++) { String string = strings[i]; // ... } for (String string : strings) { // ... } for (string in strings) { // ... } String[] strings = new String[]{"a", "b"}; 陣列列 陣列列 val strings: Array<String> = arrayOf("a", "b") for (i in 0 until strings.size) { // ... }
  18. val ice = arrayOf( arrayOf("a0", "a1", "a2", "a3", "a4"), arrayOf("b0",

    "b1", "b2", "b3", "b4"), arrayOf("c0", "c1", "c2", "c3", "c4"), arrayOf("d0", "d1", "d2", "d3", "d4") )
  19. http://tw.gigacircle.com/2409121-1 ice[0][0] ice[0][1] ice[0][2] ice[0][3] ice[0][4] ice[1][0] ice[1][1] ice[1][2] ice[1][3]

    ice[1][4] ice[2][0] ice[2][1] ice[2][2] ice[2][3] ice[2][4] ice[3][0] ice[3][1] ice[3][2] ice[3][3] ice[3][4]
  20. fun plus(a: Int, b: Int): Int { return a +

    b } 參參數1 參參數1型態 參參數2 參參數2型態 函式名字 回傳值型態 (宣告) (呼叫) val c = plus(1,3)
  21. val doneCallback: () -> Unit = { // I'm done!

    } doneCallback() doneCallback.invoke() (宣告) (呼叫) 變數型態 呼叫
  22. val doneCallback: (String) -> Unit = { str -> println(str)

    } (宣告) (呼叫) 變數型態 呼叫 doneCallback.invoke("The result")
  23. var doneCallback: ((String) -> Unit)? = null doneCallback = {

    str -> println(str) } doneCallback?.invoke("The result") (宣告) (呼叫) 可能為 null 的型態 如果是 null 就不呼叫 if (doneCallback != null) { doneCallback.invoke("The result") }
  24. 錯誤處理理 try { // ... throw IllegalArgumentException("Invalid argument.") // ...

    } catch (e: Exception) { e.printStackTrace() } 拋出錯誤 接住錯誤
  25. class MyClass { private val str = "hello" public fun

    printHello() { println(str) } } 類別名 類別變數 類別⽅方法
  26. public class MyClass { private String str = "hello"; public

    void printHello() { System.out.println(str); } } class MyClass { private val str = "hello" public fun printHello() { println(str) } } Java Kotlin
  27. class MyDataModel { var name: String? = "" var num:

    Int = 0 } val model = MyDataModel() model.name = "John" model.num = 123 建立 MyDataModel 物件
  28. Java Kotlin class Student { private String name; public Student(String

    name) { this.name = name; } public void printName() { System.out.println(name); } } class Student { var name: String constructor(name: String) { this.name = name } fun printName(){ println(name) } } 建構⼦子
  29. 建構⼦子的變形 class Student { var name: String constructor(name: String) {

    this.name = name } fun printName(){ println(name) } } class Student(var name: String) { fun printName(){ println(name) } }
  30. class MyClass { companion object { fun staticMethod() { }

    } } public class MyClass { public static void staticMethod() { } } Java Kotlin
  31. Kotlin class MyClass { companion object { fun staticMethod() {

    } } } object MyClass { fun staticMethod() { } }
  32. interface Animal { fun sound() } class Cat : Animal

    { override fun sound() { println("喵喵喵~") } } class Dog : Animal { override fun sound() { println("汪汪汪~") } } 繼承(或實作) val pet: Animal = Cat() pet.sound() // 喵喵喵~
  33. class HairClipperForDog { fun haircut(dog: Dog) { println("(理理髮器聲...)") dog.sound() println("頭⽑毛理理好了了!")

    } } class HairClipperForCat { fun haircut(cat: Cat) { println("(理理髮器聲...)") cat.sound() println("頭⽑毛理理好了了!") } } val myCat = Cat() val catClipper = HairClipperForCat() catClipper.haircut(myCat) val myDog = Dog() val dogClipper = HairClipperForDog() dogClipper.haircut(myDog)
  34. class HairClipper { fun haircut(animal: Animal) { println("(理理髮器聲...)") animal.sound() println("頭⽑毛理理好了了!")

    } } https://tw.buy.yahoo.com/gdsale/ %E6%97%A5%E8%B1%A1%E9%BB%91%E9%91%BD%E9%9B%BB%E5%8B%95%E7%90%86%E9%AB%AE%E5%99%A8%E5%85%85%E6%8F%92%E6%9C%89%E7%B7%9A%E7%84%A1%E7%B7%9AZOH-2600C-5289698.html
  35. Java Kotlin interface Animal { public void eat(); } class

    Cat implements Animal { @Override public void eat() { // ... } } class Dog implements Animal { @Override public void eat() { // ... } } interface Animal { fun eat() } class Cat : Animal { override fun eat() { // ... } } class Dog : Animal { override fun eat() { // ... } }
  36. 變數轉型 • 物件的轉型 • 要是繼承樹裡的才能轉型 open class Father { //

    ... } class Brother: Father() { // ... } var a: Father = Brother() var b = a as? Brother 如果轉型失敗就回傳 null 變數型態為:Brother?
  37. Collections • Map • HashMap • TreeMap • List •

    ArrayList • LinkedList • Stack • Queue • Set • SortedSet • HashSet • TreeSet [ 清單類 ] [ 不重複類 ] [ key-value 類 ]
  38. Java Kotlin String[] strings = new String[]{"a", "b"}; 陣列列型態 陣列列型態

    val strings: Array<String> = arrayOf("a", "b") ArrayList<String> strings = new ArrayList<>(); strings.add("a"); strings.add("b"); strings.add("c"); val strings:ArrayList<String> = ArrayList<String>() strings.add("a") strings.add("b") strings.add("c") ArrayList 型態 ArrayList 型態
  39. HashMap val students = HashMap<String, String>() students.put("A001", "王⼤大明") students.put("A002", "黎黎曉彤")

    students.put("A003", "桃⼤大姐") students.put("A001", "王⼤大明") 王⼤大明 黎黎曉彤 桃⼤大姐 A001 A002 A003 (⽤用 key-value 存放的不重複資料集 )
  40. SOLID • 單⼀一職責原則 Single responsibility • 開放封閉原則 Open-Close • ⾥里里⽒氏替換原則

    Liskov substitution • 接⼝口隔離原則 Interface segregation • 依賴反轉原則 Dependency inversion by Robert C. Martin https://ithelp.ithome.com.tw/articles/10191553 http://teddy-chen-tw.blogspot.com/2014/04/solid.html
  41. class HairClipperForDog { fun haircut(dog: Dog) { println("(理理髮器聲...)") dog.sound() println("頭⽑毛理理好了了!")

    } } class HairClipperForCat { fun haircut(cat: Cat) { println("(理理髮器聲...)") cat.sound() println("頭⽑毛理理好了了!") } } val myCat = Cat() val catClipper = HairClipperForCat() catClipper.haircut(myCat) val myDog = Dog() val dogClipper = HairClipperForDog() dogClipper.haircut(myDog)
  42. class HairClipper<T : Animal> { fun haircut(animal: T) { println("(理理髮器聲...)")

    animal.sound() println("頭⽑毛理理好了了!") } } https://tw.buy.yahoo.com/gdsale/ %E6%97%A5%E8%B1%A1%E9%BB%91%E9%91%BD%E9%9B%BB%E5%8B%95%E7%90%86%E9%AB%AE%E5%99%A8%E5%85%85%E6%8F%92%E6%9C%89%E7%B7%9A%E7%84%A1%E7%B7%9AZOH-2600C-5289698.html ⾃自⼰己取的型態 T 只限於 Animal 繼承樹上的
  43. val myDog = Dog() val hairClipperForDog = HairClipper<Dog>() hairClipperForDog.haircut(myDog) val

    myCat = Cat() val hairClipperForCat = HairClipper<Cat>() hairClipperForCat.haircut(myCat)