变量
val a = 100 var b = 200 val a1: Int = 100 var b1: Int = 200
循环语句
for (i in 0..10) {//[0,10] print(i) } for (i in 0 until 10) {//[0,10) print(i) } for (i in 0..10 step 2) {//步长加2 i+=2; print(i) } for (i in 10 downTo 1) {//降序[10,1] print(i) }
构造函数
/** * 次构造函数 主构造函数只能有一个,次构造函数可以有很多个 */ //次构造函数,次构造函数必须直接或者间接调用主构造函数 class Tom(var name: String,var age: Int, var sno: String, var grade: Int) { constructor(sno: String, grade: Int) : this("", 0, sno, grade) constructor() : this("", 0) override fun toString(): String { return "Tom(name=$name, age=$age, sno=$sno, grade=$grade)" } } //推荐 data class Mo( var name: String , var age: Int = 1, var sno: String = "", var grade: Int = 1 )
构造函数调用
val az = Tom("a", 1, "son", 2) val bb = Tom("liek", 3) Log.i(TAG, "onCreate: az = $az \n bb = $bb") Log.i(TAG, "onCreate: ${Mo("l")}")
接口
/** * 接口 */ interface testI{ fun sleep() fun eat() fun skip(){ Log.d(TAG,"跳跃") } } /** * 实现接口时不用加上小括号 */ class Dog:testI{ override fun sleep() { // TODO("Not yet implemented") Log.d(TAG,"睡觉") } override fun eat() { // TODO("Not yet implemented") Log.d(TAG,"吃饭") } }
接口调用
var dog=Dog() // dog.eat() dog.sleep() dog.skip()
集合
val listof= listOf("a","b","c")//该集合只可读取,不可增删改 for (item in listof){ print("listOf:$item") Log.d(TAG,"listOf:$item") } val mutableListof= mutableListOf<String>("d","e","f")//增删改查 mutableListof.add("g") for (item in mutableListof){ print(item) Log.d(TAG,"murableListof: $item") } val hashMap=HashMap<String,Int>() hashMap["a"]=1 hashMap["b"]=2 for (item in hashMap){ print("hashmap : $item") Log.d(TAG,"hashmap : $item") } val mapOf= mapOf<String,Int>("c" to 1,"d" to 2) for ((key,value) in mapOf){ print("key: $key vlue: $value") Log.d(TAG,"key: $key vlue: $value") }
函数
fun mehod1(x: Int, y: Int): Int { return x + y } fun method2(x: Int, y: Int): Int = x + y fun method3(x: Int, y: Int) = x + y
流程控制语句
fun xunhuan1(num1: Int, num2: Int): Int { return if (num1 > num2) { num1 } else { num2 } } fun xuhuan2(num1: Int, num2: Int) = if (num1 > num2) { num1 } else { num2 } fun xunhuan3(num1: Int, num2: Int) = if (num1 > num2) num1 else num2 fun checkNum(number: Number) { when (number) { is Int -> print("int") is Double -> print("double") is Long -> print("long") is Float -> print("float") else -> print("is not number") } } //方法要有括号 fun checkNumber(str: String) = when { str == "tom" -> 77 str == "lyc" -> 88 else -> 0 }
对象和继承
/** * 对象 kotlin中没有new这个关键字,创建对象直接用对象名跟上括号就可以 */ fun xxx() { val person = Person() person.name = "Tom" person.age = 18 person.eat() } //类不用加上小括号 open class Person { var name = "" var age = 0 fun eat() { print(name + "," + age + ",在吃饭") print("$name,$age,在吃饭") } } /** * 继承 在kotlin中是没有继承的,若实现继承需要在父类前加关键字open * 继承时要加上括号 */ class student : Person() { var sno = "" var grade = 100 }
修饰符
/** * 修饰符 * public 所有类都可以访问,kotlin默认都是public * private 只有自己可以访问 * protect 子类可以访问 * internal 比如自己开发了一个模块,但是有一些函数是在模块内部调用的,不想暴露给别人的话 */
单例
object singleClass { fun test(){ print("单例") } }
数据类
/** * 数据类 * 同java中新建一个类定义变量重写,get set toString equals hashcode的作用是一样的,这些kit都帮你默认实现了,特别适合做数据json格式 */ data class cellPhone(var brand:String,var price:Int)
笔记:
主构造函数只能有一个,次构造函数不限,可多个
类的创建没有括号,引用时带括号
接口创建与实现时都不用括号
类作为父类被继承时,需用关键字open修饰