Kotlin 学习笔记(持续更新)

45 阅读1分钟

基础知识

定义变量(define various)

val name = "None";// 只读
var age = "25";// 可修改

变量类型(various type)

关键字类型
Byte,Short,Int,Long整型
UByte,UShort,UInt,ULong无符号整型
Float,Double浮点型
String字符串
Char字符
Boolean布尔类型
val name:String = "None";
val age:Int = "25";
val salary:Double = "3000.00";
val male:Char = '♂'

集合(Collection)

关键字类 型描述创建
Array数组arrayOf(1,2,3,4)
List列表只读listOf("triangle", "square", "circle")
MutableList列表可修改 mutableListOf("triangle", "square", "circle")
Setset只读setOf("apple", "banana", "cherry", "cherry")
MutableSetset可修改 mutableSetOf("apple", "banana", "cherry", "cherry")
Mapmap只读mapOf("apple" to 100, "kiwi" to 190, "orange" to 100)
MutableMapmap可修改mutableMapOf("apple" to 100, "kiwi" to 190, "orange" to 100)

流程控制

判断(if,when)

val count = 3;
if(count>3){
    println("count>3")
}else{
    printLn("count<=3")
}

when(count){
    1->println("count ==1")
    2,3->print("count >1")
    else ->print("count>3")
}

循环(for,while)

val numbers = 1..100;
for(a in numbers){
    println(a)
}
var i = 0;
while(i<100){
    print("${numbers.elementAt(i)}\t")
    i++;
}

函数

fun systemPrint(str:String){
    print(str)
}
fun main(){
    systemPrint("Hello Kotlin")
}