kotlin运行_Kotlin允许,运行,也适用于

kotlin运行

In this tutorial, we’ll be implementing some of the important standard library functions available in Kotlin. The kotlin-stdlib provides us with useful higher order functions implementing idiomatic patterns. We’ll see how they make programming in Kotlin so easier and faster.

在本教程中,我们将实现Kotlin中可用的一些重要的标准库函数。 kotlin-stdlib为我们提供了实现惯用模式的有用的高阶函数 。 我们将看到它们如何使Kotlin编程变得如此轻松和快捷。

The functions that we’re going to discuss below are:

我们将在下面讨论的功能是:

  • let

  • run

  • also

  • apply

    应用
  • with

Kotlin让 (Kotlin let)

let takes the object it is invoked upon as the parameter and returns the result of the lambda expression.
Kotlin let is a scoping function wherein the variables declared inside the expression cannot be used outside.

let将被调用的对象作为参数,并返回lambda表达式的结果。
Kotlin let是一个作用域函数,其中在表达式内部声明的变量不能在外部使用。

An example demonstrating kotlin let function is given below.

下面给出了一个演示kotlin let函数的示例。

fun main(args: Array<String>) {
    var str = "Hello World"
    str.let { println("$it!!") }
    println(str)

}
//Prints
//Hello World!!
//Hello World

it keyword contains the copy of the property inside let.

it关键字包含let内属性的副本。

The last value from the let is returned as an argument as shown below.

let的最后一个值作为参数返回,如下所示。

var strLength = str.let { "$it function".length }
println("strLength is $strLength") //prints strLength is 25

链接让功能 (Chaining let functions)

var a = 1
var b= 2

a = a.let { it + 2 }.let { val i = it + b
        i}
println(a) //5

As you can see we’ve declared a local variable “i” inside the second let function. Setting the last statement of the let function to i returns the property to the outer property a.

kotlin let chaining

如您所见,我们在第二个let函数中声明了局部变量“ i”。 将let函数的最后一个语句设置为i会将属性返回到外部属性a

嵌套让 (Nesting let)

We can set a let expression inside another let expression as shown below.

我们可以在另一个let表达式内设置一个let表达式,如下所示。

var x = "Anupam"
x.let { outer -> outer.let { inner -> print("Inner is $inner and outer is $outer") } }

//Prints
//Inner is Anupam and outer is Anupam

For nested let, we can’t use it keyword. We need to assign explicit names to it in both the let functions.
Only the outermost let returns the value as shown below.

对于嵌套let,我们不能使用it关键字。 我们需要明确的名称指定给it在两个让利功能。
只有最外层的let会返回值,如下所示。

var x = "Anupam"
    x = x.let { outer ->
        outer.let { inner ->
            println("Inner is $inner and outer is $outer")
            "Kotlin Tutorials Inner let"
        }
        "Kotlin Tutorials Outer let" 
    }
    println(x) //prints Kotlin Tutorials Outer let

让空检查 (let for null checks)

Additionally, let is useful for checking Nullable properties as shown below.

另外,let对于检查Nullable属性很有用,如下所示。

var name : String? = "Kotlin let null check"
name?.let { println(it) } //prints Kotlin let null check
name = null
name?.let { println(it) } //nothing happens

The code inside the let expression is executed only when the property is not null. Thus let saves us from the if else null checker too!

仅当属性不为null时,才执行let表达式内的代码。 因此,让我们也从if else null检查器中省下来!

Kotlin奔跑 (Kotlin run)

Kotlin run is another interesting function. The following example demonstrates its use cases.

Kotlin run是另一个有趣的功能。 下面的示例演示其用例。

var tutorial = "This is Kotlin Tutorial"
    println(tutorial) //This is Kotlin Tutorial
    tutorial = run {
        val tutorial = "This is run function"
        tutorial
    }
    println(tutorial) //This is run function

Kotlin run expression can change the outer property. Hence in the above code, we’ve redefined it for the local scope.

Kotlin运行表达式可以更改外部属性。 因此,在上面的代码中,我们已经为本地范围重新定义了它。

  • Similar to the let function, the run function also returns the last statement.

    与let函数类似,run函数也返回last语句。
  • Unlike let, the run function doesn’t support the it keyword.

    与let不同,run函数不支持it关键字。

放手 (let and run)

Let’s combine the let and run functions together.

让我们将let和run函数结合在一起。

var p : String? = null
    p?.let { println("p is $p") } ?: run { println("p was null. Setting default value to: ")
        p = "Kotlin"}

    println(p)
//Prints
//p was null. Setting default value to: 
//Kotlin

Kotlin还 (Kotlin also)

As the name says, also expressions does some additional processing on the object it was invoked.
Unlike let, it returns the original object instead of any new return data. Hence the return data has always the same type.
Like let, also uses it too.

正如其名说, also表达做它被调用的对象上一些额外的处理。
与let不同,它返回原始对象,而不是任何新的返回数据。 因此,返回数据始终具有相同的类型。
let一样, also使用it

var m = 1
m = m.also { it + 1 }.also { it + 1 }
println(m) //prints 1

Kotlin Let vs还 (Kotlin let vs also)

Following code snippet shows a great example to differentiate between let and also.

以下代码段显示了一个出色的示例,可以区分letalso

data class Person(var name: String, var tutorial : String)
var person = Person("Anupam", "Kotlin")

var l = person.let { it.tutorial = "Android" }
var al = person.also { it.tutorial = "Android" }
    
println(l)
println(al)
println(person)

In the above code, we’ve used Data classes.

在上面的代码中,我们使用了Data类

The also expression returns the data class object whereas the let expression returns nothing (Unit) as we didn’t specify anything explicitly.

Also表达式返回数据类对象,而let表达式则不返回任何值(单位),因为我们未明确指定任何内容。

Kotlin申请 (Kotlin apply)

Kotlin apply is an extension function on a type. It runs on the object reference (also known as receiver) into the expression and returns the object reference on completion.

Kotlin apply是类型的扩展功能。 它在表达式中的对象引用(也称为接收器)上运行,并在完成时返回对象引用。

data class Person(var name: String, var tutorial : String)
var person = Person("Anupam", "Kotlin")

person.apply { this.tutorial = "Swift" }
println(person)

适用与 (apply vs also)

data class Person(var n: String, var t : String)
var person = Person("Anupam", "Kotlin")

person.apply { t = "Swift" }
println(person)

person.also { it.t = "Kotlin" }
println(person)

Note: In apply it isn’t allowed. If the property name of the data class is unique in the function, you can omit this.

注:在应用it是不允许的。 如果数据类的属性名称在函数中是唯一的,则可以省略this

We should use also only when we don’t want to shadow this.

我们also应该只在我们不想遮蔽this

Kotlin与 (Kotlin with)

Like apply, with is used to change instance properties without the need to call dot operator over the reference every time.

apply一样, with用于更改实例属性,而无需每次都在引用上调用运算符。

data class Person(var name: String, var tutorial : String)
var person = Person("Anupam", "Kotlin")

with(person)
    {
        name = "No Name"
        tutorial = "Kotlin tutorials"
    }

Kotlin with function

Again with is similar to apply except for a few differences.


同样with除了有一些区别之外,类似的apply

Kotlin申请与 (Kotlin apply vs with)

  • with runs without an object(receiver) whereas apply needs one.

    没有对象(接收者)的运行,而应用则需要一个。
  • apply runs on the object reference, whereas with just passes it as an argument.

    apply在对象引用上运行,而只是将其作为参数传递。
  • The last expression of with function returns a result.

    with函数的最后一个表达式返回结果。
var xyz = with(person)
    {
        name = "No Name"
        tutorial = "Kotlin tutorials"
        val xyz = "End of tutorial"
        xyz
    }
    println(xyz) //End of tutorial

That’s all for Kotlin standard functions to alter variables or modify objects within the function.

这就是Kotlin标准函数更改变量或修改函数中对象的全部操作。

翻译自: https://www.journaldev.com/19467/kotlin-let-run-also-apply-with

kotlin运行

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值