kotlin 语法糖

  1. Use of “when” Expression Instead of “switch”

    fun getDayOfWeek(day: Int): String {
        return when (day) {
            1 -> "Monday"
            2 -> "Tuesday"
            3 -> "Wednesday"
            4 -> "Thursday"
            5 -> "Friday"
            6 -> "Saturday"
            7 -> "Sunday"
            else -> "Invalid day"
        }
    }
    
  2. Lambda Expressions and Higher-Order Functions

    val numbers = listOf(1, 2, 3, 4, 5)
    val doubled = numbers.map { it * 2 }  // Lambda expression
    
  3. Destructuring Declarations

    data class Person(val name: String, val age: Int)
    val person = Person("Alice", 30)
    val (name, age) = person  // Destructuring declaration
    
  4. Elvis Operator

    val nullableString: String? = null
    val nonNullableString: String = nullableString ?: "Default Value"  // Elvis operator
    
  5. String Templates

    val greeting = "Hello, $name!"
    
  6. Smart Casts

    fun getStringLength(obj: Any): Int? {
        if (obj is String) {
            return obj.length  // Smart cast
        }
        return null
    }
    
  7. Single-Expression Functions

    fun square(x: Int) = x * x
    
  8. Default Parameter Values

    fun greet(name: String = "Guest") {
        println("Hello, $name!")
    }
    
  9. Extension Functions

    fun String.isPalindrome(): Boolean {
        return this == this.reversed()
    }
    
    val word = "madam"
    val isPalindrome = word.isPalindrome()
    
  10. Operator Overloading

    data class Point(val x: Int, val y: Int) {
        operator fun plus(other: Point) = Point(x + other.x, y + other.y)
    }
    
    val point1 = Point(2, 3)
    val point2 = Point(4, 5)
    val point3 = point1 + point2
    
  11. Printing All Results

    fun main() {
        println(getDayOfWeek(3))  // Output: Wednesday
        println(doubled)  // Output: [2, 4, 6, 8, 10]
        println("Name: $name, Age: $age")  // Output: Name: Alice, Age: 30
        println(nonNullableString)  // Output: Default Value
        println(greeting)  // Output: Hello, Alice!
        println(getStringLength("Kotlin"))  // Output: 6
        println(square(4))  // Output: 16
        greet()  // Output: Hello, Guest!
        println(isPalindrome)  // Output: true
        println(point3)  // Output: Point(x=6, y=8)
    }
    
    main()
    

let

The let function is used to invoke a lambda function on the object it is called on. It returns the result of the lambda expression.

val result = "Hello".let {
    println(it)
    it.length
}
println(result) // Output: 5

also

The also function is similar to let, but it returns the original object instead of the result of the lambda expression. It’s often used for side effects, such as logging or validation.

val result = "Hello".also {
    println(it)
}.toUpperCase()
println(result) // Output: HELLO

run

The run function is used to execute a block of code and return its result. It can be called on an object and within a scope.

val result = "Hello".run {
    println(this)
    this.length
}
println(result) // Output: 5

with

The with function is a non-extension function that can be used to call multiple methods on the same object without repeating the object’s name.

val result = with("Hello") {
    println(this)
    this.length
}
println(result) // Output: 5

apply

The apply function is used for configuring an object. It runs a block of code on the object and returns the object itself.

val result = StringBuilder().apply {
    append("Hello")
    append(" World")
}
println(result.toString()) // Output: Hello World

takeIf

The takeIf function returns the object if it satisfies the given predicate; otherwise, it returns null.

val result = "Hello".takeIf {
    it.length > 3
}
println(result) // Output: Hello

takeUnless

The takeUnless function returns the object if it does not satisfy the given predicate; otherwise, it returns null.

val result = "Hello".takeUnless {
    it.length > 5
}
println(result) // Output: Hello

相关推荐

  1. kotlin 语法

    2024-06-13 06:02:01       6 阅读
  2. 语法

    2024-06-13 06:02:01       25 阅读
  3. Python 语法

    2024-06-13 06:02:01       29 阅读
  4. Python的语法

    2024-06-13 06:02:01       30 阅读
  5. Python:语法

    2024-06-13 06:02:01       16 阅读
  6. 细说golang语法

    2024-06-13 06:02:01       11 阅读
  7. 细说php语法

    2024-06-13 06:02:01       14 阅读
  8. ES6 - 语法

    2024-06-13 06:02:01       11 阅读
  9. Python语法大全

    2024-06-13 06:02:01       12 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-06-13 06:02:01       10 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-06-13 06:02:01       12 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-06-13 06:02:01       11 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-06-13 06:02:01       13 阅读

热门阅读

  1. Web前端后端结合:深度解析与实战策略

    2024-06-13 06:02:01       4 阅读
  2. 字节跳动基础架构两篇论文入选 VLDB 2024

    2024-06-13 06:02:01       4 阅读
  3. payable介绍, 编写一个转账的测试合约

    2024-06-13 06:02:01       5 阅读
  4. git 常用命令

    2024-06-13 06:02:01       4 阅读
  5. 理解 Vue 中的响应式系统

    2024-06-13 06:02:01       4 阅读
  6. Springer投稿流程——Cybersecurity

    2024-06-13 06:02:01       7 阅读
  7. debian10 arm芯片安装.net6

    2024-06-13 06:02:01       5 阅读