JAVA设计模式--构造者模式

package com.rabbitmq.demo.rabbitmqdemo.test;

/**
 * @Description: 构造者模式
 * @ClassName: Student
 * @Author: DING WEI 
 * @Date: 2019/5/13 16:43
 * @Version: 1.0
 */
public class Student {

    private String name;

    private Integer age;

    private Double sal;

    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;
    }

    public Double getSal() {
        return sal;
    }

    public void setSal(Double sal) {
        this.sal = sal;
    }

    public Student() {
    }

    public Student(Student student) {
        this.name = student.name;
        this.age = student.age;
        this.sal = student.sal;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", sal=" + sal +
                '}';
    }

    static class StudentBuild{

        private String name;

        private Integer age;

        private Double sal;

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

        public StudentBuild age(Integer age){
            this.age = age;
            return this;
        }

        public StudentBuild sal(Double sal){
            this.sal = sal;
            return this;
        }

        public Student build(){
            Student student = new Student();
            student.setName(name);
            student.setAge(age);
            student.setSal(sal);
            return student;
        }

    }
}

main方法测试

package com.rabbitmq.demo.rabbitmqdemo.test;

/**
 * @Description:
 * @ClassName: TestBuild
 * @Author: DING WEI 
 * @Date: 2019/5/13 16:56
 * @Version: 1.0
 */
public class TestBuild {

    public static void main(String[] args) {
        Student student1 = new Student.StudentBuild().name("宋江").age(70).sal(2000000.00).build();
        System.out.println(student1.toString());

        Student student2 = new Student.StudentBuild().name("李逵").age(90).sal(3000000.00).build();
        System.out.println(student2.toString());
    }
}

输出结果

Student{name='宋江', age=70, sal=2000000.0}
Student{name='李逵', age=90, sal=3000000.0}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值