Kotlin 中的 kotlin 也、apply、let、use、takeIf 和 takeUnless 之间的区别 [英] difference between kotlin also, apply, let, use, takeIf and takeUnless in Kotlin

查看:105
本文介绍了Kotlin 中的 kotlin 也、apply、let、use、takeIf 和 takeUnless 之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我阅读了许多关于这些项目的 Kotlin 文档.但我看不清楚.

I read many Kotlin documents about these items. But I can't understand so clearly.

Kotlin letalsotakeIftakeUnless 的详细用法是什么?

What is the use of Kotlin let, also, takeIf and takeUnless in detail?

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

I need an example of each item. Please don't post the Kotlin documentation. I need a real-time example and use cases of these items.

推荐答案

let,also, apply, takeIf, takeUnless 是 Kotlin 中的扩展函数.

let, also, apply, takeIf, takeUnless are extension functions in Kotlin.

要理解这些函数,您必须了解 Kotlin 中的扩展函数Lambda 函数.

To understand these function you have to understand Extension functions and Lambda functions in Kotlin.

扩展功能:

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

By the use of extension function, we can create a function for a class without inheriting a class.

Kotlin,类似于 C# 和 Gosu,提供了扩展类的能力具有新功能而不必从类继承或使用任何类型的设计模式,例如装饰器.这是通过特殊的称为扩展的声明.Kotlin 支持扩展函数和扩展属性.

Kotlin, similar to C# and Gosu, provides the ability to extend a class with new functionality without having to inherit from the class or use any type of design pattern such as Decorator. This is done via special declarations called extensions. Kotlin supports extension functions and extension properties.

因此,要查找 String 中是否只有数字,您可以创建如下所示的方法,而无需继承 String 类.

So, to find if only numbers in the String, you can create a method like below without inheriting String class.

fun String.isNumber(): Boolean = this.matches("[0-9]+".toRegex())

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

you can use the above extension function like this,

val phoneNumber = "8899665544"
println(phoneNumber.isNumber())

打印true.

Lambda 函数:

Lambda 函数就像 Java 中的接口(只包含一个方法.也称为单一抽象方法).Java 8 中的 lambda 表达式也可以在 Java 中使用.在 Kotlin 中,lambda 无处不在.大多数情况下,lambdas 在函数中作为参数传递.

Lambda functions are just like Interface (contains only one method. Also called as Single Abstract Method) in Java. From Java 8 lambdas are available in java also. In Kotlin lambdas used everywhere. Mostly lambdas passed as a parameter in a function.

示例:

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

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

You can see, the block is a lambda function and it is passed as a parameter. You can use the above function like this,

val phoneNumber = "8899665544"
phoneNumber.isNumber {
   println("Block executed")
}

上面的函数会这样打印,

The above function will print like this,

Block executed

我希望,现在您对扩展函数和 Lambda 函数有所了解.现在我们可以一一进入扩展功能.

I hope, now you got an idea about Extension functions and Lambda functions. Now we can go to Extension functions one by one.

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

上述函数中使用了两种类型 T 和 R.

Two Types T and R used in the above function.

T.let

T 可以是任何对象,如字符串、数字或任何类型.所以你可以用任何对象调用这个函数.

T could be any object like String, number, or any type. So you can invoke this function with any objects.

block: (T) -> R

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

You can see the lambda function In parameter of let the invoking object is passed as a parameter of the function. So you can use the invoking class object inside the function. then it returns the R (another object).

示例:

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

在上面的例子中,让 String 作为其 lambda 函数的参数,它返回 Pair 作为回报.

In above example let takes String as a parameter of its lambda function and it returns Pair in return.

同理,其他扩展功能也一样.

In the same way, other extension functions works.

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

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

extension function also takes the invoking class as a lambda function parameter and returns nothing.

示例:

val phoneNumber = "8899665544"
phoneNumber.also { number ->
    println(number.contains("8"))
    println(number.length)
 }

申请

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

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

Same as also but the same invoking object passed as the function so you can use the functions and other properties without calling it or parameter name.

示例:

val phoneNumber = "8899665544"
phoneNumber.apply { 
    println(contains("8"))
    println(length)
 }

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

You can see in the above example the functions of String class directly invoked inside the lambda funtion.

takeIf

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

示例:

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

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

In above example number will have a string of phoneNumber only it matches the regex. Otherwise, it will be null.

除非

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

与takeIf相反.

示例:

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

number 只有在不匹配 regex 时才会有一个 phoneNumber 字符串.否则,它将是 null.

number will have a string of phoneNumber only if not matches the regex. Otherwise, it will be null.

这篇关于Kotlin 中的 kotlin 也、apply、let、use、takeIf 和 takeUnless 之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆