Kotlin开发Android应用,学习第一课

2019-05-1105:46:18APP与小程序开发Comments2,502 views字数 2059阅读模式

Kotlin开发Android应用,已逐渐成为一种趋势。因此了解并掌握Kotlin语言,势在必行。
现将本人学习Kotlin的过程记录如下,以备回顾、参照。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/xcx/12063.html

一、基础:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/xcx/12063.html

1、可以使用下划线使数字常量更易读
如:val oneMillion = 1_000_000文章源自菜鸟学院-https://www.cainiaoxueyuan.com/xcx/12063.html

2、数值变量会有一个自动装箱的过程,数字装箱不一定保留同一性。所以两个等号逻辑判断两个变量的数值相等性,三个等号还同时比较类型的同一性。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/xcx/12063.html

val a: Int = 10000
val boxedA: Int? = a
val anotherBoxedA: Int? = a
println(a == a) // 输出“true”
println(boxedA == anotherBoxedA) // 输出“true”
//========但是========
println(a === a) // 输出“true”
println(boxedA === anotherBoxedA) // !!!输出“false”!!!

3、基础数据类型中,较小的类型不能隐式转换为较大的类型。 这意味着在不进行显式转换的情况下我们不能把 Byte 型值赋给一个 Int 变量。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/xcx/12063.html

val b: Byte = 1 // OK, 字面值是静态检测的
val i: Int = b // 错误
val i: Int = () // OK:显式拓宽
print(i)// 输出1

4、控制流
If表达式
在 Kotlin 中,if是一个表达式,即它会返回一个值。 因此就不需要三元运算符(条件 ? 然后 : 否则),因为普通的 if 就能胜任这个角色。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/xcx/12063.html

var max = a 
if (a < b) max = b
// With else 
var max: Int
if (a > b) {
    max = a
} else {
    max = b
}
// 作为表达式
val max = if (a > b) a else b

When表达式
when 将它的参数与所有的分支条件顺序比较,直到某个分支满足条件。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/xcx/12063.html

when (x) {
    1 -> print("x == 1")
    2 -> print("x == 2")
    else -> { // 注意这个块
        print("x is neither 1 nor 2")
    }
}

when 也可以用来取代 if-else if链。 如果不提供参数,所有的分支条件都是简单的布尔表达式,而当一个分支的条件为真时则执行该分支文章源自菜鸟学院-https://www.cainiaoxueyuan.com/xcx/12063.html

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

For循环文章源自菜鸟学院-https://www.cainiaoxueyuan.com/xcx/12063.html

for (i in 1..3) {
    println(i)
}
for (i in 6 downTo 0 step 2) {
    println(i)
}

for (i in array.indices) {
    println(array[i])
}

for ((index, value) in array.withIndex()) {
    println("the element at $index is $value")
}

While循环
与java 相同,while 与 do…while 照常使用文章源自菜鸟学院-https://www.cainiaoxueyuan.com/xcx/12063.html

while (x > 0) {
    x--
}

do {
  val y = retrieveData()
} while (y != null) // y 在此处可见

Return的区别文章源自菜鸟学院-https://www.cainiaoxueyuan.com/xcx/12063.html

1)fun foo() {
    listOf(1, 2, 3, 4, 5).forEach {
        if (it == 3) return // 非局部直接返回到 foo() 的调用者
        println(it)
    }
    print("this point is unreachable")
}
// 结果返回:12
2)fun foo() {
    listOf(1, 2, 3, 4, 5).forEach a@{
        if (it == 3) return@a // 局部返回到该 lambda 表达式的调用者,即 forEach 循环
        println(it)
    }
    print(" done with explicit label")
}

3)fun foo() {
    listOf(1, 2, 3, 4, 5).forEach {
        if (it == 3) return@forEach // 局部返回到该 lambda 表达式的调用者,即 forEach 循环
        print(it)
    }
    print(" done with implicit label")
}

4)fun foo() {
    listOf(1, 2, 3, 4, 5).forEach(fun(value: Int) {
        if (value == 3) return  // 局部返回到匿名函数的调用者,即 forEach 循环
        print(value)
    })
    print(" done with anonymous function")
}
/*以上2)、3)4)三种写法均返回:1245 done with anonymous function
这三种写法中使用的局部返回类似于在常规循环中使用 continue。并没有 break 的直接等价形式,我们可以通过增加另一层嵌套 lambda 表达式并从其中非局部返回来模拟*/
5)fun foo() {
    run loop@{
        listOf(1, 2, 3, 4, 5).forEach {
            if (it == 3) return@loop // 从传入 run 的 lambda 表达式非局部返回
            print(it)
        }
    }
    print(" done with nested loop")
}
// 此时返回:12 done with nested loop
文章源自菜鸟学院-https://www.cainiaoxueyuan.com/xcx/12063.html
  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/xcx/12063.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定