Kotlin笔记 常用内置函数(十二)

文章目录

      • 内置函数
        • 1、apply函数
        • 2、let函数
        • 3、run函数
        • 4、with函数
        • 5、also函数
        • 6、takeIf 函数

内置函数

内置函数所在类 StandardKt.class 类中

1、apply函数

"Hello".apply {
    // 默认会有 Hello 字符串本身 this== Hello  返回 本身 Hello
    length
    
}

2、let函数

"zyb".let {
	// 参数 it 就是 zyb 本身 ,根据表达式最后一行 确定返回值 
    length // 返回 int类型 

}

3、run函数

"zyb".run {
    // 默认this==zyb  根据表达式最后一行 确定返回值 
    length
}

4、with函数

// 和run函数很像
with("zyb",{
    	// 表达式持有 默认this==zyb 最后一行作为返回值
      length
  })

5、also函数

"zyb".also {
    // also  参数 是it ,返回永远是自己本身
    it.length
}

6、takeIf 函数

// 
"zyb".takeIf {
    // true 返回本身  fase 返回null 
    it.contains("1") 
}

你可能感兴趣的:(Kotlin,kotlin,内置函数)