例如
一个接口(interface)
interface StateInterface {
val variationTypes: List<VariationType>
get() = emptyList()
object EMPTY : StateInterface
}
然后在这样的actionbean中声明它
open val stateInterface: StateInterface = StateInterface.EMPTY
它只是创建一个新界面吗?为什么我们需要这样做?
最佳答案
你不需要那样做。
interface StateInterface {
val variationTypes: List<VariationType>
get() = emptyList()
}
object EMPTY : StateInterface
可以正常工作,但作者决定他们希望使用阅读
StateInterface.EMPTY
而不仅仅是EMPTY
.选择这种方式的一个优点或原因是
EMPTY
键入 StateInterface.
后出现在代码完成中使其更容易找到。另一个可读性优势是任何引用
StateInterface.EMPTY
不需要额外的import
如果它不是嵌套对象,他们会这样做。import com.example.StateInterface
val x = StateInterface.EMPTY
这个位
open val stateInterface: StateInterface = StateInterface.EMPTY
是对象的属性。它是 open
所以后代实现可以override
它。如果他们不这样做,StateInterface.EMPTY
将是此属性的值。
关于oop - 在 kotlin 的接口(interface)中声明接口(interface)对象本身有什么好处,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53381142/