Kotlin 表达式与运算符

1、前言

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

2、分支表达式

2.一、if else 表达式

Kotlin:git

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

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

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

2.二、when 表达式

<font face= 黑体>这个 when 表达式就至关于 Java 里面的 switch case。segmentfault

Java:安全

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

Kotlin:ide

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

2.三、try catch 表达式

Java:函数

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

Kotlin:this

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

3、运算符与中缀表达式

3.一、运算符

<font face= 黑体>Kotlin 的运算符有如下特色:spa

  • <font face= 黑体>Kotlin 支持运算符重载;
  • <font face= 黑体>运算符的范围仅限官方指定的符号;

3.1.一、== 运算符

<font face= 黑体>在 Kotlin 中 == 与 equals 彻底等价。code

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

3.1.二、+ 运算符

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

// 这两个写法彻底等价
2 + 3
2.plus(3)

3.1.三、in 运算符

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

3.1.四、[] 运算符

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

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

3.二、运算符重载

<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.三、中缀表达式

<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= 黑体>运行结果:
结果

4、Lambda 表达式

4.一、匿名函数

<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/匿名函数的类型是 () -> Unit
var func: () -> Unit = fun() {
    println("Hello")
}

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

4.二、Lambda 表达式的定义

Java:

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

Kotlin:

val lambda = {
    println("Hello")
}

4.三、Lambda 表达式的类型

// 类型是 () -> Unit
val lambda: () -> Unit = {
    println("Hello")
}

// 类型是 (Int) -> Unit
val 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.四、Lambda 表达式的参数省略形式

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

<font face= 黑体>若是 Lambda 表达式只有一个参数的话,能够省略参数,因此上面的 Lambda 表达式就能够改为下面这样:

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

5、小结

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

6、源码

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

相关文章
相关标签/搜索