Kotlin之间的区别,应用,让,使用,取出和取消在Kotlin中

Bhu*_* BS 19 android kotlin extension-function

我读了很多关于这些物品的Kotlin文件.但我无法理解这么清楚.

有什么用科特林的,,takeIftakeUnless详细?

我需要每个项目的示例.请不要发布Kotlin文档.我需要一个实时示例并使用这些项目的案例.

Kev*_*tel 37

public inline fun <T, R> T.let(block: (T) -> R): R = block(this)

拿起接收器并将其传递给作为参数传递的函数.返回函数的结果.

val myVar = "hello!"
myVar.let { println(it) } // Output "hello!"
Run Code Online (Sandbox Code Playgroud)

您可以使用letnull安全检查:

val myVar = if (Random().nextBoolean()) "hello!" else null
myVar?.let { println(it) } // Output "hello!" only if myVar is not null
Run Code Online (Sandbox Code Playgroud)

public inline fun <T> T.also(block: (T) -> Unit): T { block(this); return this }

执行以接收器作为参数传递的函数并返回接收器.
它就像让但总是返回接收器,而不是功能的结果.

您可以使用它在对象上执行某些操作.

val person = Person().also {
  println("Person ${it.name} initialized!")
  // Do what you want here...
}
Run Code Online (Sandbox Code Playgroud)

takeIf

public inline fun <T> T.takeIf(predicate: (T) -> Boolean): T? = if (predicate(this)) this else null

如果函数(谓词)返回true,则返回接收者,否则返回null.

println(myVar.takeIf { it is Person } ?: "Not a person!")
Run Code Online (Sandbox Code Playgroud)

takeUnless

public inline fun <T> T.takeUnless(predicate: (T) -> Boolean): T? = if (!predicate(this)) this else null

相同takeIf,但谓词反转.如果为true,则返回null,否则返回接收者.

println(myVar.takeUnless { it is Person } ?: "It's a person!")
Run Code Online (Sandbox Code Playgroud)

救命


Bhu*_* BS 11

让,也,申请,takeIf,takeUnless是Kotlin中的扩展功能.

要理解这些功能,您必须了解Kotlin中的Extension函数Lambda函数.

扩展功能:

通过使用扩展函数,我们可以在不继承类的情况下为类创建函数.

类似于C#和Gosu,Kotlin提供了使用新功能扩展类的能力,而无需继承类或使用任何类型的设计模式,如Decorator.这是通过称为扩展的特殊声明来完成的.Kotlin支持扩展功能和扩展属性.

因此,要查找是否只有数字String,您可以创建一个类似下面的方法而不继承String类.

fun String.isNumber(): Boolean = this.matches("[0-9]+".toRegex())
Run Code Online (Sandbox Code Playgroud)

你可以像这样使用上面的扩展功能,

val phoneNumber = "8899665544"
println(phoneNumber.isNumber)
Run Code Online (Sandbox Code Playgroud)

这是印刷品true.

Lambda函数:

Lambda函数就像Java中的Interface.但是在Kotlin中,lambda函数可以作为函数中的参数传递.

例:

fun String.isNumber(block: () -> Unit): Boolean {
    return if (this.matches("[0-9]+".toRegex())) {
        block()
        true
    } else false
}
Run Code Online (Sandbox Code Playgroud)

你可以看到,块是一个lambda函数,它作为参数传递.您可以像这样使用上面的功能,

val phoneNumber = "8899665544"
    println(phoneNumber.isNumber {
        println("Block executed")
    })
Run Code Online (Sandbox Code Playgroud)

上面的功能会像这样打印,

Block executed
true
Run Code Online (Sandbox Code Playgroud)

我希望,现在你对扩展函数和Lambda函数有所了解.现在我们可以逐个转到扩展功能.

public inline fun <T, R> T.let(block: (T) -> R): R = block(this)
Run Code Online (Sandbox Code Playgroud)

上述功能中使用了两种类型T和R.

T.let
Run Code Online (Sandbox Code Playgroud)

T可以是任何像String类的对象.所以你可以用任何对象调用这个函数.

block: (T) -> R
Run Code Online (Sandbox Code Playgroud)

在let的参数中,您可以看到上面的lambda函数.此外,调用对象作为函数的参数传递.因此,您可以在函数内部使用调用类对象.然后它返回R(另一个对象).

例:

val phoneNumber = "8899665544"
val numberAndCount: Pair<Int, Int> = phoneNumber.let { it.toInt() to it.count() }
Run Code Online (Sandbox Code Playgroud)

在上面的例子中,let将String作为其lambda函数的参数,并返回Pair作为返回值.

以同样的方式,其他扩展功能起作用.

public inline fun <T> T.also(block: (T) -> Unit): T { block(this); return this }
Run Code Online (Sandbox Code Playgroud)

extension函数also将调用类作为lambda函数参数,不返回任何内容.

例:

val phoneNumber = "8899665544"
phoneNumber.also { number ->
    println(number.contains("8"))
    println(number.length)
 }
Run Code Online (Sandbox Code Playgroud)

应用

public inline fun <T> T.apply(block: T.() -> Unit): T { block(); return this }
Run Code Online (Sandbox Code Playgroud)

同样也是相同的调用对象作为函数传递,因此您可以使用函数和其他属性而无需调用它或参数名称.

例:

val phoneNumber = "8899665544"
phoneNumber.apply { 
    println(contains("8"))
    println(length)
 }
Run Code Online (Sandbox Code Playgroud)

您可以在上面的示例中看到在lambda函数中直接调用的String类的函数.

takeIf

public inline fun <T> T.takeIf(predicate: (T) -> Boolean): T? = if (predicate(this)) this else null
Run Code Online (Sandbox Code Playgroud)

例:

val phoneNumber = "8899665544"
val number = phoneNumber.takeIf { it.matches("[0-9]+".toRegex()) }
Run Code Online (Sandbox Code Playgroud)

在上面的例子number中将只有一个phoneNumber匹配的字符串regex.否则,它会null.

takeUnless

public inline fun <T> T.takeUnless(predicate: (T) -> Boolean): T? = if (!predicate(this)) this else null
Run Code Online (Sandbox Code Playgroud)

这与takeIf相反.

例:

val phoneNumber = "8899665544"
val number = phoneNumber.takeUnless { it.matches("[0-9]+".toRegex()) }
Run Code Online (Sandbox Code Playgroud)

number将只有一个字符串,phoneNumber如果不匹配regex.否则,它会null.