第二条 一个类如果有多个参数,考虑用Builder构造者模式
1.
@Data
public class Student {
//体检用
private String name;
private int age;
private int height;
private int sex;
//录取用
private String schoolName;
private String profession;
private int gradeNo;
//分班用
private String idCard;
private String stuNo;
private String labName;
private String dormitoryAddress; private Student(Builder builder) {
this.name = builder.name;
this.age = builder.age;
this.height = builder.height;
this.sex = builder.sex;
this.schoolName = builder.schoolName;
this.profession = builder.profession;
this.gradeNo = builder.gradeNo;
this.idCard = builder.idCard;
this.stuNo = builder.stuNo;
this.labName = builder.labName;
this.dormitoryAddress = builder.dormitoryAddress;
} public static class Builder{
//体检用 公用的用final
private final String name;
private final int age;
private final int height;
private final int sex; //录取用
private String schoolName;
private String profession;
private int gradeNo; //分班用
private String idCard;
private String stuNo;
private String labName;
private String dormitoryAddress; public Builder(String name, int age, int height, int sex) {
this.name = name;
this.age = age;
this.height = height;
this.sex = sex ;
} public Builder schoolName(String schoolName) {
this.schoolName = schoolName;
return this;
} public Builder profession(String profession) {
this.profession = profession;
return this;
} public Builder gradeNo(int gradeNo) {
this.gradeNo = gradeNo;
return this;
} public Builder idCard (String idCard) {
this.idCard = idCard;
return this;
}
public Builder stuNo(String stuNo) {
this.stuNo = stuNo;
return this;
}
public Builder labName(String labName) {
this.labName = labName;
return this;
} public Builder dormitoryAddress(String dormitoryAddress) {
this.dormitoryAddress = dormitoryAddress;
return this;
} public Student build() {
return new Student(this);
}
} //测试
public static void main(String[] args) {
//各取所需,公用的字段写成Builder的构造方法中!
Student zhangsan = new Student.Builder("zhangsan", 28, 168, 0).build(); //体检的张三只要四个基本参数 Student zhangsan1 = new Builder("zhangsan", 28, 168, 0).schoolName("安徽大学").profession("国贸").build(); // 录取学生信息管理系统要的字段 Student zhangsan2 = new Student.Builder("zhangsan", 28, 168, 0).schoolName("安徽大学").profession("国贸").stuNo("学号04012057")
.gradeNo(302).dormitoryAddress("寝室号502").build(); System.out.println("张三的体检信息:" + zhangsan);
System.out.println("张三被录取后信息:" + zhangsan1);
System.out.println("张三分班后:" + zhangsan2); }
}
2. 结果
张三的体检信息:Student(name=zhangsan, age=28, height=168, sex=0, schoolName=null, profession=null, gradeNo=0, idCard=null, stuNo=null, labName=null, dormitoryAddress=null)
张三被录取后信息:Student(name=zhangsan, age=28, height=168, sex=0, schoolName=安徽大学, profession=国贸, gradeNo=0, idCard=null, stuNo=null, labName=null, dormitoryAddress=null)
张三分班后:Student(name=zhangsan, age=28, height=168, sex=0, schoolName=安徽大学, profession=国贸, gradeNo=302, idCard=null, stuNo=学号04012057, labName=null, dormitoryAddress=寝室号502)
第二条 一个类如果有多个参数,考虑用Builder构造者模式的更多相关文章
- Effective Objective-C 2.0 — 第二条:类的头文件中尽量少引入其他头文件
第二条:类的头文件中尽量少引入其他头文件 使用向前声明(forward declaring) @class EOCEmployer 1, 将引入头文件的实际尽量延后,只在确有需要时才引入,这样就可以减 ...
- 【问题】多重继承时,super函数只初始化继承的第一个类,不初始化第二个类。
class A(object): def __init__(self): print("init class A") class B(object): def __init__(s ...
- c++ 中一个类或者一个对象所占的字节数
转载博客:转载地址https://www.cnblogs.com/JingHuanXiao/p/6080726.html 一个空的class在内存中多少字节?如果加入一个成员函数后是多大?这个成员函数 ...
- C++中一个类(非继承类)对象,所占内存空间大小
离职后在家里带了半年多了,这半年多里没有编写过一行代码,倒是看过一些书,但是差不多也都是囫圃吞枣.房子也快要装修,也得赶快找一个工作了,不然养车,还要玩摄影,没收入的日子真是不好过啊.呵呵. 按惯例, ...
- [转]自己写PHP扩展之创建一个类
原文:http://www.imsiren.com/archives/572 比如我们要创建一个类..PHP代码如下 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 ...
- 在SQL SERVER中获取表中的第二条数据
在SQL SERVER中获取表中的第二条数据, 思路:先根据时间逆排序取出前2条数据作为一个临时表,再按顺时排序在临时表中取出第一条数据 sql语句如下: select top 1 * from(se ...
- 【PHP面向对象(OOP)编程入门教程】4.如何抽象出一个类?
上面已经介绍过了, 面向对象程序的单位就是对象,但对象又是通过类的实例化出来的,所以我们首先要做的就是如何来声明类, 做出来一个类很容易,只要掌握基本的程序语法定义规则就可以做的出来,那么难点在那里呢 ...
- java中的反射机制,以及如何通过反射获取一个类的构造方法 ,成员变量,方法,详细。。
首先先说一下类的加载,流程.只有明确了类这个对象的存在才可以更好的理解反射的原因,以及反射的机制. 一. 类的加载 当程序要使用某个类时,如果该类还未被加载到内存中,则系统会通过加载,连接,初始化三 ...
- 一个类搞定UIScrollView那些事
前言 UIScrollView可以说是我们在日常编程中使用频率最多.扩展性最好的一个类,根据不同的需求和设计,我们都能玩出花来,当然有一些需求是大部分应用通用的,今天就聊一下以下需求,在一个categ ...
随机推荐
- NodeJS中form上传附件中针对表单的multiple attribute出现的问题总结
在express中上传附件需要在表单中添加enctype="multipart/form-data"属性,并且在新的4.0.1版本中需要手动添加中间件app.use(connect ...
- 【转】Android APK的数字签名的作用和意义
1. 什么是数字签名? 数字签名就是为你的程序打上一种标记,来作为你自己的标识,当别人看到签名的时候会知道它是与你相关的 2. 为什么要数字签名? 最简单直接的回答: 系统要求的. Andr ...
- Odoo Auto Backup Database And Set Linux task schedualer
First ,Write Database Backup Script: pg_dump -Fc yourdatabasename > /home/yourfilepath/yourdataba ...
- openstack-L版安装
参照官方install document: http://docs.openstack.org/liberty/install-guide-rdo/ 实验环境:centos7.2 桥接: 192.16 ...
- Self Service Password (SSP)
安装SSP, 依赖包包括php5, php5-ldap, php5-mcrypt 启用mcrypt功能: sudo php5enmod mcrypt 第一部分: Apache 安装Apache, 并且 ...
- Mongodb 创建索引
db.getCollection('ct_project').ensureIndex({'pro_code':1}) 创建索引 db.getCollection('ct_project').ensu ...
- 撑起大规模PHP网站的开源工具
撑起大规模PHP网站的开源工具 百万级PHP站点Poppen.de的架构 在 2011年11月27日 那天写的 已经有 3957 次阅读了 感谢 参考或原文 服务器君一共花费了54.510 ...
- php中json_decode()和json_encode()的使用方法
php中json_decode()和json_encode()的使用方法 作者: 字体:[增加 减小] 类型:转载 json_decode对JSON格式的字符串进行编码而json_encode对变 ...
- Python浮点数控制小数精度
将精度高的浮点数转换成精度低的浮点数. 1.round()内置方法 这个是使用最多的,刚看了round()的使用解释,也不是很容易懂.round()不是简单的四舍五入的处理方式. For the bu ...
- jsp页面直接编写csss
<style> .logoZL{ width:550px; float:right; text-align:right; padding-right:15px; margin-right: ...