Kotlin 基础语法
一、基础语法
.kt
.java 一个public的class
toplevel
.kt,变量,常量,类,多个。混合。
-
变量
var a:Int =2//int Int,Integer,Double,double var a = 3 val π2 = 6 var ss:String?=null lateinit var str:String//不能null,必须引用类型。 val sstr:String by lazy{ "by lazy string" }/* hhh */ const val π = 3.14
-
函数
fun sum(a:Int,b:Int):Int{//= a+b return a+b } fun div(c:Int,d:Float = 1f){ //... } fun more(vararg a:Int) :Int{ a.get(0),a.get(1),a[2] //,,,,, return 999 } /** * 注释 // /**/ */ fun main(args:String){ val sum = sum(1,2) val ss = div(2) more(2,35,23) //打印 println(sum) }
-
语法
//for,when fun main(args:String){ for(i in 0..10){ println(i) } for(i in 0 until 10) println(i) for(i in 0..10 step 2)println(i) for(i in 10 downTo 0 step 2)println(i) //when var i:Type when(i){ i is String->{} i !is Float->{} i in 0..10->{} i==2->{} else->{} } when{ boolean->{} else->{} } }
-
类
- class
class Aclass(){ //属性 var name:String = "" lateinit var b:String //函数 fun getSomeInfo():String{ this::b.isInitialized//boolean return "" } } interface AAA{ fun aaa() fun bbb():Int fun ccc(){ } }
- object
object Obclass{ //成员函数。属性,单例静态函数 fun aaa(){} } // Obclass.aaa()
- data class
data class AAA(var name:String,var age:Int = 20){ var grade:String = "" } val aa = AAA("hhh",98)
- sealed class
//密封类,类似枚举类型作用 kt sealed class Slclass() data class Ac(var a:Int) :Slclass object Obc:Slclass
-
构造函数
class AAA(private var b:Int=9) class AAA private constructor(val a:Int){ val ccc = a fun aaa(){ a } } class AAA{ constructor() constructor(var b:Int){ //kkkkk } constructor(var a :Int,var b:String="") init{ //init some data } }
-
高阶函数
//also,with,let,apply,run, var s:String?=null s?.also{it-> } s?.let{it-> } //this->仅仅为了看清楚。 s?.apply{this-> length } with(s){this-> ?. } s?.run{this-> } //typealias typealias ob = java.Observer
网友评论