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}