静态内部类:
public class BuilderObjectClass {
private int id;
private String name;
public BuilderObjectClass(int id, String name) {
this.id = id;
this.name = name;
}
public static BuilderObjectClass.Builder builder() {
return new BuilderObjectClass.Builder();
}
private static class Builder {
private int id;
private String name;
public BuilderObjectClass.Builder id(int id) {
this.id = id;
return this;
}
public BuilderObjectClass.Builder name(String name) {
this.name = name;
return this;
}
public BuilderObjectClass build() {
return new BuilderObjectClass(this.id, this.name);
}
}
@Override
public String toString() {
return "BuilderObjectClass{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
public static void main(String[] args) {
BuilderObjectClass xustang = BuilderObjectClass.builder().name("xustang").id(1).build();
System.out.println(xustang);
}
}