目录
1.1 变量
1.2 if条件语句 、switch 条件语句
1.3 循环语句
1.4 类与对象和接口
1.5 单例模式
1.6 集合
1.7 线程
1.8 空指针检查
1.1变量
```
val a = 10
var a = 10
fun main() {
val a: Int = 10;
a = a * 10
println(a)
}
```
val不可变变量, var可变变量。 main声明val报错,可变的值需要var,并且Kotlin必须从一开始就确定类型是否可变。基本数据类型跟Java一样,但不需要像Java书写变量类型。直接赋值自动识别、也可以通过 (:数据类型)来表示类型。
1.2 if条件语句、switch 条件语句
------if
```
fun larginValue(pa1: Int, pa2: Int): Int {
var value = if (pa1 > pa2) {
pa1
} else {
pa2
}
return value
}
fun checkVale(par: Int, par2: Int) = if (par > par2) {
par
}else {
par2
}
```
-----switch
匹配值 -> {执行逻辑}
```
fun caseName(name: String) {
val sadas = when (name) {
"tome" -> "12123"
"tome2" -> "122"
"tome3" -> "1231"
else -> "1212"
}
println(sadas)
}
fun caseName(name: String) = when (name) {
"tome" -> 1
"asdsad" -> 2
else -> null
}
fun checkVarType(num: Number) = when (num) {
is Int -> println("is int")
is Double -> println("is double")
else -> ""
}
fun caseName2(name: String) = when {
name.startsWith("tome") -> 86
name == "123123" -> 88
name == "123" -> 89
else -> 1
}
```
1.3 循环语句
```
val range = 0..10
for (i in range) {
println(i)
}
val range = 0 until 10
for (i in range) {
println(i)
}
val range = 0..10
for (i in range step 2) {
println(i)
}
val range = 0..10
for (i in range) {
println(i)
}
val range = 0..10
for (i in 10 downTo 0) {
println(i)
}
```
Kotlin循环有区间的概念,range是[],除此直接in之外,还附带了些别的函数
until = [0,10)
step跳过,相当于i = i+2
downTo倒序
1.4 类与对象和接口
```
interface Study {
fun readBook()
fun doHome(){
println("this is a test interface")
}
}
```
```
class Student(val name: String, val age: String, hobby: String, like: String) :
Persion(hobby, like), Study {
constructor(name: String, age: String) : this(name, age, "", "")
override fun toString(): String {
return super.toString()
}
override fun readBook() {
println("Student(name='$name', age='$age', hobby='$hobby', like='$like')")
}
override fun doHome() {
super.doHome()
println("Student(name='$name', age='$age', hobby='$hobby', like='$like')")
}
}
```
```
open class Persion(val hobby: String, val like: String) {
}
```
open表示开放,可继承。: 来继承或者实现。
接口方法 加花括号表示默认实现
Student(表示的默认值,和传输给你Persion的值)
constructor就是构造方法
1.5 单例模式
```
object Singleton {
fun singleTest() {
println("this is a single")
}
}
```
这就表示是单例了
1.6 集合
```
fun main() {
val arrayList = ArrayList<String>()
arrayList.add("organg")
arrayList.add("bear")
arrayList.add("perar")
arrayList.add("apple")
//生成一个新的 list
val listOf = listOf<String>("organg", "bear", "perar", "apple")
val newList = listOf.filter { it.length > 5 }.map { it.toUpperCase() }
for (fruit in newList) {
println(fruit)
}
//mutable-x表示可更改的数据集. 用listOf生成的List无add
val mutableListOf = mutableListOf("or", "1212")
mutableListOf.add("!23123");
val set = setOf("organg", "bear", "perar", "apple")
for (se in set) {
println(se)
}
//map创建于遍历
val mapOf = mapOf("12" to 1, "22" to 2)
for ((a, b) in mapOf) {
println(a + b)
}
//map另外一种创建于替换
val hashMap = HashMap<String, String>()
hashMap["orange2"] = "2"
hashMap["orange3"] = "3"
hashMap["orange4"] = "1"
hashMap.put("1", "or")
for ((a, b) in hashMap) {
println(a + "===" + b)
}
//map另外一种创建于替换
val hashMap2 = HashMap<String, String>()
hashMap2.put("1", "orange1")
hashMap2.put("2", "orange2")
hashMap2.put("3", "orange3")
for ((a, b) in hashMap2) {
println(a + "===" + b)
}
}
```
1.7 线程
```
fun main() {
//线程简化
//1-
Thread(object : Runnable {
override fun run() {
println("this is a thread - 1")
}
}).start()
//2-
// 根据kotlin语法,可以对其简化
Thread({
println("this is a thread - 2")
}).start()
//3-
Thread {
println("this is a thread - 3")
}.start()
}
```
1.8 空指针检查
```
var content: String? = "adasdasd"
fun main() {
printUpss()
}
fun printUpss() {
// ?: 左侧是不为空 右侧为 为空的处理
// ?. 表示判空处理 - 返回 null
// val toUpperCase = content?.toUpperCase() ?: "123123"
// !!. 表示强制执行,不管验证
val toUpperCase = content!!.toUpperCase()
println(toUpperCase)
}
fun doStudy2(study: Student?) {
// kotlin默认所有变量对象都不可为null
// 打 ? 表示可为空
study?.doHome()
study?.readBook()
// ?: 左侧是不为空 右侧为 为空的处理
// ?. 表示判空处理 - 返回 null
study?.doHome() ?: "1"
}
```