欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 教育 > 锐评 > kotlin 语法糖

kotlin 语法糖

2024/10/24 22:26:48 来源:https://blog.csdn.net/heeheeai/article/details/139637532  浏览:    关键词: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: Wednesdayprintln(doubled)  // Output: [2, 4, 6, 8, 10]println("Name: $name, Age: $age")  // Output: Name: Alice, Age: 30println(nonNullableString)  // Output: Default Valueprintln(greeting)  // Output: Hello, Alice!println(getStringLength("Kotlin"))  // Output: 6println(square(4))  // Output: 16greet()  // Output: Hello, Guest!println(isPalindrome)  // Output: trueprintln(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

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com