java 娉涘瀷璇﹁В
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 娉涘瀷璇﹁В相关的知识,希望对你有一定的参考价值。
鏍囩锛?a href='http://www.mamicode.com/so/1/%e6%b7%bb%e5%8a%a0' title='娣诲姞'>娣诲姞 false size object type and dom out 璁块棶
https://blog.csdn.net/s10461/article/details/53941091
瀵筳ava鐨勬硾鍨嬬壒鎬х殑浜嗚В浠呴檺浜庤〃闈㈢殑娴呮祬涓€灞傦紝鐩村埌鍦ㄥ涔犺璁℃ā寮忔椂鍙戠幇鏈変笉浜嗚В鐨勭敤娉曪紝鎵嶆兂璧疯缁嗙殑璁板綍涓€涓嬨€?br />鏈枃鍙傝€僯ava 娉涘瀷璇﹁В銆丣ava涓殑娉涘瀷鏂规硶銆?java娉涘瀷璇﹁В
1. 姒傝堪娉涘瀷鍦╦ava涓湁寰堥噸瑕佺殑鍦颁綅锛屽湪闈㈠悜瀵硅薄缂栫▼鍙婂悇绉嶈璁℃ā寮忎腑鏈夐潪甯稿箍娉涚殑搴旂敤銆?br />浠€涔堟槸娉涘瀷锛熶负浠€涔堣浣跨敤娉涘瀷锛?br />娉涘瀷锛屽嵆“鍙傛暟鍖栫被鍨?rdquo;銆備竴鎻愬埌鍙傛暟锛屾渶鐔熸倝鐨勫氨鏄畾涔夋柟娉曟椂鏈夊舰鍙傦紝鐒跺悗璋冪敤姝ゆ柟娉曟椂浼犻€掑疄鍙傘€傞偅涔堝弬鏁板寲绫诲瀷鎬庝箞鐞嗚В鍛紵椤惧悕鎬濅箟锛屽氨鏄皢绫诲瀷鐢卞師鏉ョ殑鍏蜂綋鐨勭被鍨嬪弬鏁板寲锛岀被浼间簬鏂规硶涓殑鍙橀噺鍙傛暟锛屾鏃剁被鍨嬩篃瀹氫箟鎴愬弬鏁板舰寮忥紙鍙互绉颁箣涓虹被鍨嬪舰鍙傦級锛岀劧鍚庡湪浣跨敤/璋冪敤鏃朵紶鍏ュ叿浣撶殑绫诲瀷锛堢被鍨嬪疄鍙傦級銆?br />娉涘瀷鐨勬湰璐ㄦ槸涓轰簡鍙傛暟鍖栫被鍨嬶紙鍦ㄤ笉鍒涘缓鏂扮殑绫诲瀷鐨勬儏鍐典笅锛岄€氳繃娉涘瀷鎸囧畾鐨勪笉鍚岀被鍨嬫潵鎺у埗褰㈠弬鍏蜂綋闄愬埗鐨勭被鍨嬶級銆備篃灏辨槸璇村湪娉涘瀷浣跨敤杩囩▼涓紝鎿嶄綔鐨勬暟鎹被鍨嬭鎸囧畾涓轰竴涓弬鏁帮紝杩欑鍙傛暟绫诲瀷鍙互鐢ㄥ湪绫汇€佹帴鍙e拰鏂规硶涓紝鍒嗗埆琚О涓烘硾鍨嬬被銆佹硾鍨嬫帴鍙c€佹硾鍨嬫柟娉曘€?br />2. 涓€涓牀瀛愪竴涓涓句簡鏃犳暟娆$殑渚嬪瓙锛?br />List arrayList = new ArrayList();arrayList.add("aaaa");arrayList.add(100);
for(int i = 0; i< arrayList.size();i++){ String item = (String)arrayList.get(i); Log.d("娉涘瀷娴嬭瘯","item = " + item);}12345678姣棤鐤戦棶锛岀▼搴忕殑杩愯缁撴灉浼氫互宕╂簝缁撴潫锛?br />java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String1ArrayList鍙互瀛樻斁浠绘剰绫诲瀷锛屼緥瀛愪腑娣诲姞浜嗕竴涓猄tring绫诲瀷锛屾坊鍔犱簡涓€涓狪nteger绫诲瀷锛屽啀浣跨敤鏃堕兘浠tring鐨勬柟寮忎娇鐢紝鍥犳绋嬪簭宕╂簝浜嗐€備负浜嗚В鍐崇被浼艰繖鏍风殑闂锛堝湪缂栬瘧闃舵灏卞彲浠ヨВ鍐筹級锛屾硾鍨嬪簲杩愯€岀敓銆?br />鎴戜滑灏嗙涓€琛屽0鏄庡垵濮嬪寲list鐨勪唬鐮佹洿鏀逛竴涓嬶紝缂栬瘧鍣ㄤ細鍦ㄧ紪璇戦樁娈靛氨鑳藉甯垜浠彂鐜扮被浼艰繖鏍风殑闂銆?br />List<String> arrayList = new ArrayList<String>();...//arrayList.add(100); 鍦ㄧ紪璇戦樁娈碉紝缂栬瘧鍣ㄥ氨浼氭姤閿?233. 鐗规€ф硾鍨嬪彧鍦ㄧ紪璇戦樁娈垫湁鏁堛€傜湅涓嬮潰鐨勪唬鐮侊細
List<String> stringArrayList = new ArrayList<String>();List<Integer> integerArrayList = new ArrayList<Integer>();
Class classStringArrayList = stringArrayList.getClass();Class classIntegerArrayList = integerArrayList.getClass();
if(classStringArrayList.equals(classIntegerArrayList)){ Log.d("娉涘瀷娴嬭瘯","绫诲瀷鐩稿悓");}123456789杈撳嚭缁撴灉锛欴/娉涘瀷娴嬭瘯: 绫诲瀷鐩稿悓銆?br />閫氳繃涓婇潰鐨勪緥瀛愬彲浠ヨ瘉鏄庯紝鍦ㄧ紪璇戜箣鍚庣▼搴忎細閲囧彇鍘绘硾鍨嬪寲鐨勬帾鏂姐€備篃灏辨槸璇碕ava涓殑娉涘瀷锛屽彧鍦ㄧ紪璇戦樁娈垫湁鏁堛€傚湪缂栬瘧杩囩▼涓紝姝g‘妫€楠屾硾鍨嬬粨鏋滃悗锛屼細灏嗘硾鍨嬬殑鐩稿叧淇℃伅鎿﹀嚭锛屽苟涓斿湪瀵硅薄杩涘叆鍜岀寮€鏂规硶鐨勮竟鐣屽娣诲姞绫诲瀷妫€鏌ュ拰绫诲瀷杞崲鐨勬柟娉曘€備篃灏辨槸璇达紝娉涘瀷淇℃伅涓嶄細杩涘叆鍒拌繍琛屾椂闃舵銆?br />瀵规鎬荤粨鎴愪竴鍙ヨ瘽锛氭硾鍨嬬被鍨嬪湪閫昏緫涓婄湅浠ョ湅鎴愭槸澶氫釜涓嶅悓鐨勭被鍨嬶紝瀹為檯涓婇兘鏄浉鍚岀殑鍩烘湰绫诲瀷銆?br />4. 娉涘瀷鐨勪娇鐢ㄦ硾鍨嬫湁涓夌浣跨敤鏂瑰紡锛屽垎鍒负锛氭硾鍨嬬被銆佹硾鍨嬫帴鍙c€佹硾鍨嬫柟娉?br />4.3 娉涘瀷绫绘硾鍨嬬被鍨嬬敤浜庣被鐨勫畾涔変腑锛岃绉颁负娉涘瀷绫汇€傞€氳繃娉涘瀷鍙互瀹屾垚瀵逛竴缁勭被鐨勬搷浣滃澶栧紑鏀剧浉鍚岀殑鎺ュ彛銆傛渶鍏稿瀷鐨勫氨鏄悇绉嶅鍣ㄧ被锛屽锛歀ist銆丼et銆丮ap銆?br />娉涘瀷绫荤殑鏈€鍩烘湰鍐欐硶锛堣繖涔堢湅鍙兘浼氭湁鐐规檿锛屼細鍦ㄤ笅闈㈢殑渚嬪瓙涓瑙o級锛?br />class 绫诲悕绉?<娉涘瀷鏍囪瘑锛氬彲浠ラ殢渚垮啓浠绘剰鏍囪瘑鍙凤紝鏍囪瘑鎸囧畾鐨勬硾鍨嬬殑绫诲瀷>{ private 娉涘瀷鏍囪瘑 /*锛堟垚鍛樺彉閲忕被鍨嬶級*/ var; .....
}}123456涓€涓渶鏅€氱殑娉涘瀷绫伙細
//姝ゅT鍙互闅忎究鍐欎负浠绘剰鏍囪瘑锛屽父瑙佺殑濡俆銆丒銆並銆乂绛夊舰寮忕殑鍙傛暟甯哥敤浜庤〃绀烘硾鍨?/鍦ㄥ疄渚嬪寲娉涘瀷绫绘椂锛屽繀椤绘寚瀹歍鐨勫叿浣撶被鍨媝ublic class Generic<T>{ //key杩欎釜鎴愬憳鍙橀噺鐨勭被鍨嬩负T,T鐨勭被鍨嬬敱澶栭儴鎸囧畾 private T key;
public Generic(T key) { //娉涘瀷鏋勯€犳柟娉曞舰鍙俴ey鐨勭被鍨嬩篃涓篢锛孴鐨勭被鍨嬬敱澶栭儴鎸囧畾 this.key = key; }
public T getKey(){ //娉涘瀷鏂规硶getKey鐨勮繑鍥炲€肩被鍨嬩负T锛孴鐨勭被鍨嬬敱澶栭儴鎸囧畾 return key; }}1234567891011121314//娉涘瀷鐨勭被鍨嬪弬鏁板彧鑳芥槸绫荤被鍨嬶紙鍖呮嫭鑷畾涔夌被锛夛紝涓嶈兘鏄畝鍗曠被鍨?/浼犲叆鐨勫疄鍙傜被鍨嬮渶涓庢硾鍨嬬殑绫诲瀷鍙傛暟绫诲瀷鐩稿悓锛屽嵆涓篒nteger.Generic<Integer> genericInteger = new Generic<Integer>(123456);
//浼犲叆鐨勫疄鍙傜被鍨嬮渶涓庢硾鍨嬬殑绫诲瀷鍙傛暟绫诲瀷鐩稿悓锛屽嵆涓篠tring.Generic<String> genericString = new Generic<String>("key_vlaue");Log.d("娉涘瀷娴嬭瘯","key is " + genericInteger.getKey());Log.d("娉涘瀷娴嬭瘯","key is " + genericString.getKey());1234567812-27 09:20:04.432 13063-13063/? D/娉涘瀷娴嬭瘯: key is 12345612-27 09:20:04.432 13063-13063/? D/娉涘瀷娴嬭瘯: key is key_vlaue12瀹氫箟鐨勬硾鍨嬬被锛屽氨涓€瀹氳浼犲叆娉涘瀷绫诲瀷瀹炲弬涔堬紵骞朵笉鏄繖鏍凤紝鍦ㄤ娇鐢ㄦ硾鍨嬬殑鏃跺€欏鏋滀紶鍏ユ硾鍨嬪疄鍙傦紝鍒欎細鏍规嵁浼犲叆鐨勬硾鍨嬪疄鍙傚仛鐩稿簲鐨勯檺鍒讹紝姝ゆ椂娉涘瀷鎵嶄細璧峰埌鏈簲璧峰埌鐨勯檺鍒朵綔鐢ㄣ€傚鏋滀笉浼犲叆娉涘瀷绫诲瀷瀹炲弬鐨勮瘽锛屽湪娉涘瀷绫讳腑浣跨敤娉涘瀷鐨勬柟娉曟垨鎴愬憳鍙橀噺瀹氫箟鐨勭被鍨嬪彲浠ヤ负浠讳綍鐨勭被鍨嬨€?br />鐪嬩竴涓緥瀛愶細
Generic generic = new Generic("111111");Generic generic1 = new Generic(4444);Generic generic2 = new Generic(55.55);Generic generic3 = new Generic(false);
Log.d("娉涘瀷娴嬭瘯","key is " + generic.getKey());Log.d("娉涘瀷娴嬭瘯","key is " + generic1.getKey());Log.d("娉涘瀷娴嬭瘯","key is " + generic2.getKey());Log.d("娉涘瀷娴嬭瘯","key is " + generic3.getKey());123456789D/娉涘瀷娴嬭瘯: key is 111111D/娉涘瀷娴嬭瘯: key is 4444D/娉涘瀷娴嬭瘯: key is 55.55D/娉涘瀷娴嬭瘯: key is false1234娉ㄦ剰锛?br />娉涘瀷鐨勭被鍨嬪弬鏁板彧鑳芥槸绫荤被鍨嬶紝涓嶈兘鏄畝鍗曠被鍨嬨€備笉鑳藉纭垏鐨勬硾鍨嬬被鍨嬩娇鐢╥nstanceof鎿嶄綔銆傚涓嬮潰鐨勬搷浣滄槸闈炴硶鐨勶紝缂栬瘧鏃朵細鍑洪敊銆?br />if(ex_num instanceof Generic<Number>){ } 124.4 娉涘瀷鎺ュ彛娉涘瀷鎺ュ彛涓庢硾鍨嬬被鐨勫畾涔夊強浣跨敤鍩烘湰鐩稿悓銆傛硾鍨嬫帴鍙e父琚敤鍦ㄥ悇绉嶇被鐨勭敓浜у櫒涓紝鍙互鐪嬩竴涓緥瀛愶細
//瀹氫箟涓€涓硾鍨嬫帴鍙ublic interface Generator<T> { public T next();}1234褰撳疄鐜版硾鍨嬫帴鍙g殑绫伙紝鏈紶鍏ユ硾鍨嬪疄鍙傛椂锛?br />/** * 鏈紶鍏ユ硾鍨嬪疄鍙傛椂锛屼笌娉涘瀷绫荤殑瀹氫箟鐩稿悓锛屽湪澹版槑绫荤殑鏃跺€欙紝闇€灏嗘硾鍨嬬殑澹版槑涔熶竴璧峰姞鍒扮被涓?nbsp;* 鍗筹細class FruitGenerator<T> implements Generator<T>{ * 濡傛灉涓嶅0鏄庢硾鍨嬶紝濡傦細class FruitGenerator implements Generator<T>锛岀紪璇戝櫒浼氭姤閿欙細"Unknown class" */class FruitGenerator<T> implements Generator<T>{ @Override public T next() { return null; }}1234567891011褰撳疄鐜版硾鍨嬫帴鍙g殑绫伙紝浼犲叆娉涘瀷瀹炲弬鏃讹細
/** * 浼犲叆娉涘瀷瀹炲弬鏃讹細 * 瀹氫箟涓€涓敓浜у櫒瀹炵幇杩欎釜鎺ュ彛,铏界劧鎴戜滑鍙垱寤轰簡涓€涓硾鍨嬫帴鍙enerator<T> * 浣嗘槸鎴戜滑鍙互涓篢浼犲叆鏃犳暟涓疄鍙傦紝褰㈡垚鏃犳暟绉嶇被鍨嬬殑Generator鎺ュ彛銆?nbsp;* 鍦ㄥ疄鐜扮被瀹炵幇娉涘瀷鎺ュ彛鏃讹紝濡傚凡灏嗘硾鍨嬬被鍨嬩紶鍏ュ疄鍙傜被鍨嬶紝鍒欐墍鏈変娇鐢ㄦ硾鍨嬬殑鍦版柟閮借鏇挎崲鎴愪紶鍏ョ殑瀹炲弬绫诲瀷 * 鍗筹細Generator<T>锛宲ublic T next();涓殑鐨凾閮借鏇挎崲鎴愪紶鍏ョ殑String绫诲瀷銆?nbsp;*/public class FruitGenerator implements Generator<String> {
private String[] fruits = new String[]{"Apple", "Banana", "Pear"};
@Override public String next() { Random rand = new Random(); return fruits[rand.nextInt(3)]; }}12345678910111213141516174.5 娉涘瀷閫氶厤绗︽垜浠煡閬揑ngeter鏄疦umber鐨勪竴涓瓙绫伙紝鍚屾椂鍦ㄧ壒鎬х珷鑺備腑鎴戜滑涔熼獙璇佽繃Generic<Ingeter>涓嶨eneric<Number>瀹為檯涓婃槸鐩稿悓鐨勪竴绉嶅熀鏈被鍨嬨€傞偅涔堥棶棰樻潵浜嗭紝鍦ㄤ娇鐢℅eneric<Number>浣滀负褰㈠弬鐨勬柟娉曚腑锛岃兘鍚︿娇鐢℅eneric<Ingeter>鐨勫疄渚嬩紶鍏ュ憿锛熷湪閫昏緫涓婄被浼间簬Generic<Number>鍜孏eneric<Ingeter>鏄惁鍙互鐪嬫垚鍏锋湁鐖跺瓙鍏崇郴鐨勬硾鍨嬬被鍨嬪憿锛?br />涓轰簡寮勬竻妤氳繖涓棶棰橈紝鎴戜滑浣跨敤Generic<T>杩欎釜娉涘瀷绫荤户缁湅涓嬮潰鐨勪緥瀛愶細
public void showKeyValue1(Generic<Number> obj){ Log.d("娉涘瀷娴嬭瘯","key value is " + obj.getKey());}123Generic<Integer> gInteger = new Generic<Integer>(123);Generic<Number> gNumber = new Generic<Number>(456);
showKeyValue(gNumber);
// showKeyValue杩欎釜鏂规硶缂栬瘧鍣ㄤ細涓烘垜浠姤閿欙細Generic<java.lang.Integer> // cannot be applied to Generic<java.lang.Number>// showKeyValue(gInteger);12345678閫氳繃鎻愮ず淇℃伅鎴戜滑鍙互鐪嬪埌Generic<Integer>涓嶈兘琚湅浣滀负`Generic<Number>鐨勫瓙绫汇€傜敱姝ゅ彲浠ョ湅鍑?鍚屼竴绉嶆硾鍨嬪彲浠ュ搴斿涓増鏈紙鍥犱负鍙傛暟绫诲瀷鏄笉纭畾鐨勶級锛屼笉鍚岀増鏈殑娉涘瀷绫诲疄渚嬫槸涓嶅吋瀹圭殑銆?br />鍥炲埌涓婇潰鐨勪緥瀛愶紝濡備綍瑙e喅涓婇潰鐨勯棶棰橈紵鎬讳笉鑳戒负浜嗗畾涔変竴涓柊鐨勬柟娉曟潵澶勭悊Generic<Integer>绫诲瀷鐨勭被锛岃繖鏄剧劧涓巎ava涓殑澶氬彴鐞嗗康鐩歌繚鑳屻€傚洜姝ゆ垜浠渶瑕佷竴涓湪閫昏緫涓婂彲浠ヨ〃绀哄悓鏃舵槸Generic<Integer>鍜孏eneric<Number>鐖剁被鐨勫紩鐢ㄧ被鍨嬨€傜敱姝ょ被鍨嬮€氶厤绗﹀簲杩愯€岀敓銆?br />鎴戜滑鍙互灏嗕笂闈㈢殑鏂规硶鏀逛竴涓嬶細
public void showKeyValue1(Generic<?> obj){ Log.d("娉涘瀷娴嬭瘯","key value is " + obj.getKey());}123绫诲瀷閫氶厤绗︿竴鑸槸浣跨敤锛熶唬鏇垮叿浣撶殑绫诲瀷瀹炲弬锛屾敞鎰忎簡锛屾澶?rsquo;锛?rsquo;鏄被鍨嬪疄鍙傦紝鑰屼笉鏄被鍨嬪舰鍙?銆傞噸瑕佽涓夐亶锛佹澶?rsquo;锛?rsquo;鏄被鍨嬪疄鍙傦紝鑰屼笉鏄被鍨嬪舰鍙?锛?姝ゅ’锛?rsquo;鏄被鍨嬪疄鍙傦紝鑰屼笉鏄被鍨嬪舰鍙?锛佸啀鐩寸櫧鐐圭殑鎰忔€濆氨鏄紝姝ゅ鐨勶紵鍜孨umber銆丼tring銆両nteger涓€鏍烽兘鏄竴绉嶅疄闄呯殑绫诲瀷锛屽彲浠ユ妸锛熺湅鎴愭墍鏈夌被鍨嬬殑鐖剁被銆傛槸涓€绉嶇湡瀹炵殑绫诲瀷銆?br />鍙互瑙e喅褰撳叿浣撶被鍨嬩笉纭畾鐨勬椂鍊欙紝杩欎釜閫氶厤绗﹀氨鏄?? 锛涘綋鎿嶄綔绫诲瀷鏃讹紝涓嶉渶瑕佷娇鐢ㄧ被鍨嬬殑鍏蜂綋鍔熻兘鏃讹紝鍙娇鐢∣bject绫讳腑鐨勫姛鑳姐€傞偅涔堝彲浠ョ敤 ? 閫氶厤绗︽潵琛ㄦ湭鐭ョ被鍨嬨€?br />4.6 娉涘瀷鏂规硶鍦╦ava涓?娉涘瀷绫荤殑瀹氫箟闈炲父绠€鍗曪紝浣嗘槸娉涘瀷鏂规硶灏辨瘮杈冨鏉備簡銆?br />灏ゅ叾鏄垜浠鍒扮殑澶у鏁版硾鍨嬬被涓殑鎴愬憳鏂规硶涔熼兘浣跨敤浜嗘硾鍨嬶紝鏈夌殑鐢氳嚦娉涘瀷绫讳腑涔熷寘鍚潃娉涘瀷鏂规硶锛岃繖鏍峰湪鍒濆鑰呬腑闈炲父瀹规槗灏嗘硾鍨嬫柟娉曠悊瑙i敊浜嗐€?br />娉涘瀷绫伙紝鏄湪瀹炰緥鍖栫被鐨勬椂鍊欐寚鏄庢硾鍨嬬殑鍏蜂綋绫诲瀷锛涙硾鍨嬫柟娉曪紝鏄湪璋冪敤鏂规硶鐨勬椂鍊欐寚鏄庢硾鍨嬬殑鍏蜂綋绫诲瀷 銆?br />/** * 娉涘瀷鏂规硶鐨勫熀鏈粙缁?nbsp;* @param tClass 浼犲叆鐨勬硾鍨嬪疄鍙?nbsp;* @return T 杩斿洖鍊间负T绫诲瀷 * 璇存槑锛?nbsp;* 1锛塸ublic 涓?杩斿洖鍊间腑闂?lt;T>闈炲父閲嶈锛屽彲浠ョ悊瑙d负澹版槑姝ゆ柟娉曚负娉涘瀷鏂规硶銆?nbsp;* 2锛夊彧鏈夊0鏄庝簡<T>鐨勬柟娉曟墠鏄硾鍨嬫柟娉曪紝娉涘瀷绫讳腑鐨勪娇鐢ㄤ簡娉涘瀷鐨勬垚鍛樻柟娉曞苟涓嶆槸娉涘瀷鏂规硶銆?nbsp;* 3锛?lt;T>琛ㄦ槑璇ユ柟娉曞皢浣跨敤娉涘瀷绫诲瀷T锛屾鏃舵墠鍙互鍦ㄦ柟娉曚腑浣跨敤娉涘瀷绫诲瀷T銆?nbsp;* 4锛変笌娉涘瀷绫荤殑瀹氫箟涓€鏍凤紝姝ゅT鍙互闅忎究鍐欎负浠绘剰鏍囪瘑锛屽父瑙佺殑濡俆銆丒銆並銆乂绛夊舰寮忕殑鍙傛暟甯哥敤浜庤〃绀烘硾鍨嬨€?nbsp;*/public <T> T genericMethod(Class<T> tClass)throws InstantiationException , IllegalAccessException{ T instance = tClass.newInstance(); return instance;}123456789101112131415Object obj = genericMethod(Class.forName("com.test.test"));14.6.1 娉涘瀷鏂规硶鐨勫熀鏈敤娉曞厜鐪嬩笂闈㈢殑渚嬪瓙鏈夌殑鍚屽鍙兘渚濈劧浼氶潪甯歌糠绯婏紝鎴戜滑鍐嶉€氳繃涓€涓緥瀛愶紝鎶婃垜娉涘瀷鏂规硶鍐嶆€荤粨涓€涓嬨€?br />public class GenericTest { //杩欎釜绫绘槸涓硾鍨嬬被锛屽湪涓婇潰宸茬粡浠嬬粛杩?nbsp; public class Generic<T>{ private T key;
public Generic(T key) { this.key = key; }
//鎴戞兂璇寸殑鍏跺疄鏄繖涓紝铏界劧鍦ㄦ柟娉曚腑浣跨敤浜嗘硾鍨嬶紝浣嗘槸杩欏苟涓嶆槸涓€涓硾鍨嬫柟娉曘€?nbsp; //杩欏彧鏄被涓竴涓櫘閫氱殑鎴愬憳鏂规硶锛屽彧涓嶈繃浠栫殑杩斿洖鍊兼槸鍦ㄥ0鏄庢硾鍨嬬被宸茬粡澹版槑杩囩殑娉涘瀷銆?nbsp; //鎵€浠ュ湪杩欎釜鏂规硶涓墠鍙互缁х画浣跨敤 T 杩欎釜娉涘瀷銆?nbsp; public T getKey(){ return key; }
/** * 杩欎釜鏂规硶鏄剧劧鏄湁闂鐨勶紝鍦ㄧ紪璇戝櫒浼氱粰鎴戜滑鎻愮ず杩欐牱鐨勯敊璇俊鎭?cannot reslove symbol E" * 鍥犱负鍦ㄧ被鐨勫0鏄庝腑骞舵湭澹版槑娉涘瀷E锛屾墍浠ュ湪浣跨敤E鍋氬舰鍙傚拰杩斿洖鍊肩被鍨嬫椂锛岀紪璇戝櫒浼氭棤娉曡瘑鍒€?nbsp; public E setKey(E key){ this.key = keu } */ }
/** * 杩欐墠鏄竴涓湡姝g殑娉涘瀷鏂规硶銆?nbsp; * 棣栧厛鍦╬ublic涓庤繑鍥炲€间箣闂寸殑<T>蹇呬笉鍙皯锛岃繖琛ㄦ槑杩欐槸涓€涓硾鍨嬫柟娉曪紝骞朵笖澹版槑浜嗕竴涓硾鍨婽 * 杩欎釜T鍙互鍑虹幇鍦ㄨ繖涓硾鍨嬫柟娉曠殑浠绘剰浣嶇疆. * 娉涘瀷鐨勬暟閲忎篃鍙互涓轰换鎰忓涓?nbsp; * 濡傦細public <T,K> K showKeyName(Generic<T> container){ * ... * } */ public <T> T showKeyName(Generic<T> container){ System.out.println("container key :" + container.getKey()); //褰撶劧杩欎釜渚嬪瓙涓剧殑涓嶅お鍚堥€傦紝鍙槸涓轰簡璇存槑娉涘瀷鏂规硶鐨勭壒鎬с€?nbsp; T test = container.getKey(); return test; }
//杩欎篃涓嶆槸涓€涓硾鍨嬫柟娉曪紝杩欏氨鏄竴涓櫘閫氱殑鏂规硶锛屽彧鏄娇鐢ㄤ簡Generic<Number>杩欎釜娉涘瀷绫诲仛褰㈠弬鑰屽凡銆?nbsp; public void showKeyValue1(Generic<Number> obj){ Log.d("娉涘瀷娴嬭瘯","key value is " + obj.getKey()); }
//杩欎篃涓嶆槸涓€涓硾鍨嬫柟娉曪紝杩欎篃鏄竴涓櫘閫氱殑鏂规硶锛屽彧涓嶈繃浣跨敤浜嗘硾鍨嬮€氶厤绗? //鍚屾椂杩欎篃鍗拌瘉浜嗘硾鍨嬮€氶厤绗︾珷鑺傛墍鎻忚堪鐨勶紝?鏄竴绉嶇被鍨嬪疄鍙傦紝鍙互鐪嬪仛涓篘umber绛夋墍鏈夌被鐨勭埗绫?nbsp; public void showKeyValue2(Generic<?> obj){ Log.d("娉涘瀷娴嬭瘯","key value is " + obj.getKey()); }
/** * 杩欎釜鏂规硶鏄湁闂鐨勶紝缂栬瘧鍣ㄤ細涓烘垜浠彁绀洪敊璇俊鎭細"UnKnown class 鈥楨鈥?" * 铏界劧鎴戜滑澹版槑浜?lt;T>,涔熻〃鏄庝簡杩欐槸涓€涓彲浠ュ鐞嗘硾鍨嬬殑绫诲瀷鐨勬硾鍨嬫柟娉曘€?nbsp; * 浣嗘槸鍙0鏄庝簡娉涘瀷绫诲瀷T锛屽苟鏈0鏄庢硾鍨嬬被鍨婨锛屽洜姝ょ紪璇戝櫒骞朵笉鐭ラ亾璇ュ浣曞鐞咵杩欎釜绫诲瀷銆?nbsp; public <T> T showKeyName(Generic<E> container){ ... } */
/** * 杩欎釜鏂规硶涔熸槸鏈夐棶棰樼殑锛岀紪璇戝櫒浼氫负鎴戜滑鎻愮ず閿欒淇℃伅锛?UnKnown class 鈥楾鈥?" * 瀵逛簬缂栬瘧鍣ㄦ潵璇碩杩欎釜绫诲瀷骞舵湭椤圭洰涓0鏄庤繃锛屽洜姝ょ紪璇戜篃涓嶇煡閬撹濡備綍缂栬瘧杩欎釜绫汇€?nbsp; * 鎵€浠ヨ繖涔熶笉鏄竴涓纭殑娉涘瀷鏂规硶澹版槑銆?nbsp; public void showkey(T genericObj){
} */
public static void main(String[] args) {
}}1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374754.6.2 绫讳腑鐨勬硾鍨嬫柟娉曞綋鐒惰繖骞朵笉鏄硾鍨嬫柟娉曠殑鍏ㄩ儴锛屾硾鍨嬫柟娉曞彲浠ュ嚭鐜版潅浠讳綍鍦版柟鍜屼换浣曞満鏅腑浣跨敤銆備絾鏄湁涓€绉嶆儏鍐垫槸闈炲父鐗规畩鐨勶紝褰撴硾鍨嬫柟娉曞嚭鐜板湪娉涘瀷绫讳腑鏃讹紝鎴戜滑鍐嶉€氳繃涓€涓緥瀛愮湅涓€涓?br />public class GenericFruit { class Fruit{ @Override public String toString() { return "fruit"; } }
class Apple extends Fruit{ @Override public String toString() { return "apple"; } }
class Person{ @Override public String toString() { return "Person"; } }
class GenerateTest<T>{ public void show_1(T t){ System.out.println(t.toString()); }
//鍦ㄦ硾鍨嬬被涓0鏄庝簡涓€涓硾鍨嬫柟娉曪紝浣跨敤娉涘瀷E锛岃繖绉嶆硾鍨婨鍙互涓轰换鎰忕被鍨嬨€傚彲浠ョ被鍨嬩笌T鐩稿悓锛屼篃鍙互涓嶅悓銆?nbsp; //鐢变簬娉涘瀷鏂规硶鍦ㄥ0鏄庣殑鏃跺€欎細澹版槑娉涘瀷<E>锛屽洜姝ゅ嵆浣垮湪娉涘瀷绫讳腑骞舵湭澹版槑娉涘瀷锛岀紪璇戝櫒涔熻兘澶熸纭瘑鍒硾鍨嬫柟娉曚腑璇嗗埆鐨勬硾鍨嬨€?nbsp; public <E> void show_3(E t){ System.out.println(t.toString()); }
//鍦ㄦ硾鍨嬬被涓0鏄庝簡涓€涓硾鍨嬫柟娉曪紝浣跨敤娉涘瀷T锛屾敞鎰忚繖涓猅鏄竴绉嶅叏鏂扮殑绫诲瀷锛屽彲浠ヤ笌娉涘瀷绫讳腑澹版槑鐨凾涓嶆槸鍚屼竴绉嶇被鍨嬨€?nbsp; public <T> void show_2(T t){ System.out.println(t.toString()); } }
public static void main(String[] args) { Apple apple = new Apple(); Person person = new Person();
GenerateTest<Fruit> generateTest = new GenerateTest<Fruit>(); //apple鏄疐ruit鐨勫瓙绫伙紝鎵€浠ヨ繖閲屽彲浠?nbsp; generateTest.show_1(apple); //缂栬瘧鍣ㄤ細鎶ラ敊锛屽洜涓烘硾鍨嬬被鍨嬪疄鍙傛寚瀹氱殑鏄疐ruit锛岃€屼紶鍏ョ殑瀹炲弬绫绘槸Person //generateTest.show_1(person);
//浣跨敤杩欎袱涓柟娉曢兘鍙互鎴愬姛 generateTest.show_2(apple); generateTest.show_2(person);
//浣跨敤杩欎袱涓柟娉曚篃閮藉彲浠ユ垚鍔?nbsp; generateTest.show_3(apple); generateTest.show_3(person); }}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657584.6.3 娉涘瀷鏂规硶涓庡彲鍙樺弬鏁板啀鐪嬩竴涓硾鍨嬫柟娉曞拰鍙彉鍙傛暟鐨勪緥瀛愶細
public <T> void printMsg( T... args){ for(T t : args){ Log.d("娉涘瀷娴嬭瘯","t is " + t); }}12345printMsg("111",222,"aaaa","2323.4",55.55);14.6.4 闈欐€佹柟娉曚笌娉涘瀷闈欐€佹柟娉曟湁涓€绉嶆儏鍐甸渶瑕佹敞鎰忎竴涓嬶紝閭e氨鏄湪绫讳腑鐨勯潤鎬佹柟娉曚娇鐢ㄦ硾鍨嬶細闈欐€佹柟娉曟棤娉曡闂被涓婂畾涔夌殑娉涘瀷锛涘鏋滈潤鎬佹柟娉曟搷浣滅殑寮曠敤鏁版嵁绫诲瀷涓嶇‘瀹氱殑鏃跺€欙紝蹇呴』瑕佸皢娉涘瀷瀹氫箟鍦ㄦ柟娉曚笂銆?br />鍗筹細濡傛灉闈欐€佹柟娉曡浣跨敤娉涘瀷鐨勮瘽锛屽繀椤诲皢闈欐€佹柟娉曚篃瀹氫箟鎴愭硾鍨嬫柟娉?銆?br />public class StaticGenerator<T> { .... .... /** * 濡傛灉鍦ㄧ被涓畾涔変娇鐢ㄦ硾鍨嬬殑闈欐€佹柟娉曪紝闇€瑕佹坊鍔犻澶栫殑娉涘瀷澹版槑锛堝皢杩欎釜鏂规硶瀹氫箟鎴愭硾鍨嬫柟娉曪級 * 鍗充娇闈欐€佹柟娉曡浣跨敤娉涘瀷绫讳腑宸茬粡澹版槑杩囩殑娉涘瀷涔熶笉鍙互銆?nbsp; * 濡傦細public static void show(T t){..},姝ゆ椂缂栬瘧鍣ㄤ細鎻愮ず閿欒淇℃伅锛?nbsp; "StaticGenerator cannot be refrenced from static context" */ public static <T> void show(T t){
}}123456789101112134.6.5 娉涘瀷鏂规硶鎬荤粨娉涘瀷鏂规硶鑳戒娇鏂规硶鐙珛浜庣被鑰屼骇鐢熷彉鍖栵紝浠ヤ笅鏄竴涓熀鏈殑鎸囧鍘熷垯锛?br />鏃犺浣曟椂锛屽鏋滀綘鑳藉仛鍒帮紝浣犲氨璇ュ敖閲忎娇鐢ㄦ硾鍨嬫柟娉曘€備篃灏辨槸璇达紝濡傛灉浣跨敤娉涘瀷鏂规硶灏嗘暣涓被娉涘瀷鍖栵紝閭d箞灏卞簲璇ヤ娇鐢ㄦ硾鍨嬫柟娉曘€傚彟澶栧浜庝竴涓猻tatic鐨勬柟娉曡€屽凡锛屾棤娉曡闂硾鍨嬬被鍨嬬殑鍙傛暟銆傛墍浠ュ鏋渟tatic鏂规硶瑕佷娇鐢ㄦ硾鍨嬭兘鍔涳紝灏卞繀椤讳娇鍏舵垚涓烘硾鍨嬫柟娉曘€?br />4.6 娉涘瀷涓婁笅杈圭晫鍦ㄤ娇鐢ㄦ硾鍨嬬殑鏃跺€欙紝鎴戜滑杩樺彲浠ヤ负浼犲叆鐨勬硾鍨嬬被鍨嬪疄鍙傝繘琛屼笂涓嬭竟鐣岀殑闄愬埗锛屽锛氱被鍨嬪疄鍙傚彧鍑嗕紶鍏ユ煇绉嶇被鍨嬬殑鐖剁被鎴栨煇绉嶇被鍨嬬殑瀛愮被銆?br />涓烘硾鍨嬫坊鍔犱笂杈圭晫锛屽嵆浼犲叆鐨勭被鍨嬪疄鍙傚繀椤绘槸鎸囧畾绫诲瀷鐨勫瓙绫诲瀷
public void showKeyValue1(Generic<? extends Number> obj){ Log.d("娉涘瀷娴嬭瘯","key value is " + obj.getKey());}123Generic<String> generic1 = new Generic<String>("11111");Generic<Integer> generic2 = new Generic<Integer>(2222);Generic<Float> generic3 = new Generic<Float>(2.4f);Generic<Double> generic4 = new Generic<Double>(2.56);
//杩欎竴琛屼唬鐮佺紪璇戝櫒浼氭彁绀洪敊璇紝鍥犱负String绫诲瀷骞朵笉鏄疦umber绫诲瀷鐨勫瓙绫?/showKeyValue1(generic1);
showKeyValue1(generic2);showKeyValue1(generic3);showKeyValue1(generic4);1234567891011濡傛灉鎴戜滑鎶婃硾鍨嬬被鐨勫畾涔変篃鏀逛竴涓?
public class Generic<T extends Number>{ private T key;
public Generic(T key) { this.key = key; }
public T getKey(){ return key; }}1234567891011//杩欎竴琛屼唬鐮佷篃浼氭姤閿欙紝鍥犱负String涓嶆槸Number鐨勫瓙绫籊eneric<String> generic1 = new Generic<String>("11111");12鍐嶆潵涓€涓硾鍨嬫柟娉曠殑渚嬪瓙锛?br />//鍦ㄦ硾鍨嬫柟娉曚腑娣诲姞涓婁笅杈圭晫闄愬埗鐨勬椂鍊欙紝蹇呴』鍦ㄦ潈闄愬0鏄庝笌杩斿洖鍊间箣闂寸殑<T>涓婃坊鍔犱笂涓嬭竟鐣岋紝鍗冲湪娉涘瀷澹版槑鐨勬椂鍊欐坊鍔?/public <T> T showKeyName(Generic<T extends Number> container)锛岀紪璇戝櫒浼氭姤閿欙細"Unexpected bound"public <T extends Number> T showKeyName(Generic<T> container){ System.out.println("container key :" + container.getKey()); T test = container.getKey(); return test;}1234567閫氳繃涓婇潰鐨勪袱涓緥瀛愬彲浠ョ湅鍑猴細娉涘瀷鐨勪笂涓嬭竟鐣屾坊鍔狅紝蹇呴』涓庢硾鍨嬬殑澹版槑鍦ㄤ竴璧?銆?br />4.7 鍏充簬娉涘瀷鏁扮粍瑕佹彁涓€涓嬬湅鍒颁簡寰堝鏂囩珷涓兘浼氭彁璧锋硾鍨嬫暟缁勶紝缁忚繃鏌ョ湅sun鐨勮鏄庢枃妗o紝鍦╦ava涓槸”涓嶈兘鍒涘缓涓€涓‘鍒囩殑娉涘瀷绫诲瀷鐨勬暟缁?rdquo;鐨勩€?br />涔熷氨鏄涓嬮潰鐨勮繖涓緥瀛愭槸涓嶅彲浠ョ殑锛?br />List<String>[] ls = new ArrayList<String>[10]; 1鑰屼娇鐢ㄩ€氶厤绗﹀垱寤烘硾鍨嬫暟缁勬槸鍙互鐨勶紝濡備笅闈㈣繖涓緥瀛愶細
List<?>[] ls = new ArrayList<?>[10]; 1杩欐牱涔熸槸鍙互鐨勶細
List<String>[] ls = new ArrayList[10];1涓嬮潰浣跨敤Sun鐨勪竴绡囨枃妗g殑涓€涓緥瀛愭潵璇存槑杩欎釜闂锛?br />List<String>[] lsa = new List<String>[10]; // Not really allowed. Object o = lsa; Object[] oa = (Object[]) o; List<Integer> li = new ArrayList<Integer>(); li.add(new Integer(3)); oa[1] = li; // Unsound, but passes run time store check String s = lsa[1].get(0); // Run-time error: ClassCastException.1234567杩欑鎯呭喌涓嬶紝鐢变簬JVM娉涘瀷鐨勬摝闄ゆ満鍒讹紝鍦ㄨ繍琛屾椂JVM鏄笉鐭ラ亾娉涘瀷淇℃伅鐨勶紝鎵€浠ュ彲浠ョ粰oa[1]璧嬩笂涓€涓狝rrayList鑰屼笉浼氬嚭鐜板紓甯革紝浣嗘槸鍦ㄥ彇鍑烘暟鎹殑鏃跺€欏嵈瑕佸仛涓€娆$被鍨嬭浆鎹紝鎵€浠ュ氨浼氬嚭鐜癈lassCastException锛屽鏋滃彲浠ヨ繘琛屾硾鍨嬫暟缁勭殑澹版槑锛屼笂闈㈣鐨勮繖绉嶆儏鍐靛湪缂栬瘧鏈熷皢涓嶄細鍑虹幇浠讳綍鐨勮鍛婂拰閿欒锛屽彧鏈夊湪杩愯鏃舵墠浼氬嚭閿欍€?br />鑰屽娉涘瀷鏁扮粍鐨勫0鏄庤繘琛岄檺鍒讹紝瀵逛簬杩欐牱鐨勬儏鍐碉紝鍙互鍦ㄧ紪璇戞湡鎻愮ず浠g爜鏈夌被鍨嬪畨鍏ㄩ棶棰橈紝姣旀病鏈変换浣曟彁绀鸿寮哄緢澶氥€?br />涓嬮潰閲囩敤閫氶厤绗︾殑鏂瑰紡鏄鍏佽鐨?鏁扮粍鐨勭被鍨嬩笉鍙互鏄被鍨嬪彉閲忥紝闄ら潪鏄噰鐢ㄩ€氶厤绗︾殑鏂瑰紡锛屽洜涓哄浜庨€氶厤绗︾殑鏂瑰紡锛屾渶鍚庡彇鍑烘暟鎹槸瑕佸仛鏄惧紡鐨勭被鍨嬭浆鎹㈢殑銆?br />List<?>[] lsa = new List<?>[10]; // OK, array of unbounded wildcard type. Object o = lsa; Object[] oa = (Object[]) o; List<Integer> li = new ArrayList<Integer>(); li.add(new Integer(3)); oa[1] = li; // Correct. Integer i = (Integer) lsa[1].get(0); // OK 12345675. 鏈€鍚庢湰鏂囦腑鐨勪緥瀛愪富瑕佹槸涓轰簡闃愯堪娉涘瀷涓殑涓€浜涙€濇兂鑰岀畝鍗曚妇鍑虹殑锛屽苟涓嶄竴瀹氭湁鐫€瀹為檯鐨勫彲鐢ㄦ€с€傚彟澶栵紝涓€鎻愬埌娉涘瀷锛岀浉淇″ぇ瀹剁敤鍒版渶澶氱殑灏辨槸鍦ㄩ泦鍚堜腑锛屽叾瀹烇紝鍦ㄥ疄闄呯殑缂栫▼杩囩▼涓紝鑷繁鍙互浣跨敤娉涘瀷鍘荤畝鍖栧紑鍙戯紝涓旇兘寰堝ソ鐨勪繚璇佷唬鐮佽川閲忋€?mdash;———————————————鐗堟潈澹版槑锛氭湰鏂囦负CSDN鍗氫富銆孷ieLei銆嶇殑鍘熷垱鏂囩珷锛岄伒寰?CC 4.0 BY-SA 鐗堟潈鍗忚锛岃浆杞借闄勪笂鍘熸枃鍑哄閾炬帴鍙婃湰澹版槑銆傚師鏂囬摼鎺ワ細https://blog.csdn.net/s10461/article/details/53941091
以上是关于java 娉涘瀷璇﹁В的主要内容,如果未能解决你的问题,请参考以下文章
Android MVP + 娉涘瀷锛屽疄鐜颁簡鍙嬪ソVP浜や簰鍙夾ctivity娼滃湪鐨勫唴瀛樻硠闇茬殑浼樺寲