【发布时间】:2017-08-10 04:57:08
【问题描述】:
我是 kotlin 世界的新手。我有一个用 Java 编写的现有构建器,并且想将其转换为 Kotlin,因为我正在将项目迁移到 Android 中的 kotlin。但是,Android Studio 内置工具似乎有一些错误,则转换后的代码无法编译。它显示我的UserBuilder 类中的变量无法访问。
这是来自tutorial的Java代码
public class Person {
private final String firstName; // required
private final String lastName; // required
private final int age; // optional
private final String phone; // optional
private final String address; // optional
private Person(UserBuilder builder) {
this.firstName = builder.firstName;
this.lastName = builder.lastName;
this.age = builder.age;
this.phone = builder.phone;
this.address = builder.address;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public int getAge() {
return age;
}
public String getPhone() {
return phone;
}
public String getAddress() {
return address;
}
public static class UserBuilder {
private final String firstName;
private final String lastName;
private int age;
private String phone;
private String address;
public UserBuilder(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public UserBuilder age(int age) {
this.age = age;
return this;
}
public UserBuilder phone(String phone) {
this.phone = phone;
return this;
}
public UserBuilder address(String address) {
this.address = address;
return this;
}
public Person build() {
return new Person(this);
}
}
}
自动转换的kotlin代码:
class Person private constructor(builder: UserBuilder) {
val firstName: String // required
val lastName: String // required
val age: Int // optional
val phone: String // optional
val address: String // optional
init {
//cannot access the variables, they are private in UserBuilder
this.firstName = builder.firstName
this.lastName = builder.lastName
this.age = builder.age
this.phone = builder.phone
this.address = builder.address
}
class UserBuilder(private val firstName: String, private val lastName: String) {
private var age: Int = 0
private var phone: String? = null
private var address: String? = null
fun age(age: Int): UserBuilder {
this.age = age
return this
}
fun phone(phone: String): UserBuilder {
this.phone = phone
return this
}
fun address(address: String): UserBuilder {
this.address = address
return this
}
fun build(): Person {
return Person(this)
}
}
}
更新
class Person private constructor(builder: UserBuilder) {
val firstName: String // required
val lastName: String // required
val age: Int // optional
val phone: String? // optional
val address: String? // optional
init {
this.firstName = builder.firstName
this.lastName = builder.lastName
this.age = builder.age
this.phone = builder.phone
this.address = builder.address
}
class UserBuilder(internal val firstName: String, internal val lastName: String) {
internal var age: Int = 0
internal var phone: String? = null
internal var address: String? = null
fun age(age: Int): UserBuilder {
this.age = age
return this
}
fun phone(phone: String): UserBuilder {
this.phone = phone
return this
}
fun address(address: String): UserBuilder {
this.address = address
return this
}
fun build(): Person {
return Person(this)
}
}
}
【问题讨论】:
-
这是一个糟糕的构建器设计,一个与另一个耦合。
-
如果你能提供一个更好的想法,我很想看看它在 Kotlin 中是如何工作的,因为我是 Kotlin 世界的新手。
-
对不起,我在移动设备上,我认为 kcoppock 的回答可以满足您的需求。我说的是:“
User不应该依赖于UserBuilder”,:)
标签: kotlin