kotlin中的函数
最基本的打印函数
//kotlin 中是不需要分号的 (如果一行中只有一条语句)
fun main() {
print("hello world")
println("hello world")
}
//输出结果为 : hello worldhello world
//println 为打印一行 print只是普通的打印
返回整数类型
//注意 在kotlin中不需要显式对变量名进行定义 var 可以代表一切
//注意 函数的形式 fun 函数名 (第一个参数名:参数的类型 , 第二个参数名:参数的类型):返回值的类型{return 返回值}
fun main() {
var c = sum(3,5); // 突然发现这里加了分号好像也不报错哎
print(c)
}
fun sum(a:Int,b:Int):Int{
return a+b
}
//如果只有一条语句的话 上述函数也可以简单的写为 : fun sum(a:Int,b:Int):Int = a+b
//输出结果为 8
无显示定义返回值类型
//程序会自己推断出返回值的类型
fun sum(a:Int,b:Int) = a+b
返回为 void 的函数
fun printSum(a: Int, b: Int) {
println("sum of $a and $b is ${a + b}")
//输出结果 : sum of 3 and 5 is 8
//注意这里的美元符,直接将 变量所带的值在输出语句里输出
}
kotlin中的变量
/* kotlin 的变量在定义时,
可以不加类型 程序自动判断 也可以显示的增加类型
可以在声明时赋值 也可以在声明后赋值*/
val a: Int = 1 // immediate assignment
val b = 2 // `Int` type is inferred
val c: Int; c = 3
/*在同一行上有两条语句的时候,要加 分号 */
//kotlin 中也可以设置全局变量
val PI = 3.14
var x = 0
fun incrementX() {
x += 1
}
kotlin中的类和实例
类的创建
class Shape //这样就创建了一个类
//这样就是带参数的创建了一个类
class Rectangle(var height: Double, var length: Double) {
var perimeter = (height + length) * 2
}
类的继承
//可继承的类前面要加 open 不然默认是 final 不可继承
open class Shape
// : 后面跟着父类
class Rectangle(var height: Double, var length: Double): Shape() {
var perimeter = (height + length) * 2
}
条件语句
if - else
fun maxOf(a: Int, b: Int): Int {
if (a > b) {
return a
} else {
return b
}
}
//kotlin 中没有三目运算符 但是 if - else 语句可以以相同的形式使用 例如:
fun maxOf(a: Int, b: Int) = if (a > b) a else b
循环语句
for 循环
//以元素的形式遍历 list
val items = listOf("apple", "banana", "kiwifruit")
for (item in items) {
println(item)
}
//以索引的形式遍历 list
val items = listOf("apple", "banana", "kiwifruit")
for (index in items.indices) {
println("item at $index is ${items[index]}")
}
while循环
val items = listOf("apple", "banana", "kiwifruit")
var index = 0
// 喔 原来可以直接 items.size 那上面 items.indices 是做什么的咧
while (index < items.size) {
println("item at $index is ${items[index]}")
index++
}
Ranges 语句
使用 in 运算符检查数字是否在范围内。
val x = 10
val y = 9
// in 后面接开头 接 两个 . 接结尾 表示正向遍历
if (x in 1..y+1) {
println("fits in range")
}
使用 !in 运算符检查数字是否不在范围内。
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 循环 与 in 的配合使用
//正向单步遍历 1 ~ 5
for (x in 1..5) {
print(x)
}
//正向 以 step = 2 来遍历 1 ~ 10
for(x in 1..10 step 2){
print(x)
}
//逆序 以 step = 3 来遍历 9 ~ 0
for(x in 9 downTo 0 step 3){
print(x)
}
Collections
Collections 的定义
val fruit = listOf("banana","avocado","apple","kiwifruit")
Collections 的遍历
for(item in items){
println(item)
}
Collections 与 in 的配合
when{
"orange" in items -> println("juicy")
"apple" in items -> println("apple is fine too")
}
lambda 表达式在 Collections 中的应用
fruits
.filter { it.startsWith("a") } // 选出所有以 a 开头的
.sortedBy { it } // 排序
.map { it.uppercase() } // 将它的大写字母存进 map 中
.forEach { println(it) } // 遍历打印
可空值和空检查
使用有可能返回 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("'$arg1' or '$arg2' is not a number")
}
}
类型检查和自动转换
is 操作符
fun getStringLength(obj: Any): Int? { // Int ? 表示返回值可能是整数类型 或者 NULL
if (obj is String) { // 检查 obj 是不是字符串类型
// `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
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!