Kotlin数据类复制功能不起作用

时间:2015-02-20 02:36:55

标签: java kotlin data-class

也许我误解了copy类的data函数是如何工作的,或者是否存在错误,但以下是copy函数未按预期工作的示例:< / p>

科特林:

data class A {
    public var x: String? = null
    public var y: String? = null
    public var z: B = B.ONE
}

enum class B {
    ONE
    TWO
    THREE
}

爪哇

A a1 = new A()
a1.setX("Hello")
a1.setY("World")
a1.setZ(B.TWO)

A a2 = a1.copy()
// a2.x is null
// a2.y is null
// a2.z is B.ONE

似乎copy只是在创建A的新实例而不是复制值。如果我将变量放在构造函数中,则会分配值,但这与构造新实例没有什么不同。

4 个答案:

答案 0 :(得分:6)

好的,我在文档中错过了这句话:

  

如果这些函数中的任何一个在类体中明确定义或从基类型继承,则不会生成它。

事实上,使copy没有比Java互操作的构造函数更好。

答案 1 :(得分:1)

如何克服Kotlin copy()的限制,就是在数据类中创建自己的复制函数。示例如下:

let params = {
    'client_id': GOOGLE_API_CLIEND_ID,
    'redirect_uri': `${location.origin}/auth/google`,
    'response_type': 'id_token token',
    'scope': GOOGLE_API_SCOPES,
    'state': 'af0ifjsldkj',
    'nonce': 'n-0S6_WzA2Mj'
};

答案 2 :(得分:0)

对于java的互操作,您可以使用kotlin生成的函数.copy

@Entity
data class User(@PrimaryKey var id: Int = 0,
            var firstName: String? = null,
            var lastName: String? = null,
            var phone: String? = null,
            var email: String? = null,
            var phoneCode: String? = null,
            var tokenId: String? = null,
            var provider: SocialProvider? = null) : Serializable {


var countryCodeIso: String? = null
    set(countryCodeIso) {
        if (countryCodeIso != null) {
            field = countryCodeIso.toLowerCase()
        }
    }

fun javaCopy(): User {
    val user = copy()
    user.countryCodeIso = countryCodeIso
    return user
}}

答案 3 :(得分:0)

此问题的搜索排名很高,并且可能会使那些对kotlin陌生的人感到困惑,因为该问题的示例代码不是典型的kotlin代码或复制功能的用法。我在下面添加了一些示例代码,以帮助阐明正在发生的事情,并还显示了数据类的典型用法。
简而言之,copy函数在从kotlin类中调用时最有用。我同意从Java代码中调用它的行为并不明显。

//
// A.kt
//

// this is an idiomatic kotlin data class. note the parens around the properties, not braces.
data class A(
    val x: String? = null,
    val y: String? = null,
    val z: B = B.ONE
) {
    // this javaCopy function is completely unnecessary when being called from kotlin; it's only here to show a fairly simple way to make kotlin-java interop a little easier (like what Nokuap showed).
    fun javaCopy(): A {
        return this.copy()
    }
}

enum class B {
    ONE,
    TWO,
    THREE
}

fun main() {
    val a1 = A("Hello", "World", B.TWO)

    // here's what copy usage looks like for idiomatic kotlin code.
    val a2 = a1.copy()
    assert(a2.x == "Hello")
    assert(a2.y == "World")
    assert(a2.z == B.TWO)

    // more typical is to `copy` the object and modify one or more properties during the copy. e.g.:
    val a3 = a1.copy(y = "Friend")
    assert(a2.x == "Hello")
    assert(a3.y == "Friend")
}
public class App {

    public static void main(String[] args) {
        A a1 = new A("Hello", "World", B.TWO);

        // the kotlin copy function is primarily meant for kotlin <-> kotlin interop
        // copy works when called from java, but it requires all the args.
        // calling the `javaCopy` function gives the expected behavior.
        A a2 = a1.javaCopy();
        assert a2.getX().equals("Hello");
        assert a2.getY().equals("World");
        assert a2.getZ().equals(B.TWO);
    }
}

有关数据类的官方文档,包括copy函数:
https://kotlinlang.org/docs/reference/data-classes.html