gpt4 book ai didi

kotlin - Kotlin 中的构建器模式

转载 作者:IT老高 更新时间:2023-10-28 13:40:24 25 4
gpt4 key购买 nike

我是 kotlin 世界的新手。我有一个用 Java 编写的现有构建器,并希望在将项目迁移到 Android 中的 kotlin 时将其转换为 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,您实际上可以使用数据类使这变得简单得多。

data class User(val firstName: String, 
val lastName: String,
val age: Int = 0,
val phone: String? = null,
val address: String? = null)

就是这样!前两个参数是必需的,但如果要指定更多,只需按名称指定即可:

val user = User("John", "Doe", phone = "555-1212")

无需构建器!

关于kotlin - Kotlin 中的构建器模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45604789/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com