每天三分鐘瞭解Kotlin的祕密:1.基礎入門
什麼要學習Kotlin
首先,看這篇文章前,應該先明確一個問題: 我們為什麼要學習Kotlin?
如下圖所示:

k1.png
而Kotlin是一門非常優秀的語言,相容了N多種語言的優點,學習Kotlin有助於提升我們多層程式設計開發的認識。
從一個類開始
感謝開源專案 ofollow,noindex">https://github.com/githubwing/GankClient-Kotlin
以及 https://github.com/huanglizhuo/kotlin-in-chinese 的Kotlin翻譯
我們從GankClient的某個類開始學習Kotlin
這是Kotlin的一個樣例類(終於不用再像java一樣寫多餘的分號了):
package com.wingsofts.gankclient.mvp.model import com.wingsofts.gankclient.api.GankApi import com.wingsofts.gankclient.bean.FuckGoods import com.wingsofts.gankclient.bean.JsonResult import com.wingsofts.gankclient.mvp.contract.RandomContract import rx.Observable import javax.inject.Inject class RandomModel @Inject constructor(private val api: GankApi) : RandomContract.Model { override fun getRandom(type: String): Observable<JsonResult<List<FuckGoods>>> { return api.getRandom(type) } var counter = 0 set(value) { if (value >= 0) field = value } fun max(a: Int, b: Int) = if (a > b) a else b }
導包
與Java一樣,Kotlin也含有預設匯入的特性。Kotlin的每個檔案都預設匯入瞭如下常用包,
-- kotlin.* -- kotlin.annotation.* -- kotlin.collections.* -- kotlin.comparisons.* (since 1.1) -- kotlin.io.* -- kotlin.ranges.* -- kotlin.sequences.* -- kotlin.text.*
而Kotlin的導包方式和Java也類似,但有一點,Kotlin不支援靜態匯入。
Kotlin為什麼不支援靜態匯入?
因為靜態匯入會增加程式碼的閱讀成本。
函式宣告
fun
Kotlin 中用關鍵字 fun 宣告函式。
override fun getRandom(type: String): Observable<JsonResult<List<FuckGoods>>> { return api.getRandom(type) }
Kotlin為什麼使用fun來宣告函式?為什麼不是function?或者def?
JetBrains團隊說:
We use “fun” because we like it - and yes, we do know what the word means in English.
我們使用“fun”因為我們喜歡它,而且我們知道這個詞在英語中是啥意思!
“fun”在英語中是什麼意思?
來自谷歌翻譯的 fun 的翻譯:動詞 開玩笑,名詞 玩笑。
引數
引數宣告
Kotlin的引數使用Pascal符號定義,引數之間和java類似通過“ , ”分隔,引數需要指明型別。
fun test(count: Int, test2: Int) { }
為什麼Kotlin像Pascal一樣,引數宣告型別要在引數名後面?count: Int
Rob Pike曾解釋過這個問題:因為這樣更加清晰易懂,當然,我覺得這樣存在很大的爭議,不過也和工程師自身的習慣有關係。
預設引數
Kotlin引數可以設定預設值,當需要忽略該引數的時候可以使用引數的預設值。Like this: off: Int = 0
fun read(b: Array<Byte>, off: Int = 0, len: Int = b.size ) { ... }
Kotlin引數為什麼支援預設值?
你聽說過過載爆炸嗎?:chestnut:

2.png
空返回值
在Java中返回空關鍵字為void,在Kotlin中的空返回值為Unit,並且可以省略.Unit和void不同,Unit是個真實的類並且是個單例。
// 不省略 fun printHello(name: String?): Unit { ... } // 省略 fun printHello(name: String?) { ... }
為什麼使用Unit?
“Unit” just stands for “something that has only one value”, it’s a traditional name, comes from functional languages. I agree that this name is not very intuitive, but we failed to invent a better name.
“Unit”代表“只有一個值”,它是一個來自函式式語言的傳統的名稱。我同意這個名字不是很直觀,但我們沒有想到一個更好的名字。
區域性變數
Kotlin區域性變數分為val和var
var 關鍵字宣告可變屬性,和Java變數宣告類似;
val 關鍵字宣告只讀屬性,和Java中final變數類似,在初始化時需要賦值,以後不能改變。更多的應該使用val關鍵字。
val a: Int = 1 var x = 5
為什麼使用val關鍵字?
val具有Immutable或readonly的特性,一旦建立,不可改變。沒有競爭條件,沒有併發問題,無需去同步,保證了執行緒安全,不需要擔心空安全問題。
為什麼是val和var?
因為val是value的縮寫,而var是variable。
Using vals in your code makes you think about alternative, immutable, functional code. […] Removing vars leads to refactoring. The refactoring leads to new coding patterns. New coding patterns leads to a shift in your approach to programming. This shift in approach leads to transformative code that has fewer defects and is easier to maintain.
— Beginning Scala, David Pollack