当前位置: 首页 > news >正文

【Android、 kotlin】kotlin学习笔记

基本语法

fun main(){val a=2var 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

99802211d7634aa694ad8d16ec6263b8.jpg

控制流control flow

if语句

max = if (a > b) a else b
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

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. 

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

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

fun sum(a: Int, b: Int): Int {return a + b
}

Function body can be expression

只有一条表达式语句

fun sum(a: Int, b: Int) = a + b

Function without return value

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

http://www.lryc.cn/news/331843.html

相关文章:

  • Debian 配置国内软件源
  • 选数(dfs,isprime)
  • RocketMQ(版本4.9.4)+RocketMQ_Dashbord环境搭建(生产者、消费者的前置环境搭建)
  • css隐藏溢出隐藏的滚动条
  • scss常用混入(mixin)、@inclue
  • 补代码随想录算法训练营第44天 | 完全背包、518. 零钱兑换 II 、377. 组合总和 Ⅳ
  • 【Linux】网络基础常识{OSI七层模型/ TCP/IP / 端口号 /各种协议}
  • python--面向对象编程和类的定义,对象的创建
  • nssm 工具把asp.net core mvc变成 windows服务,使用nginx反向代理访问
  • String Encryptor custom Bean not found with name ‘jasyptStringEncryptor‘...
  • FastAPI+React全栈开发14 FastAPI如何开发REST接口
  • 在 DDD 中,如何处理领域对象的持久化?
  • centos 如何安装nvidia-container-runtime
  • 非写代码无以致远
  • 刷题之Leetcode34题(超级详细)
  • 从0到1构建uniapp应用-store状态管理
  • Uinx线程详解
  • 线性代数笔记23--马尔可夫矩阵、傅里叶级数
  • Elasticsearch 压测实践总结
  • Spirngboot JWT快速配置和使用
  • 【Java SE】继承
  • 设计模式(19):策略模式
  • Linux 命令 top 详解
  • Android安卓开发 - 简单介绍(一)
  • AJAX —— 学习(二)
  • CSC博士联培申请时间线
  • 大数据实验三-HBase编程实践
  • 【Python】Pillow支持的图像文件格式
  • 算法——最小生成树
  • OpenHarmony相机和媒体库-如何在ArkTS中调用相机拍照和录像。