内置函数学习笔记apply ,let,run,with,also###
/**
* DESC :
*
* 内置函数的总结:
*
* 1.apply (info.apply)
* 1.1 apply函数返回永远都是info本身, 此条和 also 一样
* 1.2 apply函数的匿名函数里持有的是this ==info本身 此条和 run 一样
*
* 2. let函数 (info.let)
* 2.1 let函数返回类型,是根据匿名函数里最后一行的变化而变化 此条和 run 一样
* 2.1 let函数的匿名函数里持有的是it == info本身 此条和 also一样
*
* 3.run函数 (info.run)
* 3.1 run函数返回类型,是根据匿名函数里最后一行的变化而变化 此条和 let 一样
* 3.2 run函数的匿名函数里持有的是this ==info本身 此条和 apply 一样
*
* 4.with函数 with(info, 参数二),with和run基本上一样,只不过使用的时候不同
* 4.1 with函数返回类型,是根据匿名函数里最后一行的变化而变化 此条和 let 一样
* 4.2 with函数的匿名函数里持有的是this ==info本身 此条和 apply 一样
*
* 5. also函数 (info.also)
* 5.1 also函数返回永远都是info本身, 此条和 apply 一样
* 5.2 also函数的匿名函数里持有的是it == info本身 此条和 let 一样
*/
const val TAG = "KtBaseInternalFunTest01"
class KtBaseInternalFunTest01 {
/**
* TODO
* apply 特点:apply始终返回info本身,String类型
*/
fun testApply01() {
val info = "Hello Lanny"
Log.d(TAG, "testApply01==>info length=${info.length}"
+", info last=${info[info.length - 1]}"
+", info toLowercase=${info.toLowerCase()}")
/**
* apply 内置函数的方式
* apply 特点:apply始终返回info本身,String类型
*/
info.apply {
//一般大部分情况下,匿名函数都会持有一个it,但是apply函数不会持有it,却会持有当前的this,this==info本身
Log.d(TAG, "testApply01==>info length=${this.length}"
+", info last=${this[this.length - 1]}"
+", info toLowercase=${this.toLowerCase()}")
}
//TODO 真正使用apply函数规则如下:
//apply始终返回info本身,所以可以链式调用(类似builder建造模式)
info.apply {
Log.d(TAG, "testApply01==>info length=${this.length}")
}.apply {
Log.d(TAG, "info last=${this[this.length - 1]}")
}.apply {
Log.d(TAG,"info toLowercase=${this.toLowerCase()}")
}
}
/**
* TODO
* let特点:函数的最后一行作为返回值,但apply函数却是返回调用者本身
*
*/
fun testLet01() {
//普通方式,对集合中第一个和最后一个元素相加
val list = listOf<Int>(1,2,3,4,5,6)
val firstValue = list.first()//第一个元素
val lastValue = list.last()
val result = firstValue + lastValue
Log.d(TAG, "testLet01==>result="+result)
//let函数方式,
val result2 /*: Int*/ = list.let {
//持有的it == list集合
Log.d(TAG, "testLet01=${it}")//[1, 2, 3, 4, 5, 6]
Log.d(TAG, "testLet01=${it.first() + it.last()}")//7
it.first() + it.last()//let特点:函数的最后一行作为返回值,但apply函数却是返回调用者本身
//true 如果最后一行返回true,那么函数返回Boolean类型
}
Log.d(TAG, "testLet01==>result2="+result2)
}
fun testLet02() {
Log.d(TAG, "letMethod01="+letMethod01(/*"hello world"*/""))
Log.d(TAG, "letMethod02="+letMethod02("hello world"))
Log.d(TAG, "letMethod03="+letMethod03("hello world"))
Log.d(TAG, "letMethod04="+letMethod04(/*"hello world"*/""))
}
//普通方式1
fun letMethod01(info : String?) : String {
return if (info == null) "info is null" else "info=${info}"
}
//普通方式2
fun letMethod02(info :String ?) : String = if (info == null) "info is null" else "info=${info}"
//let函数 + 空合并操作符 ?: 对值判空并返回, (写法一)
fun letMethod03(info : String ?) : String {
return info?.let {
if (!it.isBlank()) "info=${it}" else "info is blank"
} ?: "info is null"
}
//let函数 + 空合并操作符 ?: 对值判空并返回,(写法二,简化版本)
fun letMethod04(info : String ?) : String = info?.let {
if (!it.isBlank()) "info=${it}" else "info is blank"
} ?: "info is null"
/**
* TODO
* 1.run函数特点:字符串延时
* 2.具名函数判断长度,isLong
* 3.具名函数检测合格,showText
* 4.具名函数增加一个括号,mapText
* 5.具名函数输出内容
*/
fun testRun01() {
val info = "hello lance"
//1.匿名函数配合run
info.run {
//this == info本身
Log.d(TAG, "testRun01==>${this}")//hello lance
}
//2.具名函数配合run
//info.run(::具名函数)
val result : String = info
.run(::isLong)
.run(::showText)
.run(::mapText)
Log.d(TAG, "testRun01==>result="+result)//result=[字符串合格]
//3.匿名函数配合run
val result2 = info.run {
//本次执行的结果Boolean类型, 作为下一个run函数的参数
if (info.length > 5) true else false
}.run {
//本次执行的结果String类型, 作为下一个run函数的参数
if (this) "字符串合格" else "字符串不合格"
}.run {
"[${this}]"
}
Log.d(TAG, "testRun01==>result2="+result2)//[字符串合格]
//下面用let函数也可以实现
val result3 =
info.let(::isLong)
.let(::showText)
.let(::mapText)
Log.d(TAG, "testRun01==>result3="+result3)//[字符串合格]
}
//具名函数isLong
fun isLong(info : String) = if(info.length > 5) true else false
//具名函数showText
fun showText(isLong : Boolean) : String = if (isLong) "字符串合格" else "字符串不合格"
//具名函数mapText
fun mapText(info: String) = "[${info}]"
/**
* TODO
* with函数特点:
* with(参数1:info,参数二:函数)
*/
fun testWith01() {
val info = "hello lanny"
// info.with()//无法直接这样调用with()函数,
/*with(info) {
//this == info本身
}*/
//with具名函数操作
val r1 /* : Int */= with(info, ::getInfoLength)//第一次执行的结果r1:Int作为下一次的参数
val r2 /* : String */= with(r1, ::printInfoLength)//第二次执行的结果r2:String作为下一次的参数
val r3 /* : String*/ = with(r2, ::mapInfo)
Log.d(TAG, "testWith01==>r1="+r1
+", r2="+r2
+", r3="+r3)//r1=11, r2=info长度为11, r3=[info长度为11]
//with匿名函数操作
val r4 = with(with(with(info) {
length
}) {
"info 长度:${this}"
}) {
"[${this}]"
}
Log.d(TAG, "testWith01==>r4="+r4)//[info 长度:11]
}
fun getInfoLength(info : String): Int {
return info.length
}
fun printInfoLength(length : Int) : String {
return "info长度为${length}"
}
fun mapInfo(info : String) : String {
return "[${info}]"
}
/**
* TODO
* also函数
*/
fun testAlso01() {
val info = "have a good time"
info.also {
//持有一个it==info本身
Log.d(TAG, "testAlso01==>it=${it}")
}
/**
* also函数特点:返回info本身
*/
val r1 = info.also {
99
true
"哈哈哈"
'A'
"88.8f"
}
Log.d(TAG, "testAlso01==>r1="+r1)
/**
* TODO 真正使用also函数的规则如下:
* info.also函数返回info本身,所以可以链式调用
*
* info=have a good time
* info=[have a good time]
* info finish
*/
info.also {
Log.d(TAG, "info=${info}")
}.also {
Log.d(TAG, "info=[${info}]")
}.also {
Log.d(TAG, "info finish")
}
}
}
网友评论