詳解java創(chuàng)建一個(gè)女朋友類(對象啥的new一個(gè)就是)==建造者模式,一鍵重寫
創(chuàng)建一個(gè)女朋友,她有很多的屬性,比如:性別,年齡,身高,體重,類型等等,雖然每個(gè)女朋友都有這些屬性,但是每個(gè)人找女朋友的要求都是不一樣的,有的人喜歡男的,有的人喜歡女的,有的喜歡胖的,不同的人可以根據(jù)自己的喜好去建造不同的女朋友,我們不需要關(guān)心她是怎么建造的,我們只需要去指定她的屬性就行了
相比如文字解釋,我更習(xí)慣擼代碼來解釋,下面來一步步實(shí)現(xiàn)怎么用java來為你創(chuàng)建一個(gè)女朋友
首先定義一個(gè)女朋友類:
package nuoyanli;
/**
* Created by ${nuoyanli} on 2019/4/7.
*/
public class GirlFriend {
private String sex;//性別
private int age;//年齡
private int stature;//身高
private int weight;//體重
private String type;//類型
按照我們以往的理解,要?jiǎng)?chuàng)建一個(gè)女朋友是不是要直接new出來,我們可以通過構(gòu)造方法把屬性傳過去
例如:我對女朋友的要求只有一個(gè),是女的就行,定義一個(gè)構(gòu)造方法:
public GirlFriend(String sex) {
this.sex = sex;
}
然后再需要的時(shí)候來創(chuàng)建她:
GirlFriend girlFriend = new GirlFriend("女");
如果我們要求性別和身高就要定義:
public GirlFriend(String sex, int stature) {
this.sex = sex;
this.stature = stature;
}
你想想每個(gè)人的要求都不一樣,你得創(chuàng)建多少個(gè)構(gòu)造方法,而且參數(shù)多了,可讀性很差比如:
GirlFriend girlFriend = new GirlFriend("女",19,170,90,"聲優(yōu)");
java有一個(gè)建造者模式:
建造一個(gè)GirlFriendBuilder類:
package nuoyanli;
/**
* Created by ${nuoyanli} on 2019/4/7.
*/
public class GirlFriendBuilder {
String sex;//性別
int age;//年齡
int stature;//身高
int weight;//體重
String type;//類型
public GirlFriendBuilder setSex(String sex) {
this.sex = sex;
return this;
}
public GirlFriendBuilder setAge(int age) {
this.age = age;
return this;
}
public GirlFriendBuilder setStature(int stature) {
this.stature = stature;
return this;
}
public GirlFriendBuilder setWeight(int weight) {
this.weight = weight;
return this;
}
public GirlFriendBuilder setType(String type) {
this.type = type;
return this;
}
/**
*返回一個(gè)GirlFriend對象
*/
public GirlFriend build() {
return new GirlFriend(this);
}
}
然后在GirlFriend類里面構(gòu)造方法傳入GirlFriendBuilder對象:
public GirlFriend(GirlFriendBuilder builder) {
this.sex = builder.sex;
this.age = builder.age;
this.stature = builder.stature;
this.weight = builder.weight;
this.type = builder.type;
}
然后創(chuàng)建的時(shí)候:
GirlFriend girlFrie1nd = new GirlFriendBuilder()
.setAge(19)
.setSex("女")
.setType("聲優(yōu)")
.setStature(175)
.build();
這樣就成功創(chuàng)建了一個(gè)女朋友,代碼的可讀性也挺高的
如果對這個(gè)女朋友不滿意,可以自定義屬性哦,由于筆者水平有限,并且找不到女朋友所以只能先new一個(gè)girFriend對象
以上所述是小編給大家介紹的java建造者模式一鍵重寫詳解整合,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
SpringMVC Mybatis配置多個(gè)數(shù)據(jù)源并切換代碼詳解
這篇文章主要介紹了SpringMVC Mybatis配置多個(gè)數(shù)據(jù)源并切換代碼詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11
springboot讀取nacos配置文件的實(shí)現(xiàn)
SpringBoot注冊服務(wù)到Nacos上,由Nacos來做服務(wù)的管理,本文主要介紹了springboot讀取nacos配置文件的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05
Springboot基礎(chǔ)之RedisUtils工具類
本文來說下RedisUtils工具類,主要介紹了整合Redis、MyBatis,封裝RedisUtils工具類等知識(shí),文中有非常詳細(xì)的代碼示例,對正在學(xué)習(xí)java的小伙伴們有很好的幫助,需要的朋友可以參考下2021-05-05
Jedis零基礎(chǔ)入門及操作Redis中的數(shù)據(jù)結(jié)構(gòu)詳解
Jedis 的 API 方法跟 Redis 的命令基本上完全一致,熟悉 Redis 的操作命令,自然就很容易使用 Jedis,因此官方也推薦 Java 使用 Jedis 來連接和操作 Redis2022-09-09
在java中main函數(shù)如何調(diào)用外部非static方法
這篇文章主要介紹了在java中main函數(shù)如何調(diào)用外部非static方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-12-12

