当参数多的时候可以使用建造者模式做链式赋值
public class House {
private String basics;
private String wall;
private String roof;
private House(HouseBulider houseBulider){
basics = houseBulider.basics;
wall = houseBulider.wall;
roof = houseBulider.roof;
}
private static class HouseBulider{
private String basics;
private String wall;
private String roof;
public HouseBulider setBasics(String basics) {
this.basics = basics;
return this;
}
public HouseBulider setWall(String wall) {
this.wall = wall;
return this;
}
public HouseBulider setRoof(String roof) {
this.roof = roof;
return this;
}
public House bulider(){
return new House (this);
}
}
@Override
public String toString() {
return "House{" +
"basics='" + basics + '\'' +
", wall='" + wall + '\'' +
", roof='" + roof + '\'' +
'}';
}
public static void main(String[] args) {
House bulider = new HouseBulider ().setBasics ("地基10米").setWall ("砌墙0.5米").setRoof ("屋顶5米").bulider ();
System.out.println (bulider.toString ());
}
}
网友评论