今天看到 《effective java》构建器,自己小试牛刀,代码如下:
public class TestContruct { private int a; private int b; private int c; private int d; public static class Builder{ private int a = 0; private int b = 0; private int c = 0; private int d = 0; public Builder(int a,int b){ this.a = a; this.b = b; } public void setC(int c){ c = c; } public void setD(int d){ d = d; } public TestContruct builder(){ return new TestContruct(this); } } private TestContruct(Builder builder){ this.a = builder.a; this.b = builder.b; this.c = builder.c; this.d = builder.d; } }
TestContruct testContruct = new TestContruct.Builder(1,2).builder();