Kotlin学习

Kotlin静态内部类,常量,三元运算符

2018-05-18  本文已影响0人  GexYY

静态内部变量和常量定义方法

companion object {
        private const val MSG_UPDATE_PROGRESS = 1
        private const val MSG_UPDATE_TIME= 1000
    }

静态内部类

class Parent{
    //默认就是 java的 public static
    class Child
}
fun main(args: Array<String>) {
    val inner = Parent.Child()

}

非静态内部类

class Parent{
    //非静态内部类声明
    inner class Child
}
fun main(args: Array<String>) {
    val inner = Parent().Child()
}

内部类访问外部持有类的this

class Parent{
    val a:Int = 0
    inner class Child{
        val a:Int = 5
        fun hello(){
            println(this@Parent.a)
        }
    }
}
fun main(args: Array<String>) {
    val inner = Parent().Child()
    inner.hello()
}

三元运算符

 //Java写法
int size=list!=null?list.size:0
//kotlin写法
val size=if(list!=null) list.size() else 0
上一篇 下一篇

猜你喜欢

热点阅读