Android 开发 Kotlin学习(一)

131 阅读2分钟

Kotlin基础

学习第一步,Hello, world!

输出Hello, world!

fun main() {
    println("Hello, world!")
    // Hello, world!
}

变量

  1. 只读变量 val
  2. 可变变量 var

tips: 变量可声明在main函数之外

基础数据类型

CategoryBasic types
IntegersByteShortIntLong
Unsigned integersUByteUShortUIntULong
Floating-point numbersFloatDouble
BooleansBoolean
CharactersChar
StringsString

声明变量的两种方式

//1. Variable declared without initialization
val d: Int
// Variable initialized
d = 3

//2. Variable explicitly typed and initialized
val e: String = "hello"

集合类型

Collection typeDescription
ListsOrdered collections of items
SetsUnique unordered collections of items
MapsSets of key-value pairs where keys are unique and map to only one value
List
  1. 只读List,声明方式 listOf()
  2. 可变List, 声明方式 mutableListOf()
// Read only list
val readOnlyShapes = listOf("triangle", "square", "circle")
println(readOnlyShapes)
// [triangle, square, circle]

// Mutable list with explicit type declaration
val shapes: MutableList<String> = mutableListOf("triangle", "square", "circle")
println(shapes)
// [triangle, square, circle]

tips: 声明空list需说明数据类型

var titles = mutableListOf<Int>();
Set
  1. 只读Set,声明方式 setOf()
  2. 可变Set, 声明方式 mutableSetOf()
// Read-only set
val readOnlyFruit = setOf("apple", "banana", "cherry", "cherry")
// Mutable set with explicit type declaration
val fruit: MutableSet<String> = mutableSetOf("apple", "banana", "cherry", "cherry")

println(readOnlyFruit)
// [apple, banana, cherry]
Map
  1. 只读Met,声明方式 mapOf()
  2. 可变Met, 声明方式 mutableMapOf()
// Read-only map
val readOnlyJuiceMenu = mapOf("apple" to 100, "kiwi" to 190, "orange" to 100)
println(readOnlyJuiceMenu)
// {apple=100, kiwi=190, orange=100}

// Mutable map with explicit type declaration
val juiceMenu: MutableMap<String, Int> = mutableMapOf("apple" to 100, "kiwi" to 190, "orange" to 100)
println(juiceMenu)
// {apple=100, kiwi=190, orange=100}

控制流程

if

和其他语言写法类似

val d: Int
val check = true

if (check) {
    d = 1
} else {
    d = 2
}
println(d)
// 1
When

当条件表达式具有多个分支时使用,条件和其他语言switch语法类似 区别:When有返回值

val obj = "Hello"

when (obj) {
    // Checks whether obj equals to "1"
    "1" -> println("One")
    // Checks whether obj equals to "Hello"
    "Hello" -> println("Greeting")
    // Default statement
    else -> println("Unknown")     
}
// Greeting

When有返回值

val obj = "Hello"    

val result = when (obj) {
    // If obj equals "1", sets result to "one"
    "1" -> "One"
    // If obj equals "Hello", sets result to "Greeting"
    "Hello" -> "Greeting"
    // Sets result to "Unknown" if no previous condition is satisfied
    else -> "Unknown"
}
println(result)
// Greeting

还可以使用表达式当做判断条件

val temp = 18

val description = when {
    // If temp < 0 is true, sets description to "very cold"
    temp < 0 -> "very cold"
    // If temp < 10 is true, sets description to "a bit cold"
    temp < 10 -> "a bit cold"
    // If temp < 20 is true, sets description to "warm"
    temp < 20 -> "warm"
    // Sets description to "hot" if no previous condition is satisfied
    else -> "hot"             
}
println(description)
// warm
For

for使用方法类似

for (number in 1..5) { 
    // number is the iterator and 1..5 is the range
    print(number)
}
// 12345

集合遍历

val cakes = listOf("carrot", "cheese", "chocolate")

for (cake in cakes) {
    println("Yummy, it's a $cake cake!")
}
// Yummy, it's a carrot cake!
// Yummy, it's a cheese cake!
// Yummy, it's a chocolate cake!
While

使用语法类似

var cakesEaten = 0
while (cakesEaten < 3) {
    println("Eat a cake")
    cakesEaten++
}
// Eat a cake
// Eat a cake
// Eat a cake