Kotlin 語法基礎(chǔ)大全,從例子著手的 從0到1的學(xué)習(xí) -- 基礎(chǔ)介紹
Kotlin 語法基礎(chǔ)大全油啤,從例子著手的 從0到1的學(xué)習(xí) -- 流程控制
Kotlin 語法基礎(chǔ)大全评凝,從例子著手的 從0到1的學(xué)習(xí) -- 特殊的類
Kotlin 語法基礎(chǔ)大全,從例子著手的 從0到1的學(xué)習(xí) -- 函數(shù)
Kotlin 語法基礎(chǔ)大全外邓,從例子著手的 從0到1的學(xué)習(xí) -- 集合
Kotlin 語法基礎(chǔ)大全撤蚊,從例子著手的 從0到1的學(xué)習(xí) -- 作用域
Kotlin 語法基礎(chǔ)大全,從例子著手的 從0到1的學(xué)習(xí) -- 代理
Kotlin 語法基礎(chǔ)大全损话,從例子著手的 從0到1的學(xué)習(xí) -- 產(chǎn)品級特性
翻譯來源
let
let 是一個kotlin 的標(biāo)準(zhǔn)庫侦啸,可以用于實例檢查和null檢查,當(dāng)let被調(diào)用再一個實例上的時候丧枪,你可以通過it 來再作用域內(nèi)訪問這個實例光涂。作用域內(nèi)的最后一句表達式,將會作為返回值返回
fun customPrint(s: String) {
print(s.toUpperCase())
}
fun main() {
val empty = "test".let { // 1
customPrint(it) // 2
it.isEmpty() // 3
}
println(" is empty: $empty")
fun printNonNull(str: String?) {
println("Printing \"$str\":")
str?.let { // 4
print("\t")
customPrint(it)
println()
}
}
printNonNull(null)
printNonNull("my string")
}
/*
TEST is empty: false
Printing "null":
Printing "my string":
MY STRING
*/
- 1 在“test”上執(zhí)行這一個代碼塊拧烦,并且返回一個值作為結(jié)果
- 2 調(diào)用一個方法忘闻,通過it訪問到“test”實例
- 3 返回let代碼塊的返回值
- 4 安全調(diào)用let代碼塊,只有當(dāng)str不為空是才會執(zhí)行l(wèi)et代碼塊恋博。
run
run 和let 很相似齐佳,執(zhí)行一個代碼塊私恬,返回一個結(jié)果。兩者不同的是 let中使用it 來訪問外部調(diào)用實例炼吴,而run中使用this 來訪問外部調(diào)用實例本鸣,這在嗲用外部調(diào)用實例的方法是會提供一些便捷。
fun main() {
fun getNullableLength(ns: String?) {
println("for \"$ns\":")
ns?.run { // 1
println("\tis empty? " + isEmpty()) // 2
println("\tlength = $length")
length // 3
}
}
getNullableLength(null)
getNullableLength("")
getNullableLength("some string with Kotlin")
}
/*
for "null":
for "":
is empty? true
length = 0
for "some string with Kotlin":
is empty? false
length = 23
*/
- 1 在一個可控變量上執(zhí)行一個代碼塊
- 2 不需要顯示的調(diào)用this.xxx 直接調(diào)用 isempty缺厉, 這里就相當(dāng)于調(diào)用 this.isEmpty()
- 3 返回this.length
with
class Configuration(var host: String, var port: Int)
fun main() {
val configuration = Configuration(host = "127.0.0.1", port = 9000)
with(configuration) {
println("$host:$port")
}
// instead of:
println("${configuration.host}:${configuration.port}")
}
/*
127.0.0.1:9000
127.0.0.1:9000
*/
with是一個非擴展方法永高,在with的作用域里,你可以簡潔地訪問到參數(shù)地成員提针,意思就是在作用域里面你可以隱藏實例地名字當(dāng)訪問屬性地時候命爬。
apply
apply 在一個實例上運行一段代碼,讓后將返回這個實例辐脖。在代碼塊中饲宛,實例可以用this來代替訪問。這個apply嗜价,可以簡單地用于初始化實例艇抠。
data class Person(var name: String, var age: Int, var about: String) {
constructor() : this("", 0, "")
}
fun main() {
val jake = Person() // 1
val stringDescription = jake.apply { // 2
name = "Jake" // 3
age = 30
about = "Android developer"
}.toString() // 4
println(stringDescription)
}
//Person(name=Jake, age=30, about=Android developer)
- 1 創(chuàng)建一個實例
- 2 調(diào)用一個apply 代碼塊
- 3 修改屬性
- 4 apply返回地還是 這個實例,在這里可以調(diào)用 toString(), 這樣你就可以鏈?zhǔn)降卣{(diào)用久锥。
also
also 和apply的工作方式很像家淤,他執(zhí)行了一個代碼塊,然后返回這個實例瑟由。在代碼塊中絮重,你需要使用it來訪問這個實例,這是和apply不同的地方歹苦。所以這更像是用來執(zhí)行一個額外的工作青伤。比如在調(diào)用鏈中打印一些東西
data class Person(var name: String, var age: Int, var about: String) {
constructor() : this("", 0, "")
}
fun writeCreationLog(p: Person) {
println("A new person ${p.name} was created.")
}
fun main() {
val jake = Person("Jake", 30, "Android developer") // 1
.also { // 2
writeCreationLog(it) // 3
}
}
//A new person Jake was created.
- 1 創(chuàng)建一個帶有屬性值的 Person
- 2 調(diào)用also代碼塊,代碼塊的返回值就是他自己
- 3 調(diào)用一個方法殴瘦,打印一些內(nèi)容