Kotlin修炼指南
Posted Android开发中文站
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Kotlin修炼指南相关的知识,希望对你有一定的参考价值。
Kotlin修炼指南
作用域函数
run
let
apply
also
takeIf
takeUnless
with
repeat
class TestBean {
var name: String = "xuyisheng"
var age: Int = 18
}
fun main(args: Array<String>) {
val test = TestBean()
val resultRun = test.run {
name = "xys"
age = 3
println( "Run内部 $this")
age
}
println( "run返回值 $resultRun")
val resultLet = test.let {
it.name = "xys"
it.age = 3
println( "let内部 $it")
it.age
}
println( "let返回值 $resultLet")
val resultApply = test.apply {
name = "xys"
age = 3
println( "apply内部 $this")
age
}
println( "apply返回值 $resultApply")
val resultAlso = test.also {
it.name = "xys"
it.age = 3
println( "also内部 $it")
it.age
}
println( "also返回值 $resultAlso")
val resultWith = with(test) {
name = "xys"
age = 3
println( "with内部 $this")
age
}
println( "with返回值 $resultWith")
test.age = 33
val resultTakeIf = test.takeIf {
it.age > 3
}
println( "takeIf $resultTakeIf")
val resultTakeUnless = test.takeUnless {
it.age > 3
}
println( "takeUnless $resultTakeUnless")
}
Run内部 TestBean@ 27c170f0
run返回值 3
let内部 TestBean@ 27c170f0
let返回值 3
apply内部 TestBean@ 27c170f0
apply返回值 TestBean@ 27c170f0
also内部 TestBean@ 27c170f0
also返回值 TestBean@ 27c170f0
with内部 TestBean@ 27c170f0
with返回值 3
takeIf TestBean@ 27c170f0
takeUnless null
顶级函数使用场景
run
fun testRun() {
var str = "I am xys"
run {
val str = "I am zj"
println(str) // I am xys
}
println(str) // I am zj
}
repeat
repeat( 5){
print( "repeat")
}
with
with(ArrayList<String>()) {
add( "a")
add( "b")
add( "c")
println( "this = " + this)
this
}
拓展函数使用场景
?.结合拓展函数
// 对result进行了判空并bindData
result?. let {
if (it.isNotEmpty()) {
bindData(it)
}
}
简化对象的创建
// 使用普通的方法创建一个Fragment
fun createInstance(args: Bundle) : MyFragment {
val fragment = MyFragment()
fragment.arguments = args
return fragment
}
// 通过apply来创建一个Fragment
fun createInstance(args: Bundle)
= MyFragment().apply { arguments = args }
// 使用普通的方法创建Intent
fun createIntent(intentData: String, intentAction: String): Intent {
val intent = Intent()
intent.action = intentAction
intent. data = Uri.parse(intentData)
return intent
}
// 通过apply函数的链式调用创建Intent
fun createIntent(intentData: String, intentAction: String) =
Intent().apply { action = intentAction }
.apply { data = Uri.parse(intentData) }
// 正常方法
fun makeDir(path: String): File {
val result = File(path)
result.mkdirs()
return result
}
// 改进方法
fun makeDir(path: String)
= path.let{ File(it) }.also{ it.mkdirs() }
同一对象的多次操作
val linearLayout = LinearLayout(itemView.context).apply {
orientation = LinearLayout.VERTICAL
layoutParams = LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT)
}
progressBar.apply {
progress = newProgress
visibility = if (newProgress in 1. .99) View.VISIBLE else View.GONE
}
stringResult?. let {
nonNullString ->
println( "The non null string is $nonNullString")
}
条件操作
url = intent.getStringExtra(EXTRA_URL)?.takeIf { it.isNotEmpty() } ?: run {
toast( "url空")
activity.finish()
}
test.takeIf { it.name.isNotEmpty() }?.also { print( "name is $it.name") } ?: print( "name empty")
链式调用
test.also {
// todo something
}.apply {
// todo something
}.name = "xys"
val original = "abc"
// 改变值并且传递到下一链条
original.let {
println( "The original String is $it") // "abc"
it.reversed() // 改变参数并且传递到下一链条
}.let {
println( "The reverse String is $it") // "cba"
it.length // 改变参数类型并且传递到下一链条
}.let {
println( "The length of the String is $it") // 3
}
original.also {
println( "The original String is $it") // "abc"
it.reversed() // 即使我们改变它,也是没用的
}.also {
println( "The reverse String is ${it}") // "abc"
it.length // 即使我们改变它,也是没用的
}.also {
println( "The length of the String is ${it}") // "abc"
}
国际惯例
also & apply
test?.also {
println( "some log")
}?.apply {
name = "xys"
}
let & run
作者:xuyisheng
链接:https://juejin.im/post/5d9e85ae51882509751b172a
以上是关于Kotlin修炼指南的主要内容,如果未能解决你的问题,请参考以下文章