public class Person {
enum Sex {
MAN(true), WOMAN(false);
public boolean value;
Sex(boolean value) {
this.value = value;
}
}
private final String name;
private final int age;
private final Sex sex;
private final String telPhone;
private final String country;
private final String address;
public static class PersonBuilder implements Builder<Person> {
//必填参数,通过构造函数指定
private final String name;
private final int age;
private final Sex sex;
//可选参数,通过方法设置
private String telPhone;
private String country;
private String address;
public PersonBuilder(String name, int age, Sex sex) {
this.name = name;
this.age = age;
this.sex = sex;
}
public PersonBuilder telPhone(String telPhone) {
this.telPhone = telPhone;
return this;
}
public PersonBuilder country(String country) {
this.country = country;
return this;
}
public PersonBuilder address(String address) {
this.address = address;
return this;
}
@Override
public Person build() {
return new Person(this);
}
}
public Person(PersonBuilder personBuilder) {
this.name = personBuilder.name;
this.age = personBuilder.age;
this.sex = personBuilder.sex;
this.telPhone = personBuilder.telPhone;
this.country = personBuilder.country;
this.address = personBuilder.address;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
", sex=" + sex +
", telPhone='" + telPhone + '\'' +
", country='" + country + '\'' +
", address='" + address + '\'' +
'}';
}
}