美文网首页Kotlin学习
Kotlin静态内部类,常量,三元运算符

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

作者: GexYY | 来源:发表于2018-05-18 17:18 被阅读0次

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

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

相关文章

网友评论

    本文标题:Kotlin静态内部类,常量,三元运算符

    本文链接:https://www.haomeiwen.com/subject/xqagdftx.html