Kotlin 数据 class 和 30 多个属性

Kotlin data class and 30+ attributes

我们如何在 kotlin 中创建数据 classes,其中 class 包含超过 25 个属性。

用例是 java 中有一个 class,它具有超过 25+ 个属性,需要转换为 kotlin 数据 class,而且问题是这样做是合适的吗?如果可以,我们该怎么做?

有可能。举个例子:

data class Data(
    val a00: Int,
    val a01: Int,
    val a02: Int,
    val a03: Int,
    val a04: Int,
    val a05: Int,
    val a06: Int,
    val a07: Int,
    val a08: Int,
    val a09: Int,
    val a10: Int,
    val a11: Int,
    val a12: Int,
    val a13: Int,
    val a14: Int,
    val a15: Int,
    val a16: Int,
    val a17: Int,
    val a18: Int,
    val a19: Int,
    val a20: Int,
    val a21: Int,
    val a22: Int,
    val a23: Int,
    val a24: Int,
    val a25: Int,
    val a26: Int,
    val a27: Int,
    val a28: Int,
    val a29: Int,
    val a30: Int,
    val a31: Int,
    val a32: Int,
    val a33: Int,
    val a34: Int,
    val a35: Int,
    val a36: Int,
    val a37: Int,
    val a38: Int,
    val a39: Int,
    val a40: Int,
    val a41: Int,
    val a42: Int,
    val a43: Int,
    val a44: Int,
    val a45: Int,
    val a46: Int,
    val a47: Int,
    val a48: Int,
    val a49: Int
)

fun main(args: Array<String>) {
    val d = Data(0,1,2,3,4,5,6,7,8,9,
        10,11,12,13,14,15,16,17,18,19,
        20,21,22,23,24,25,26,27,28,29,
        30,31,32,33,34,35,36,37,38,39,
        40,41,42,43,44,45,46,47,48,49)

    val copy = d.copy(a42 = 99)
}