日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

JAVA构造对象的几种方式(构建器、构造器)

發(fā)布時(shí)間:2023/12/10 编程问答 43 豆豆
生活随笔 收集整理的這篇文章主要介紹了 JAVA构造对象的几种方式(构建器、构造器) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

大家好,我是烤鴨:

? ? 今天說一下初始化對(duì)象的幾種方式:

? ????? 1.? ? 多參數(shù)構(gòu)造器

? ? ????2.? ? 構(gòu)建器

? ? ????3.? ? 構(gòu)造器后 + get/set方法


舉個(gè)例子:

????這里有個(gè)機(jī)構(gòu)entity,提供一個(gè)默認(rèn)構(gòu)造器

package com.xxx.xxx.modules.sys.entity;/*** 機(jī)構(gòu)Entity* @version 2013-05-15*/ public class Office {private static final long serialVersionUID = 1L;private Area area; // 歸屬區(qū)域private String code; // 機(jī)構(gòu)編碼private String type; // 機(jī)構(gòu)類型(1:公司;2:部門;3:小組)private String grade; // 機(jī)構(gòu)等級(jí)(1:一級(jí);2:二級(jí);3:三級(jí);4:四級(jí))private String address; // 聯(lián)系地址private String zipCode; // 郵政編碼private String master; // 負(fù)責(zé)人private String phone; // 電話private String fax; // 傳真private String email; // 郵箱private String useable;//是否可用private User primaryPerson;//主負(fù)責(zé)人private User deputyPerson;//副負(fù)責(zé)人private List<String> childDeptList;//快速添加子部門private String officeCode; //新增字段,門店id,和code值一樣private String businessArea; //2.0新增字段,營(yíng)業(yè)面積private String businessHours; //2.0新增字段,營(yíng)業(yè)時(shí)間public Office(){super();} }

如果想創(chuàng)建一個(gè)這樣的對(duì)象進(jìn)行參數(shù)傳遞或者進(jìn)行其他操作(數(shù)據(jù)庫等等)


1.? ?多參數(shù)構(gòu)造器

? ?? ??這是全參構(gòu)造器:

public Office(Area area, String code, String type, String grade, String address, String zipCode, String master, String phone, String fax, String email, String useable, User primaryPerson, User deputyPerson, List<String> childDeptList, String officeCode, String businessArea, String businessHours, String jxName) {this.area = area;this.code = code;this.type = type;this.grade = grade;this.address = address;this.zipCode = zipCode;this.master = master;this.phone = phone;this.fax = fax;this.email = email;this.useable = useable;this.primaryPerson = primaryPerson;this.deputyPerson = deputyPerson;this.childDeptList = childDeptList;this.officeCode = officeCode;this.businessArea = businessArea;this.businessHours = businessHours;this.jxName = jxName;}


2.? ?構(gòu)建

?????這是全參構(gòu)建器:

private Office(Office.Builder builder){this.id = builder.id;this.area = builder.area;this.code = builder.code;this.type = builder.type;this.grade = builder.grade;this.address = builder.address;this.name = builder.name;this.email = builder.email;this.phone = builder.phone;this.zipCode = builder.zipCode;this.master = builder.master;this.parent = builder.parent;this.parentIds = builder.parentIds;this.fax = builder.fax;this.sort = builder.sort;this.primaryPerson = builder.primaryPerson;this.deputyPerson = builder.deputyPerson;this.childDeptList = builder.childDeptList;this.officeCode = builder.officeCode;this.businessArea = builder.businessArea;this.useable = builder.useable;this.businessHours = builder.businessHours;this.delFlag = builder.delFlag;this.createBy = builder.createBy;this.updateBy = builder.updateBy;this.updateDate = builder.updateDate;this.createDate = builder.createDate;}//利用構(gòu)建器創(chuàng)建對(duì)象public static class Builder extends Office{private static final long serialVersionUID = 1L;private Area area; // 歸屬區(qū)域private String code; // 機(jī)構(gòu)編碼private String type; // 機(jī)構(gòu)類型(1:公司;2:部門;3:小組)private String grade; // 機(jī)構(gòu)等級(jí)(1:一級(jí);2:二級(jí);3:三級(jí);4:四級(jí))private String address; // 聯(lián)系地址private String zipCode; // 郵政編碼private String master; // 負(fù)責(zé)人private String phone; // 電話private String fax; // 傳真private String email; // 郵箱private String useable;//是否可用private User primaryPerson;//主負(fù)責(zé)人private User deputyPerson;//副負(fù)責(zé)人private List<String> childDeptList;//快速添加子部門private String officeCode; //新增字段,門店id,和code值一樣private String businessArea; //2.0新增字段,營(yíng)業(yè)面積private String businessHours; //2.0新增字段,營(yíng)業(yè)時(shí)間public Builder() {super();}public Builder id(String id){this.id = id;return this;}public Office build(){return new Office(this);}public Builder area(Area area){this.area = area;return this;}public Builder name(String name) {this.name = name;return this;}public Builder master(String master) {this.master = master;return this;}public Builder code(String code) {this.code = code;return this;}public Builder type(String type){this.type = type;return this;}public Builder grade(String grade) {this.grade = grade;return this;}public Builder address(String address) {this.address = address;return this;}public Builder zipCode(String zipCode) {this.zipCode = zipCode;return this;}public Builder password(String master) {this.master = master;return this;}public Builder parent(Office parent) {this.parent = parent;return this;}public Builder parentIds(String parentIds) {this.parentIds = parentIds;return this;}public Builder phone(String phone) {this.phone = phone;return this;}public Builder fax(String fax) {this.fax = fax;return this;}public Builder email(String email) {this.email = email;return this;}public Builder useable(String useable) {this.useable = useable;return this;}public Builder sort(Integer sort) {this.sort = sort;return this;}public Builder primaryPerson(User primaryPerson) {this.primaryPerson = primaryPerson;return this;}public Builder deputyPerson(User deputyPerson) {this.deputyPerson = deputyPerson;return this;}public Builder childDeptList(List<String> childDeptList) {this.childDeptList = childDeptList;return this;}public Builder officeCode(String officeCode) {this.officeCode = officeCode;return this;}public Builder businessArea(String businessArea) {this.businessArea = businessArea;return this;}public Builder businessHours(String businessHours) {this.businessHours = businessHours;return this;}public Builder delFlag(String delFlag) {this.delFlag = delFlag;if(StringUtils.isBlank(delFlag)){this.delFlag = IDBConstant.APPLICATION_DELETE_FLAG_VALID + "";}return this;}public Builder createBy(User createBy) {this.createBy = createBy;return this;}public Builder updateBy(User updateBy) {this.updateBy = updateBy;return this;}public Builder createDate(Date createDate) {this.createDate = createDate;return this;}public Builder updateDate(Date updateDate) {this.updateDate = updateDate;return this;}}


3.? ?get/set方法

???? 自動(dòng)生成就行 ????????public List<String> getChildDeptList() {return childDeptList;}public void setChildDeptList(List<String> childDeptList) {this.childDeptList = childDeptList;}public String getUseable() {return useable;}public void setUseable(String useable) {this.useable = useable;}public User getPrimaryPerson() {return primaryPerson;}public void setPrimaryPerson(User primaryPerson) {this.primaryPerson = primaryPerson;}public User getDeputyPerson() {return deputyPerson;}public void setDeputyPerson(User deputyPerson) {this.deputyPerson = deputyPerson;}public Office getParent() {return parent;}public void setParent(Office parent) {this.parent = parent;}@NotNullpublic Area getArea() {return area;}public void setArea(Area area) {this.area = area;}@Length(min=1, max=1)public String getType() {return type;}public void setType(String type) {this.type = type;}@Length(min=1, max=1)public String getGrade() {return grade;}public void setGrade(String grade) {this.grade = grade;}@Length(min=0, max=255)public String getAddress() {return address;}public void setAddress(String address) {this.address = address;}@Length(min=0, max=100)public String getZipCode() {return zipCode;}public void setZipCode(String zipCode) {this.zipCode = zipCode;}@Length(min=0, max=100)public String getMaster() {return master;}public void setMaster(String master) {this.master = master;}@Length(min=0, max=200)public String getPhone() {return phone;}public void setPhone(String phone) {this.phone = phone;}@Length(min=0, max=200)public String getFax() {return fax;}public void setFax(String fax) {this.fax = fax;}@Length(min=0, max=200)public String getEmail() {return email;}public void setEmail(String email) {this.email = email;}@Length(min=0, max=100)public String getCode() {return code;}public void setCode(String code) {this.code = code;}@Overridepublic String toString() {return name;}public String getOfficeCode() {return officeCode;}public void setOfficeCode(String officeCode) {this.officeCode = officeCode;}public String getBusinessArea() {return businessArea;}public void setBusinessArea(String businessArea) {this.businessArea = businessArea;}public String getBusinessHours() {return businessHours;}public void setBusinessHours(String businessHours) {this.businessHours = businessHours;} }public List<String> getChildDeptList() {return childDeptList;}public void setChildDeptList(List<String> childDeptList) {this.childDeptList = childDeptList;}public String getUseable() {return useable;}public void setUseable(String useable) {this.useable = useable;}public User getPrimaryPerson() {return primaryPerson;}public void setPrimaryPerson(User primaryPerson) {this.primaryPerson = primaryPerson;}public User getDeputyPerson() {return deputyPerson;}public void setDeputyPerson(User deputyPerson) {this.deputyPerson = deputyPerson;}public Office getParent() {return parent;}public void setParent(Office parent) {this.parent = parent;}@NotNullpublic Area getArea() {return area;}public void setArea(Area area) {this.area = area;}@Length(min=1, max=1)public String getType() {return type;}public void setType(String type) {this.type = type;}@Length(min=1, max=1)public String getGrade() {return grade;}public void setGrade(String grade) {this.grade = grade;}@Length(min=0, max=255)public String getAddress() {return address;}public void setAddress(String address) {this.address = address;}@Length(min=0, max=100)public String getZipCode() {return zipCode;}public void setZipCode(String zipCode) {this.zipCode = zipCode;}@Length(min=0, max=100)public String getMaster() {return master;}public void setMaster(String master) {this.master = master;}@Length(min=0, max=200)public String getPhone() {return phone;}public void setPhone(String phone) {this.phone = phone;}@Length(min=0, max=200)public String getFax() {return fax;}public void setFax(String fax) {this.fax = fax;}@Length(min=0, max=200)public String getEmail() {return email;}public void setEmail(String email) {this.email = email;}@Length(min=0, max=100)public String getCode() {return code;}public void setCode(String code) {this.code = code;}@Overridepublic String toString() {return name;}public String getOfficeCode() {return officeCode;}public void setOfficeCode(String officeCode) {this.officeCode = officeCode;}public String getBusinessArea() {return businessArea;}public void setBusinessArea(String businessArea) {this.businessArea = businessArea;}public String getBusinessHours() {return businessHours;}public void setBusinessHours(String businessHours) {this.businessHours = businessHours;}


4.? ? 用法

????????如果我想構(gòu)造一個(gè)對(duì)象

? ? 4.1? ? 構(gòu)造器

????????直接上圖吧:

? ??

????當(dāng)我new Office()的時(shí)候,我不知道需要傳入什么類型的參數(shù),也不知道每個(gè)參數(shù)代表哪個(gè)字段。

多個(gè)字段的時(shí)候,不推薦這種方式。幾個(gè)字段算多?我覺得5+吧。

? ? 4.2??? 構(gòu)建器

????

????? ? 上圖的字段比例子中的多了幾個(gè),?構(gòu)建器構(gòu)造的對(duì)象很清晰,而且相對(duì)利于維護(hù),構(gòu)造器的話,需要修改構(gòu)造方法,構(gòu)建器在builder對(duì)象中加屬性就好了。為什么說構(gòu)建器更安全,因?yàn)橐粋€(gè)對(duì)象在可能有多個(gè)構(gòu)造器,通過構(gòu)造器來創(chuàng)建,沒法保證一致性。比如:new Office(id)和new Office(name),這兩個(gè)對(duì)象怎么保證一致呢。

? ? 4.3? ? get/set方法

????? ? 不演示了,就拿上圖來說,set屬性需要多少行代碼?起碼多兩倍不止。


5.? ??關(guān)于構(gòu)建器和構(gòu)造器

????JavaBean模式自身有嚴(yán)重的缺點(diǎn),因?yàn)闃?gòu)造過程被分到幾個(gè)調(diào)用中,在構(gòu)造過程中Javabean可能處于不一致的狀態(tài),類無法僅僅通過檢驗(yàn)構(gòu)造器參數(shù)的有效性來保證一致性。JavaBean模式阻止了把類做成不可變的可能,這就需要程序員付出額外的努力確保線程安全 。

Java中傳統(tǒng)的抽象工廠實(shí)現(xiàn)是Class對(duì)象,newInstance方法總是企圖調(diào)用類的無參構(gòu)造器,這個(gè)構(gòu)造器甚至可能根本不存在。Class.newInstance破壞了編譯時(shí)的異常檢查。Builder模式也存在不足。為了創(chuàng)建對(duì)象,必須先創(chuàng)建它的構(gòu)建器。在十分注重性能的情況下,可能就成問題了。Builder模式還比重疊構(gòu)造器模式更加冗長(zhǎng),因此它只在有很多參數(shù)的時(shí)候才使用,比如4個(gè)或者更多個(gè)參數(shù)。通常最好一開始就使用構(gòu)建器。

  如果類的構(gòu)造器或者靜態(tài)工廠中具有多個(gè)參數(shù),設(shè)計(jì)這種類時(shí),Builder模式就是種不錯(cuò)的選擇,特別是當(dāng)大多數(shù)參數(shù)都是可選的時(shí)候。與使用傳統(tǒng)的重疊構(gòu)造器模式相比,使用Builder模式的客戶端代碼將更易于閱讀和編寫,構(gòu)建器也比JavaBeans更加安全。


 參考資料

《Effective Java 中文版 第2版》?


????????

總結(jié)

以上是生活随笔為你收集整理的JAVA构造对象的几种方式(构建器、构造器)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。