Kotlin基础了解

基本语法:http://kotlinlang.org/docs/reference/basic-syntax.html
特点:http://kotlinlang.org/docs/reference/idioms.html
编码规范:http://kotlinlang.org/docs/reference/coding-conventions.html

使用Gradle编译kotlin
http://kotlinlang.org/docs/reference/using-gradle.html

Koans:一个开源demo,可以学习到kotlin系列语法及用法
http://kotlinlang.org/docs/tutorials/koans.html

基本语法~~

定义包~~

package my.demo

import java.util.*

// ...

详情:https://kotlinlang.org/docs/reference/packages.html

定义方法~~:

1,常规定义:

fun sum(a: Int, b: Int): Int {
    return a + b
}
2,推断类型定义:
fun sum(a: Int, b: Int) = a + b

3,无返回值函数  Unit 也可以省去
fun printSum(a: Int, b: Int): Unit {
    println("sum of $a and $b is ${a + b}")
}

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

详情:https://kotlinlang.org/docs/reference/functions.html

定义本地变量~~

val 只读变量
val a: Int = 1  // 指定类型
val b = 2   // 推断类型
val c: Int  // 指定类型未赋值
c = 3       // 延期赋值

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

详情:https://kotlinlang.org/docs/reference/properties.html

Using string templates~~

fun main(args: Array<String>) {
    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"
    println(s2)
}

结果:a was 1, but now is 2

详情:https://kotlinlang.org/docs/reference/basic-types.html#string-templates

if条件

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

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

详情:http://kotlinlang.org/docs/reference/control-flow.html

Using nullable values and checking for null
空值判断,空值检测,具体可看详情,主要是 ?,!= ,!!的使用及相关语法。

var a: String = "abc"
a = null // compilation error

var b: String? = "abc"
b = null // ok

详情:http://kotlinlang.org/docs/reference/null-safety.html

Using type checks and automatic casts
类型检测及自动转换
is !is

fun getStringLength(obj: Any): Int? {
    if (obj is String) {
        // `obj` is automatically cast to `String` in this branch
        return obj.length
    }

    // `obj` is still of type `Any` outside of the type-checked branch
    return null
}


fun main(args: Array<String>) {
    fun printLength(obj: Any) {
        println("'$obj' string length is ${getStringLength(obj) ?: "... err, not a string"} ")
    }
    printLength("Incomprehensibilities")
    printLength(1000)
    printLength(listOf(Any()))
}

详情:See Classes and Type casts.

Using a for loop

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

items.indices // 索引

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

详情:http://kotlinlang.org/docs/reference/control-flow.html#for-loops

Using a while loop

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

详情:http://kotlinlang.org/docs/reference/control-flow.html#when-expression

Using when expression

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

fun main(args: Array<String>) {
    println(describe(1))
    println(describe("Hello"))
    println(describe(1000L))
    println(describe(2))
    println(describe("other"))
}

详情:http://kotlinlang.org/docs/reference/control-flow.html#when-expression

Using ranges
语法:in !in
范围:1…10(1到10的范围) 1…y+1 (1 到 y+1的范围)

fun main(args: Array<String>) {
    val x = 10
    val y = 9
    if (x in 1..y+1) {
        println("fits in range")
    }
}

详情:http://kotlinlang.org/docs/reference/ranges.html

Using collections

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

val items = setOf("apple", "banana", "kiwi")
    when {
        "orange" in items -> println("juicy")
        "apple" in items -> println("apple is fine too")
    }
    
lambda表达式:

fun main(args: Array<String>) {
    val fruits = listOf("banana", "avocado", "apple", "kiwi")
    fruits
    .filter { it.startsWith("a") }
    .sortedBy { it }
    .map { it.toUpperCase() }
    .forEach { println(it) }
}

详情:http://kotlinlang.org/docs/reference/lambdas.html

Creating basic classes and their instances

val rectangle = Rectangle(5.0, 2.0) //no 'new' keyword required
val triangle = Triangle(3.0, 4.0, 5.0)

See classes and objects and instances.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值