Kotlin之let,apply,with,run函數(shù)區(qū)別
重新整理
重新整理了各種函數(shù)的區(qū)別,請移步到這里。
以下作廢
很長一段時間內(nèi)都一直使用Kotlin這門語言,也只是純粹使用簡單語法,最近有時候寫的代碼,編輯器自動提示使用let等函數(shù)革半,然后就專門花點時間研究了下。
let
首先let()的定義是這樣的流码,默認當前這個對象作為閉包的it參數(shù)又官,返回值是函數(shù)里面最后一行,或者指定return
fun <T, R> T.let(f: (T) -> R): R = f(this)
簡單示例:
fun testLet(): Int {
// fun <T, R> T.let(f: (T) -> R): R { f(this)}
"testLet".let {
println(it)
println(it)
println(it)
return 1
}
}
//運行結果
//testLet
//testLet
//testLet
可以看看最后生成的class文件漫试,代碼已經(jīng)經(jīng)過格式化了赏胚,編譯器只是在我們原先的變量后面添加了let里面的內(nèi)容。
public static final int testLet() {
String str1 = "testLet";
String it = (String)str1;
int $i$a$1$let;
System.out.println(it);
System.out.println(it);
System.out.println(it);
return 1;
}
來個復雜一定的例子
fun testLet(): Int {
// fun <T, R> T.let(f: (T) -> R): R { f(this)}
"testLet".let {
if (Random().nextBoolean()) {
println(it)
return 1
} else {
println(it)
return 2
}
}
}
編譯過后的class文件
public static final int testLet() {
String str1 = "testLet";
String it = (String)str1;
int $i$a$1$let;
if (new Random().nextBoolean())
{
System.out.println(it);
return 1;
}
System.out.println(it);
return 2;
}
apply
apply函數(shù)是這樣的商虐,調(diào)用某對象的apply函數(shù)觉阅,在函數(shù)范圍內(nèi),可以任意調(diào)用該對象的任意方法秘车,并返回該對象
fun <T> T.apply(f: T.() -> Unit): T { f(); return this }
代碼示例
fun testApply() {
// fun <T> T.apply(f: T.() -> Unit): T { f(); return this }
ArrayList<String>().apply {
add("testApply")
add("testApply")
add("testApply")
println("this = " + this)
}.let { println(it) }
}
// 運行結果
// this = [testApply, testApply, testApply]
// [testApply, testApply, testApply]
編譯過后的class文件
public static final void testApply()
{
ArrayList localArrayList1 = new ArrayList();
ArrayList localArrayList2 = (ArrayList)localArrayList1;
int $i$a$1$apply;
ArrayList $receiver;
$receiver.add("testApply");
$receiver.add("testApply");
$receiver.add("testApply");
String str = "this = " + $receiver;
System.out.println(str);
localArrayList1 = localArrayList1;
ArrayList it = (ArrayList)localArrayList1;
int $i$a$2$let;
System.out.println(it);
}
with
with函數(shù)是一個單獨的函數(shù)典勇,并不是Kotlin中的extension,所以調(diào)用方式有點不一樣叮趴,返回是最后一行割笙,然后可以直接調(diào)用對象的方法,感覺像是let和apply的結合。
fun <T, R> with(receiver: T, f: T.() -> R): R = receiver.f()
代碼示例:
fun testWith() {
// fun <T, R> with(receiver: T, f: T.() -> R): R = receiver.f()
with(ArrayList<String>()) {
add("testWith")
add("testWith")
add("testWith")
println("this = " + this)
}.let { println(it) }
}
// 運行結果
// this = [testWith, testWith, testWith]
// kotlin.Unit
class文件
public static final void testWith()
{
Object localObject = new ArrayList();
ArrayList localArrayList1 = (ArrayList)localObject;
int $i$a$1$with;
ArrayList $receiver;
$receiver.add("testWith");
$receiver.add("testWith");
$receiver.add("testWith");
String str = "this = " + $receiver;
System.out.println(str);
localObject = Unit.INSTANCE;
Unit it = (Unit)localObject;
int $i$a$2$let;
System.out.println(it);
}
run
run函數(shù)和apply函數(shù)很像伤溉,只不過run函數(shù)是使用最后一行的返回般码,apply返回當前自己的對象。
fun <T, R> T.run(f: T.() -> R): R = f()
代碼示例
fun testRun() {
// fun <T, R> T.run(f: T.() -> R): R = f()
"testRun".run {
println("this = " + this)
}.let { println(it) }
}
// 運行結果
// this = testRun
// kotlin.Unit
class文件
public static final void testRun()
{
Object localObject = "testRun";
String str1 = (String)localObject;
int $i$a$1$run;
String $receiver;
String str2 = "this = " + $receiver;
System.out.println(str2);
localObject = Unit.INSTANCE;
Unit it = (Unit)localObject;
int $i$a$2$let;
System.out.println(it);
}
總結
怎么樣乱顾,是不是看暈了板祝,沒關系,我們來總結下走净。
函數(shù)名 | 定義 | 參數(shù) | 返回值 | extension | 其他 |
---|---|---|---|---|---|
let | fun <T, R> T.let(f: (T) -> R): R = f(this) | it | 閉包返回 | 是 | |
apply | fun <T> T.apply(f: T.() -> Unit): T { f(); return this } | 無券时,可以使用this | this | 是 | |
with | fun <T, R> with(receiver: T, f: T.() -> R): R = receiver.f() | 無,可以使用this | 閉包返回 | 否 | 調(diào)用方式與其他不同 |
run | fun <T, R> T.run(f: T.() -> R): R = f() | 無伏伯,可以使用this | 閉包返回 | 是 |