- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章Kotlin 基础语法详细介绍由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
Kotlin 基础语法详细介绍 。
基础语法 。
定义包名 。
包名的定义应当在源文件的头部 。
1
2
3
4
5
|
package
my.demo
import
java.util.*
// ...
|
文件路径和包名并不要求匹配,源文件可以被放置在文件系统任意位置 。
参考:包 。
定义函数 。
函数有两个Int类型参数和Int类型返回值:
1
2
3
|
fun sum(a: Int, b: Int): Int {
return
a + b
}
|
函数体中只有一个表达式并且作为函数的返回值:
1
|
fun sum(a: Int, b: Int) = a + b
|
函数没有返回值:
1
2
3
|
fun printSum(a: Int, b: Int): Unit {
print(a + b)
}
|
Unit类型的返回值类型可以省略:
1
2
3
|
fun printSum(a: Int, b: Int) {
print(a + b)
}
|
参见:函数 。
定义局部变量 。
定义只读类型的局部变量:
1
2
3
4
|
val a: Int =
1
val b =
1
// `Int` 类型是被编译器推理出
val c: Int
// 当变量的初始值没有被提供时,需要定义变量的类型
c =
1
// 赋值
|
可变局部变量 。
1
2
|
var x =
5
// `Int` 类型是被编译器推理出的
x +=
1
|
可参见:属性与变量 。
注释 。
就像Java与JavaScripe,Kotlin也支持行注释与代码块注释.
1
2
3
4
|
// 这是一段行注释
/* 这是一段代码块
注释 */
|
不像Java,代码块注释在Kotlin中是可以被叠加的.
参见:Kotlin代码文档 。
使用字符串模板 。
1
2
3
4
5
|
fun main(args: Array<String>) {
if
(args.size ==
0
)
return
print(
"First argument: ${args[0]}"
)
}
|
参见:字符串模板 。
使用条件表达式 。
1
2
3
4
5
6
|
fun max(a: Int, b: Int): Int {
if
(a > b)
return
a
else
return
b
}
|
使用 if 作为一个表达式返回值:
1
|
fun max(a: Int, b: Int) =
if
(a > b) a
else
b
|
参见:if 表达式 。
使用可空变量并检测是否为空 。
一个引用必须明确的被设置为可为空当其可能为空值时.
返回值为null 如果str 没有包含数值:
1
2
3
|
fun parseInt(str: String): Int? {
// ...
}
|
函数的返回值可能为空:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
fun main(args: Array<String>) {
if
(args.size <
2
) {
print(
"Two integers expected"
)
return
}
val x = parseInt(args[
0
])
val y = parseInt(args[
1
])
// 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
print(x * y)
}
}
|
或者:
1
2
3
4
5
6
7
8
9
10
11
12
|
// ...
if
(x ==
null
) {
print(
"Wrong number format in '${args[0]}'"
)
return
}
if
(y ==
null
) {
print(
"Wrong number format in '${args[1]}'"
)
return
}
// x and y are automatically cast to non-nullable after null check
print(x * y)
|
参见:空安全 。
使用类型检测和类型自动转换 。
is 操作符用于检测一个表达式是否是某一种类型,假如可变参数或者属性被检测为某一种类型,那么就不在需要明确的类型转换:
1
2
3
4
5
6
7
8
9
|
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
}
|
或者:
1
2
3
4
5
6
7
|
fun getStringLength(obj: Any): Int? {
if
(obj !is String)
return
null
// `obj` is automatically cast to `String` in this branch
return
obj.length
}
|
或者:
1
2
3
4
5
6
7
|
fun getStringLength(obj: Any): Int? {
// `obj` is automatically cast to `String` on the right-hand side of `&&`
if
(obj is String && obj.length >
0
)
return
obj.length
return
null
}
|
参见:类和类型转换 。
使用for循环 。
1
2
3
4
|
fun main(args: Array<String>) {
for
(arg in args)
print(arg)
}
|
或者:
1
2
|
for
(i in args.indices)
print(args[i])
|
参见:for循环 。
使用while循环 。
1
2
3
4
5
|
fun main(args: Array<String>) {
var i =
0
while
(i < args.size)
print(args[i++])
}
|
参见:while循环 。
使用when表达式 。
1
2
3
4
5
6
7
8
9
|
fun cases(obj: Any) {
when (obj) {
1
-> print(
"One"
)
"Hello"
-> print(
"Greeting"
)
is Long -> print(
"Long"
)
!is String -> print(
"Not a string"
)
else
-> print(
"Unknown"
)
}
}
|
参见:when表达式 。
使用范围表达式 。
检测一个数值是否在一个范围内,若在则使用in操作符 。
1
2
|
if
(x in
1
..y-
1
)
print(
"OK"
)
|
检测一个数值是否不在一个范围内,若不在:
1
2
|
if
(x !in
0
..array.lastIndex)
print(
"Out"
)
|
迭代一个数据范围项:
1
2
|
for
(x in
1
..
5
)
print(x)
|
参见:范围 。
使用集合 。
迭代一个集合:
1
2
|
for
(name in names)
println(name)
|
检测一个集合是否包含某个数据项,使用in操作符 。
1
2
|
if
(text in names)
// names.contains(text) is called
print(
"Yes"
)
|
使用lambda表达式过滤或者转换一个集合:
1
2
3
4
5
|
names
.filter { it.startsWith(
"A"
) }
.sortedBy { it }
.map { it.toUpperCase() }
.forEach { print(it) }
|
参见:高阶函数和Lambda表达式 。
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持! 。
原文链接:http://blog.csdn.net/qq_23547831/article/details/52923248 。
最后此篇关于Kotlin 基础语法详细介绍的文章就讲到这里了,如果你想了解更多关于Kotlin 基础语法详细介绍的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
在此处回答的另一个问题中,我发现了以下 JavaScript代码: function _dom_trackActiveElement(evt) { if (evt && evt.target)
if (A == 0) OR (B == 0) 怎么说? 最佳答案 只是为了讽刺: if (A === 0 || B === 0) 关于语法,我们在Stack Overflow上找到一个类似的问题:
var ret = [] ,xresult = document.evaluate(exp, rootEl, null, X
我一直在寻找一些类似于下例的 JavaScript。有人可以解释一下吗,因为我以前从未见过这样编写的 JavaScript。 “SomethingHere”和冒号代表什么?我习惯于看到函数 myFun
这是我的程序: delimiter // drop procedure if exists migContactToActor; create procedure migContactToActor(
我遇到了一个问题。我一直在使用 gcc 编译/汇编我的 C 代码一段时间,并且习惯了阅读 Intel 汇编语法。我在生成程序集文件时使用了 -masm=intel 标志。 但是最近因为公司迁移,拿到了
自上而下和自下而上语法有什么区别?举个例子就太好了。 最佳答案 首先,语法本身不是自上而下或自下而上的,解析器是(尽管有些语法可以被其中一个解析,但不能被另一个解析)。 从实践的角度来看,主要区别在于
我知道这是草率的代码,但它是: display dialog ("Start Screensaver. Please type: matrix, coffee, waffles, star, wate
这个问题已经有答案了: Giving name to a loop (6 个回答) 已关闭 8 年前。 我见过这个字符在 C# 中使用,就像 Java 中的扩展一样,但最近我在代码中发现了这个 loo
我正在尝试编写一个函数来检查字符串是否为回文,但我认为在使用字符串指针时存在一些错误。这段代码有什么问题? #include #include #define MAX 1000 int IsPalin
所以在this question我询问了一些 Javascript 是如何被压缩的。问题已得到解答,但以下片段让我非常困惑,以至于我不得不问另一个问题。在这里: for (Y = 0; $ = 'zx
假设我有一个接受这些参数的函数。 int create(Ptr * p,void * (*insert)(void *, void *)) { //return something later } 结
这个问题已经有答案了: Bitwise '&' operator (6 个回答) 已关闭 5 年前。 我在代码中找到了这个,但我从未遇到过像 & 这样的事情,仅 && if ((code & 1) =
我在处理继承类及其中的构造函数和方法的语法时遇到了问题。 我想实现一个类日期和一个子类 date_ISO,它们将按特定顺序设置给定的日、月、年,并通过一种方法将其写入字符串。我觉得我的基类日期工作正常
我正在尝试通过存储过程填充表,如下所示: SET @resultsCount = (SELECT COUNT(*) FROM tableA); SET @i = 0; WHILE @i THEN
谁能解释一下下面代码中的“<<”? mysql test<
刚刚开始学习 MySQL,这是一个菜鸟问题,也是我在 StackOverflow 上的第一个问题。 假设我有 12 个订单状态,我想从其中的 5 个中选择总计。我会使用: SELECT SUM(tot
我的编程背景是在学校学过一点Java。由于某些原因,JavaScript 语法往往让我感到困惑。下面的 JavaScript 代码是一种我不知道如何构成的语法模式: foo.ready = funct
我正在阅读 javascript 源代码,并且我以前没有编写过 javascript。我对它的一些语法感到困惑。 $(function () { window.onload=function
我什至不知道如何命名我想要的东西。那么让我举个例子来解释一下。 虽然火狐使用textContent,但其他浏览器支持innerText属性。顺便说一句,如果我使用了错误的术语,请纠正我。无论如何,到目
我是一名优秀的程序员,十分优秀!