gpt4 book ai didi

Kotlin 基础语法详细介绍

转载 作者:qq735679552 更新时间:2022-09-27 22:32:09 25 4
gpt4 key购买 nike

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的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com