Posted java杩涢樁鏋舵瀯甯?/a>

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了相关的知识,希望对你有一定的参考价值。

鍏ㄧ綉閮芥槸澶嶅埗绮樿创鐨勬枃绔狅紝鎴戣繖閲屼竴鐩村潥鎸佽緭鍑?span>鍘熷垱

涓嶈閿欒繃姣忎竴绡囧箍鍛婏紝浠ュ強绮惧僵鍘熷垱锛?/span>

澶у濂斤紝鎴戞槸楂樺喎灏辨槸鑼冨効锛屽緢楂樺叴鍙堝拰澶у瑙侀潰浜嗐€傪煒婁粖澶╂垜浠户缁璁℃ā寮忕殑鎺㈢储涔嬭矾銆傚墠鍑犵瘒鐨勫唴瀹规湁灏忎紮浼磋繕娌℃湁闃呰杩囩殑锛屽彲浠ラ槄璇讳竴涓嬨€?/p>

鍓嶆枃鍥為【
馃憠
馃憠璁╄璁℃ā寮忛涓€浼氬効|鈶″崟渚嬫ā寮?br>馃憠

浠婂ぉ鎴戜滑鎺ヤ笅鏉ヨ鑱婄殑鏄?span>鍘熷瀷妯″紡銆?/p>

銆愬師鍒涖€戣璁捐妯″紡椋炰竴浼氬効|鈶e師鍨嬫ā寮?></p> 
 <blockquote class=

鉂撲綍涓哄師鍨嬶紵

缁村熀鐧剧涓婄粰鍑虹殑姒傚康锛氬師鍨嬫槸棣栧垱鐨勬ā鍨嬶紝浠h〃鍚屼竴绫诲瀷鐨勪汉鐗┿€佺墿浠躲€佹垨瑙傚康銆?/p>

浠ユ垜鐨勭悊瑙h兘鍔涜В閲婁竴涓嬶紝灏辨槸璇达紝瀹冩槸涓€绉嶇被鍨嬬殑棣栧垱瀵硅薄銆傚湪闈㈠悜瀵硅薄缂栫▼涓紝鎵€璋撶殑绫诲瀷灏辨槸鎸囩被锛屼篃灏辨槸璇达紝瀹冩槸杩欎釜绫荤殑涓€涓簮瀹炰緥銆?/p>

涓轰粈涔堥渶瑕佸師鍨嬫ā寮忥紵

鎴戣繕鏄潥鎸佸墠闈㈠嚑绡囦竴璐殑椋庢牸锛屽湪娣卞叆浜嗚В璇ユā寮忎箣鍓嶏紝鍏堟潵鎬濊€冧竴涓嬶紝杩欎釜妯″紡瀹冨嚭鐜扮殑鍘熷洜浠ュ強瀛樺湪鐨勬剰涔夋槸浠€涔堬紵

棣栧厛锛岃繖涓ā寮忎篃鏄睘浜?span>鍒涘缓鍨嬫ā寮?/strong>锛屼篃鏄敤鏉ュ垱寤哄璞°€傝繕鏄洖鍒颁箣鍓嶅弽澶嶈杩囩殑涓€涓棶棰橈紝灏辨槸鎴戜滑鍒涘缓瀵硅薄涓轰粈涔堜竴瀹氳浣跨敤鍘熷瀷妯″紡鍛紵

鍍忎箣鍓嶆垜浠杩囩殑鍗曚緥妯″紡鏄洜涓洪渶瑕佹帶鍒跺璞′釜鏁板繀椤绘槸鍗曚釜銆?span>宸ュ巶妯″紡鏄渶瑕佸皢瀵硅薄鍒涘缓鍜屼娇鐢ㄨВ鑰︼紝浣垮緱鍙互鍦ㄤ笉闇€瑕佺煡閬撳垱寤虹粏鑺傝€屼娇鐢ㄤ竴涓璞★紝閭d粖澶╄瀛︿範鐨勫師鍨嬫ā寮忓畠鐢ㄥ湪鍒涘缓瀵硅薄涓婂張鏄嚭浜庝粈涔堝師鍥犲憿锛?/p>

涓句釜绠€鍗曚緥瀛?/p>

姣斿鏌愪竴涓叕鍙告湁A鍜孊涓や釜浜у搧绾匡紝鐜板湪鍋囪闇€瑕佸湪姣忎竴涓骇鍝侀攢鍞嚭鍘讳箣鍓嶅仛涓€娆℃鏌ワ紝妫€鏌ユ爣鍑嗘槸锛屽亣濡傝浜у搧鐨勯噸閲忚秴杩?0kg锛屽氨閲嶆柊鐢熶骇涓€涓柊鐨勩€傜幇鍦ㄥ笇鏈涘皢鎵€鏈変骇鍝佺殑妫€鏌ラ€昏緫鐢ㄥ悓涓€涓€氱敤鏂规硶瀹炵幇锛屽苟涓斿悗缁鍔犳柊浜у搧鍚庡彲浠ユ柟渚挎墿灞曪紝鎬庝箞瀹炵幇杩欎釜闇€姹傦紵

浣犲彲鑳戒細濡備笅瀹炵幇锛堜吉浠g爜锛夛紝

public class ProductCheck {
public void check(Product product) {
if (product.weight > 10) {
//濡傛灉璇ヤ骇鍝佹槸A浜у搧灏卞垱寤轰竴涓柊鐨凙瀵硅薄銆?/span>
//濡傛灉璇ヤ骇鍝佹槸B浜у搧灏卞垱寤轰竴涓柊鐨勫璞°€?/span>
}
}
}

浣嗘槸鐜板湪鍑洪棶棰樹簡锛屽啓涓嶄笅鍘诲彂鐜版病鏈夛紵涓轰粈涔堬紵

鍥犱负鐜板湪妫€鏌ョ殑杩欎釜瀵硅薄閲嶉噺瓒呰繃10kg浜嗭紝鎵€浠ラ渶瑕佸垱寤轰竴涓柊鐨勫璞★紝浣嗛棶棰樿繖涓椂鍊欐垜骞朵笉鐭ラ亾浼犲叆鐨?code class="mq-43">product瀵硅薄鏄粈涔堢被鍨嬪晩锛熸槸A绫诲瀷锛熻繕鏄疊绫诲瀷锛熻繖涓湪浣犵紪璇戞椂鏈熶綘鏄笉鐭ラ亾鐨勶紝鑷劧杩欎唬鐮佷綘灏辨病娉曞啓涓嬪幓浜?.....

閭h鎬庝箞瑙e喅杩欎釜闂鍛紵

鏈夎仾鏄庣殑绋嬪簭鐚胯浜嗭紝杩欒繕涓嶇畝鍗曞悧锛熺洿鎺ュ湪check()鏂规硶涓姞涓?code class="mq-47">if else鍒ゆ柇涓€涓嬩笉灏卞ソ浜嗗悧锛熶簬鏄唬鐮佷紭鍖栨垚濡備笅锛?/p>

public class ProductCheck {
public void check(Product product) {
if (product.weight > 10) {
if (product instanceof PA) {
//濡傛灉璇ヤ骇鍝佹槸A浜у搧灏卞垱寤轰竴涓柊鐨凙瀵硅薄銆?/span>
} else if (product instanceof PB) {
//濡傛灉璇ヤ骇鍝佹槸B浜у搧灏卞垱寤轰竴涓柊鐨勫璞°€?/span>
}
}
}
}

鍏朵腑PA鍜?code class="mq-70">PB鍒嗗埆鏄?code class="mq-71">Product鎺ュ彛鐨勫瓙绫伙紝琛ㄧずA浜у搧鍜孊浜у搧銆備笂闈唬鐮佺湅涓婂幓璨屼技纭疄娌″暐闂锛岄€氳繃瀵逛紶鍏ョ殑product绫诲瀷鍒ゆ柇浠庤€屽垱寤轰笉鍚岀被鍨嬬殑瀵硅薄锛屽緢姝e父鍢涳紵

鉂撲絾鏄繖鏍峰啓鏈変釜鍟ラ棶棰橈紵

涓婇潰闇€姹傛槸杩欎釜閫氱敤鏂规硶闇€瑕佹弧瓒筹紝鍚庣画澧炲姞鏂颁骇鍝佸悗鍙互鏂逛究鎵╁睍銆傜幇鍦ㄥ亣璁捐繖涓叕鍙告柊澧炰簡涓€绉岰浜у搧锛屼篃闇€瑕佷娇鐢ㄨ繖涓鏌ユ柟娉曟€庝箞鍔烇紵杩欎釜鏃跺€欎綘灏卞繀椤昏淇敼check()鏂规硶鐨勪唬鐮侊紝澧炲姞else if (product instanceof PC)鐨勯€昏緫锛岃繕璁板緱寮€闂師鍒欏悧锛熻繖鏄剧劧杩濆弽浜嗗紑闂師鍒欙紝鎵€浠ヨ繖涓柟妗堜笉鍙彇銆傚鏋滅湅杩囦笂绡囧伐鍘傛ā寮忕殑鍚屽鍙兘鎯宠捣鐐逛粈涔堬紵杩欎釜鏈夌偣绫讳技宸ュ巶妯″紡閲岄潰鐨勭畝鍗曞伐鍘傛ā寮忓槢锛?/p>

寮曡捣杩欎釜闂鐨勬湰璐ㄥ湪浜庡摢閲岋紵

娌¢敊锛屽氨鏄洜涓?code class="mq-81">check()璺熷叿浣撶殑浜у搧绫昏€﹀悎浜嗐€?/p>

褰撴椂鏄€庝箞瑙e喅寮€闂師鍒欑殑闂鐨勶紵

娌¢敊鏄€氳繃宸ュ巶鏂规硶妯″紡瑙e喅鐨勶紝浜庢槸浼樺寲鍚庝唬鐮佸涓嬶細

public class ProductCheck {
public void check(ProductFactory factory) {
Product product = factory.createProduct();
if (product.weight > 10) {
Product product2 = factory.createProduct();
}
}
}

鍏朵腑A浜у搧鍜孊浜у搧鍚勪細鏈?code class="mq-99">ProductFactory鐨勫疄鐜扮被锛岃繖鏍峰綋鏂板浜у搧鏃跺氨涓嶄細鍑虹幇寮€闂師鍒欑殑闂浜嗐€傛病閿欙紝杩欎釜闂鐢ㄥ伐鍘傛柟娉曟ā寮忓畬鍏ㄥ彲浠ヨВ鍐筹紝娌¢棶棰樸€備絾鏄粖澶╁憿锛屾垜浠皢瑕佽亰鐨勫師鍨嬫ā寮忎篃鍙兘瑙e喅杩欎釜闂銆傝繖涓椂鍊欒偗瀹氫細鏈変汉闂簡锛屾棦鐒跺伐鍘傛ā寮忓凡缁忓彲浠ヨВ鍐宠繖涓€闂锛岄偅涓轰粈涔堣繕瑕佷綘鐨勫師鍨嬫ā寮忓憿锛?/p>

杩欎釜闂鎴戜細鐣欏埌鍚庨潰璁诧紝鐜板湪鍏堣鎴戜滑鐪嬩竴涓嬪師鍨嬫ā寮忔槸鎬庝箞瑙e喅杩欎釜闂鐨勶紵

鍘熷瀷妯″紡鐨勫師鐞嗘槸杩欐牱鐨勶紝鍘熷瀷妯″紡瑕佹眰锛屾瘡涓€涓璞¢渶瑕佸畾涔変竴涓厠闅嗚嚜宸辩殑鏂规硶銆備粈涔堟剰鎬濓紵姣斿涓€涓狝瀵硅薄锛屼粬闇€瑕佹彁渚涗竴涓柟娉曪紝璋冪敤杩欎釜鏂规硶灏嗕細杩斿洖涓€涓嚜宸辩殑鍓湰銆備竴鑸潵璇达紝浼氱粰鎵€鏈夐渶瑕佸厠闅嗚嚜宸辩殑瀵硅薄鎻愪緵涓€涓叕鍏辩殑鎺ュ彛锛岃繖涓帴鍙i噷闈細鎻愪緵涓€涓厠闅嗚嚜韬殑鏂规硶锛屽涓嬶紝

interface CloneableObj {
Object cloneSelf();
}

鐒跺悗璁╂墍鏈夐渶瑕佸厠闅嗚嚜宸辩殑绫诲幓瀹炵幇璇ユ帴鍙o紝鑷劧浼氶渶瑕佸疄鐜?code class="mq-111">cloneSelf()鏂规硶锛岃繖涓柟娉曞唴閮ㄥ氨鏄厠闅嗚嚜宸辩殑閫昏緫瀹炵幇銆傞偅濡備綍瀹炵幇鍏嬮殕鍛紵

鏈€鍌荤摐鐨勫姙娉曪紝鐩存帴鍏坣ew涓€涓嚜宸卞璞$殑瀹炰緥锛岀劧鍚庢妸鑷繁瀹炰緥涓殑鏁版嵁鍙栧嚭鏉ワ紝璁剧疆鍒版柊鐨勫璞″疄渚嬩腑鍘伙紝涓嶅氨鍙互瀹屾垚瀹炰緥鐨勫鍒朵簡鍢涳紵杩欐牱杩欎釜cloneSelf()鏂规硶杩斿洖鐨勫氨鏄竴涓窡鑷韩涓€妯′竴鏍风殑瀵硅薄浜嗐€備互涓嬫槸浠g爜瀹炵幇锛?/p>

class PA extends Product implements CloneableObj{
@Override
public Object cloneSelf() {
PA a = new PA();
a.weight = weight;
return a;
}
}

杩欎釜鏃跺€欏亣璁句綘闇€瑕佸湪宸ョ▼鍏朵粬浠g爜涓渶瑕侀€氳繃鍏嬮殕鏂瑰紡蹇€熷緱鍒颁竴涓狿A瀵硅薄锛屽氨鍙互閫氳繃璋冪敤鍘熷瀷PA瀵硅薄锛堝亣璁炬槸a锛?code class="mq-131">a.cloneSelf()杞绘澗蹇€熺殑寰楀埌涓€涓?code class="mq-132">PA瀵硅薄浜嗐€?/p>

娌¢敊锛岃繖涓氨鏄渶鏈川鐨勫師鍨嬫ā寮忋€傚叾瀹炶鐨勭畝鍗曚竴鐐癸紝鎵€璋撶殑鍘熷瀷妯″紡锛屽氨鏄鍒讹紙鎴栧厠闅嗭級妯″紡锛屽氨鏄€氳繃褰撳墠瀵硅薄锛堝師鍨嬪璞★級杩斿洖涓€涓窡褰撳墠瀵硅薄瀹屽叏鐩稿悓鐨勫璞★紝鍖呮嫭鍏朵腑鐨勫睘鎬у€笺€?/p>

杩欎篃鏄師鍨嬫ā寮忚窡鐩存帴new鐨勪竴涓尯鍒紝鎴戜滑鐭ラ亾new鐢熸垚鐨勫璞$殑灞炴€у€奸兘鏄粯璁ょ殑锛岃€岄€氳繃鍘熷瀷妯″紡杩斿洖鐨勫璞℃槸灏嗗睘鎬у€间竴鍚屽鍒躲€?/strong>

鍏跺疄锛屽師鍨嬫ā寮忓苟涓嶅己鍒惰姹傚厠闅嗙敓鎴愮殑瀵硅薄鍜屽師鍨嬪璞″畬鍏ㄧ浉鍚岋紝鑰屼笖涔熸病鏈夎瀹氬叿浣撻噰鐢ㄧ殑鍏嬮殕鎶€鏈紝杩欎釜鍙互鐢辩▼搴忚嚜宸卞疄鐜般€傚彧鏄湪澶ч儴鍒嗗疄闄呭簲鐢ㄥ満鏅腑锛岀敤鍘熷瀷妯″紡鐢熸垚鐨勫璞¢兘鏄拰鍘熷瀷瀵硅薄瀹屽叏鐩稿悓鎴栬€呯浉杩戙€?/p>

Object.clone()

鍏跺疄锛屼笂闈㈣繖涓緥瀛愭槸涓轰簡鏇村ソ鐨勭悊瑙e師鍨嬫ā寮忕殑鏈川锛屼负浜嗘彁楂樺厠闅嗘晥鐜囷紝JDK宸茬粡璁捐浜嗗叧浜庡璞″厠闅嗙殑鍔熻兘銆傚湪Object绫讳腑鏈変竴涓?code class="mq-143">clone()鏂规硶锛岃鏂规硶灏卞彲浠ヨ交鏉剧殑瀹炵幇瀵瑰璞℃湰韬繘琛屽厠闅嗐€備笂闈緥瀛愪腑搴曞眰杩樻槸閲囩敤new鐨勬柟寮忓垱寤哄璞★紝浣嗘槸Object.clone()搴曞眰鏄洿鎺ュ浜岃繘鍒舵暟鎹祦鎿嶄綔锛屽洜姝ゆ晥鐜囦細姣旂洿鎺ew鐨勬柟寮忛珮寰楀锛堢湅鍒板悗闈紝鍏跺疄杩欏彞璇濊鐨勪笉涓ヨ皑锛夈€備笉杩囪浣跨敤Object.clone()鏉ュ鑷韩瀵硅薄鍏嬮殕鏈変釜闄愬埗锛屽氨鏄瀵硅薄鎵€瀵瑰簲鐨勭被蹇呴』瑕佸疄鐜?code class="mq-147">java.lang.Cloneable鎺ュ彛锛屽惁鍒欎細鎶涘嚭CloneNotSupportedException寮傚父銆傚彟澶栵紝涓€鑸渶瑕佽鍏嬮殕鐨勭被閮介渶瑕侀噸鍐?code class="mq-149">Object.clone()锛屽苟涓斿皢璁块棶淇グ绗︽敼涓?code class="mq-150">public锛屼互鏂逛究鍦ㄥ叾浠栫被涓娇鐢ㄣ€備簬鏄唬鐮佸疄鐜板涓嬶細

public class ProductCheck {
public void check(Product product) throws CloneNotSupportedException {
if (product.weight > 10) {
Object o = product.clone();
}
}
}

鏈変汉鍙堜細鏈夌枒闂簡锛岃繖鏍峰仛鐩告瘮杈冨墠闈㈢殑宸ュ巶妯″紡鏈夊暐浼樺娍锛熷伐鍘傛柟娉曟ā寮忎篃瀹屽叏鍙互瀹炵幇鐩稿悓鐨勯渶姹傚晩锛?/p>

鍘熷瀷妯″紡鍜屽伐鍘傛ā寮?/span>

鍘熷瀷妯″紡鍜屽伐鍘傛柟娉曟ā寮忎竴涓叡鍚岀殑浼樼偣鏄紝浠栦滑閮藉彲浠ュ湪涓嶇煡閬撳叿浣撶殑绫诲瀷鎯呭喌涔嬩笅锛屽垱寤哄嚭鏌愮被鍨嬪璞°€傛瘮濡備笂闈緥瀛愪腑鐨?code class="mq-170">Product锛岃繖鍙槸涓€涓娊璞℃帴鍙o紝鍏朵笅浼氭湁寰堝鐨勫瓙绫伙紝鍏蜂綋鍒涘缓鍝绫诲瀷鐨勫瓙绫诲璞″彇鍐充簬杩愯鏃舵湡銆傚師鍨嬫ā寮忔槸閫氳繃鍏嬮殕鑷韩鐨勬柟寮忓疄鐜扮殑锛岃€屽伐鍘傛柟娉曟ā寮忔槸閫氳繃涓嶅悓瀛愮被鐨勫伐鍘傜被瀹炵幇鐨勩€?/p>

浣嗘槸鍘熷瀷妯″紡鐩告瘮浜庡伐鍘傛柟娉曟ā寮忕殑浼樺娍鍦ㄤ簬锛屽伐鍘傛柟娉曟ā寮忓簳灞傝繕鏄噰鐢?code class="mq-172">new鐨勬柟寮忓垱寤哄璞★紝骞朵笖闇€瑕佹墜鍔ㄧ殑涓哄睘鎬ц祴鍊硷紝鏁堢巼杈冨樊銆傝€岄€氳繃Object.clone()瀹炵幇鐨勫師鍨嬫ā寮忕洿鎺ユ槸鎿嶄綔浜岃繘鍒舵祦瀹炵幇锛岃€屼笖鍏嬮殕鐢熸垚鐨勫璞℃槸宸茬粡璧嬪ソ鍊间簡銆傚洜姝ゆ晥鐜囪楂樺緱澶氥€?/p>

閭d箞锛岄€氳繃new鐨勬柟寮忓垱寤哄璞″拰璋冪敤clone()鏂瑰紡鍒涘缓瀵硅薄锛屾晥鐜囩浉宸灏戯紵

涓嬮潰缁欏嚭涓€涓畝鍗曠殑娴嬭瘯渚嬪瓙锛?/p>

public class A implements Cloneable {
private String a = "a";

@Override
public Object clone() throws CloneNotSupportedException {
return super.clone();
}
public A() {}
}

public class Demo {
public static void main(String[] args) throws CloneNotSupportedException {
A a = new A();
long s1 = System.nanoTime();
for (int i = 0; i < 10000000; i++) {
new A();
}
long e1 = System.nanoTime();
System.out.println("new cost total: " + TimeUnit.NANOSECONDS.toMillis(e1 - s1));
long s2 = System.nanoTime();
for (int i = 0; i < 10000000; i++) {
a.clone();
}
long e2 = System.nanoTime();
System.out.println("clone cost total: " + TimeUnit.NANOSECONDS.toMillis(e2 - s2));
}
}

涓婇潰杈撳嚭鐨勭粨鏋滃涓嬶細

銆愬師鍒涖€戣璁捐妯″紡椋炰竴浼氬効|鈶e師鍨嬫ā寮?></p> 
 <p class=鐩镐俊鐪嬪埌杩欎釜缁撴灉鐨勬湅鍙嬭偗瀹氫細澶у悆涓€鎯娾亯锔忛渿鎯婏紒鎬庝箞clone鐨勯€熷害姣?code class="mq-233">new杩樻參浜嗚繖涔堝鍊?.....馃槺鍜屼箣鍓嶇殑璁ょ煡澶х浉寰勫涵浜嗐€?/p>

閭g粨鏋滅湡鐨勬槸杩欐牱鍚椻潛

鎴戜滑鍋氫竴涓嬪皬鏀瑰姩锛屽叾瀹冧唬鐮侀兘涓嶅仛淇敼锛屼絾杩欎竴娆℃垜浠湪new鎵€闇€瑕佺殑鏋勯€犲櫒涓姞鍏ヤ竴浜涜€楁椂鎿嶄綔锛屽涓嬶細

public class A implements Cloneable {
//鍏朵綑浠g爜鍜屼笂闈㈢殑渚嬪瓙涓€鏍凤紝鐪佺暐锛屽敮涓€鍖哄埆鍦ㄤ簬鍔犲叆涓嬮潰鐨勪唬鐮?/span>
public A() {
for (int i = 0; i < 1000; i++)
a += "a";
}
}

涓轰簡鑺傜渷娴嬭瘯鏃堕棿锛屾垜浠妸Demo涓殑寰幆娆℃暟鍑忓皯鍒?0000灏卞ソ锛屽涓嬶細

public class Demo {
public static void main(String[] args) throws CloneNotSupportedException {
A a = new A();
long s1 = System.nanoTime();
for (int i = 0; i < 10000; i++) {
new A();
}
long e1 = System.nanoTime();
System.out.println("new cost total: " + TimeUnit.NANOSECONDS.toMillis(e1 - s1));
long s2 = System.nanoTime();
for (int i = 0; i < 10000; i++) {
a.clone();
}
long e2 = System.nanoTime();
System.out.println("clone cost total: " + TimeUnit.NANOSECONDS.toMillis(e2 - s2));
}
}

杩欎竴娆℃祴璇曠粨鏋滃涓嬶細

杩欎竴娆℃€荤畻鍑虹幇绗﹀悎棰勬湡鐨勭粨鏋滀簡銆?/p>

涔熷氨鏄锛屽浜庢湰韬垱寤鸿繃绋嬩笉鏄緢鑰楁椂鐨勭畝鍗曞璞℃潵璇达紝鐩存帴new鐨勬晥鐜囪姣攃lone瑕侀珮銆備絾鏄鏋滄槸鍒涘缓杩囩▼寰堝鏉傚緢鑰楁椂鐨勫璞★紝閭d娇鐢╟lone鐨勬柟寮忚姣攏ew鐨勬柟寮忔晥鐜囬珮寰楀銆傝繖涔熸槸clone()鏂规硶鐨勬剰涔夋墍鍦ㄣ€?/strong>

涔熷氨鏄锛屽浜庡垱寤鸿€楁椂澶嶆潅鐨勫璞★紝鐢ㄥ師鍨嬫ā寮忓彲浠ュぇ澶ф彁楂樺垱寤哄璞$殑鏁堢巼銆傚埌杩欓噷浼拌寰堝浜哄簲璇ヨ兘鎯冲埌锛屾棦鐒惰繖鏍凤紝鎶婅繖涓よ€呯粨鍚堜竴涓嬩笉灏卞彲浠ュ讥琛ュ伐鍘傛柟娉曟ā寮忕殑缂洪櫡浜嗗悧锛?/p>

娌¢敊锛屼紶缁熺殑宸ュ巶鏂规硶妯″紡涓紝鍚勫瓙绫荤殑宸ュ巶绫诲垱寤哄璞$殑鏂规硶锛屾瘮濡備笂闈㈢殑factory.createProduct()搴曞眰杩樻槸閲囩敤new鐨勬柟寮忥紝濡傛灉鏀规垚鍏嬮殕鏂瑰紡灏卞彲浠ュぇ澶ф彁楂樺垱寤哄璞$殑鏁堢巼浜嗐€傛€濊矾姣旇緝绠€鍗曪紝鍏蜂綋浠g爜杩欒竟灏变笉婕旂ず浜嗐€?/p>

鍙﹀锛屽湪鍘熷瀷妯″紡涓繕浼氭秹鍙婂埌涓€涓?span>娴呭厠闅嗗拰娣卞厠闅?/strong>鐨勯棶棰橈紝鎬庝箞鐞嗚В鍛紵鎴戜妇涓€涓畝鍗曠殑渚嬪瓙锛?/p>

//浠ヤ笅浠g爜鍏ㄩ儴鐪佺暐setter銆乬etter銆乼oString
public class A {
private int a;
}

public class B implements Cloneable{
private int b;
private A a;

@Override
public Object clone() throws CloneNotSupportedException {
return super.clone();
}
}

public class ShallowClone {
public static void main(String[] args) throws CloneNotSupportedException {
A a = new A();
a.setA(1);
B b = new B();
b.setA(a);
b.setB(2);
B b2 = (B)b.clone(); 鉂?br> System.out.println("b-->" + b);
System.out.println("b2-->" + b2);
b2.setB(3); 鉂?br> System.out.println("b-->" + b);
System.out.println("b2-->" + b2);
b2.getA().setA(10);鉂?br> System.out.println("b-->" + b);
System.out.println("b2-->" + b2);
}
}

//杈撳嚭锛?/span>
b-->B{b=2, a=A{a=1}}
b2-->B{b=2, a=A{a=1}}
b-->B{b=2, a=A{a=1}}
b2-->B{b=3, a=A{a=1}}
b-->B{b=2, a=A{a=10}}
b2-->B{b=3, a=A{a=10}}

浠庝笂闈㈣繖涓唬鐮佸垎鏋愬彲寰楀嚭锛?/p>

  • 鉂疯澶勶紝淇敼b2瀵硅薄涓殑b灞炴€э紝鍙戠幇骞舵病鏈夊奖鍝嶅師鍨嬪璞涓璪灞炴€у€笺€俠灞炴€ф槸int绫诲瀷銆?/p>

  • 鉂歌澶勶紝淇敼b2瀵硅薄涓殑a灞炴€т腑鐨刟灞炴€у€硷紝鍙戠幇鍘熷瀷瀵硅薄b涓殑a灞炴€х殑a灞炴€у€间篃鍙戠敓浜嗘敼鍙樸€俛灞炴€ф槸涓€涓?code class="mq-360">A绫诲瀷锛堝紩鐢ㄧ被鍨嬶級銆?/p>

鐢辨锛屾垜浠彲浠ュ緱鍑轰竴涓粨璁猴紝Object.clone()瀹炵幇鐨勫叾瀹炴槸涓€绉?span>娴呭厠闅?/strong>妯″紡銆?/p>

鍦ㄦ祬鍏嬮殕妯″紡涓嬶紝鍏嬮殕鐢熸垚瀵硅薄鐨勫熀鏈暟鎹被鍨嬶紙鍖呮嫭瀵瑰簲鍖呰绫伙級灞炴€у拰String鎷疯礉鐨勬槸鍊硷紝鍚庣画淇敼鍏嬮殕瀵硅薄鐨勮灞炴€у€硷紝骞朵笉浼氬奖鍝嶅師鏉ョ殑瀵硅薄閲岀殑鍊笺€備絾濡傛灉鏄紩鐢ㄧ被鍨嬪睘鎬ф嫹璐濈殑鏄紩鐢紝鎷疯礉寰楀埌鐨勫璞″拰鍘熸潵鐨勫璞$殑灞炴€ф寚鍚戝悓涓€涓璞°€傛墍浠ワ紝鍚庣画淇敼鍏跺睘鎬у€硷紝灏变細褰卞搷鍘熸潵鐨勫璞¢噷鐨勫搴旂殑灞炴€у€笺€?/p>

鑰屽湪鏈変簺鍦哄悎涓嬶紝鎴戜滑鏄笇鏈涘師鍨嬪璞″拰鏂板垱寤虹殑瀵硅薄涓嶈鐩镐簰骞叉壈銆傝繖灏辨槸娣卞厠闅?/strong>妯″紡銆?/p>

鉂撻偅鎬庝箞瀹炵幇鍛紵

public class A implements Cloneable{
private int a;

@Override
public Object clone() throws CloneNotSupportedException {
return super.clone();
}
}

public class B implements Cloneable{
private Integer b;
private A a;

@Override
public Object clone() throws CloneNotSupportedException {
B b = (B) super.clone(); 鉂?br> A a = b.getA();
b.setA((A) a.clone());
return b;
}
}

public class DeepClone {
public static void main(String[] args) throws CloneNotSupportedException {
A a = new A();
a.setA(1);
B b = new B();
b.setA(a);
b.setB(2);
B b2 = (B)b.clone();
System.out.println("b-->" + b);
System.out.println("b2-->" + b2);
b2.setB(3);
System.out.println("b-->" + b);
System.out.println("b2-->" + b2);
b2.getA().setA(10);
System.out.println("b-->" + b);
System.out.println("b2-->" + b2);
}
}
//杈撳嚭锛?/span>
b-->B{b=2, a=A{a=1}}
b2-->B{b=2, a=A{a=1}}
b-->B{b=2, a=A{a=1}}
b2-->B{b=3, a=A{a=1}}
b-->B{b=2, a=A{a=1}}
b2-->B{b=3, a=A{a=10}}

閫氳繃涓婇潰浠g爜鎵ц缁撴灉鎴戜滑涓嶉毦鐪嬪嚭锛岃繖涓椂鍊欐棤璁烘槸淇敼b2涓殑a灞炴€э紙寮曠敤绫诲瀷锛夎繕鏄痓灞炴€э紙鍩烘湰绫诲瀷锛夛紝閮戒笉浼氬奖鍝嶅埌鍘熷瀷瀵硅薄涓殑鍊间簡銆?/p>

鉂撻偅杩欎釜鏄€庝箞瀹炵幇鐨勫憿锛?/p>

娣卞厠闅嗘ā寮忓疄鐜扮殑鍏抽敭鍦ㄤ簬鉂惰澶勶紝鍦?code class="mq-441">B瀵硅薄閫氳繃璋冪敤clone()澶嶅埗鑷繁鐨勫悓鏃讹紝灏哸灞炴€?寮曠敤绫诲瀷)涔?code class="mq-443">clone浜嗕竴浠斤紝骞朵笖璧嬪€肩粰鐢熸垚鐨刡2瀵硅薄銆?/p>

娣卞厠闅嗗師鐞嗗氨鏄湪姣忎竴涓師鍨嬪璞℃墽琛?code class="mq-445">clone()鏂规硶鐨勬椂鍊欙紝鍚屾椂灏嗚瀵硅薄涓瘡涓€涓紩鐢ㄧ被鍨嬬殑灞炴€х殑鍐呭涔熸嫹璐濅竴浠斤紝骞惰缃埌鏂板垱寤虹殑瀵硅薄涓€傚亣璁撅紝姣忎竴涓紩鐢ㄧ被鍨嬩腑鍙堝祵濂楃潃鍏跺畠鐨勫紩鐢ㄧ被鍨嬬殑灞炴€э紝鍐嶉噸澶嶄笂闈㈡搷浣滐紝浠ユ绫绘帹锛岄€掑綊鎵ц涓嬪幓......杩欎腑闂村彧瑕佹湁涓€涓病鏈夎繖鏍锋搷浣滐紝娣卞厠闅嗗氨澶辫触銆?/p>

杩欎篃鏄師鍨嬫ā寮忎竴澶х己鐐癸紝鍦ㄥ疄鐜版繁鍏嬮殕澶嶅埗鏃讹紝姣忎釜鍘熷瀷鐨勫瓙绫婚兘蹇呴』瀹炵幇clone()鐨勬搷浣滐紝灏ゅ叾鏄寘鍚?span>澶氬眰宓屽寮曠敤绫诲瀷鐨勫璞℃椂锛屽繀椤昏閫掑綊鐨勮鎵€鏈夌浉鍏冲璞¢兘姝g‘鐨勫疄鐜板厠闅嗘搷浣滐紝鍗佸垎绻佺悙鏄撻敊銆?/p>

閭f湁娌℃湁鏇村ソ鐨勫姙娉曟潵瀹炵幇娣卞厠闅嗗憿锛?/strong>

褰撶劧鏈夛紒馃槑

搴忓垪鍖栧拰鍙嶅簭鍒楀寲瀹炵幇娣卞厠闅?/span>

鍙互浣跨敤搴忓垪鍖栧拰鍙嶅簭鍒楀寲鐨勬墜娈靛疄鐜板璞$殑娣卞厠闅嗭紒

public class A implements Serializable {
private int a;
}
public class B implements Serializable {
private Integer b;
private A a;
}

public class DeepClone2 {
public static void main(String[] args) throws IOException, ClassNotFoundException {
A a = new A();
a.setA(1);
B b = new B();
b.setA(a);
b.setB(2);

ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(b);
ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()));
B b2 = (B) ois.readObject();
System.out.println("b-->" + b);
System.out.println("b2-->" + b2);
b2.setB(3);
System.out.println("b-->" + b);
System.out.println("b2-->" + b2);
b2.getA().setA(10);
System.out.println("b-->" + b);
System.out.println("b2-->" + b2);
}
}
//杈撳嚭锛?/span>
b-->B{b=2, a=A{a=1}}
b2-->B{b=2, a=A{a=1}}
b-->B{b=2, a=A{a=1}}
b2-->B{b=3, a=A{a=1}}
b-->B{b=2, a=A{a=1}}
b2-->B{b=3, a=A{a=10}}

閫氳繃涓婇潰鐨勪唬鐮佷笉闅剧湅鍑猴紝搴忓垪鍖栧拰鍙嶅簭鍒楀寲纭疄瀹炵幇浜嗘繁鍏嬮殕锛岃€屼笖鍦ㄥ疄鐜版柟寮忎笂姣斾箣鍓嶇敤閲嶅啓clone()鐨勬柟寮忚绠€鍗曠殑澶氾紝鍞竴闇€瑕佸仛鐨勫氨鏄粰闇€瑕佸厠闅嗙殑瀵硅薄浠ュ強寮曠敤绫诲瀷瀹炵幇Serializable鎺ュ彛鍗冲彲銆?/p>

鏈€鍚庢潵鍋氫釜鎬荤粨锛屽叾瀹炲師鍨嬫ā寮忔洿閫傚悎鍙仛鍏嬮殕妯″紡锛屽畠鐨勬湰璐ㄥ氨鍦ㄤ簬閫氳繃涓€瀹氭妧鏈墜娈电敓鎴愪竴涓嚜韬殑鍓湰銆傝繖鍙互閫氳繃鎴戜滑鍦ㄦ枃绔犳渶寮€濮嬮偅鏍锋墜鍔╪ew涓€涓紝涔熷彲浠ラ€氳繃Object.clone()锛岃繕鍙互閫氳繃搴忓垪鍖栧拰鍙嶅簭鍒楀寲瀹炵幇銆傚鏋滃師鍨嬪璞′腑瀛樺湪寮曠敤绫诲瀷鐨勫睘鎬э紝鏍规嵁鏄惁鍚屾椂鍏嬮殕璇ュ睘鎬у彲浠ュ垎涓烘繁鍏嬮殕妯″紡鍜屾祬鍏嬮殕妯″紡銆?/p>

浣曟椂浣跨敤鍘熷瀷妯″紡锛?/span>

鍦ㄥぇ閮ㄥ垎鍦烘櫙涓嬶紝鎴戜滑涓昏浼氫娇鐢?code class="mq-521">Object.clone()鏂规硶鏉ュ疄鐜板厠闅嗭紝鏍规嵁涓婇潰瀵筩lone()鏂规硶鎵ц鎬ц兘娴嬭瘯缁撴灉锛屽湪鍒涘缓澶ч噺澶嶆潅瀵硅薄鏃讹紝杩欎釜鏂规硶鐨勫垱寤烘晥鐜囪杩滈珮浜巒ew鐨勬柟寮忋€傚洜姝ゅ鏋滈渶瑕佸垱寤哄ぇ閲忓苟涓斿鏉傚璞℃椂鍙互閲囩敤鍘熷瀷妯″紡銆?/p>

鍙﹀锛屽師鍨嬫ā寮忓彲浠ュ儚宸ュ巶鏂规硶妯″紡涓€鏍凤紝鍙互鍦ㄤ簨鍏堜笉鐭ラ亾鍏蜂綋绫诲瀷鐨勫墠鎻愪笅鍒涘缓鍑哄璞★紝涔熷氨鏄?span>鍩轰簬鎺ュ彛鍒涘缓瀵硅薄锛岃€屼笖瀹炵幇鏂瑰紡姣斿伐鍘傛ā寮忔洿楂樻晥绠€鍗曘€?/p>

濂戒簡锛屼粖澶╁叧浜庡師鍨嬫ā寮忕殑鎶€鏈垎浜氨鍒版缁撴潫锛屼笅涓€绡囨垜浼氱户缁垎浜彟涓€涓璁℃ā寮忊€斺€?span>寤洪€犺€呮ā寮?/strong>锛屼竴璧锋帰璁ㄨ璁℃ā寮忕殑濂ョ銆傚挶浠笉瑙佷笉鏁c€傪煒?/p>

鈥斺€斺€斺€?nbsp; e n d 鈥斺€斺€斺€?/span>


鐪嬪埌杩欓噷锛岃鏄庝綘鍠滄鏈枃

浣犵殑杞彂锛屾槸瀵规垜鏈€澶х殑榧撳姳锛?/strong>鍦ㄧ湅浜︽槸鏀寔鈫?/strong>

以上是关于的主要内容,如果未能解决你的问题,请参考以下文章

谷歌浏览器调试jsp 引入代码片段,如何调试代码片段中的js

片段和活动之间的核心区别是啥?哪些代码可以写成片段?

VSCode自定义代码片段——.vue文件的模板

VSCode自定义代码片段6——CSS选择器

VSCode自定义代码片段——声明函数

VSCode自定义代码片段8——声明函数