kotlin 语言入门指南(一)--基础语法
2017-05-18 19:17
716 查看
基于官网的Getting Start的基础语法教程部分,一共三节,这篇是第一节,翻译如下:
基础语法
定义一个包
包的声明必须放在文件头部:
不需要加上package的路径,kotlin可以自动定位package的位置。
查看更多packages
定义函数
参数是两个int型,并且返回值也是int型的函数:
没有指定返回值的函数表达式,编辑器会自动推断返回类型:
无返回值的函数(Unit,相当于java的Void):
Unit声明可以省略:
查看Functions
定义变量
定义不可变变量:
定义可变变量:
查看更多 Properties And Fields
注释
同java与javascript,支持单行与块注释:
不同于java的是,kotlin的区块注释是可以嵌套的。
查看更多Documenting Kotlin Code
字符串模板
查看更多String templates
If语句
if的表达式写法:
查看更多 if-expressions
Null的用法
引用的参数可能为null时,应该标记为null。
例:str 不能被转换为 Int 型时会返回null:
函数中使用null:
或
查看更多 Null-safety
类型检查与自动转换
用 is 操作符来判断对象是否属于某一类型,如果不可变变量或者属性是被检查的类型的实例,就不需要再为变量指定类型,在作用域内可以直接当成被检查的类型使用:
或
亦或
查看更多Classes and Type casts
for循环
or
查看更多 for loop
while
查看更多See while loop
when语句
查看更多when expression
Range
判断数字是否在范围内:
判断数字是否超出范围:
范围遍历:
过程遍历:
查看更多Ranges
使用Collections
遍历collections:
使用 in 关键字判断集合(collection)里是否包含某个对象(object):
使用lambda表达式来过滤(filter)或映射(map)集合(collections):
转载请注明原文地址:http://www.cnblogs.com/joejs/p/6875128.html
基础语法
定义一个包
包的声明必须放在文件头部:
package my.demo import java.util.* // ...
不需要加上package的路径,kotlin可以自动定位package的位置。
查看更多packages
定义函数
参数是两个int型,并且返回值也是int型的函数:
fun sum(a: Int, b: Int): Int { return a + b }
没有指定返回值的函数表达式,编辑器会自动推断返回类型:
fun sum(a: Int, b: Int) = a + b
无返回值的函数(Unit,相当于java的Void):
fun printSum(a: Int, b: Int): Unit { println("sum of $a and $b is ${a + b}") }
Unit声明可以省略:
fun printSum(a: Int, b: Int) { println("sum of $a and $b is ${a + b}") }
查看Functions
定义变量
定义不可变变量:
val a: Int = 1 // 指定类型的变量 val b = 2 // 推断为`Int`型 val c: Int // 没有初始值时必须指定变量类型 c = 3 // 赋值
定义可变变量:
var x = 5 // 推断为`Int`型 x += 1
查看更多 Properties And Fields
注释
同java与javascript,支持单行与块注释:
// This is an end-of-line comment /* This is a block comment on multiple lines. */
不同于java的是,kotlin的区块注释是可以嵌套的。
查看更多Documenting Kotlin Code
字符串模板
var a = 1 // 字符变量: val s1 = "a is $a" a = 2 // 任意表达式: val s2 = "${s1.replace("is", "was")}, but now is $a"
查看更多String templates
If语句
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
查看更多 if-expressions
Null的用法
引用的参数可能为null时,应该标记为null。
例:str 不能被转换为 Int 型时会返回null:
fun parseInt(str: String): Int? { // ... }
函数中使用null:
fun printProduct(arg1: String, arg2: String) { val x = parseInt(arg1) val y = parseInt(arg2) // Using `x * y` yields error because they may hold nulls. if (x != null && y != null) { // x and y are automatically cast to non-nullable after null check println(x * y) } else { println("either '$arg1' or '$arg2' is not a number") } }
或
// ... if (x == null) { println("Wrong number format in arg1: '${arg1}'") return } if (y == null) { println("Wrong number format in arg2: '${arg2}'") return } // x and y are automatically cast to non-nullable after null check println(x * y)
查看更多 Null-safety
类型检查与自动转换
用 is 操作符来判断对象是否属于某一类型,如果不可变变量或者属性是被检查的类型的实例,就不需要再为变量指定类型,在作用域内可以直接当成被检查的类型使用:
fun getStringLength(obj: Any): Int? { if (obj is String) { // 在此作用域里`obj` 自动被当成 `String` 类型使用 return obj.length } // `obj` 在外层仍然是 `Any` 类型 return null }
或
fun getStringLength(obj: Any): Int? { if (obj !is String) return null // `obj` 自动转为 `string` 类型 return obj.length }
亦或
fun getStringLength(obj: Any): Int? { // `&&` 操作符右边的 `obj` 自动转为 `String` 类型 if (obj is String && obj.length > 0) { return obj.length } return null }
查看更多Classes and Type casts
for循环
val items = listOf("apple", "banana", "kiwi") for (item in items) { println(item) }
or
val items = listOf("apple", "banana", "kiwi") for (index in items.indices) { println("item at $index is ${items[index]}") }
查看更多 for loop
while
val items = listOf("apple", "banana", "kiwi") var index = 0 while (index < items.size) { println("item at $index is ${items[index]}") index++ }
查看更多See while loop
when语句
fun describe(obj: Any): String = when (obj) { 1 -> "One" "Hello" -> "Greeting" is Long -> "Long" !is String -> "Not a string" else -> "Unknown" }
查看更多when expression
Range
判断数字是否在范围内:
val x = 10 val y = 9 if (x in 1..y+1) { println("fits in range") }
判断数字是否超出范围:
val list = listOf("a", "b", "c") if (-1 !in 0..list.lastIndex) { println("-1 is out of range") } if (list.size !in list.indices) { println("list size is out of valid list indices range too") }
范围遍历:
for (x in 1..5) { print(x) }
过程遍历:
for (x in 1..10 step 2) { print(x) } for (x in 9 downTo 0 step 3) { print(x) }
查看更多Ranges
使用Collections
遍历collections:
for (item in items) { println(item) }
使用 in 关键字判断集合(collection)里是否包含某个对象(object):
when { "orange" in items -> println("juicy") "apple" in items -> println("apple is fine too") }
使用lambda表达式来过滤(filter)或映射(map)集合(collections):
fruits .filter { it.startsWith("a") } .sortedBy { it } .map { it.toUpperCase() } .forEach { println(it) }
转载请注明原文地址:http://www.cnblogs.com/joejs/p/6875128.html
相关文章推荐
- Android 谷歌官方Kotlin语言入门教程(一)基础语法
- Markdown入门指南&基础语法
- Kotlin-01.入门介绍和基础语法(Basic Syntax)
- kotlin 语言入门指南(三)--编码习惯
- Kotlin 入门基础语法学习笔记
- Android kotlin入门与基础语法一
- Android kotlin入门与基础语法二
- Kotlin学习笔记1-1 入门-语法基础
- kotlin 语言入门指南(二)--代码风格
- Go语言入门教程之基础语法快速入门
- 0712c#第一天C#语言基础介绍与语法入门
- python入门系列(3) -- python语言基础语法
- jsp入门语法基础
- LINQ——语言级集成查询入门指南
- JavaFX脚本语言入门(1)――语法概要
- MSDN-C#编程指南-笔记-(二)-C#语言基础
- salesforce.com开发平台 Apex语言介绍 (一.基础语法介绍)
- 零基础易语言入门教程(三)之了解控制台程序
- 基础知识03 - 零基础入门学习汇编语言03
- MS SQL入门基础:Transact-SQL 语言概述