package builder; public class Person { private Integer id; private String name; private Integer age; private Person () {} public static class PersonBuilder { Person person = new Person(); public PersonBuilder id(Integer id) { person.id = id; return this; } public PersonBuilder name(String name) { person.name = name; return this; } public PersonBuilder age(Integer age) { person.age = age; return this; } public Person build() { return this.person; } } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } }
package builder; public class Main { public static void main(String[] args) { Person person = new Person.PersonBuilder().id(1).name("小明").age(10).build(); } }