一、前言

<font face= 黑体>在 Kotlin 中的类与接口 和 Kotlin 空类型平安与智能类型转换 中咱们曾经将 Kotlin 中的类型初步讲完了,明天咱们来讲 Kotlin中的表达式

二、分支表达式

2.1、if else 表达式

Kotlin:

if (a == 3) {    c = 4} else {    c = 5}

<font face= 黑体>下面代码能够等价写成上面这种模式:

c = if (a == 3) 4 else 5 

2.2、when 表达式

<font face= 黑体>这个 when 表达式就相当于 Java 外面的 switch case。

Java:

switch (a) {    case 0:        c = 5;        break;    case 1:        c = 100;        break;    default:        c = 20;}

Kotlin:

when (a) {    0 -> c = 5    1 -> c = 100    else -> c = 20}<==> 表达式的写法c = when(a) {    0 -> 5    1 -> 100    else -> 20}

2.3、try catch 表达式

Java:

try {    c = a / b;} catch (Exception e) {    e.printStackTrace();    c = 0;}

Kotlin:

try {    c = a / b} catch (e: Exception) {    e.printStackTrace()    c = 0}<==> 表达式的写法c = try {    c = a / b} catch (e: Exception) {    e.printStackTrace()    0}

三、运算符与中断表达式

3.1、运算符

<font face= 黑体>Kotlin 的运算符有以下特点:

  • <font face= 黑体>Kotlin 反对运算符重载;
  • <font face= 黑体>运算符的范畴仅限官网指定的符号;

3.1.1、== 运算符

<font face= 黑体>在 Kotlin 中 == 与 equals 齐全等价。

// 这两个写法齐全等价"Hello" == "World""Hello".equals("World")

3.1.2、+ 运算符

<font face= 黑体>在 Kotlin 中 + 与 plus 齐全等价。

// 这两个写法齐全等价2 + 32.plus(3)

3.1.3、in 运算符

val list = listOf(1, 2, 3, 4)2 in list

3.1.4、[] 运算符

val map = mutableMapOf(    "Hello" to 2,    "World" to 3)val value = map["Hello"]map["World"] = 4

...
<font face= 黑体>这里只举几个运算符来讲,具体大家能够查看官网的运算符的用法。

3.2、运算符重载

<font face= 黑体>运算符重载就是对已有的运算符赋予他们新的含意,重载的修饰符是 operator。

<font face= 黑体>运算符重载咱们能够看上面这个例子,这里咱们是对复数 Complex 的运算符进行重载。

//复数class Complex(var real: Double, var image: Double){    override fun toString() = "$real + ${image}i"}operator fun Complex.plus(other: Complex): Complex {    return Complex(this.real + other.real, this.image + other.image)}operator fun Complex.plus(other: Double): Complex {    return Complex(this.real + other, this.image)}operator fun Complex.plus(other: Int): Complex {    return Complex(this.real + other, this.image)}operator fun Complex.minus(real: Double): Complex {    return Complex(this.real - real, this.image)}operator fun Complex.get(index: Int): Double = when(index){    0 -> this.real    1 -> this.image    else -> throw IndexOutOfBoundsException()}fun main() {    val c1 = Complex(3.0, 4.0)    val c2 = Complex(2.0, 2.0)    println(c1 + 2.0)    println(c1 + c2)    println(c1 + 3)    println(c1 - 3.0)    println(c1[0])    println(c1[1])    println(c1[2])}

3.3、中断表达式

<font face= 黑体>只有一个参数,且用 infix 润饰的函数。

// 本没有 vs,自定义一个,不须要类名.办法即可调用infix fun Int.vs(num: Int): String =    when {         this - num < 0 -> "$this 小于 $num"         this - num > 0 -> "$this 大于 $num"         else -> "$this 等于 $num"     }

<font face= 黑体>调用:

fun main() {    println(5 vs 6)    println(5 vs 5)    println(7 vs 6)}

<font face= 黑体>运行后果:

四、Lambda 表达式

4.1、匿名函数

<font face= 黑体>咱们先来看一下一个一般函数:

// func 是函数名fun func() {    println("Hello")}

<font face= 黑体>咱们把函数名 func 去掉就变成了匿名函数,如下:

// 去掉 func 变成匿名函数fun() {    println("Hello")}

<font face= 黑体>函数是能够传递的,所以匿名函数天然也能传递,如下:

// func 是变量名var func = fun() {    println("Hello")}

<font face= 黑体>咱们晓得一个变量是有类型的,那么 func 变量的类型是什么呢? func 变量的类型就是匿名函数的类型,如下:

// func/匿名函数的类型是 () -> Unitvar func: () -> Unit = fun() {    println("Hello")}

<font face= 黑体>咱们之所以说匿名函数的起因是 Lambda 表达式就是匿名函数的一个更具备表现力的写法,它实质上就是一个匿名函数。

4.2、Lambda 表达式的定义

Java:

Runnable lambda = () -> {    System.out.println("Hello");};

Kotlin:

val lambda = {    println("Hello")}

4.3、Lambda 表达式的类型

// 类型是 () -> Unitval lambda: () -> Unit = {    println("Hello")}// 类型是 (Int) -> Unitval f1: (Int) -> Unit = { p: Int ->    println(p)}<==> 等价写法val f1: Function1<Int, Unit> = { p ->    println(p)}<==> 类型推倒val f1 = { p: Int ->    println(p)}

<font face= 黑体>Lambda 表达式的最初一行为返回值,上面这个 Lambda 表达式的意思就是承受一个整型参数,并且返回一个字符串 "Helll"。

val f1 = { p: Int ->    println(p)    "Hello"}

4.4、Lambda 表达式的参数省略模式

val f1: Function1<Int, Unit> = { p ->    println(p)}

<font face= 黑体>如果 Lambda 表达式只有一个参数的话,能够省略参数,所以下面的 Lambda 表达式就能够改成上面这样:

val f1: Function1<Int, Unit> = {    println(it)}

五、小结

<font face= 黑体>本篇博客次要讲了 Kotlin 中的表达式运算符,下一节咱们讲 Kotin 的函数进阶

六、源码

源码 已上传至 github,有须要能够间接下载。