一、object
object DemoConstant1 {
const val subName = ""
fun a(){
}
}
获取调用:
DemoConstant1.subName
DemoConstant1.a()
二、class
class DemoConstant2 {
val subName = ""
fun a(){
}
}
获取调用:
var demoConstant2 = DemoConstant2()
demoConstant2.subName
demoConstant2.a()
三、class -> object
MyObject为DemoConstant3的静态内部类,
subName属于内部类MyObject属性
class DemoConstant3 {
object MyObject{
const val subName = ""
fun a(){
}
}
}
获取调用:
DemoConstant3.MyObject.subName
DemoConstant3.MyObject.a()
四、class -> companion object (一)
Companion 为 DemoConstant 的静态内部类,
subName属于 DemoConstant 的属性
class DemoConstant4 {
companion object {
const val subName = ""
fun a() {
}
}
}
获取调用:
DemoConstant4.subName
DemoConstant4.a()
五、class -> companion object (二)
MyObject 为 DemoConstant 的静态内部类,
subName属于 DemoConstant 的属性
class DemoConstant {
companion object MyObject {
const val subName = ""
fun a(){
}
}
}
获取调用:
DemoConstant.subName
DemoConstant.a()