1. TODO
方法声明:
@kotlin.internal.InlineOnly
public inline fun TODO(): Nothing = throw NotImplementedError()
方法使用:
fun test(){
TODO("Not implemented")
}
方法说明:有点类似于java的//todo
,但是不同的是,因为它的方法告诉我们,它会抛出一个异常,也就是上面这个方法会导致程序崩溃。它的好处是:配合IDE自动声明的TODO, 会强制开发者去实现这个TODO或者删除它。
2.also
方法声明:
@kotlin.internal.InlineOnly
@SinceKotlin("1.1")
public inline fun <T> T.also(block: (T) -> Unit): T { block(this); return this }
方法使用:
var tvDisplay: TextView = ...
tvDisplay.also{ textView->
textView.textSize = 13
textView.textColor = Color.WHITE
}.setText("3333")
方法说明:also方法有点类似于Builder模式,如上它的好处是明显的,可以使得代码阅读性很强
3. apply
方法声明:
public inline fun <T> T.apply(block: T.() -> Unit): T { block(); return this }
方法使用:
var tvDisplay: TextView = ...
tvDisplay.apply{
textSize = 13
textColor = Color.WHITE
}.setText("3333")
方法说明:和also方法一样
4. run
方法声明:
@kotlin.internal.InlineOnly
public inline fun <T, R> T.run(block: T.() -> R): R = block()
方法使用:
val items = listOf(
"America" to listOf("Brazil", "Canada", "United States"),
"Asia" to listOf("China", "India", "Japan"),
"Europe" to listOf("France", "Germany", "Spain", "United Kingdom"))
var b = items.run {
var m = get(0).first
var n = get(0).second
m + n
}
println(b)
//输出结果:America[Brazil, Canada, United States]
方法说明:对items对象,进行run方法,然后转换成另外一个对象R
5.with
方法声明:
@kotlin.internal.InlineOnly
public inline fun <T, R> with(receiver: T, block: T.() -> R): R = receiver.block()
方法使用:
val items = listOf(
"America" to listOf("Brazil", "Canada", "United States"),
"Asia" to listOf("China", "India", "Japan"),
"Europe" to listOf("France", "Germany", "Spain", "United Kingdom"))
var b = with(items) {
var m = get(0).first
var n = get(0).second
m + n
}
println(b)
方法说明:和run方法一样,不一样的是: with是 (T), run是T.()
6. let
方法声明:
@kotlin.internal.InlineOnly
public inline fun <T, R> T.let(block: (T) -> R): R = block(this)
方法使用:
val items = listOf(
"America" to listOf("Brazil", "Canada", "United States"),
"Asia" to listOf("China", "India", "Japan"),
"Europe" to listOf("France", "Germany", "Spain", "United Kingdom"))
items.let { list->
println(list[0].first)
println(list[0].second)
}
//输出结果:America
//[Brazil, Canada, United States]
7.takeIf
方法声明:
@kotlin.internal.InlineOnly
@SinceKotlin("1.1")
public inline fun <T> T.takeIf(predicate: (T) -> Boolean): T? = if (predicate(this)) this else null
方法使用:
val items = listOf(
"America" to listOf("Brazil", "Canada", "United States"),
"Asia" to listOf("China", "India", "Japan"),
"Europe" to listOf("France", "Germany", "Spain", "United Kingdom"))
var result = items.takeIf{
it[0].first.equals("Asia")
}
println(result)
//输出结果是null,因为it[0].first是"America",所以不满足条件的情况下返回null
8.takeUnless
方法声明:
@kotlin.internal.InlineOnly
@SinceKotlin("1.1")
public inline fun <T> T.takeUnless(predicate: (T) -> Boolean): T? = if (!predicate(this)) this else null
方法使用:
val items = listOf(
"America" to listOf("Brazil", "Canada", "United States"),
"Asia" to listOf("China", "India", "Japan"),
"Europe" to listOf("France", "Germany", "Spain", "United Kingdom"))
var result = items.takeUnless{
it[0].first.equals("Asia")
}
println(result)
//输出结果:[(America, [Brazil, Canada, United States]), (Asia, [China, India, Japan]), (Europe, [France, Germany, Spain, United Kingdom])]
方法说明:刚好和takeIf方法相反
9.repeat
方法声明:
@kotlin.internal.InlineOnly
public inline fun repeat(times: Int, action: (Int) -> Unit) {
for (index in 0..times - 1) {
action(index)
}
}
方法使用:
repeat(9) {
println(it)
}
输出:0 1 2 3 4 5 6 7 8
//重复9次,打印索引