少年恃险若平地,独倚长剑凌清秋。这篇文章主要讲述Kotlin之let,apply,with,run函数区别(转)相关的知识,希望能为你提供帮助。
转自:https://blog.csdn.net/guijiaoba/article/details/54615036
Kotlin之let,apply,with,run函数区别重新整理重新整理了各种函数的区别,请移步到这里。
以下作废
很长一段时间内都一直使用Kotlin这门语言,也只是纯粹使用简单语法,最近有时候写的代码,编辑器自动提示使用let等函数,然后就专门花点时间研究了下。
let首先let()的定义是这样的,默认当前这个对象作为闭包的it参数,返回值是函数里面最后一行,或者指定return
fun <
T, R>
T.let(f: (T) ->
R): R = f(this)
- 1
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
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
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;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
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
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
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;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
fun <
T>
T.apply(f: T.() ->
Unit): T { f();
return this }
- 1
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]
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
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);
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
fun <
T, R>
with(receiver: T, f: T.() ->
R): R = receiver.f()
- 1
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
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
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);
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
fun <
T, R>
T.run(f: T.() ->
R): R = f()
- 1
fun testRun() {
// fun <
T, R>
T.run(f: T.() ->
R): R = f()
"testRun".run {
println("this = " + this)
}.let { println(it) }
}
// 运行结果
// this = testRun
// kotlin.Unit
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
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);
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
函数名 | 定义 | 参数 | 返回值 | extension | 其他 |
---|---|---|---|---|---|
let | fun T.let(f: (T) -> R): R = f(this) | it | 闭包返回 | 是 | |
apply | fun T.apply(f: T.() -> Unit): T { f(); return this } | 无,可以使用this | this | 是 | |
with | fun with(receiver: T, f: T.() -> R): R = receiver.f() | 无,可以使用this | 闭包返回 | 否 | 调用方式与其他不同 |
run | fun T.run(f: T.() -> R): R = f() | 无,可以使用this | 闭包返回 | 是 |
推荐阅读
- 支付宝支付之App支付
- xcrun: error: unable to find utility "PackageApplication", not a developer tool or in PATH
- Android开发中常见的设计模式——单例模式
- ARC-100C - Linear Approximation
- spring基础----事件(Applicaition Event)
- Appium-实现手势密码登陆
- 012_call和apply区别
- ApplicationListener接口的生命周期
- CS:APP CH01.A Tour of Computer Systems