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= 2a = a.let { it + 2 }.let { val i = it + bi}
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.

如您所见,我们在第二个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 Tutorialtutorial = 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? = nullp?.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"}

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运行

kotlin运行_Kotlin允许,运行,也适用于相关推荐

  1. Kotlin 知识梳理(13) 运行时的泛型

    一.本文概要 本文是对<<Kotlin in Action>>的学习笔记,如果需要运行相应的代码可以访问在线环境 try.kotlinlang.org,这部分的思维导图为: 二 ...

  2. java se运行环境_Java运行环境Java SE Runtime Environment (JRE) 下载

    Java SE Runtime Environment又称JRE,是Java的核心运行环境,如果需要运行JAVA程序,JRE是必不可少的环境的集合.Java平台使您可以在台式机和服务器以及当今苛刻的嵌 ...

  3. iOS子构建Debug运行正常Release运行失败,提示证书问题

    iOS子构建Debug运行正常Release运行失败,提示证书问题 在老版本的Xcode上创建子构建时会自动创建对应的证书和描述文件,但是在Xcode12上不会自动创建Release对应的证书和描述文 ...

  4. html 服务器运行exe,html运行exe文件,兼容浏览器

    8种机械键盘轴体对比 本人程序员,要买一个写代码的键盘,请问红轴和茶轴怎么选? 这种方式是通过修改注册表打开本地的exe可执行文件. 第一步 打开记事本,输入下列神秘代码Windows Registr ...

  5. C#如何得到运行中和杀死运行中的进程?

    C#如何得到运行中和杀死运行中的进程? 得到所有进程且杀死QQ的进程: foreach (System.Diagnostics.Process thisproc in System.Diagnosti ...

  6. java运行机制以及 运行流程

    目录 java运行机制以及运行流程 java运行机制以及运行流程 总结:先创建一个java后缀的文件,里面编写helloword 使用javac生成.class文件   在使用java 运行文件

  7. LoadRunner中进程运行和线程运行区别

    LoadRunner中进程运行和线程运行区别 发布时间: 2012-5-23 10:29    作者: 田志良    来源: 51Testing软件测试网采编 字体:  小  中  大  | 上一篇 ...

  8. 下载的c语言程序代码怎么运行,CFree怎么运行程序 编译运行C语言程序代码的方法...

    CFree是一款C语言编译软件,用户可以利用这款软件编译C/C++程序 ,如果你想要运行已经编写好的C语言代码,只需要几个简单的操作即可实现,如果你还不知道怎么运行,就赶快来看看下面的教程吧! 1.首 ...

  9. Linux学习笔记(三):系统运行级与运行级的切换

    1.Linux系统与其他的操作系统不同,它设有运行级别.该运行级指定操作系统所处的状态.Linux系统在任何时候都运行于某个运行级上,且在不同的运行级上运行的程序和服务都不同,所要完成的工作和所要达到 ...

  10. AndroidStudio_android通过服务,检测本程序是否已经终止运行_终止运行后发送通知给Http服务器---Android原生开发工作笔记246

    先做的一个项目,大体是,我这边如果程序已经停止,需要发送一个通知给远程的Httpserver服务器, 然后,远程服务器会通过websocket,通知,显示在大屏上的页面,去清空大屏数据,这样就有问题了 ...

最新文章

  1. linux ssh 连接错误 WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!
  2. 使用SVCUtil.exe生成客户端代理类和配置文件
  3. python有什么好玩的库_你知道Python很火!那你知道它有哪些好玩的库吗?
  4. 和功率的计算公式_电机电流的计算公式是什么,具体怎么计算?
  5. bash: /opt/hisi-linux/x86-arm/arm-hisiv300-linux/target/bin/arm-hisiv300-linux-gcc: 没有那个文件或目录。...
  6. 2021牛客暑期多校训练营9
  7. 打开端口_打印机ip及端口设置
  8. python os.access_os.access(path, mode)
  9. Kubernetes弹性伸缩全场景解读(五) - 定时伸缩组件发布与开源
  10. ARM 架构演进对未来计算意味着什么?
  11. 前端面试知识点归纳:vue,react,webpack,bable,项目开发
  12. 【数字信号处理】基于matlab数字信号同步压缩变换【含Matlab源码 1535期】
  13. 大学本科计算机考试小抄,大学考试竟允许光明正大打小抄你也考不过
  14. 微信公众号之免登陆快速入门
  15. 运算放大器(OPA)超详细参数讲解-运放---以及8个型号的运算放大器分析对比
  16. Code Review之前中后
  17. mediawiki php7,centos7搭建mediawiki
  18. echarts去除网格线_echarts实现去掉X轴、Y轴和网格线效果实例分享
  19. Windows 下有什么软件能够极大地提高工作效率
  20. 【正点原子Linux连载】第四十四章 设备树下的LED驱动实验 -摘自【正点原子】I.MX6U嵌入式Linux驱动开发指南V1.0

热门文章

  1. 实习笔记(数据库相关)-2014
  2. [转载] Python print输出不换行没空格
  3. springBoot,thymeleaf页面中js使用标签
  4. ByteToByte64String、Base64StringToBytes
  5. 【Java】 归并排序的非递归实现
  6. macOs 使用Homebrew升级到MySQL 8系列之后,php无法连接解决方法
  7. 谈谈使用Redis缓存时批量删除的几种实现
  8. 蒟蒻的HNOI2017滚粗记
  9. JAVA 1.7并发之LinkedTransferQueue原理理解
  10. silverlight体验之三:简单控件堆成个Login