1、变量
- val (value) 声明一个不可变的变量,对应java中的final变量。
- var(variable) 声明一个可变的变量,对应java中的非final变量。
val a1 = 10 // final int a1 = 10 ;
val a2: Int = 20 // final int a2 = 20 ;
var b = "java" // String b = "java" ;
// 延迟初始化
private val userId:Int by lazy {
10001
}
private lateinit var userName :String
2、函数
// java
private int sum(int num1, int num2) {
return num1 + num2;
}
// kotlin
fun sum(num1: Int, num2: Int): Int {
return num1 + num2
}
// 简化版
fun sum(num1: Int, num2: Int): Int = num1 + num2
3、if语句,与java几乎没有任何区别
// kotlin
fun maxNum(num1: Int, num2: Int): Int {
var num = 0
if (num1 > num2) {
num = num1
} else {
num = num2
}
return num
}
// 简化版
fun maxNum(num1: Int, num2: Int) = if (num1 > num2) num1 else num2
4、when 条件语句,对应java的switch
// java
private String getCountry(String code) {
String country = "";
switch (code) {
case "zh-CN":
country = "中国";
break;
case "en":
country = "英国";
break;
case "fr":
country = "法国";
break;
default:
break;
}
return country;
}
// kotlin
fun getCountry(code: String): String {
var country: String
when (code) {
"zh-CN" -> country = "中国"
"en" -> country = "英国"
"fr" -> country = "法国"
else -> country = ""
}
return country
}
// 简化版
fun getCountry(code: String) = when (code) {
"zh-CN" -> "中国"
"en" -> "英国"
"fr" -> "法国"
else -> ""
}
// 与java区别
fun getCountry(code: String) = when {
code.startsWith("zh") -> "中国"
code == "en" -> "英国"
code.contains("fr") -> "法国"
else -> ""
}
5、for 循环
// kotlin
val array = arrayOf("a", "b", "c", "d", "e")
// 获取元素
for (element in array) {
println("---$element")
}
// until 从0到4,不包括5
for (i in 0 until 5) {
println("i==$i---${array[i]}")
}
// .. 从0到4
for (i in 0..4) {
println("i==$i>>>${array[i]}")
}
// downTo 从4到0
for (i in 4 downTo 0) {
println("i==$i###${array[i]}")
}
// step 步长为2
for (i in 0..4 step 2) {
println("i==$i---${array[i]}")
}
6、 类、继承、构造函数
// 加上 open 关键词才能被继承,主构造函数
open class Person(private val pName:String, private val pGender:String){
init {
println("pName=$pName")
println("pGender=$pGender")
}
}
class Student(name: String, gender: String,age:Int) : Person(name, gender) {
private var score:Int = 0
constructor(score:Int) : this("", "",0) {
this.score = score;
}
}
7、判空操作
// kotlin 变量后面加上?,表示可以为空
fun getStudent(student: Student?) {
if (student != null) {
student.score = 80
}
// 可以使用?.代替上面的if判断
student?.score = 80
student?.let {
it.score = 90
}
//当 ?: 前面的对象为空时,返回后面的值
student?.score ?: 0
// !!表示不去检查是否为空,如果为空直接抛异常
student!!.score = 60
}
}
8、扩展函数
val Float.dp get() = android.util.TypedValue.applyDimension(
android.util.TypedValue.COMPLEX_UNIT_DIP,
this,
android.content.res.Resources.getSystem().displayMetrics
)
fun android.app.Activity.toast(message: CharSequence, duration: Int = android.widget.Toast.LENGTH_SHORT){
android.widget.Toast.makeText(this, message, duration).show()
}
9、拓展函数
// let 函数就是一个作用域函数,用于对象统一做判空处理。
obj?.let{
it.funA()
it.funB()
}
// with 函数用于调用同一个类的多个方法是可以省去类名重复
with(obj){
funA()
funB()
}
// run 函数结合let函数与with函数,返回最后一句代码的值
obj?.run{
funA()
funB()
}
// apply 函数与run函数很像,区别是返回的是传入的对象本身
obj?.apply{
funA()
funB()
}
// also函数与let函数很像,区别是返回的是对象本身
obj?.also{
it.funA()
it.funB()
}