public class MyPojo{
String required;
Integer optionalOne;
Integer optionalTwo;
private MyPojo(Builder builder){
this.required = builder.required
this.optionalOne = builder.one;
this.optionalTwo = builder.two;
}
public static class Builder{
String required;
Integer optionalOne =0;
Integer optionalTwo =0;
public Builder(String req){
this.required = req;
return this;
}
public Builder optionalOne(Integer one){
this.one = one;
return this;
}
public Builder optionalTwo(Integer two){
this.two = two;
return this;
}
public MyPojo build(){
return new MyPojo(this);
}
}
}
次に、次のように呼び出されます。
MyPojo myPojo = new MyPojo.Builder("req").optionalOne(1).optionalTwo(2).build();
それはすべて素敵ですが、いくつかの部分がわかりません。
呼び出しステートメントに 1 つ、メソッドに 1 つの2 つの Newbuild()ステートメントがありますが、作成される新しいオブジェクトは 1 つだけです ?
また、2 番目のオプションのパラメーターを指定せずに 2 回目の呼び出しを行うと、次のようになります。
MyPojo myPojo = new MyPojo.Builder("req").optionalOne(1).build();
optionalTwo がデフォルト値 (ゼロ) に戻るのはなぜですか。そして、最初に渡された値を保持しないでください(2)。これは静的クラスなので、1 つのインスタンスがすべての MyPojo 間で共有されますか?