Kotlin的基本语法

Kotlin

中文官网: https://www.kotlincn.net/docs/reference/

val positives = list.filter { x -> x > 0 }
val positives = list.filter { it > 0 }
检测元素是否存在于集合中
if ("john@example.com" in emailsList) { …… }
if ("jane@example.com" !in emailsList) { …… }
when类型判断
when (x) {
    is Foo //-> ……
    is Bar //-> ……
    else   //-> ……
}
fun describe(obj: Any): String =
    when (obj) {
        1          -> "One"
        "Hello"    -> "Greeting"
        is Long    -> "Long"
        !is String -> "Not a string"
        else       -> "Unknown"
    }

when (x) {
    in 1..10 -> print("x is in the range")
    in validNumbers -> print("x is valid")
    !in 10..20 -> print("x is outside the range")
    else -> print("none of the above")
}

when {
    x.isOdd() -> print("x is odd")
    x.isEven() -> print("x is even")
    else -> print("x is funny")
}

//另一种可能性是检测一个值是(is)或者不是(!is)一个特定类型的值。注意: 由于智能转换,你可以访问该类型的方法与属性而无需任何额外的检测。
fun hasPrefix(x: Any) = when(x) {
    is String -> x.startsWith("prefix")
    else -> false
}

fun Request.getBody() =
        when (val response = executeRequest()) {
            is Success -> response.body
            is HttpError -> throw HttpException(response.status)
        }
if判断

if 的分支可以是代码块,最后的表达式作为该块的值:

// 作为表达式
val max = if (a > b) a else b

//if 的分支可以是代码块,最后的表达式作为该块的值:
val max = if (a > b) {
    print("Choose a")
    a
} else {
    print("Choose b")
    b
}
遍历 map/pair型list
for ((k, v) in map) {
    println("$k -> $v")
}
for (item: Int in ints) {
    // ……
}

for (i in 1..3) {
    println(i)
}

for (i in 6 downTo 0 step 2) {
    println(i)
}
//或者你可以用库函数 withIndex
for ((index, value) in array.withIndex()) {
    println("the element at $index is $value")
}
使用区间
for (i in 1..100) { …… }  // 闭区间:包含 100
for (i in 1 until 100) { …… } // 半开区间:不包含 100
for (x in 2..10 step 2) { …… }
for (x in 10 downTo 1) { …… }
if (x in 1..10) { …… }
while 循环
val items = listOf("apple", "banana", "kiwifruit")
var index = 0
while (index < items.size) {
    println("item at $index is ${items[index]}")
    index++
}
for 循环
val items = listOf("apple", "banana", "kiwifruit")
for (item in items) {
    println(item)
}
使用区间(range)

使用 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")
}

区间迭代:

for (x in 1..5) {
    print(x)
}

或数列迭代:

for (x in 1..10 step 2) {
    print(x)
}
println()
for (x in 9 downTo 0 step 3) {
    print(x)
}
集合

对集合进行迭代:

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

使用 in 运算符来判断集合内是否包含某实例:

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

使用 lambda 表达式来过滤(filter)与映射(map)集合:

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

构造函数

在 Kotlin 中的一个类可以有一个主构造函数以及一个或多个次构造函数。主构造函数是类头的一部分:它跟在类名(与可选的类型参数)后。

class Person constructor(firstName: String) { /*……*/ }

如果主构造函数没有任何注解或者可见性修饰符,可以省略这个 constructor 关键字。

class Person(firstName: String) { /*……*/ }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值