请帮我把Kotlin翻译成Java

时间:2018-11-22 07:24:19

标签: kotlin

我不知道如何将kotlin行转换为Java。

data class ContactData(val name: String? = "", val online: Boolean = false)

这是我尝试过的方法,但是我不确定。

public final class ContactData {

private String name = "";
private boolean online = false;


public final String getName() {
    return name;
}

public final boolean getOnline() {
    return online;
}

ContactData(String name, boolean online) {
    this.name = name;
    this.online = online;
}

public final String name() {
    return this.name;
}

public final boolean online() {
    return this.online;
}

}

请帮助我修复它

2 个答案:

答案 0 :(得分:1)

我会说它将是这样的:

import org.jetbrains.annotations.NotNull;

public class ContactDataa {

    @NotNull
    private final String name;
    private final boolean online;

    public ContactDataa() {
        this("", false);
    }

    public ContactDataa(final String name) {
        this(name, false);
    }

    public ContactDataa(final boolean online) {
        this("", online);
    }

    public ContactDataa(final String name, final boolean online) {
        if (name == null) {
            throw new IllegalArgumentException();
        }
        this.name = name;
        this.online = online;
    }

    public String getName() {
        return name;
    }

    public boolean isOnline() {
        return online;
    }

    public String component1() {
        return name;
    }

    public boolean component2() {
        return online;
    }

    @NotNull
    public final ContactData copy(@NotNull String name, boolean online) {
        if (name == null) {
            throw new IllegalArgumentException();
        }
        return new ContactData(name, online);
    }

    @NotNull
    public final ContactData copy(@NotNull String name) {
        if (name == null) {
            throw new IllegalArgumentException();
        }
        return new ContactData(name, online);
    }

    @NotNull
    public final ContactData copy(boolean online) {
        return new ContactData(name, online);
    }

    @NotNull
    public final ContactData copy() {
        return new ContactData(name, online);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        ContactDataa that = (ContactDataa) o;

        if (online != that.online) return false;
        return name != null ? name.equals(that.name) : that.name == null;
    }

    @Override
    public int hashCode() {
        int result = name != null ? name.hashCode() : 0;
        result = 31 * result + (online ? 1 : 0);
        return result;
    }

    @Override
    public String toString() {
        return "ContactDataa{" +
                "name='" + name + '\'' +
                ", online=" + online +
                '}';
    }
}

这里的问题是与其他Kotlin班级的完全接口。另外,如果您使用Intellij IDEA或Android Studio,则只需get Kotlin bytecode and decompile it to Java

答案 1 :(得分:0)

如果您想要与Kotlin中的默认值相同的行为,则需要三个额外的构造函数:

ContactData() {
}

ContactData(String name) {
    this.name = name;
}

ContactData(boolean online) {
    this.online = online;
}

此外,您还必须重写hashCode()和equals(),toString和copy(),并且组件函数请参见https://kotlinlang.org/docs/reference/data-classes.html