美文网首页
Kotlin常用语法

Kotlin常用语法

作者: Gavin_2020 | 来源:发表于2020-11-09 09:53 被阅读0次

    常用语法

    此文主要用于基本语法简单的使用,后续会记录更多语法.

    函数

    下面的函数接受两个 Int 型参数,返回值为 Int

    fun sum(a: Int, b: Int): Int {
        return a + b
    }
    
    

    具有表达式主体和推断的返回类型的函数:

    fun sum(a: Int, b: Int) = a + b
    
    

    返回无意义的值:

    fun printSum(a: Int, b: Int): Unit {
        println("sum of $a and $b is ${a + b}")
    }
    
    

    Unit 的返回类型可以省略:

    fun printSum(a: Int, b: Int) {
      println("sum of $a and $b is ${a + b}")
    }
    
    

    变量

    只读的本地变量通过val关键字定义.该类变量只能赋值一次:

    val a: Int = 1  // 立刻赋值
    val b = 2   // `Int` 类型是自推导的
    val c: Int  // 没有初始化器时要指定类型
    c = 3       // 推断型赋值
    
    

    被关键字var修饰的变量可以重新赋值:

    var x = 5 // `Int` type is inferred
    x += 1
    
    

    顶级变量:

    val PI = 3.14
    var x = 0
    
    fun incrementX() { 
        x += 1 
    }
    
    

    注释

    与多数现代语言一样,Kotlin 支持单行注释(行尾注释)和多行注释(块注释)。

    // 行尾注释
    
    /*  这是块注释
        可以在多行注释 */
    
    

    Kotlin 块注释可以嵌套.

    /* The comment starts here
    /* contains a nested comment */     
    and ends here. */
    
    

    字符串模板

    var a = 1
    // simple name in template:
    val s1 = "a is $a" 
    
    a = 2
    // arbitrary expression in template:
    val s2 = "${s1.replace("is", "was")}, but now is $a"
    
    

    条件表达式

    fun maxOf(a: Int, b: Int): Int {
        if (a > b) {
            return a
        } else {
            return b
        }
    }
    
    

    kotin 中可以使用 if 作为表达式:

    fun maxOf(a: Int, b: Int) = if (a > b) a else b
    
    

    可空变量以及空值检查

    当空值可能出现时必须明确标注该引用可空。

    当 str 中不包含整数时返回空:

    fun parseInt(str: String): Int? {
        // ...
    }
    
    

    使用函数返回空值:

    fun printProduct(arg1: String, arg2: String) {
        val x = parseInt(arg1)
        val y = parseInt(arg2)
    
        // Using `x * y` yields error because they may hold nulls.
        if (x != null && y != null) {
            // x and y are automatically cast to non-nullable after null check
            println(x * y)
        }
        else {
            println("'$arg1' or '$arg2' is not a number")
        }    
    }
    
    

    或者

    if (x == null) {
        println("Wrong number format in arg1: '$arg1'")
        return
    }
    if (y == null) {
        println("Wrong number format in arg2: '$arg2'")
        return
    }
    
    // x and y are automatically cast to non-nullable after null check
    println(x * y)
    
    

    类型检查以及自动转换

    is 操作符可以检查表达式是否是是某个类型的实例。如果不可变的局部变量或属性进行过了类型检查,就没有必要显示转换:

    fun getStringLength(obj: Any): Int? {
      if (obj is String) {
        // obj 将会在这个分支中自动转换为 `String` 类型
        return obj.length
      }
    
      // obj 在类型检查分支外仍然是 Any 类型
      return null
    }
    
    

    或者

    fun getStringLength(obj: Any): Int? {
      if (obj !is String) return null
    
      // obj 将会在这个分支中自动转换为 `String` 类型
      return obj.length
    }
    
    

    甚至可以这样

    fun getStringLength(obj: Any): Int? {
        // obj 将会在&&右边自动转换为 String 类型
      if (obj is String && obj.length > 0) {
        return obj.length
      }
    
      return null
    }
    
    

    for 循环

    val items = listOf("apple", "banana", "kiwifruit")
    for (item in items) {
        println(item)
    }
    
    

    或者

    val items = listOf("apple", "banana", "kiwifruit")
    for (index in items.indices) {
        println("item at $index is ${items[index]}")
    }
    
    

    while 循环

    val items = listOf("apple", "banana", "kiwifruit")
    var index = 0
    while (index < items.size) {
        println("item at $index is ${items[index]}")
        index++
    }
    
    

    when 表达式

    fun describe(obj: Any): String =
        when (obj) {
            1          -> "One"
            "Hello"    -> "Greeting"
            is Long    -> "Long"
            !is String -> "Not a string"
            else       -> "Unknown"
        }
    
    

    ranges

    使用 in 操作符判断数值是否在某个范围内:

    val x = 10
    val y = 9
    if (x in 1..y+1) {
        println("fits in range")
    }
    
    

    检查数值是否在范围外:

    val list = listOf("a", "b", "c")
    
    if (-1 !in 0..list.lastIndex) {
        println("-1 is out of range")
    }
    if (list.size !in list.indices) {
        println("list size is out of valid list indices range, too")
    }
    
    

    参看Ranges

    集合

    遍历集合:

    for (item in items) {
        println(item)
    }
    
    

    使用 in 操作符检查集合中是否包含某个对象:

    when {
        "orange" in items -> println("juicy")
        "apple" in items -> println("apple is fine too")
    }
    
    

    使用lambda表达式过滤和映射集合:

    val fruits = listOf("banana", "avocado", "apple", "kiwifruit")
    fruits
      .filter { it.startsWith("a") }
      .sortedBy { it }
      .map { it.toUpperCase() }
      .forEach { println(it) }
    
    

    参看集合概述

    创建基本类以及实例:

    val rectangle = Rectangle(5.0, 2.0)
    val triangle = Triangle(3.0, 4.0, 5.0)
    

    相关文章

      网友评论

          本文标题:Kotlin常用语法

          本文链接:https://www.haomeiwen.com/subject/lahtbktx.html