我有這個在Java中:如何將此Java類重寫爲Kotlin數據類?
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 +
'}';
}
}
在科特林類公司是這樣的:
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和getters