Questions tagged «kotlin-companion»

4
为什么我们用“伴侣对象”代替Kotlin中的Java静态字段?
“伴侣对象”的预期含义是什么?到目前为止,我一直在使用它只是static在需要时替换Java 。 我感到困惑: 为什么叫“同伴”? 这是否意味着要创建多个静态属性,我必须将其分组在一起companion object? 为了立即创建一个作用于一个类的单例实例,我经常写 : companion object { val singleton by lazy { ... } } 这似乎是一种惯用的方式。有什么更好的方法?
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.