大家好,我是William。
從今天開(kāi)始,我將不會(huì)按部就班地人工翻譯官方文檔了,太無(wú)聊了,而且學(xué)習(xí)效率低下。
其實(shí)學(xué)習(xí)Kotlin最快的方法就是直接做Koan測(cè)試題,不懂就看測(cè)試題中的提示。
做完整套Koan測(cè)試題,面試Kotlin工程師的崗位基本也沒(méi)問(wèn)題了(目前至少是這樣子)。
Koan總共有42關(guān),我打算每天一關(guān),每關(guān)做個(gè)簡(jiǎn)析(個(gè)人吐槽)。
接下來(lái)開(kāi)始今天的內(nèi)容吧,Koan第一關(guān)。
關(guān)卡鏈接:
https://try.kotlinlang.org/#/Kotlin%20Koans/Introduction/Hello,%20world!/Task.kt
Intrduction
Hello, world!
Simple Functions
Take a look at function syntax and make the function start return the string "OK".
In the tasks the function TODO() is used that throws an exception. Your job during the koans will be to replace this function invocation with a meaningful code according to the problem.
fun start(): String = TODO()
譯:
本題先叫你看看函數(shù)語(yǔ)法糖。然后寫(xiě)個(gè)返回"OK"的字符串的函數(shù)。
解:
假設(shè)你之前對(duì)Kotlin一竅不通,那么直接點(diǎn)擊上面[函數(shù)語(yǔ)法糖]鏈接,跳進(jìn)去看看函數(shù)表達(dá)式是什么,看完后你會(huì)發(fā)現(xiàn)函數(shù)表達(dá)式其實(shí)跟Lambda表達(dá)式很像。于是直接刪除TODO(),然后寫(xiě)上"OK"。
答:
fun start(): String = "OK"
小結(jié)
今天就這么簡(jiǎn)單,不然Koan第一關(guān)就不叫Introduction了。記得寫(xiě)完后點(diǎn)擊按鈕Check,Kotlin在線編譯器會(huì)自動(dòng)調(diào)取單元測(cè)試,然后輸出單元測(cè)試結(jié)果。沒(méi)事別點(diǎn)擊第三個(gè)按鈕Show answer。題目中的超鏈接function syntax記得有空多看看,全面了解Kotlin Function機(jī)制和特性。