Let
-
函數(shù)定義
public inline fun <T, R> T.let(block: (T) -> R): R = block(this)
-
函數(shù)說明
Calls the specified function block with this value as its argument and returns its result.
調(diào)用某對(duì)象的let函數(shù)挥下,即將該對(duì)象作為let函數(shù)的參數(shù)撩嚼。在函數(shù)塊內(nèi)阐滩,可以通過it獲取該對(duì)象蒿偎。函數(shù)返回值為函數(shù)塊的最后一行或者指定的return結(jié)果急迂。 -
函數(shù)實(shí)例
/** * 姓名拆分,通過空格拆分出姓和名 */ fun nameSplit(name: String): Boolean { name.let { if (it.indexOf(" ") == -1) return false println("Your first Name is " + it.substring(0, it.indexOf(" "))) println("Your last Name is " + it.substring(it.indexOf(" "))) return true } }
-
測(cè)試函數(shù)
private KotlinUnit mUnit = new KotlinUnit(); @Test public void testLet_0() throws Exception { assertTrue("testLet_0",mUnit.nameSplit("ted xiong")); } //輸出結(jié)果 //Your first Name is ted //Your last Name is xiong
Apply
-
函數(shù)定義
public inline fun <T> T.apply(block: T.() -> Unit): T { block(); return this }
-
函數(shù)說明
Calls the specified function [block] with
this
value as its receiver and returnsthis
value.
調(diào)用某對(duì)象的apply函數(shù)饺饭,在函數(shù)塊內(nèi)惯悠,可以任意調(diào)用該對(duì)象的任意方法卤材,最后返回自身。 -
函數(shù)實(shí)例
/** * 在一個(gè)空的數(shù)組里塞入日期和時(shí)間的字符串 */ fun getDateStr(space: ArrayList<String>): Boolean { space.apply { add(SimpleDateFormat("yyyy-MM-dd", Locale.CHINA).format(Date())) add(SimpleDateFormat.getTimeInstance().format(Date())) }.let { println("The date info is " + it[0]) println("The time info is " + it[1]) } return true }
-
測(cè)試函數(shù)
private KotlinUnit mUnit = new KotlinUnit(); @Test public void testApply_0() throws Exception { ArrayList<String> space = new ArrayList<>(); assertTrue("testApply_0", mUnit.getDateStr(space)); } //輸出結(jié)果 //The date info is 2017-08-16 //The time info is 16:38:47
With
-
函數(shù)定義
public inline fun <T, R> with(receiver: T, block: T.() -> R): R = receiver.block()
-
函數(shù)說明
Calls the specified function [block] with the given [receiver] as its receiver and returns its result.
With跟其他函數(shù)不一樣峦失,并不是變量的擴(kuò)展扇丛,而是單獨(dú)的一個(gè)函數(shù)。將一個(gè)對(duì)象作為參數(shù)傳遞給With函數(shù)尉辑,在函數(shù)塊內(nèi)可以任意調(diào)用該對(duì)象的任意方法晕拆,并且可以通過this獲取到該對(duì)象,差不多算是let和apply的混合材蹬。 -
函數(shù)實(shí)例
/** * 獲取到編譯環(huán)境相關(guān)的系統(tǒng)信息 */ fun getCompileInfo(map: HashMap<String, String>): Boolean { return with(map) { put("Java環(huán)境版本", System.getProperties().getProperty("java.version")) put("Java環(huán)境供應(yīng)商", System.getProperties().getProperty("java.vendor")) put("操作系統(tǒng)名稱", System.getProperties().getProperty("os.name")) put("操作系統(tǒng)架構(gòu)", System.getProperties().getProperty("os.arch")) put("操作系統(tǒng)版本", System.getProperties().getProperty("os.version")) println(this) true } }
-
測(cè)試函數(shù)
private KotlinUnit mUnit = new KotlinUnit(); @Test public void testWith_0() throws Exception { HashMap<String,String> map = new HashMap<>(); assertTrue("testWith_0", mUnit.getCompileInfo(map)); } //輸出結(jié)果 //{操作系統(tǒng)名稱=Mac OS X, Java環(huán)境供應(yīng)商=JetBrains s.r.o, 操作系統(tǒng)架構(gòu)=x86_64, Java環(huán)境版本=1.8.0_112-release, 操作系統(tǒng)版本=10.12.6}
Run
-
函數(shù)定義
public inline fun <T, R> T.run(block: T.() -> R): R = block()
-
函數(shù)說明
Calls the specified function [block] with
this
value as its receiver and returns its result.
Run 跟Apply近似实幕,唯一不同的是Apply返回的是自身對(duì)象,而Run返回的是最后一行 -
函數(shù)實(shí)例
/** * 接收一個(gè)姓名堤器,打印三句話 */ fun sayHiTo(name: String): Boolean { println(name.run { println("Hi," + name) println("Nice to meet you," + this) "The run has over!!!!" }) return true }
-
測(cè)試函數(shù)
private KotlinUnit mUnit = new KotlinUnit(); @Test public void testRun_0() throws Exception { assertTrue("testRun_0", mUnit.sayHiTo("xiongwei")); } //輸出結(jié)果 //Hi,xiongwei //Nice to meet you,xiongwei //The run has over!!!!
Also
-
函數(shù)定義
@SinceKotlin("1.1")
public inline fun <T> T.also(block: (T) -> Unit): T { block(this); return this }
```
-
函數(shù)說明
Calls the specified function [block] with
this
value as its argument and returnsthis
value.
Also跟Apply很接近昆庇,唯一一點(diǎn)不同是在函數(shù)塊內(nèi)Also可以使用it來指向?qū)ο蟊颈旧恚鳤pply不行闸溃。 -
函數(shù)實(shí)例
/** * 計(jì)算圓周長(zhǎng) */ fun getCircumference(dia: Int): Boolean { println("The dia is " + dia.also { println("The Circumference is " + Math.PI * it) } ) return true }
-
測(cè)試函數(shù)
private KotlinUnit mUnit = new KotlinUnit(); @Test public void testAlso_0() throws Exception { assertTrue("testAlso_0", mUnit.getCircumference(10)); } //輸出結(jié)果 //The Circumference is 31.41592653589793 //The dia is 10
總結(jié)
function | des | this | it | return |
---|---|---|---|---|
Let | 對(duì)象擴(kuò)展函數(shù)整吆,通過it獲取對(duì)象本身 | 所屬class | 對(duì)象本身 | 指定return或者最后一行 |
Apply | 對(duì)象擴(kuò)展函數(shù),在函數(shù)塊類調(diào)用對(duì)象的任意方法 | 對(duì)象本身 | - | 對(duì)象本身 |
With | 獨(dú)立函數(shù)辉川,將對(duì)象作為參數(shù)傳入表蝙, | 對(duì)象本身 | - | 指定return或者最后一行 |
Run | 對(duì)象擴(kuò)展函數(shù),類似Apply乓旗,不同之處在于Apply返回自身 | 對(duì)象本身 | - | 指定return或者最后一行 |
Also | 對(duì)象擴(kuò)展函數(shù)府蛇,類似Apply,比Apply多了it | 所屬class | 對(duì)象本身 | 對(duì)象本身 |
參考文章:
https://medium.com/@tpolansk/the-difference-between-kotlins-functions-let-apply-with-run-and-else-ca51a4c696b8
項(xiàng)目源碼:https://git.tedxiong.com/TedXiong/KotlinSample