科特林。接口作为参数

Kotlin. Interface as parameter

我在枚举中存储静态数据。它们可以有很多,但它们具有相同的结构,由一个特殊的接口指定,以简化所有它们的工作。我在 Swift 中使用了类似的逻辑并且它有效,但 Kotlin 不允许我使用这样的逻辑。

interface DataElement {
    val code: UInt
    val name: String
}

enum class DataEnum1: DataElement {

    Apple {
        override val code: UInt
            get() = 1u
    },

    Orange {
        override val code: UInt
            get() = 2u
    },

    Watermelon {
        override val code: UInt
            get() = 3u
    }
}

enum class DataEnum2: DataElement {

    Blueberry {
        override val code: UInt
            get() = 4u
    },

    Strawberry {
        override val code: UInt
            get() = 5u
    },

    Blackberry {
        override val code: UInt
            get() = 6u
    }
}

问题来了

fun someFun() {
  val array = DataEnum1.values()
  anotherFun(array) // TYPE MISMATCH
  //Required:
  //Array<PeripheralDataElement>
  //Found:
  //Array<DataEnum1>
}
    
fun anotherFun(items: Array<DataElement>) {
     // some logic
}

数组在 Kotlin 中是不变的,这意味着您不能直接将 Array<SubClass> 传递给接受 Array<SuperClass> 的函数。但是,您可以像这样定义函数以使其接受协变数组:

fun anotherFun(items: Array<out DataElement>) {
    // ...
}

现在您的函数也接受 Array<SubClass>

旁注: 这自动适用于列表,因为 List 在接口级别的类型参数中被定义为协变,例如 interface List<out T>.