美文网首页
创建型-建造者模式(特殊模式)

创建型-建造者模式(特殊模式)

作者: 程序男保姆 | 来源:发表于2020-04-23 14:48 被阅读0次
image.png

public class Student {

    private String name;

    private String age;


    private Student(Builder builder) {
        this.name = builder.name;
        this.age = builder.age;
    }

    public static class Builder {
        private String name;

        private String age;

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

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

        public Student build() {
            return new Student(this);
        }
    }
}

相关文章

网友评论

      本文标题:创建型-建造者模式(特殊模式)

      本文链接:https://www.haomeiwen.com/subject/pzcaihtx.html