首先,看這篇文章前,應該先明確一個問題:咱們爲何要學習Kotlin?java
以下圖所示:git
而Kotlin是一門很是優秀的語言,兼容了N多種語言的優勢,學習Kotlin有助於提高咱們多層編程開發的認識。github
感謝開源項目https://github.com/githubwing/GankClient-Kotlin編程
以及https://github.com/huanglizhuo/kotlin-in-chinese的Kotlin翻譯api
咱們從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的每一個文件都默認導入了以下經常使用包,app
-- kotlin.*dom
-- kotlin.annotation.*ide
-- kotlin.collections.*
-- kotlin.comparisons.* (since 1.1)
-- kotlin.io.*
-- kotlin.ranges.*
-- kotlin.sequences.*
-- kotlin.text.*
而Kotlin的導包方式和Java也相似,但有一點,Kotlin不支持靜態導入。
Kotlin爲何不支持靜態導入?
由於靜態導入會增長代碼的閱讀成本。
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參數爲何支持默認值?
你據說太重載爆炸嗎?🌰
在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