Kotlin之间的区别还在于,Apply,Let,Use,takeIf和Take除非在Kotlin中 [英] difference between kotlin also, apply, let, use, takeIf and takeUnless in Kotlin

查看:684
本文介绍了Kotlin之间的区别还在于,Apply,Let,Use,takeIf和Take除非在Kotlin中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我阅读了许多有关这些物品的Kotlin文档.但是我听不清楚.

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

详细介绍Kotlin let takeIf takeUnless 的用途?

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.

推荐答案

同时,应用,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类似,提供了扩展类的能力 具有新功能,而不必从类继承或使用 任何类型的设计模式,例如Decorator.这是通过特殊方式完成的 声明称为扩展. 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中的Interface一样.但是在Kotlin中,可以将lambda函数作为参数传递给函数.

Lambda functions are just like Interface in Java. But in Kotlin, lambda functions can be passed as a parameter in functions.

示例:

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"
    println(phoneNumber.isNumber {
        println("Block executed")
    })

上面的功能将这样打印,

The above function will print like this,

Block executed
true

我希望,现在您对扩展函数和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可以是任何类似于String类的对象.因此您可以对任何对象调用此函数.

T could be any object like String class. so you can invoke this function with any objects.

block: (T) -> R

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

In parameter of let, you can see the above lambda function. Also, 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 function 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只有与regex匹配的字符串才为phoneNumber.否则,它将为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相反.

It is the reverse of 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之间的区别还在于,Apply,Let,Use,takeIf和Take除非在Kotlin中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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