package com.example.administrator.buildermode;
/**
* Created by $wu on 2017-10-09 上午 10:16.
*/
public class Person {
private String name;
private int age;
private String sex;
private Person(Builder builder) {
this.name = builder.name;
this.age = builder.age;
this.sex = builder.sex;
}
public static class Builder {
private String name;
private int age;
private String sex;
public Builder name(String name) {
this.name = name;
return this;
}
public Builder age(int age) {
this.age = age;
return this;
}
public Builder sex(String sex) {
this.sex = sex;
return this;
}
public Person build() {
return new Person(this);
}
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
", sex='" + sex + '\'' +
'}';
}
}
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Person.Builder builder = new Person.Builder();
Person person = builder.age(18)
.name("WZQ")
.sex("男")
.build();
Log.e("111", "person=" + person.toString());
}
}