Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
[ASJ2021] 從零開始學 Android - Unit 1-1: Introduction to Kotlin
前言
這是用來記錄參加《Android App 開發培訓計劃 2021》的學習筆記,也是我第一次接觸 Android App 開發。
剛好最近工作上需要和 App Team 合作(其實是因為想要 T-shirt),Web 端會使用到有關 Native SDK 提供的功能,就藉這個機會把基本觀念給補起來,期許自己能夠在期限內完成課程,內容如有錯誤歡迎指正!
學習目標
Unit 1: Kotlin basics for Android
關於 Andorid App
- Andorid App:使用 Java 程式語言設計
- Kotlin:於 2017 年由 Google 宣布成為 Android 官方開發語言
- Android Studio:是 Android 平台開發程式的整合式開發環境(IDE),支援 Java 和 Kotlin 兩種程式語言
Kotlin 入門實戰
我們可透過 Web 的程式碼編輯器 Kotlin Playground 來練習 Kotlin 語法:
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
點選頁面中的綠色箭頭,即可運行編輯器中的程式碼:
- fun:function 函式。
- fun main():main 為函式名稱,是程式的唯一入口(入口函式),後面接
{}
填入要實作的方法。
- println():印出參數,若傳入參數為字串,則必須加引號,否則程式會報錯:
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
修正後的輸出為:
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
//
撰寫註解
在開發過程,我們可透過 //
為程式碼加上註解,這些反灰的文字不會影響程式運作,目的是讓未來的自己或其他開發者快速理解,提高程式碼的可讀性。
輸出結果如下:
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
Variables 變數
Kotlin 和其他程式語言一樣,可透過宣告變數來取代重複出現、或想要替換的文字。
舉例來說,在上述 main() function 中,我們可建立一個變數 age,程式碼寫法如下:
- 代表宣告一個變數 age 並賦值為 5
- 使用 val 宣告的變數只能賦值一次,之後不可再改變
- 使用 var 宣告一個可改變的變數
若要在 println() 中放入變數,則需加上錢字號和大括弧 ${variable}
,如以下範例:
Coding Convention 命名慣例
在開發過程中,函式名稱和變數皆依循命名慣例:camelCase(駝峰式命名),這是為了統一程式碼風格,並提高可讀性。
改寫先前的範例:
輸出結果如下,這樣的改寫稱為「重構」,可避免重複撰寫同樣的程式碼:
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
repeat() 重複執行
我們也可透過 repeat() 函式來執行需要重複的程式碼,這樣程式碼就只需要寫一次 =
符號:
重構之後會得到相同輸出結果:
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
傳入參數的型別
在 function 傳入參數時,必須註記參數的型別,例如 border: String
表示變數 border 是 String 型別,否則程式會報錯:
輸出結果如下:

我們也可傳入一個以上的參數,同樣必須標註該參數的型別,以宣告 timesToRepeat 變數(型別是 Int),取代重複次數為例:
輸出結果如下:

Nesting Loops 巢狀迴圈
接著我們要繼續完成生日蛋糕,預計輸出會長這樣:
程式碼範例如下,這裡會使用兩個 repeat() 實作巢狀迴圈:
小結
- 透過模板表達式在字串中傳入變數:
print("Hello, ${name}")
。
- 使用
val
宣告的變數,一旦賦值就不能再改變,如:val name = "QQ"
。
- 函式可以傳入一個或以上的參數,如:
fun printCakeBottom(age: Int, layers: Int) {}
。
- 實作 loop:使用
repeat()
可重複輸出傳入的參數。
- 實作 Nested loops:
repeat(layers)
裡再一層 repeat(age)
可決定重複輸出傳入參數幾次。