Home Articles

如何将这个Java类重写为Kotlin数据类?

Asked
Viewed 616 times
0

我在Java中有这个:

public class User {

    String name;
    String id;
    int age;

    public User(String name) {
        this.name = name;
    }

    public User(String name, String id) {
        this.name = name;
        this.id = id;
    }

    public User(String id, int age) {
        this.id = id;
        this.age = age;
    }

    public User() {
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", id='" + id + '\'' +
                ", age=" + age +
                '}';
    }
}

在Kotlin类中,它是这样的:

class User {

    var name: String?=null
    var id: String?=null
    var age: Int = 0

    constructor(name: String) {
        this.name = name
    }

    constructor(name: String, id: String) {
        this.name = name
        this.id = id
    }

    constructor(id: String, age: Int) {
        this.id = id
        this.age = age
    }

    constructor() {}

    override fun toString(): String {
        return "User{" +
                "name='" + name + '\'' +
                ", id='" + id + '\'' +
                ", age=" + age +
                '}'
    }
}

我需要知道的是,如何在具有4个不同构造函数和setter和getter的数据类中执行此操作

4 Answers

  • 4

    Android studio提供了一种非常简单的方法来实现将任何Java类转换为Kotlin . 只需在文件搜索选项卡中键入"convert java to kotlin",IDE就会为您执行此操作
    enter image description here

  • 0

    您可以使用默认参数和命名参数的组合,因此不需要多个构造函数 . 像这样定义您的数据类:

    data class User(var name: String? = null, var age: Int = 0, var id: String? = null)
    

    然后以任意多种方式构建它 .

    User(name = "Foo") // returns User("Foo", 0, null)
    User(name = "Foo", age = 21) // returns User("Foo", 21, null)
    User(id = "x") // returns User(null, 0, "x")
    User("Foo", 21, "x") // returns User("Foo", 21, "x")
    User(id = "x", name = "Foo", age = 21) // returns User("Foo", 21, "x")
    etc...
    

    getter和setter是为您定义的,无需定义它们 .

  • 0

    在Kotlin中,我们可以在像这样的单个构造函数中使用它

    data class User(var name: String, var age: Int = 0, var id: String = "ABC")
    

    如果我们想暴露getter而不是setter,我们只提供 val 而不是 var ,如下所示,

    data class User(val name: String, val age: Int = 0, val id: String = "ABC")
    

    getters / setters可见性修饰符我们可以在这样的构造函数中提供它们,

    data class User(internal val name, internal val age: Int = 0, internal var id: String = "ABC")
    

    在上面的例子中,只有该模块才能访问getter .

    我们可以创建这样的对象,

    var user = User("XYZ")
        var user1 = User("XYZ", 29)
        var user2 = User("XYZ", 29, "AXCVFC")
    
  • 0

    只是我从其他人那里添加了不同的答案,因为如果你在User数据类中有太多变量,那么它应该像下面那样太长 .

    data class UserTooLongClass(var id: String, var name : String, var age : String, var location : String, var mobileNo : String, var mailId : String)
    

    所以你可以创建如下,

    data class User(var id: String = "1") {
        var name: String = ""
        var age: Int = 0
        var location : String = ""
        var mobileNo :  String = ""
        var mailId : String = ""
    
        override fun toString(): String {
            return " id : $id, name : $name, age : $age, location : $location, mobileNo : $mobileNo, mailId : $mailId"
        }
    }
    

    Implementation:

    val user : User = User()
        user.name = "TestName"
        user.id = "1223"
        user.age = 23
        user.mobileNo = "333"
        user.mailId = "ddd@gmail.com"
        user.location = "xxxxx"
        Log.d("TAG"," user : "+user.toString())
    

    Output :

    TAG:  user :  id : 1223, name : TestName, age : 23, location : xxxxx, mobileNo : 333, mailId : ddd@gmail.com
    

Related