链式结构(案例)

public class Student {

public Student(){

}

protected Builder builder(){
return new Builder();
}

public class Builder{

private String name;

private String hoby;

private int age;

public String getName() {
return name;
}

public Builder setName(String name) {
this.name = name;
return this;
}

public String getHoby() {
return hoby;
}

public Builder setHoby(String hoby) {
this.hoby = hoby;
return this;
}

public int getAge() {
return age;
}

public Builder setAge(int age) {
this.age = age;
return this;
}

@Override
public String toString() {
return "name:"+this.getName()+" age:"+this.getAge()+" hoby:"+this.getHoby();
}
}
}

public class LinkTest {

public static void main(String[] args){
Student student=new Student();
System.out.println(student.builder().setAge(10).setHoby("play basketball").setName("austin").toString());
}

}


原文地址:https://www.cnblogs.com/austinspark-jessylu/p/8109668.html