首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

小弟我的 effective java - 构建器

2012-08-28 
我的 effective java -- 构建器??public class TestConstruct {private final int serviceSizeprivate fi

我的 effective java -- 构建器

?

?

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();}}

热点排行