前言:
在线编辑IDE:https://try.kotlinlang.org/#/Examples/Hello,%20world!/Simplest%20version/Simplest%20version.kt
本地PDF文档:file:///C:/Users/Administrator.PC-201704222111/Desktop/kotlin-reference-chinese.pdf
1.基础语法
1.1带有两个Int参数、返回Int的函数:
fun sum(a:Int,b:Int):Int{
return a+b
}
fun main(args: Array) {
println("sum 1 and 3 is");
println(sum(1,3))
}
打印结果:
1.2将表达式作为主体,返回值类型自动推断的函数:
fun sum(a:Int,b:Int)=a+b
fun main(args: Array) {
println("sum 1 and 3 is ${sum(1,3)}");
}
打印结果:
1.21函数返回无意义的值(Unit返回类型可以省略)
fun printSum(a:Int,b:Int):Unit{
println("sum of $a and $b is ${a+b}");
}
fun main(args: Array) {
printSum(-1,8);
}
打印结果:
1.3定义变量
1.3.1一次赋值只读的局部变量
fun main(args: Array) {
val a:Int =1
val b=2
val c:Int
c=1
println("a=$a,b=$b,c=$c");
}
打印结果:
1.3.2可变变量
fun main(args: Array) {
var x=5
x+=1
println("x=$x");
}
打印结果:
1.3.3顶层变量
val PI=3.14
var x=0
fun incrementX(){
x+=1
}
fun main(args: Array) {
println("x=$x,PI=$PI")
incrementX()
println("incrementX()")
println("x=$x,PI=$PI")
}
打印结果:
1.4使用字符串模板
fun main(args: Array) {
var a=1
val s1="a is $a"
a=2
val s2="${s1.replace("is","was")},but now is $a"
println(s2)
}
打印结果:
1.5使用条件表达式
fun maxOf(a:Int,b:Int):Int{
if(a>b){
return a
}else{
return b
}
}
//使用if作为表达式
fun maxOf(a:Int,b:Int)=if(a>b)a else b
fun main(args: Array) {
println("max of 11 and 42 is ${maxOf(11,42)}");
}
打印结果:
1.6使用可空值及null检测
1.6.1当某个变量可以为null的时候,必须在声明处的类型后面?来标识该引用可以为null
fun parseInt(str:String):Int?{
return str.toIntOrNull()
}
fun printProduct(arg1:String,arg2:String){
val x=parseInt(arg1)
val y=parseInt(arg2)
if(x!=null&&y!=null){
println(x*y);
}else{
println("either '$arg1' or '$arg2' is not a number")
}
}
fun main(args: Array) {
printProduct("6","7")
printProduct("a","7")
printProduct("a","b")
}
打印结果:
1.7使用类型检测及自动类型转换
fun getStringLength(obj:Any):Int?{
if(obj is String){
return obj.length
}
return null
}
fun main(args: Array) {
fun printLength(obj:Any){
println("'$obj' String length is ${getStringLength(obj)?:"err,not a string"}");
}
printLength("Asmewill");
printLength(10000);
printLength(listOf(Any()));
}
打印结果:
1.8 使用for循环
fun main(args:Array){
val items=listOf("aaa","bbb","cccc")
for(index in items.indices){
println("item at $index is ${items[index]}")
}
}
或者
fun main(args: Array) {
val items=listOf("aaa","bbb","ccc","1321479214hkhk");
for(Asmewill in items){
println(Asmewill)
}
}
打印结果:
1.9使用while循环
fun main(args: Array) {
val items=listOf("aaa","bbb","ccc","1321479214hkhk");
var index=0
while (index
1.10使用When表达式
fun describe(obj:Any):String?=
when(obj){
1 ->"one"
"hello"->"Greeting"
is Long ->"Long"
!is String ->"Not a String"
else ->null
}
fun main(args: Array) {
println(describe(1))
println(describe("hello"))
println(describe(1000L))
println(describe(2))
println(describe("other"))
}
打印结果:
1.11使用区间(range)
1.11.1使用in 运算符来检测某个数字是否在指定的区间内:
fun main(args: Array) {
val x=10;
val y=9;
if(x in 1..y+1){
println("fits in range ${1..y+1}");
}else{
println("fits is not in range");
}
}
打印结果:
1.11.2检测某个数字是否在指定的区间外
fun main(args: Array) {
val list=listOf("a","b","c");
if(-1 !in 0..list.lastIndex){
println("-1 is out range ${0..list.lastIndex}");
}
if("d" !in list){
println("list size is out of valid list indices range too");
}
}
打印结果:
1.11.3区间迭代
fun main(args: Array) {
val list=listOf("a","b","c")
for(x in list){
println(x)
}
}
fun main(args: Array) {
for(x in 1..5){
println(x)
}
}
打印结果:
1.11.4数列迭代
fun main(args: Array) {
for(x in 1..10 step 2){
println(x);
}
println("-_-");
for(x in 9 downTo 0 step 3){
println(x);
}
}
打印结果:
1.12 使用集合
1.12.1对集合进行迭代
fun main(args: Array) {
val items=listOf("Asmewill","Huangbo","zhouxj");
for(item in items){
println(item);
}
}
打印结果:
1.12.2使用in 运算符来判断集合是否包含某个实例,使用lambda表达式来过滤filter和映射map集合:
fun main(args: Array) {
val items=listOf("Asmewill","Huangbo","zhouxj")
for(item in items){
println(item)
}
println("----------")
when{
"Asmewill" in items -> println("he is in android group")
"zhougm" in items -> println("he is not in android group")
}
println("----------")
val andGroups=listOf("asmewill","apple","avocado","lallla")
andGroups
.filter{it.startsWith("a")}
.sortedBy{it}
.map{it.toUpperCase()}
.forEach{println(it)}
}
打印结果: