在 java 中使用 Builder 更新对象的特定字段
Update specific field of Object using Builder in java
我有两个 class 如下所述。
我想创建 class A
.
的两个实例
我想从现有实例 obj1 创建实例 obj2,并将 a3 属性的更新值设为 "Java"
.
我曾尝试使用 Builder 在下面的行中进行操作,但它不起作用。
A obj2 = obj1.builder().a3("Java").build();
我可以通过调用构造函数来完成,但我只想使用 Builder
模式。
@Builder
@Data
class A {
String a1;
String a2;
String a3;
B b;
A(String b1, String b2, String b3, B b) {
this.a1 = b1;
this.a2 = b2;
this.a3 = b3;
this.b = b;
}
}
@Builder
@Data
class B {
String b1;
String b2;
String b3;
B(String b1, String b2, String b3) {
this.b1 = b1;
this.b2 = b2;
this.b3 = b3;
}
}
class Main {
public static void main(String[] args) {
B b = new B("a", "b", "b");
A obj1 = new A("a1", "b1", "b1", b);
A obj2 = new A("x1", "y1", "z1", b);
List<A> list= new ArrayList<>();
list.add(obj1);
list.add(obj2);
list.forEach(a -> {a.toBuilder().a1("newA1").a2("newA2").build()});
A obj3 = obj1.toBuilder().a3("Java").build();
}
}
}
}
如更新代码中所述,我有 A 列表,我想使用生成器更新列表中所有元素的 a1 and a2
属性。但是构建器不能很好地处理 lambda。
如果我将 setter 与以下代码一起使用,它工作正常。
list.forEach(a -> {
a.setA1("newA1");
a.setA2("newA2");
});
如果 Builder
和 Lambda
实际上,您必须在注释中启用 toBuilder()
。 toBuilder()
允许您编辑当前对象。
@Builder(toBuilder = true)
然后 obj1.toBuilder().a3("Java").build()
应该可以。
obj1.builder()
是一种允许但令人困惑的说法 A.builder()
。不建议在实例上调用静态方法。无论哪种方式,都会创建一个全新的 A
。
@Builder(toBuilder = true)
可能就是您要找的东西
If true, generate an instance method to obtain a builder that is initialized with the values of this instance. Legal only if @Builder
is used on a constructor, on the type itself, or on a static method that returns an instance of the declaring type.
boolean toBuilder() default false;
我有两个 class 如下所述。
我想创建 class A
.
我想从现有实例 obj1 创建实例 obj2,并将 a3 属性的更新值设为 "Java"
.
我曾尝试使用 Builder 在下面的行中进行操作,但它不起作用。
A obj2 = obj1.builder().a3("Java").build();
我可以通过调用构造函数来完成,但我只想使用 Builder
模式。
@Builder
@Data
class A {
String a1;
String a2;
String a3;
B b;
A(String b1, String b2, String b3, B b) {
this.a1 = b1;
this.a2 = b2;
this.a3 = b3;
this.b = b;
}
}
@Builder
@Data
class B {
String b1;
String b2;
String b3;
B(String b1, String b2, String b3) {
this.b1 = b1;
this.b2 = b2;
this.b3 = b3;
}
}
class Main {
public static void main(String[] args) {
B b = new B("a", "b", "b");
A obj1 = new A("a1", "b1", "b1", b);
A obj2 = new A("x1", "y1", "z1", b);
List<A> list= new ArrayList<>();
list.add(obj1);
list.add(obj2);
list.forEach(a -> {a.toBuilder().a1("newA1").a2("newA2").build()});
A obj3 = obj1.toBuilder().a3("Java").build();
}
}
} }
如更新代码中所述,我有 A 列表,我想使用生成器更新列表中所有元素的 a1 and a2
属性。但是构建器不能很好地处理 lambda。
如果我将 setter 与以下代码一起使用,它工作正常。
list.forEach(a -> {
a.setA1("newA1");
a.setA2("newA2");
});
如果 Builder
和 Lambda
实际上,您必须在注释中启用 toBuilder()
。 toBuilder()
允许您编辑当前对象。
@Builder(toBuilder = true)
然后 obj1.toBuilder().a3("Java").build()
应该可以。
obj1.builder()
是一种允许但令人困惑的说法 A.builder()
。不建议在实例上调用静态方法。无论哪种方式,都会创建一个全新的 A
。
@Builder(toBuilder = true)
可能就是您要找的东西
If true, generate an instance method to obtain a builder that is initialized with the values of this instance. Legal only if
@Builder
is used on a constructor, on the type itself, or on a static method that returns an instance of the declaring type.boolean toBuilder() default false;