当前位置:首页 > 开发 > 编程语言 > Java > 正文

我的 effective java -- 构建器

发表于: 2012-05-21   作者:12616383   来源:转载   浏览:
摘要:     public class TestConstruct { private final int serviceSize; private final int servings; private final int cal; private final String fat; private final boolean istrue; pu

 

 

public class TestConstruct {

private final int serviceSize;

private final int servings;

private final int cal;

private final String fat;

private final boolean istrue;

public static class Builder {

private final int serviceSize;

private final int servings;

private  int cal = 0;

private  String fat = "";

private  boolean istrue = false;

public Builder(int serviceSize, int servings){

this.serviceSize = serviceSize;

this.servings = servings;

}

public Builder calMe(int val){

cal = val;

return this;

}

public Builder fatMe(String val){

fat = val;

return this;

}

public Builder istrueMe(boolean val){

istrue = val;

return this;

}

public TestConstruct build(){

return new TestConstruct(this);

}

}

public TestConstruct(Builder builder){

serviceSize = builder.serviceSize;

servings = builder.servings;

cal = builder.cal;

fat = builder.fat;

istrue = builder.istrue;

}

public void printAll(){

System.out.println(serviceSize);

System.out.println(servings);

System.out.println(cal);

System.out.println(fat);

System.out.println(istrue);

}

}


public class ConstructClient {
public static void main(String[] args) {
TestConstruct tt = new TestConstruct.Builder(1,20).calMe(1).fatMe("123").istrueMe(true).build();
tt.printAll();
}
}

我的 effective java -- 构建器

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

推荐文章
编辑推荐
版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号