Kotlin数据类和30多个属性

时间:2018-10-17 19:15:48

标签: kotlin data-class

我们如何在kotlin中创建数据类,其中该类包含超过25个以上的属性。

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

1 个答案:

答案 0 :(得分:1)

有可能。这里是一个例子:

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)
}