【Android、 kotlin】kotlin学习笔记

基本语法

Kotlin 复制代码
fun main(){
    val a=2
    var b = "Hello"
    println("$ (a - 1} $b Kotlin!")

}

Variables

只赋值一次用val read-only variables with val

赋值多次用var mutable variables with var

Standard output

printin() and print() functions

String templates

Template expressions start with $

Evaluate a niece of code with {}

基本数据类型basic types

声明变量Declare Variables

val pi : Double =3.14

var p : Int

p = pi.tolnt()

需要强制类型转换Explicit number conversions

控制流control flow

if语句

Kotlin 复制代码
max = if (a > b) a else b
Kotlin 复制代码
val a = 4
val b = 5
val max : Int
if (a > b) {
	max = a
}
else {
	max = b
}
println("max = $max")

range函数

Ranges - create a range to use ..

1..4 equivalent to 1, 2, 3, 4

1..<4 equivalent to 1, 2, 3

4 downTo 1 equivalent to 4, 3, 2, 1

1..5 step 2 equivalent to 1, 3, 5

when函数

Use when when you have a conditional expression with multiple branches

Kotlin 复制代码
val temp = 20
val description = when(temp) {
    	0 -> "zero"
    	in 1..10 -> "a bit cold"
    	in 11..<20 -> "warm"
    	else -> "hot"             
}
print(description)

for循环

The for loop iterates through anything that provides an iterator.

Kotlin 复制代码
for (i in 1..3) {
    println(i)
}
for (i in array.indices) {
    println(array[i])
}
for ((index, value) in array.withIndex()) {
    println("elem at $index is $value")
}

Array

Kotlin 复制代码
val simpleArray = arrayOf(1, 2, 3)
val intArray : Array<Int> = arrayOf(4, 5, 6)
simpleArray[0] = 10
val exampleArray = IntArray(3)

Collections: List, Set, Map

函数functions

Basic function

Kotlin 复制代码
fun sum(a: Int, b: Int): Int {
    return a + b
}

Function body can be expression

只有一条表达式语句

Kotlin 复制代码
fun sum(a: Int, b: Int) = a + b

Function without return value

Kotlin 复制代码
fun printSum(a: Int, b: Int): Unit {
    println("sum of $a and $b is ${a + b}")
}

Lambda expressions

如果lambda表达式有多个语句,最后一个语句是返回值

fun main() {

var upperCaseString : (String) -> String

upperCaseString = { string: String -> string.uppercase() }

println(upperCaseString("hello"))

}

高阶函数higher-order function

A higher-order function is a function that takes functions as parameters, or returns a function.

包含函数或者函数返回值的函数是高阶函数

fun main() {

val result = operateTwoNumber(1, 2, { a: Int, b: Int -> a + b })#最后一个变量是函数,可以它放在括号外面

}

fun operateTwoNumber(x: Int, y: Int, op: (Int, Int) -> Int): Int {

return op(x, y)

}

`it`

主要用于函数类型中,当函数只有一个参数时,`it`用来表示这个参数对象。

类classes

相关推荐
JMchen1233 小时前
Android UDP编程:实现高效实时通信的全面指南
android·经验分享·网络协议·udp·kotlin
JMchen1234 小时前
Android网络安全实战:从HTTPS到双向认证
android·经验分享·网络协议·安全·web安全·https·kotlin
JMchen12318 小时前
Android后台服务与网络保活:WorkManager的实战应用
android·java·网络·kotlin·php·android-studio
儿歌八万首21 小时前
硬核春节:用 Compose 打造“赛博鞭炮”
android·kotlin·compose·春节
消失的旧时光-19431 天前
从 Kotlin 到 Dart:为什么 sealed 是处理「多种返回结果」的最佳方式?
android·开发语言·flutter·架构·kotlin·sealed
有位神秘人1 天前
kotlin与Java中的单例模式总结
java·单例模式·kotlin
Jinkxs1 天前
Gradle - 与Groovy/Kotlin DSL对比 构建脚本语言选择指南
android·开发语言·kotlin
&有梦想的咸鱼&1 天前
Kotlin委托机制的底层实现深度解析(74)
android·开发语言·kotlin
golang学习记1 天前
IntelliJ IDEA 2025.3 重磅发布:K2 模式全面接管 Kotlin —— 告别 K1,性能飙升 40%!
java·kotlin·intellij-idea
gjxDaniel3 天前
Kotlin编程语言入门与常见问题
android·开发语言·kotlin