Java鎬荤粨
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java鎬荤粨相关的知识,希望对你有一定的参考价值。
鏍囩锛?a href='http://www.mamicode.com/so/1/%e8%bf%90%e7%ae%97' title='杩愮畻'>杩愮畻
search else sign 5.0 闅愬紡 pipeJava鍩虹鐭ヨ瘑鎬荤粨銆€銆€
闆跺熀纭€2700琛屽叆闂↗ava
涓€锛歫ava姒傝堪锛?/h2>
1991 骞碨un鍏徃鐨凧ames Gosling绛変汉寮€濮嬪紑鍙戝悕绉颁负 Oak 鐨勮瑷€锛屽笇鏈涚敤浜庢帶鍒跺祵鍏ュ湪鏈夌嚎鐢佃浜ゆ崲鐩掋€丳DA绛夌殑寰鐞嗗櫒锛?br>1994骞村皢Oak璇█鏇村悕涓篔ava锛?br>
Java鐨勪笁绉嶆妧鏈灦鏋?
JAVAEE锛欽ava Platform Enterprise Edition锛屽紑鍙戜紒涓氱幆澧冧笅鐨勫簲鐢ㄧ▼搴忥紝涓昏閽堝web绋嬪簭寮€鍙戯紱
JAVASE锛欽ava Platform Standard Edition锛屽畬鎴愭闈㈠簲鐢ㄧ▼搴忕殑寮€鍙戯紝鏄叾瀹冧袱鑰呯殑鍩虹锛?br>JAVAME锛欽ava Platform Micro Edition锛屽紑鍙戠數瀛愭秷璐逛骇鍝佸拰宓屽叆寮忚澶囷紝濡傛墜鏈轰腑鐨勭▼搴忥紱
1锛孞DK锛欽ava Development Kit锛宩ava鐨勫紑鍙戝拰杩愯鐜锛宩ava鐨勫紑鍙戝伐鍏峰拰jre銆?br>2锛孞RE锛欽ava Runtime Environment锛宩ava绋嬪簭鐨勮繍琛岀幆澧冿紝java杩愯鐨勬墍闇€鐨勭被搴?JVM(java铏氭嫙鏈?銆?br>3锛岄厤缃幆澧冨彉閲忥細璁﹋ava jdkin鐩綍涓嬬殑宸ュ叿锛屽彲浠ュ湪浠绘剰鐩綍涓嬭繍琛岋紝鍘熷洜鏄紝灏嗚宸ュ叿鎵€鍦ㄧ洰褰曞憡璇変簡绯荤粺锛屽綋浣跨敤璇ュ伐鍏锋椂锛岀敱绯荤粺甯垜浠幓鎵炬寚瀹氱殑鐩綍銆?鐜鍙橀噺鐨勯厤缃細
1锛夛細姘镐箙閰嶇疆鏂瑰紡锛欽AVA_HOME=%瀹夎璺緞%Javajdk
path=%JAVA_HOME%in
2锛夛細涓存椂閰嶇疆鏂瑰紡锛歴et path=%path%;C:Program FilesJavajdkin
鐗圭偣锛氱郴缁熼粯璁ゅ厛鍘诲綋鍓嶈矾寰勪笅鎵捐鎵ц鐨勭▼搴忥紝濡傛灉娌℃湁锛屽啀鍘籶ath涓缃殑璺緞涓嬫壘銆?br>classpath鐨勯厤缃?
1锛夛細姘镐箙閰嶇疆鏂瑰紡锛歝lasspath=.;c:;e:
2锛夛細涓存椂閰嶇疆鏂瑰紡锛歴et classpath=.;c:;e:
娉ㄦ剰锛氬湪瀹氫箟classpath鐜鍙橀噺鏃讹紝闇€瑕佹敞鎰忕殑鎯呭喌
濡傛灉娌℃湁瀹氫箟鐜鍙橀噺classpath锛宩ava鍚姩jvm鍚庯紝浼氬湪褰撳墠鐩綍涓嬫煡鎵捐杩愯鐨勭被鏂囦欢锛?br>濡傛灉鎸囧畾浜哻lasspath锛岄偅涔堜細鍦ㄦ寚瀹氱殑鐩綍涓嬫煡鎵捐杩愯鐨勭被鏂囦欢銆?br>杩樹細鍦ㄥ綋鍓嶇洰褰曟壘鍚楋紵涓ょ鎯呭喌锛?br> 1锛夛細濡傛灉classpath鐨勫€肩粨灏惧鏈夊垎鍙凤紝鍦ㄥ叿浣撹矾寰勪腑娌℃湁鎵惧埌杩愯鐨勭被锛屼細榛樿鍦ㄥ綋鍓嶇洰褰曞啀鎵句竴娆°€?br> 2锛夛細濡傛灉classpath鐨勫€肩粨鏋滃嚭娌℃湁鍒嗗彿锛屽湪鍏蜂綋鐨勮矾寰勪腑娌℃湁鎵惧埌杩愯鐨勭被锛屼笉浼氬啀褰撳墠鐩綍鎵俱€?br> 涓€鑸笉鎸囧畾鍒嗗彿锛屽鏋滄病鏈夊湪鎸囧畾鐩綍涓嬫壘鍒拌杩愯鐨勭被鏂囦欢锛屽氨鎶ラ敊锛岃繖鏍峰彲浠ヨ皟璇曠▼搴忋€?br>
4锛宩avac鍛戒护鍜宩ava鍛戒护鍋氫粈涔堜簨鎯呭憿锛?/h3>
瑕佺煡閬搄ava鏄垎涓ら儴鍒嗙殑锛氫竴涓槸缂栬瘧锛屼竴涓槸杩愯銆?br> javac锛氳礋璐g殑鏄紪璇戠殑閮ㄥ垎锛屽綋鎵цjavac鏃讹紝浼氬惎鍔╦ava鐨勭紪璇戝櫒绋嬪簭銆傚鎸囧畾鎵╁睍鍚嶇殑.java鏂囦欢杩涜缂栬瘧銆?鐢熸垚浜唈vm鍙互璇嗗埆鐨勫瓧鑺傜爜鏂囦欢銆備篃灏辨槸class鏂囦欢锛屼篃灏辨槸java鐨勮繍琛岀▼搴忋€?br> java锛氳礋璐h繍琛岀殑閮ㄥ垎.浼氬惎鍔╦vm.鍔犺浇杩愯鏃舵墍闇€鐨勭被搴?骞跺class鏂囦欢杩涜鎵ц.
涓€涓枃浠惰琚墽琛?蹇呴』瑕佹湁涓€涓墽琛岀殑璧峰鐐?杩欎釜璧峰鐐瑰氨鏄痬ain鍑芥暟.
----------------------------------------------------------------------------------------------
浜岋細java璇硶鍩虹锛?/h2>
涓€涓枃浠惰琚墽琛?蹇呴』瑕佹湁涓€涓墽琛岀殑璧峰鐐?杩欎釜璧峰鐐瑰氨鏄痬ain鍑芥暟.
----------------------------------------------------------------------------------------------
浜岋細java璇硶鍩虹锛?/h2>
1锛屽叧閿瓧锛氬叾瀹炲氨鏄煇绉嶈瑷€璧嬩簣浜嗙壒娈婂惈涔夌殑鍗曡瘝銆?/p>
淇濈暀瀛楋細鍏跺疄灏辨槸杩樻病鏈夎祴浜堢壒娈婂惈涔夛紝浣嗘槸鍑嗗鏃ュ悗瑕佷娇鐢ㄨ繃鐨勫崟璇嶃€?br>2锛屾爣绀虹锛氬叾瀹炲氨鏄湪绋嬪簭涓嚜瀹氫箟鐨勫悕璇嶃€傛瘮濡傜被鍚嶏紝鍙橀噺鍚嶏紝鍑芥暟鍚嶃€傚寘鍚?0-9銆乤-z銆?銆乢 锛?br> 娉ㄦ剰锛?br> 1锛夛紝鏁板瓧涓嶅彲浠ュ紑澶淬€?br> 2锛夛紝涓嶅彲浠ヤ娇鐢ㄥ叧閿瓧銆?br>3锛屽父閲忥細鏄湪绋嬪簭涓殑涓嶄細鍙樺寲鐨勬暟鎹€?br>4锛屽彉閲忥細鍏跺疄灏辨槸鍐呭瓨涓殑涓€涓瓨鍌ㄧ┖闂达紝鐢ㄤ簬瀛樺偍甯搁噺鏁版嵁銆?br> 浣滅敤锛氭柟渚夸簬杩愮畻銆傚洜涓烘湁浜涙暟鎹笉纭畾銆傛墍浠ョ‘瀹氳鏁版嵁鐨勫悕璇嶅拰瀛樺偍绌洪棿銆?br> 鐗圭偣锛氬彉閲忕┖闂村彲浠ラ噸澶嶄娇鐢ㄣ€?br>浠€涔堟椂鍊欏畾涔夊彉閲忥紵鍙鏄暟鎹笉纭畾鐨勬椂鍊欙紝灏卞畾涔夊彉閲忋€?br>
鍙橀噺绌洪棿鐨勫紑杈熼渶瑕佷粈涔堣绱犲憿锛?br> 1锛岃繖涓┖闂磋瀛樺偍浠€涔堟暟鎹紵鏁版嵁绫诲瀷銆?br> 2锛岃繖涓┖闂村彨浠€涔堝悕瀛楀晩锛熷彉閲忓悕绉般€?br> 3锛岃繖涓┖闂寸殑绗竴娆$殑鏁版嵁鏄粈涔堬紵 鍙橀噺鐨勫垵濮嬪寲鍊笺€?br>
鍙橀噺鐨勪綔鐢ㄥ煙鍜岀敓瀛樻湡:
鍙橀噺鐨勪綔鐢ㄥ煙锛?br> 浣滅敤鍩熶粠鍙橀噺瀹氫箟鐨勪綅缃紑濮嬶紝鍒拌鍙橀噺鎵€鍦ㄧ殑閭e澶ф嫭鍙风粨鏉燂紱鐢熷懡鍛ㄦ湡锛?br> 鍙橀噺浠庡畾涔夌殑浣嶇疆寮€濮嬪氨鍦ㄥ唴瀛樹腑娲讳簡锛?br> 鍙橀噺鍒拌揪瀹冩墍鍦ㄧ殑浣滅敤鍩熺殑鏃跺€欏氨鍦ㄥ唴瀛樹腑娑堝け浜嗭紱
鏁版嵁绫诲瀷锛?/h2>
1锛夛細鍩烘湰鏁版嵁绫诲瀷锛歜yte銆乻hort銆乮nt銆乴ong銆乫loat銆乨ouble銆乧har銆乥oolean
2锛夛細寮曠敤鏁版嵁绫诲瀷: 鏁扮粍銆佺被銆佹帴鍙c€?br>绾у埆浠庝綆鍒伴珮涓猴細byte,char,short(杩欎笁涓钩绾?-->int-->float-->long-->double
鑷姩绫诲瀷杞崲锛氫粠浣庣骇鍒埌楂樼骇鍒紝绯荤粺鑷姩杞殑锛?br>寮哄埗绫诲瀷杞崲锛氫粈涔堟儏鍐典笅浣跨敤?鎶婁竴涓珮绾у埆鐨勬暟璧嬬粰涓€涓埆璇ユ暟鐨勭骇鍒綆鐨勫彉閲忥紱
杩愮畻绗﹀彿锛?/h3>
1锛夈€佺畻鏈繍绠楃銆?/h4>
+ - * / % %:浠讳綍鏁存暟妯?涓嶆槸0灏辨槸1锛屾墍浠ュ彧瑕佹敼鍙樿妯℃暟灏卞彲浠ュ疄鐜板紑鍏宠繍绠椼€?br> +:杩炴帴绗︺€?br> ++,--
2锛夈€佽祴鍊艰繍绠楃銆?/h4>
= += -= *= /= %=
3锛夈€佹瘮杈冭繍绠楃銆?/h4>
鐗圭偣锛氳杩愮畻绗︾殑鐗圭偣鏄細杩愮畻瀹岀殑缁撴灉锛岃涔堟槸true锛岃涔堟槸false銆? 4锛夈€侀€昏緫杩愮畻绗︺€?/h4>
& | ^ ! && ||
閫昏緫杩愮畻绗﹂櫎浜?! 澶栭兘鏄敤浜庤繛鎺ヤ袱涓猙oolean绫诲瀷琛ㄨ揪寮忋€?br> &: 鍙湁涓よ竟閮戒负true缁撴灉鏄痶rue銆傚惁鍒欏氨鏄痜alse銆?br> |:鍙涓よ竟閮戒负false缁撴灉鏄痜alse锛屽惁鍒欏氨鏄痶rue
^:寮傛垨锛氬拰鎴栨湁鐐逛笉涓€鏍枫€?br> 涓よ竟缁撴灉涓€鏍凤紝灏变负false銆?br> 涓よ竟缁撴灉涓嶄竴鏍凤紝灏变负true.
& 鍜?&&鍖哄埆锛?& 锛氭棤璁哄乏杈圭粨鏋滄槸浠€涔堬紝鍙宠竟閮藉弬涓庤繍绠椼€?br> &&:鐭矾涓庯紝濡傛灉宸﹁竟涓篺alse锛岄偅涔堝彸杈逛笉鍙傛暟涓庤繍绠椼€?br> | 鍜寍| 鍖哄埆锛殀锛氫袱杈归兘杩愮畻銆?br> ||锛氱煭璺垨锛屽鏋滃乏杈逛负true锛岄偅涔堝彸杈逛笉鍙備笌杩愮畻銆? 5锛夈€佷綅杩愮畻绗?鐢ㄤ簬鎿嶄綔浜岃繘鍒朵綅鐨勮繍绠楃銆?/h4>
& | ^
<< >> >>>(鏃犵鍙峰彸绉?
缁冧範锛氬涓や釜鍙橀噺鐨勬暟鎹繘琛屼簰鎹€備笉闇€瑕佺涓夋柟鍙橀噺銆?br> int a = 3,b = 5;-->b = 3,a = 5;
a = a + b; //a = 8;
b = a - b;// b = 3;
a = a - b; //a = 5;
a = a ^ b;//
b = a ^ b;//b = a ^ b ^ b = a
a = a ^ b;//a = a ^ b ^ a = b;
缁冧範锛氶珮鏁堢殑绠楀嚭 2*8 = 2<<3;
5锛岃鍙ャ€?/h2>
If switch do while while for
杩欎簺璇彞浠€涔堟椂鍊欑敤锛?br> 1锛夈€佸綋鍒ゆ柇鍥哄畾涓暟鐨勫€肩殑鏃跺€欙紝鍙互浣跨敤if锛屼篃鍙互浣跨敤switch銆?br> 浣嗘槸寤鸿浣跨敤switch锛屾晥鐜囩浉瀵硅緝楂樸€?br>switch(鍙橀噺){
case 鍊?瑕佹墽琛岀殑璇彞;break;
鈥?br> default:瑕佹墽琛岀殑璇彞;
}
宸ヤ綔鍘熺悊锛氱敤灏忔嫭鍙蜂腑鐨勫彉閲忕殑鍊间緷娆″拰case鍚庨潰鐨勫€艰繘琛屽姣旓紝鍜屽摢涓猚ase鍚庨潰鐨勫€肩浉鍚屼簡
灏辨墽琛屽摢涓猚ase鍚庨潰鐨勮鍙ワ紝濡傛灉娌℃湁鐩稿悓鐨勫垯鎵цdefault鍚庨潰鐨勮鍙ワ紱
缁嗚妭锛?锛夛細break鏄彲浠ョ渷鐣ョ殑锛屽鏋滅渷鐣ヤ簡灏变竴鐩存墽琛屽埌閬囧埌break涓烘锛?br> 2锛夛細switch 鍚庨潰鐨勫皬鎷彿涓殑鍙橀噺搴旇鏄痓yte,char,short,int鍥涚绫诲瀷涓殑涓€绉嶏紱
3锛夛細default鍙互鍐欏湪switch缁撴瀯涓殑浠绘剰浣嶇疆锛涘鏋滃皢default璇彞鏀惧湪浜嗙涓€琛岋紝鍒欎笉绠xpression涓巆ase涓殑value鏄惁鍖归厤锛岀▼搴忎細浠巇efault寮€濮嬫墽琛岀洿鍒扮涓€涓猙reak鍑虹幇銆?br> 2锛夈€佸綋鍒ゆ柇鏁版嵁鑼冨洿锛岃幏鍙栧垽鏂繍绠楃粨鏋渂oolean绫诲瀷鏃讹紝闇€瑕佷娇鐢╥f銆?br> 3锛夈€佸綋鏌愪簺璇彞闇€瑕佹墽琛屽緢澶氭鏃讹紝灏辩敤寰幆缁撴瀯銆?br> while鍜宖or鍙互杩涜浜掓崲銆?br> 鍖哄埆鍦ㄤ簬锛氬鏋滈渶瑕佸畾涔夊彉閲忔帶鍒跺惊鐜鏁般€傚缓璁娇鐢╢or銆傚洜涓篺or寰幆瀹屾瘯锛屽彉閲忓湪鍐呭瓨涓噴鏀俱€?br>
break:浣滅敤浜巗witch 锛屽拰寰幆璇彞锛岀敤浜庤烦鍑猴紝鎴栬€呯О涓虹粨鏉熴€?br>break璇彞鍗曠嫭瀛樺湪鏃讹紝涓嬮潰涓嶈瀹氫箟鍏朵粬璇彞锛屽洜涓烘墽琛屼笉鍒帮紝缂栬瘧浼氬け璐ャ€傚綋寰幆宓屽鏃讹紝break鍙烦鍑哄綋鍓嶆墍鍦ㄥ惊鐜€傝璺冲嚭宓屽涓殑澶栭儴寰幆锛屽彧瑕佺粰寰幆璧峰悕瀛楀嵆鍙紝杩欎釜鍚嶅瓧绉颁箣涓烘爣鍙枫€?br>
continue:鍙綔鐢ㄤ簬寰幆缁撴瀯锛岀户缁惊鐜敤鐨勩€?br>浣滅敤锛氱粨鏉熸湰娆″惊鐜紝缁х画涓嬫寰幆銆傝璇彞鍗曠嫭瀛樺湪鏃讹紝涓嬮潰涓嶅彲浠ュ畾涔夎鍙ワ紝鎵ц涓嶅埌銆?br>
6锛屽嚱 鏁帮細
涓轰簡鎻愰珮浠g爜鐨勫鐢ㄦ€э紝鍙互灏嗗叾瀹氫箟鎴愪竴涓崟鐙殑鍔熻兘锛岃鍔熻兘鐨勪綋鐜板氨鏄痡ava涓殑鍑芥暟銆傚嚱鏁板氨鏄綋鐜颁箣涓€銆?br>java涓殑鍑芥暟鐨勫畾涔夋牸寮忥細
淇グ绗?杩斿洖鍊肩被鍨?鍑芥暟鍚?鍙傛暟绫诲瀷 褰㈠紡鍙傛暟1锛屽弬鏁扮被鍨?褰㈠紡鍙傛暟1锛屸€?{
鎵ц璇彞锛?br> return 杩斿洖鍊硷紱
}
褰撳嚱鏁版病鏈夊叿浣撶殑杩斿洖鍊兼椂锛岃繑鍥炵殑杩斿洖鍊肩被鍨嬬敤void鍏抽敭瀛楄〃绀恒€?br>濡傛灉鍑芥暟鐨勮繑鍥炲€肩被鍨嬫槸void鏃讹紝return璇彞鍙互鐪佺暐涓嶅啓鐨勶紝绯荤粺浼氬府浣犺嚜鍔ㄥ姞涓娿€?br>return鐨勪綔鐢細缁撴潫鍑芥暟銆傜粨鏉熷姛鑳姐€?br>
濡備綍瀹氫箟涓€涓嚱鏁帮紵
鍑芥暟鍏跺疄灏辨槸涓€涓姛鑳斤紝瀹氫箟鍑芥暟灏辨槸瀹炵幇鍔熻兘锛岄€氳繃涓や釜鏄庣‘鏉ュ畬鎴愶細
1锛夈€佹槑纭鍔熻兘鐨勮繍绠楀畬鐨勭粨鏋滐紝鍏跺疄鏄湪鏄庣‘杩欎釜鍑芥暟鐨勮繑鍥炲€肩被鍨嬨€?br> 2锛夈€佸湪瀹炵幇璇ュ姛鑳界殑杩囩▼涓槸鍚︽湁鏈煡鍐呭鍙備笌浜嗚繍绠楋紝鍏跺疄灏辨槸鍦ㄦ槑纭繖涓嚱鏁扮殑鍙傛暟鍒楄〃(鍙傛暟绫诲瀷&鍙傛暟涓暟)銆?br>鍑芥暟鐨勪綔鐢細
1锛夈€佺敤浜庡畾涔夊姛鑳姐€?br>2锛夈€佺敤浜庡皝瑁呬唬鐮佹彁楂樹唬鐮佺殑澶嶇敤鎬с€?br>娉ㄦ剰锛氬嚱鏁颁腑鍙兘璋冪敤鍑芥暟锛屼笉鑳藉畾涔夊嚱鏁般€?br>涓诲嚱鏁帮細
1锛夈€佷繚璇佽绫荤殑鐙珛杩愯銆?br> 2锛夈€佸洜涓哄畠鏄▼搴忕殑鍏ュ彛銆?br> 3锛夈€佸洜涓哄畠鍦ㄨjvm璋冪敤銆?br>
鍑芥暟瀹氫箟鍚嶇О鏄负浠€涔堝憿锛?br>绛旓細1锛夈€佷负浜嗗璇ュ姛鑳借繘琛屾爣绀猴紝鏂逛究浜庤皟鐢ㄣ€?br> 2锛夈€佷负浜嗛€氳繃鍚嶇О灏卞彲浠ユ槑纭嚱鏁扮殑鍔熻兘锛屼负浜嗗鍔犱唬鐮佺殑闃呰鎬с€?br>
閲嶈浇鐨勫畾涔夋槸锛氬湪涓€涓被涓紝濡傛灉鍑虹幇浜嗕袱涓垨鑰呬袱涓互涓婄殑鍚屽悕鍑芥暟锛屽彧瑕佸畠浠殑鍙傛暟鐨勪釜鏁帮紝鎴栬€呭弬鏁扮殑绫诲瀷涓嶅悓锛屽嵆鍙О涔嬩负璇ュ嚱鏁伴噸杞戒簡銆?br>濡備綍鍖哄垎閲嶈浇锛氬綋鍑芥暟鍚屽悕鏃讹紝鍙湅鍙傛暟鍒楄〃銆傚拰杩斿洖鍊肩被鍨嬫病鍏崇郴銆?br>
7锛屾暟缁?/h2>
1锛夈€佺畻鏈繍绠楃銆?/h4>
+ - * / % %:浠讳綍鏁存暟妯?涓嶆槸0灏辨槸1锛屾墍浠ュ彧瑕佹敼鍙樿妯℃暟灏卞彲浠ュ疄鐜板紑鍏宠繍绠椼€?br> +:杩炴帴绗︺€?br> ++,--
2锛夈€佽祴鍊艰繍绠楃銆?/h4>
= += -= *= /= %=
3锛夈€佹瘮杈冭繍绠楃銆?/h4>
鐗圭偣锛氳杩愮畻绗︾殑鐗圭偣鏄細杩愮畻瀹岀殑缁撴灉锛岃涔堟槸true锛岃涔堟槸false銆? 4锛夈€侀€昏緫杩愮畻绗︺€?/h4>
& | ^ ! && ||
閫昏緫杩愮畻绗﹂櫎浜?! 澶栭兘鏄敤浜庤繛鎺ヤ袱涓猙oolean绫诲瀷琛ㄨ揪寮忋€?br> &: 鍙湁涓よ竟閮戒负true缁撴灉鏄痶rue銆傚惁鍒欏氨鏄痜alse銆?br> |:鍙涓よ竟閮戒负false缁撴灉鏄痜alse锛屽惁鍒欏氨鏄痶rue
^:寮傛垨锛氬拰鎴栨湁鐐逛笉涓€鏍枫€?br> 涓よ竟缁撴灉涓€鏍凤紝灏变负false銆?br> 涓よ竟缁撴灉涓嶄竴鏍凤紝灏变负true.
& 鍜?&&鍖哄埆锛?& 锛氭棤璁哄乏杈圭粨鏋滄槸浠€涔堬紝鍙宠竟閮藉弬涓庤繍绠椼€?br> &&:鐭矾涓庯紝濡傛灉宸﹁竟涓篺alse锛岄偅涔堝彸杈逛笉鍙傛暟涓庤繍绠椼€?br> | 鍜寍| 鍖哄埆锛殀锛氫袱杈归兘杩愮畻銆?br> ||锛氱煭璺垨锛屽鏋滃乏杈逛负true锛岄偅涔堝彸杈逛笉鍙備笌杩愮畻銆? 5锛夈€佷綅杩愮畻绗?鐢ㄤ簬鎿嶄綔浜岃繘鍒朵綅鐨勮繍绠楃銆?/h4>
& | ^
<< >> >>>(鏃犵鍙峰彸绉?
缁冧範锛氬涓や釜鍙橀噺鐨勬暟鎹繘琛屼簰鎹€備笉闇€瑕佺涓夋柟鍙橀噺銆?br> int a = 3,b = 5;-->b = 3,a = 5;
a = a + b; //a = 8;
b = a - b;// b = 3;
a = a - b; //a = 5;
a = a ^ b;//
b = a ^ b;//b = a ^ b ^ b = a
a = a ^ b;//a = a ^ b ^ a = b;
缁冧範锛氶珮鏁堢殑绠楀嚭 2*8 = 2<<3;
5锛岃鍙ャ€?/h2>
If switch do while while for
杩欎簺璇彞浠€涔堟椂鍊欑敤锛?br> 1锛夈€佸綋鍒ゆ柇鍥哄畾涓暟鐨勫€肩殑鏃跺€欙紝鍙互浣跨敤if锛屼篃鍙互浣跨敤switch銆?br> 浣嗘槸寤鸿浣跨敤switch锛屾晥鐜囩浉瀵硅緝楂樸€?br>switch(鍙橀噺){
case 鍊?瑕佹墽琛岀殑璇彞;break;
鈥?br> default:瑕佹墽琛岀殑璇彞;
}
宸ヤ綔鍘熺悊锛氱敤灏忔嫭鍙蜂腑鐨勫彉閲忕殑鍊间緷娆″拰case鍚庨潰鐨勫€艰繘琛屽姣旓紝鍜屽摢涓猚ase鍚庨潰鐨勫€肩浉鍚屼簡
灏辨墽琛屽摢涓猚ase鍚庨潰鐨勮鍙ワ紝濡傛灉娌℃湁鐩稿悓鐨勫垯鎵цdefault鍚庨潰鐨勮鍙ワ紱
缁嗚妭锛?锛夛細break鏄彲浠ョ渷鐣ョ殑锛屽鏋滅渷鐣ヤ簡灏变竴鐩存墽琛屽埌閬囧埌break涓烘锛?br> 2锛夛細switch 鍚庨潰鐨勫皬鎷彿涓殑鍙橀噺搴旇鏄痓yte,char,short,int鍥涚绫诲瀷涓殑涓€绉嶏紱
3锛夛細default鍙互鍐欏湪switch缁撴瀯涓殑浠绘剰浣嶇疆锛涘鏋滃皢default璇彞鏀惧湪浜嗙涓€琛岋紝鍒欎笉绠xpression涓巆ase涓殑value鏄惁鍖归厤锛岀▼搴忎細浠巇efault寮€濮嬫墽琛岀洿鍒扮涓€涓猙reak鍑虹幇銆?br> 2锛夈€佸綋鍒ゆ柇鏁版嵁鑼冨洿锛岃幏鍙栧垽鏂繍绠楃粨鏋渂oolean绫诲瀷鏃讹紝闇€瑕佷娇鐢╥f銆?br> 3锛夈€佸綋鏌愪簺璇彞闇€瑕佹墽琛屽緢澶氭鏃讹紝灏辩敤寰幆缁撴瀯銆?br> while鍜宖or鍙互杩涜浜掓崲銆?br> 鍖哄埆鍦ㄤ簬锛氬鏋滈渶瑕佸畾涔夊彉閲忔帶鍒跺惊鐜鏁般€傚缓璁娇鐢╢or銆傚洜涓篺or寰幆瀹屾瘯锛屽彉閲忓湪鍐呭瓨涓噴鏀俱€?br>
break:浣滅敤浜巗witch 锛屽拰寰幆璇彞锛岀敤浜庤烦鍑猴紝鎴栬€呯О涓虹粨鏉熴€?br>break璇彞鍗曠嫭瀛樺湪鏃讹紝涓嬮潰涓嶈瀹氫箟鍏朵粬璇彞锛屽洜涓烘墽琛屼笉鍒帮紝缂栬瘧浼氬け璐ャ€傚綋寰幆宓屽鏃讹紝break鍙烦鍑哄綋鍓嶆墍鍦ㄥ惊鐜€傝璺冲嚭宓屽涓殑澶栭儴寰幆锛屽彧瑕佺粰寰幆璧峰悕瀛楀嵆鍙紝杩欎釜鍚嶅瓧绉颁箣涓烘爣鍙枫€?br>
continue:鍙綔鐢ㄤ簬寰幆缁撴瀯锛岀户缁惊鐜敤鐨勩€?br>浣滅敤锛氱粨鏉熸湰娆″惊鐜紝缁х画涓嬫寰幆銆傝璇彞鍗曠嫭瀛樺湪鏃讹紝涓嬮潰涓嶅彲浠ュ畾涔夎鍙ワ紝鎵ц涓嶅埌銆?br>
6锛屽嚱 鏁帮細
涓轰簡鎻愰珮浠g爜鐨勫鐢ㄦ€э紝鍙互灏嗗叾瀹氫箟鎴愪竴涓崟鐙殑鍔熻兘锛岃鍔熻兘鐨勪綋鐜板氨鏄痡ava涓殑鍑芥暟銆傚嚱鏁板氨鏄綋鐜颁箣涓€銆?br>java涓殑鍑芥暟鐨勫畾涔夋牸寮忥細
淇グ绗?杩斿洖鍊肩被鍨?鍑芥暟鍚?鍙傛暟绫诲瀷 褰㈠紡鍙傛暟1锛屽弬鏁扮被鍨?褰㈠紡鍙傛暟1锛屸€?{
鎵ц璇彞锛?br> return 杩斿洖鍊硷紱
}
褰撳嚱鏁版病鏈夊叿浣撶殑杩斿洖鍊兼椂锛岃繑鍥炵殑杩斿洖鍊肩被鍨嬬敤void鍏抽敭瀛楄〃绀恒€?br>濡傛灉鍑芥暟鐨勮繑鍥炲€肩被鍨嬫槸void鏃讹紝return璇彞鍙互鐪佺暐涓嶅啓鐨勶紝绯荤粺浼氬府浣犺嚜鍔ㄥ姞涓娿€?br>return鐨勪綔鐢細缁撴潫鍑芥暟銆傜粨鏉熷姛鑳姐€?br>
濡備綍瀹氫箟涓€涓嚱鏁帮紵
鍑芥暟鍏跺疄灏辨槸涓€涓姛鑳斤紝瀹氫箟鍑芥暟灏辨槸瀹炵幇鍔熻兘锛岄€氳繃涓や釜鏄庣‘鏉ュ畬鎴愶細
1锛夈€佹槑纭鍔熻兘鐨勮繍绠楀畬鐨勭粨鏋滐紝鍏跺疄鏄湪鏄庣‘杩欎釜鍑芥暟鐨勮繑鍥炲€肩被鍨嬨€?br> 2锛夈€佸湪瀹炵幇璇ュ姛鑳界殑杩囩▼涓槸鍚︽湁鏈煡鍐呭鍙備笌浜嗚繍绠楋紝鍏跺疄灏辨槸鍦ㄦ槑纭繖涓嚱鏁扮殑鍙傛暟鍒楄〃(鍙傛暟绫诲瀷&鍙傛暟涓暟)銆?br>鍑芥暟鐨勪綔鐢細
1锛夈€佺敤浜庡畾涔夊姛鑳姐€?br>2锛夈€佺敤浜庡皝瑁呬唬鐮佹彁楂樹唬鐮佺殑澶嶇敤鎬с€?br>娉ㄦ剰锛氬嚱鏁颁腑鍙兘璋冪敤鍑芥暟锛屼笉鑳藉畾涔夊嚱鏁般€?br>涓诲嚱鏁帮細
1锛夈€佷繚璇佽绫荤殑鐙珛杩愯銆?br> 2锛夈€佸洜涓哄畠鏄▼搴忕殑鍏ュ彛銆?br> 3锛夈€佸洜涓哄畠鍦ㄨjvm璋冪敤銆?br>
鍑芥暟瀹氫箟鍚嶇О鏄负浠€涔堝憿锛?br>绛旓細1锛夈€佷负浜嗗璇ュ姛鑳借繘琛屾爣绀猴紝鏂逛究浜庤皟鐢ㄣ€?br> 2锛夈€佷负浜嗛€氳繃鍚嶇О灏卞彲浠ユ槑纭嚱鏁扮殑鍔熻兘锛屼负浜嗗鍔犱唬鐮佺殑闃呰鎬с€?br>
閲嶈浇鐨勫畾涔夋槸锛氬湪涓€涓被涓紝濡傛灉鍑虹幇浜嗕袱涓垨鑰呬袱涓互涓婄殑鍚屽悕鍑芥暟锛屽彧瑕佸畠浠殑鍙傛暟鐨勪釜鏁帮紝鎴栬€呭弬鏁扮殑绫诲瀷涓嶅悓锛屽嵆鍙О涔嬩负璇ュ嚱鏁伴噸杞戒簡銆?br>濡備綍鍖哄垎閲嶈浇锛氬綋鍑芥暟鍚屽悕鏃讹紝鍙湅鍙傛暟鍒楄〃銆傚拰杩斿洖鍊肩被鍨嬫病鍏崇郴銆?br>
7锛屾暟缁?/h2>
3锛夈€佹瘮杈冭繍绠楃銆?/h4>
鐗圭偣锛氳杩愮畻绗︾殑鐗圭偣鏄細杩愮畻瀹岀殑缁撴灉锛岃涔堟槸true锛岃涔堟槸false銆? 4锛夈€侀€昏緫杩愮畻绗︺€?/h4>
& | ^ ! && ||
閫昏緫杩愮畻绗﹂櫎浜?! 澶栭兘鏄敤浜庤繛鎺ヤ袱涓猙oolean绫诲瀷琛ㄨ揪寮忋€?br> &: 鍙湁涓よ竟閮戒负true缁撴灉鏄痶rue銆傚惁鍒欏氨鏄痜alse銆?br> |:鍙涓よ竟閮戒负false缁撴灉鏄痜alse锛屽惁鍒欏氨鏄痶rue
^:寮傛垨锛氬拰鎴栨湁鐐逛笉涓€鏍枫€?br> 涓よ竟缁撴灉涓€鏍凤紝灏变负false銆?br> 涓よ竟缁撴灉涓嶄竴鏍凤紝灏变负true.
& 鍜?&&鍖哄埆锛?& 锛氭棤璁哄乏杈圭粨鏋滄槸浠€涔堬紝鍙宠竟閮藉弬涓庤繍绠椼€?br> &&:鐭矾涓庯紝濡傛灉宸﹁竟涓篺alse锛岄偅涔堝彸杈逛笉鍙傛暟涓庤繍绠椼€?br> | 鍜寍| 鍖哄埆锛殀锛氫袱杈归兘杩愮畻銆?br> ||锛氱煭璺垨锛屽鏋滃乏杈逛负true锛岄偅涔堝彸杈逛笉鍙備笌杩愮畻銆? 5锛夈€佷綅杩愮畻绗?鐢ㄤ簬鎿嶄綔浜岃繘鍒朵綅鐨勮繍绠楃銆?/h4>
& | ^
<< >> >>>(鏃犵鍙峰彸绉?
缁冧範锛氬涓や釜鍙橀噺鐨勬暟鎹繘琛屼簰鎹€備笉闇€瑕佺涓夋柟鍙橀噺銆?br> int a = 3,b = 5;-->b = 3,a = 5;
a = a + b; //a = 8;
b = a - b;// b = 3;
a = a - b; //a = 5;
a = a ^ b;//
b = a ^ b;//b = a ^ b ^ b = a
a = a ^ b;//a = a ^ b ^ a = b;
缁冧範锛氶珮鏁堢殑绠楀嚭 2*8 = 2<<3;
5锛岃鍙ャ€?/h2>
If switch do while while for
杩欎簺璇彞浠€涔堟椂鍊欑敤锛?br> 1锛夈€佸綋鍒ゆ柇鍥哄畾涓暟鐨勫€肩殑鏃跺€欙紝鍙互浣跨敤if锛屼篃鍙互浣跨敤switch銆?br> 浣嗘槸寤鸿浣跨敤switch锛屾晥鐜囩浉瀵硅緝楂樸€?br>switch(鍙橀噺){
case 鍊?瑕佹墽琛岀殑璇彞;break;
鈥?br> default:瑕佹墽琛岀殑璇彞;
}
宸ヤ綔鍘熺悊锛氱敤灏忔嫭鍙蜂腑鐨勫彉閲忕殑鍊间緷娆″拰case鍚庨潰鐨勫€艰繘琛屽姣旓紝鍜屽摢涓猚ase鍚庨潰鐨勫€肩浉鍚屼簡
灏辨墽琛屽摢涓猚ase鍚庨潰鐨勮鍙ワ紝濡傛灉娌℃湁鐩稿悓鐨勫垯鎵цdefault鍚庨潰鐨勮鍙ワ紱
缁嗚妭锛?锛夛細break鏄彲浠ョ渷鐣ョ殑锛屽鏋滅渷鐣ヤ簡灏变竴鐩存墽琛屽埌閬囧埌break涓烘锛?br> 2锛夛細switch 鍚庨潰鐨勫皬鎷彿涓殑鍙橀噺搴旇鏄痓yte,char,short,int鍥涚绫诲瀷涓殑涓€绉嶏紱
3锛夛細default鍙互鍐欏湪switch缁撴瀯涓殑浠绘剰浣嶇疆锛涘鏋滃皢default璇彞鏀惧湪浜嗙涓€琛岋紝鍒欎笉绠xpression涓巆ase涓殑value鏄惁鍖归厤锛岀▼搴忎細浠巇efault寮€濮嬫墽琛岀洿鍒扮涓€涓猙reak鍑虹幇銆?br> 2锛夈€佸綋鍒ゆ柇鏁版嵁鑼冨洿锛岃幏鍙栧垽鏂繍绠楃粨鏋渂oolean绫诲瀷鏃讹紝闇€瑕佷娇鐢╥f銆?br> 3锛夈€佸綋鏌愪簺璇彞闇€瑕佹墽琛屽緢澶氭鏃讹紝灏辩敤寰幆缁撴瀯銆?br> while鍜宖or鍙互杩涜浜掓崲銆?br> 鍖哄埆鍦ㄤ簬锛氬鏋滈渶瑕佸畾涔夊彉閲忔帶鍒跺惊鐜鏁般€傚缓璁娇鐢╢or銆傚洜涓篺or寰幆瀹屾瘯锛屽彉閲忓湪鍐呭瓨涓噴鏀俱€?br>
break:浣滅敤浜巗witch 锛屽拰寰幆璇彞锛岀敤浜庤烦鍑猴紝鎴栬€呯О涓虹粨鏉熴€?br>break璇彞鍗曠嫭瀛樺湪鏃讹紝涓嬮潰涓嶈瀹氫箟鍏朵粬璇彞锛屽洜涓烘墽琛屼笉鍒帮紝缂栬瘧浼氬け璐ャ€傚綋寰幆宓屽鏃讹紝break鍙烦鍑哄綋鍓嶆墍鍦ㄥ惊鐜€傝璺冲嚭宓屽涓殑澶栭儴寰幆锛屽彧瑕佺粰寰幆璧峰悕瀛楀嵆鍙紝杩欎釜鍚嶅瓧绉颁箣涓烘爣鍙枫€?br>
continue:鍙綔鐢ㄤ簬寰幆缁撴瀯锛岀户缁惊鐜敤鐨勩€?br>浣滅敤锛氱粨鏉熸湰娆″惊鐜紝缁х画涓嬫寰幆銆傝璇彞鍗曠嫭瀛樺湪鏃讹紝涓嬮潰涓嶅彲浠ュ畾涔夎鍙ワ紝鎵ц涓嶅埌銆?br>
6锛屽嚱 鏁帮細
涓轰簡鎻愰珮浠g爜鐨勫鐢ㄦ€э紝鍙互灏嗗叾瀹氫箟鎴愪竴涓崟鐙殑鍔熻兘锛岃鍔熻兘鐨勪綋鐜板氨鏄痡ava涓殑鍑芥暟銆傚嚱鏁板氨鏄綋鐜颁箣涓€銆?br>java涓殑鍑芥暟鐨勫畾涔夋牸寮忥細
淇グ绗?杩斿洖鍊肩被鍨?鍑芥暟鍚?鍙傛暟绫诲瀷 褰㈠紡鍙傛暟1锛屽弬鏁扮被鍨?褰㈠紡鍙傛暟1锛屸€?{
鎵ц璇彞锛?br> return 杩斿洖鍊硷紱
}
褰撳嚱鏁版病鏈夊叿浣撶殑杩斿洖鍊兼椂锛岃繑鍥炵殑杩斿洖鍊肩被鍨嬬敤void鍏抽敭瀛楄〃绀恒€?br>濡傛灉鍑芥暟鐨勮繑鍥炲€肩被鍨嬫槸void鏃讹紝return璇彞鍙互鐪佺暐涓嶅啓鐨勶紝绯荤粺浼氬府浣犺嚜鍔ㄥ姞涓娿€?br>return鐨勪綔鐢細缁撴潫鍑芥暟銆傜粨鏉熷姛鑳姐€?br>
濡備綍瀹氫箟涓€涓嚱鏁帮紵
鍑芥暟鍏跺疄灏辨槸涓€涓姛鑳斤紝瀹氫箟鍑芥暟灏辨槸瀹炵幇鍔熻兘锛岄€氳繃涓や釜鏄庣‘鏉ュ畬鎴愶細
1锛夈€佹槑纭鍔熻兘鐨勮繍绠楀畬鐨勭粨鏋滐紝鍏跺疄鏄湪鏄庣‘杩欎釜鍑芥暟鐨勮繑鍥炲€肩被鍨嬨€?br> 2锛夈€佸湪瀹炵幇璇ュ姛鑳界殑杩囩▼涓槸鍚︽湁鏈煡鍐呭鍙備笌浜嗚繍绠楋紝鍏跺疄灏辨槸鍦ㄦ槑纭繖涓嚱鏁扮殑鍙傛暟鍒楄〃(鍙傛暟绫诲瀷&鍙傛暟涓暟)銆?br>鍑芥暟鐨勪綔鐢細
1锛夈€佺敤浜庡畾涔夊姛鑳姐€?br>2锛夈€佺敤浜庡皝瑁呬唬鐮佹彁楂樹唬鐮佺殑澶嶇敤鎬с€?br>娉ㄦ剰锛氬嚱鏁颁腑鍙兘璋冪敤鍑芥暟锛屼笉鑳藉畾涔夊嚱鏁般€?br>涓诲嚱鏁帮細
1锛夈€佷繚璇佽绫荤殑鐙珛杩愯銆?br> 2锛夈€佸洜涓哄畠鏄▼搴忕殑鍏ュ彛銆?br> 3锛夈€佸洜涓哄畠鍦ㄨjvm璋冪敤銆?br>
鍑芥暟瀹氫箟鍚嶇О鏄负浠€涔堝憿锛?br>绛旓細1锛夈€佷负浜嗗璇ュ姛鑳借繘琛屾爣绀猴紝鏂逛究浜庤皟鐢ㄣ€?br> 2锛夈€佷负浜嗛€氳繃鍚嶇О灏卞彲浠ユ槑纭嚱鏁扮殑鍔熻兘锛屼负浜嗗鍔犱唬鐮佺殑闃呰鎬с€?br>
閲嶈浇鐨勫畾涔夋槸锛氬湪涓€涓被涓紝濡傛灉鍑虹幇浜嗕袱涓垨鑰呬袱涓互涓婄殑鍚屽悕鍑芥暟锛屽彧瑕佸畠浠殑鍙傛暟鐨勪釜鏁帮紝鎴栬€呭弬鏁扮殑绫诲瀷涓嶅悓锛屽嵆鍙О涔嬩负璇ュ嚱鏁伴噸杞戒簡銆?br>濡備綍鍖哄垎閲嶈浇锛氬綋鍑芥暟鍚屽悕鏃讹紝鍙湅鍙傛暟鍒楄〃銆傚拰杩斿洖鍊肩被鍨嬫病鍏崇郴銆?br>
7锛屾暟缁?/h2>
閫昏緫杩愮畻绗﹂櫎浜?! 澶栭兘鏄敤浜庤繛鎺ヤ袱涓猙oolean绫诲瀷琛ㄨ揪寮忋€?br> &: 鍙湁涓よ竟閮戒负true缁撴灉鏄痶rue銆傚惁鍒欏氨鏄痜alse銆?br> |:鍙涓よ竟閮戒负false缁撴灉鏄痜alse锛屽惁鍒欏氨鏄痶rue
^:寮傛垨锛氬拰鎴栨湁鐐逛笉涓€鏍枫€?br> 涓よ竟缁撴灉涓€鏍凤紝灏变负false銆?br> 涓よ竟缁撴灉涓嶄竴鏍凤紝灏变负true.
& 鍜?&&鍖哄埆锛?& 锛氭棤璁哄乏杈圭粨鏋滄槸浠€涔堬紝鍙宠竟閮藉弬涓庤繍绠椼€?br> &&:鐭矾涓庯紝濡傛灉宸﹁竟涓篺alse锛岄偅涔堝彸杈逛笉鍙傛暟涓庤繍绠椼€?br> | 鍜寍| 鍖哄埆锛殀锛氫袱杈归兘杩愮畻銆?br> ||锛氱煭璺垨锛屽鏋滃乏杈逛负true锛岄偅涔堝彸杈逛笉鍙備笌杩愮畻銆?
5锛夈€佷綅杩愮畻绗?鐢ㄤ簬鎿嶄綔浜岃繘鍒朵綅鐨勮繍绠楃銆?/h4>
& | ^
<< >> >>>(鏃犵鍙峰彸绉?
缁冧範锛氬涓や釜鍙橀噺鐨勬暟鎹繘琛屼簰鎹€備笉闇€瑕佺涓夋柟鍙橀噺銆?br> int a = 3,b = 5;-->b = 3,a = 5;
a = a + b; //a = 8;
b = a - b;// b = 3;
a = a - b; //a = 5;
a = a ^ b;//
b = a ^ b;//b = a ^ b ^ b = a
a = a ^ b;//a = a ^ b ^ a = b;
缁冧範锛氶珮鏁堢殑绠楀嚭 2*8 = 2<<3;
5锛岃鍙ャ€?/h2>
If switch do while while for
杩欎簺璇彞浠€涔堟椂鍊欑敤锛?br> 1锛夈€佸綋鍒ゆ柇鍥哄畾涓暟鐨勫€肩殑鏃跺€欙紝鍙互浣跨敤if锛屼篃鍙互浣跨敤switch銆?br> 浣嗘槸寤鸿浣跨敤switch锛屾晥鐜囩浉瀵硅緝楂樸€?br>switch(鍙橀噺){
case 鍊?瑕佹墽琛岀殑璇彞;break;
鈥?br> default:瑕佹墽琛岀殑璇彞;
}
宸ヤ綔鍘熺悊锛氱敤灏忔嫭鍙蜂腑鐨勫彉閲忕殑鍊间緷娆″拰case鍚庨潰鐨勫€艰繘琛屽姣旓紝鍜屽摢涓猚ase鍚庨潰鐨勫€肩浉鍚屼簡
灏辨墽琛屽摢涓猚ase鍚庨潰鐨勮鍙ワ紝濡傛灉娌℃湁鐩稿悓鐨勫垯鎵цdefault鍚庨潰鐨勮鍙ワ紱
缁嗚妭锛?锛夛細break鏄彲浠ョ渷鐣ョ殑锛屽鏋滅渷鐣ヤ簡灏变竴鐩存墽琛屽埌閬囧埌break涓烘锛?br> 2锛夛細switch 鍚庨潰鐨勫皬鎷彿涓殑鍙橀噺搴旇鏄痓yte,char,short,int鍥涚绫诲瀷涓殑涓€绉嶏紱
3锛夛細default鍙互鍐欏湪switch缁撴瀯涓殑浠绘剰浣嶇疆锛涘鏋滃皢default璇彞鏀惧湪浜嗙涓€琛岋紝鍒欎笉绠xpression涓巆ase涓殑value鏄惁鍖归厤锛岀▼搴忎細浠巇efault寮€濮嬫墽琛岀洿鍒扮涓€涓猙reak鍑虹幇銆?br> 2锛夈€佸綋鍒ゆ柇鏁版嵁鑼冨洿锛岃幏鍙栧垽鏂繍绠楃粨鏋渂oolean绫诲瀷鏃讹紝闇€瑕佷娇鐢╥f銆?br> 3锛夈€佸綋鏌愪簺璇彞闇€瑕佹墽琛屽緢澶氭鏃讹紝灏辩敤寰幆缁撴瀯銆?br> while鍜宖or鍙互杩涜浜掓崲銆?br> 鍖哄埆鍦ㄤ簬锛氬鏋滈渶瑕佸畾涔夊彉閲忔帶鍒跺惊鐜鏁般€傚缓璁娇鐢╢or銆傚洜涓篺or寰幆瀹屾瘯锛屽彉閲忓湪鍐呭瓨涓噴鏀俱€?br>
break:浣滅敤浜巗witch 锛屽拰寰幆璇彞锛岀敤浜庤烦鍑猴紝鎴栬€呯О涓虹粨鏉熴€?br>break璇彞鍗曠嫭瀛樺湪鏃讹紝涓嬮潰涓嶈瀹氫箟鍏朵粬璇彞锛屽洜涓烘墽琛屼笉鍒帮紝缂栬瘧浼氬け璐ャ€傚綋寰幆宓屽鏃讹紝break鍙烦鍑哄綋鍓嶆墍鍦ㄥ惊鐜€傝璺冲嚭宓屽涓殑澶栭儴寰幆锛屽彧瑕佺粰寰幆璧峰悕瀛楀嵆鍙紝杩欎釜鍚嶅瓧绉颁箣涓烘爣鍙枫€?br>
continue:鍙綔鐢ㄤ簬寰幆缁撴瀯锛岀户缁惊鐜敤鐨勩€?br>浣滅敤锛氱粨鏉熸湰娆″惊鐜紝缁х画涓嬫寰幆銆傝璇彞鍗曠嫭瀛樺湪鏃讹紝涓嬮潰涓嶅彲浠ュ畾涔夎鍙ワ紝鎵ц涓嶅埌銆?br>
6锛屽嚱 鏁帮細
涓轰簡鎻愰珮浠g爜鐨勫鐢ㄦ€э紝鍙互灏嗗叾瀹氫箟鎴愪竴涓崟鐙殑鍔熻兘锛岃鍔熻兘鐨勪綋鐜板氨鏄痡ava涓殑鍑芥暟銆傚嚱鏁板氨鏄綋鐜颁箣涓€銆?br>java涓殑鍑芥暟鐨勫畾涔夋牸寮忥細
淇グ绗?杩斿洖鍊肩被鍨?鍑芥暟鍚?鍙傛暟绫诲瀷 褰㈠紡鍙傛暟1锛屽弬鏁扮被鍨?褰㈠紡鍙傛暟1锛屸€?{
鎵ц璇彞锛?br> return 杩斿洖鍊硷紱
}
褰撳嚱鏁版病鏈夊叿浣撶殑杩斿洖鍊兼椂锛岃繑鍥炵殑杩斿洖鍊肩被鍨嬬敤void鍏抽敭瀛楄〃绀恒€?br>濡傛灉鍑芥暟鐨勮繑鍥炲€肩被鍨嬫槸void鏃讹紝return璇彞鍙互鐪佺暐涓嶅啓鐨勶紝绯荤粺浼氬府浣犺嚜鍔ㄥ姞涓娿€?br>return鐨勪綔鐢細缁撴潫鍑芥暟銆傜粨鏉熷姛鑳姐€?br>
濡備綍瀹氫箟涓€涓嚱鏁帮紵
鍑芥暟鍏跺疄灏辨槸涓€涓姛鑳斤紝瀹氫箟鍑芥暟灏辨槸瀹炵幇鍔熻兘锛岄€氳繃涓や釜鏄庣‘鏉ュ畬鎴愶細
1锛夈€佹槑纭鍔熻兘鐨勮繍绠楀畬鐨勭粨鏋滐紝鍏跺疄鏄湪鏄庣‘杩欎釜鍑芥暟鐨勮繑鍥炲€肩被鍨嬨€?br> 2锛夈€佸湪瀹炵幇璇ュ姛鑳界殑杩囩▼涓槸鍚︽湁鏈煡鍐呭鍙備笌浜嗚繍绠楋紝鍏跺疄灏辨槸鍦ㄦ槑纭繖涓嚱鏁扮殑鍙傛暟鍒楄〃(鍙傛暟绫诲瀷&鍙傛暟涓暟)銆?br>鍑芥暟鐨勪綔鐢細
1锛夈€佺敤浜庡畾涔夊姛鑳姐€?br>2锛夈€佺敤浜庡皝瑁呬唬鐮佹彁楂樹唬鐮佺殑澶嶇敤鎬с€?br>娉ㄦ剰锛氬嚱鏁颁腑鍙兘璋冪敤鍑芥暟锛屼笉鑳藉畾涔夊嚱鏁般€?br>涓诲嚱鏁帮細
1锛夈€佷繚璇佽绫荤殑鐙珛杩愯銆?br> 2锛夈€佸洜涓哄畠鏄▼搴忕殑鍏ュ彛銆?br> 3锛夈€佸洜涓哄畠鍦ㄨjvm璋冪敤銆?br>
鍑芥暟瀹氫箟鍚嶇О鏄负浠€涔堝憿锛?br>绛旓細1锛夈€佷负浜嗗璇ュ姛鑳借繘琛屾爣绀猴紝鏂逛究浜庤皟鐢ㄣ€?br> 2锛夈€佷负浜嗛€氳繃鍚嶇О灏卞彲浠ユ槑纭嚱鏁扮殑鍔熻兘锛屼负浜嗗鍔犱唬鐮佺殑闃呰鎬с€?br>
閲嶈浇鐨勫畾涔夋槸锛氬湪涓€涓被涓紝濡傛灉鍑虹幇浜嗕袱涓垨鑰呬袱涓互涓婄殑鍚屽悕鍑芥暟锛屽彧瑕佸畠浠殑鍙傛暟鐨勪釜鏁帮紝鎴栬€呭弬鏁扮殑绫诲瀷涓嶅悓锛屽嵆鍙О涔嬩负璇ュ嚱鏁伴噸杞戒簡銆?br>濡備綍鍖哄垎閲嶈浇锛氬綋鍑芥暟鍚屽悕鏃讹紝鍙湅鍙傛暟鍒楄〃銆傚拰杩斿洖鍊肩被鍨嬫病鍏崇郴銆?br>
7锛屾暟缁?/h2>
杩欎簺璇彞浠€涔堟椂鍊欑敤锛?br> 1锛夈€佸綋鍒ゆ柇鍥哄畾涓暟鐨勫€肩殑鏃跺€欙紝鍙互浣跨敤if锛屼篃鍙互浣跨敤switch銆?br> 浣嗘槸寤鸿浣跨敤switch锛屾晥鐜囩浉瀵硅緝楂樸€?br>switch(鍙橀噺){
case 鍊?瑕佹墽琛岀殑璇彞;break;
鈥?br> default:瑕佹墽琛岀殑璇彞;
}
宸ヤ綔鍘熺悊锛氱敤灏忔嫭鍙蜂腑鐨勫彉閲忕殑鍊间緷娆″拰case鍚庨潰鐨勫€艰繘琛屽姣旓紝鍜屽摢涓猚ase鍚庨潰鐨勫€肩浉鍚屼簡
灏辨墽琛屽摢涓猚ase鍚庨潰鐨勮鍙ワ紝濡傛灉娌℃湁鐩稿悓鐨勫垯鎵цdefault鍚庨潰鐨勮鍙ワ紱
缁嗚妭锛?锛夛細break鏄彲浠ョ渷鐣ョ殑锛屽鏋滅渷鐣ヤ簡灏变竴鐩存墽琛屽埌閬囧埌break涓烘锛?br> 2锛夛細switch 鍚庨潰鐨勫皬鎷彿涓殑鍙橀噺搴旇鏄痓yte,char,short,int鍥涚绫诲瀷涓殑涓€绉嶏紱
3锛夛細default鍙互鍐欏湪switch缁撴瀯涓殑浠绘剰浣嶇疆锛涘鏋滃皢default璇彞鏀惧湪浜嗙涓€琛岋紝鍒欎笉绠xpression涓巆ase涓殑value鏄惁鍖归厤锛岀▼搴忎細浠巇efault寮€濮嬫墽琛岀洿鍒扮涓€涓猙reak鍑虹幇銆?br> 2锛夈€佸綋鍒ゆ柇鏁版嵁鑼冨洿锛岃幏鍙栧垽鏂繍绠楃粨鏋渂oolean绫诲瀷鏃讹紝闇€瑕佷娇鐢╥f銆?br> 3锛夈€佸綋鏌愪簺璇彞闇€瑕佹墽琛屽緢澶氭鏃讹紝灏辩敤寰幆缁撴瀯銆?br> while鍜宖or鍙互杩涜浜掓崲銆?br> 鍖哄埆鍦ㄤ簬锛氬鏋滈渶瑕佸畾涔夊彉閲忔帶鍒跺惊鐜鏁般€傚缓璁娇鐢╢or銆傚洜涓篺or寰幆瀹屾瘯锛屽彉閲忓湪鍐呭瓨涓噴鏀俱€?br>
break:浣滅敤浜巗witch 锛屽拰寰幆璇彞锛岀敤浜庤烦鍑猴紝鎴栬€呯О涓虹粨鏉熴€?br>break璇彞鍗曠嫭瀛樺湪鏃讹紝涓嬮潰涓嶈瀹氫箟鍏朵粬璇彞锛屽洜涓烘墽琛屼笉鍒帮紝缂栬瘧浼氬け璐ャ€傚綋寰幆宓屽鏃讹紝break鍙烦鍑哄綋鍓嶆墍鍦ㄥ惊鐜€傝璺冲嚭宓屽涓殑澶栭儴寰幆锛屽彧瑕佺粰寰幆璧峰悕瀛楀嵆鍙紝杩欎釜鍚嶅瓧绉颁箣涓烘爣鍙枫€?br>
continue:鍙綔鐢ㄤ簬寰幆缁撴瀯锛岀户缁惊鐜敤鐨勩€?br>浣滅敤锛氱粨鏉熸湰娆″惊鐜紝缁х画涓嬫寰幆銆傝璇彞鍗曠嫭瀛樺湪鏃讹紝涓嬮潰涓嶅彲浠ュ畾涔夎鍙ワ紝鎵ц涓嶅埌銆?br>
6锛屽嚱 鏁帮細
涓轰簡鎻愰珮浠g爜鐨勫鐢ㄦ€э紝鍙互灏嗗叾瀹氫箟鎴愪竴涓崟鐙殑鍔熻兘锛岃鍔熻兘鐨勪綋鐜板氨鏄痡ava涓殑鍑芥暟銆傚嚱鏁板氨鏄綋鐜颁箣涓€銆?br>java涓殑鍑芥暟鐨勫畾涔夋牸寮忥細淇グ绗?杩斿洖鍊肩被鍨?鍑芥暟鍚?鍙傛暟绫诲瀷 褰㈠紡鍙傛暟1锛屽弬鏁扮被鍨?褰㈠紡鍙傛暟1锛屸€?{
鎵ц璇彞锛?br> return 杩斿洖鍊硷紱
}
褰撳嚱鏁版病鏈夊叿浣撶殑杩斿洖鍊兼椂锛岃繑鍥炵殑杩斿洖鍊肩被鍨嬬敤void鍏抽敭瀛楄〃绀恒€?br>濡傛灉鍑芥暟鐨勮繑鍥炲€肩被鍨嬫槸void鏃讹紝return璇彞鍙互鐪佺暐涓嶅啓鐨勶紝绯荤粺浼氬府浣犺嚜鍔ㄥ姞涓娿€?br>return鐨勪綔鐢細缁撴潫鍑芥暟銆傜粨鏉熷姛鑳姐€?br>
濡備綍瀹氫箟涓€涓嚱鏁帮紵
鍑芥暟鍏跺疄灏辨槸涓€涓姛鑳斤紝瀹氫箟鍑芥暟灏辨槸瀹炵幇鍔熻兘锛岄€氳繃涓や釜鏄庣‘鏉ュ畬鎴愶細
1锛夈€佹槑纭鍔熻兘鐨勮繍绠楀畬鐨勭粨鏋滐紝鍏跺疄鏄湪鏄庣‘杩欎釜鍑芥暟鐨勮繑鍥炲€肩被鍨嬨€?br> 2锛夈€佸湪瀹炵幇璇ュ姛鑳界殑杩囩▼涓槸鍚︽湁鏈煡鍐呭鍙備笌浜嗚繍绠楋紝鍏跺疄灏辨槸鍦ㄦ槑纭繖涓嚱鏁扮殑鍙傛暟鍒楄〃(鍙傛暟绫诲瀷&鍙傛暟涓暟)銆?br>鍑芥暟鐨勪綔鐢細
1锛夈€佺敤浜庡畾涔夊姛鑳姐€?br>2锛夈€佺敤浜庡皝瑁呬唬鐮佹彁楂樹唬鐮佺殑澶嶇敤鎬с€?br>娉ㄦ剰锛氬嚱鏁颁腑鍙兘璋冪敤鍑芥暟锛屼笉鑳藉畾涔夊嚱鏁般€?br>涓诲嚱鏁帮細
1锛夈€佷繚璇佽绫荤殑鐙珛杩愯銆?br> 2锛夈€佸洜涓哄畠鏄▼搴忕殑鍏ュ彛銆?br> 3锛夈€佸洜涓哄畠鍦ㄨjvm璋冪敤銆?br>
鍑芥暟瀹氫箟鍚嶇О鏄负浠€涔堝憿锛?br>绛旓細1锛夈€佷负浜嗗璇ュ姛鑳借繘琛屾爣绀猴紝鏂逛究浜庤皟鐢ㄣ€?br> 2锛夈€佷负浜嗛€氳繃鍚嶇О灏卞彲浠ユ槑纭嚱鏁扮殑鍔熻兘锛屼负浜嗗鍔犱唬鐮佺殑闃呰鎬с€?br>
閲嶈浇鐨勫畾涔夋槸锛氬湪涓€涓被涓紝濡傛灉鍑虹幇浜嗕袱涓垨鑰呬袱涓互涓婄殑鍚屽悕鍑芥暟锛屽彧瑕佸畠浠殑鍙傛暟鐨勪釜鏁帮紝鎴栬€呭弬鏁扮殑绫诲瀷涓嶅悓锛屽嵆鍙О涔嬩负璇ュ嚱鏁伴噸杞戒簡銆?br>濡備綍鍖哄垎閲嶈浇锛氬綋鍑芥暟鍚屽悕鏃讹紝鍙湅鍙傛暟鍒楄〃銆傚拰杩斿洖鍊肩被鍨嬫病鍏崇郴銆?br>
7锛屾暟缁?/h2>
//浜屽垎鏌ユ壘娉曘€?/h3>
public static int halfSeach_2(int[] arr,int key){
int min,max,mid;
min = 0;
max = arr.length-1;
mid = (max+min)>>1; //(max+min)/2;
while(arr[mid]!=key){
if(key>arr[mid]){
min = mid + 1;
}
else if(key<arr[mid])
max = mid - 1;
if(max<min)
return -1;
mid = (max+min)>>1;
}
return mid;
}
---------------------------------------------------------
java鍒嗕簡5鐗囧唴瀛樸€?br>1锛氬瘎瀛樺櫒銆?锛氭湰鍦版柟娉曞尯銆?锛氭柟娉曞尯銆?锛氭爤銆?锛氬爢銆?br>鏍堬細瀛樺偍鐨勯兘鏄眬閮ㄥ彉閲?( 鍑芥暟涓畾涔夌殑鍙橀噺锛屽嚱鏁颁笂鐨勫弬鏁帮紝璇彞涓殑鍙橀噺 )锛?br> 鍙鏁版嵁杩愮畻瀹屾垚鎵€鍦ㄧ殑鍖哄煙缁撴潫锛岃鏁版嵁灏变細琚噴鏀俱€?br>鍫嗭細鐢ㄤ簬瀛樺偍鏁扮粍鍜屽璞★紝涔熷氨鏄疄浣撱€傚暐鏄疄浣撳晩锛熷氨鏄敤浜庡皝瑁呭涓暟鎹殑銆?br>1锛氭瘡涓€涓疄浣撻兘鏈夊唴瀛橀鍦板潃鍊笺€?br>2锛氬爢鍐呭瓨涓殑鍙橀噺閮芥湁榛樿鍒濆鍖栧€笺€傚洜涓烘暟鎹被鍨嬩笉鍚岋紝鍊间篃涓嶄竴鏍枫€?br>3锛氬瀮鍦惧洖鏀舵満鍒躲€?br>----------------------------------------------------------------------------------------------
涓夛細闈㈠悜瀵硅薄锛氣槄鈽呪槄鈽呪槄
鐗圭偣锛?br>1锛氬皢澶嶆潅鐨勪簨鎯呯畝鍗曞寲銆?br>2锛氶潰鍚戝璞″皢浠ュ墠鐨勮繃绋嬩腑鐨勬墽琛岃€咃紝鍙樻垚浜嗘寚鎸ヨ€呫€?br>3锛氶潰鍚戝璞¤繖绉嶆€濇兂鏄鍚堢幇鍦ㄤ汉浠€濊€冧範鎯殑涓€绉嶆€濇兂銆?br>杩囩▼鍜屽璞″湪鎴戜滑鐨勭▼搴忎腑鏄浣曚綋鐜扮殑鍛紵杩囩▼鍏跺疄灏辨槸鍑芥暟锛涘璞℃槸灏嗗嚱鏁扮瓑涓€浜涘唴瀹硅繘琛屼簡灏佽銆?br>
鍖垮悕瀵硅薄浣跨敤鍦烘櫙锛?br>1锛氬綋瀵规柟娉曞彧杩涜涓€娆¤皟鐢ㄧ殑鏃跺€欙紝鍙互浣跨敤鍖垮悕瀵硅薄銆?br>2锛氬綋瀵硅薄瀵规垚鍛樿繘琛屽娆¤皟鐢ㄦ椂锛屼笉鑳戒娇鐢ㄥ尶鍚嶅璞°€傚繀椤荤粰瀵硅薄璧峰悕瀛椼€?br>
鍦ㄧ被涓畾涔夊叾瀹為兘绉颁箣涓烘垚鍛樸€傛垚鍛樻湁涓ょ锛?br>1锛氭垚鍛樺彉閲忥細鍏跺疄瀵瑰簲鐨勫氨鏄簨鐗╃殑灞炴€с€?br>2锛氭垚鍛樺嚱鏁帮細鍏跺疄瀵瑰簲鐨勫氨鏄簨鐗╃殑琛屼负銆?br>
鎵€浠ワ紝鍏跺疄瀹氫箟绫伙紝灏辨槸鍦ㄥ畾涔夋垚鍛樺彉閲忓拰鎴愬憳鍑芥暟銆備絾鏄湪瀹氫箟鍓嶏紝蹇呴』鍏堣瀵逛簨鐗╄繘琛屽睘鎬у拰琛屼负鐨勫垎鏋愶紝鎵嶅彲浠ョ敤浠g爜鏉ヤ綋鐜般€?br>
private int age;//绉佹湁鐨勮闂潈闄愭渶浣庯紝鍙湁鍦ㄦ湰绫讳腑鐨勮闂湁鏁堛€?br>娉ㄦ剰锛氱鏈変粎浠呮槸灏佽鐨勪竴绉嶄綋鐜板舰寮忚€屽凡銆?br>
绉佹湁鐨勬垚鍛橈細鍏朵粬绫讳笉鑳界洿鎺ュ垱寤哄璞¤闂紝鎵€浠ュ彧鏈夐€氳繃鏈被瀵瑰鎻愪緵鍏蜂綋鐨勮闂柟寮忔潵瀹屾垚瀵圭鏈夌殑璁块棶锛屽彲浠ラ€氳繃瀵瑰鎻愪緵鍑芥暟鐨勫舰寮忓鍏惰繘琛岃闂€?br>濂藉锛氬彲浠ュ湪鍑芥暟涓姞鍏ラ€昏緫鍒ゆ柇绛夋搷浣滐紝瀵规暟鎹繘琛屽垽鏂瓑鎿嶄綔銆?br>
鎬荤粨锛氬紑鍙戞椂锛岃浣忥紝灞炴€ф槸鐢ㄤ簬瀛樺偍鏁版嵁鐨勶紝鐩存帴琚闂紝瀹规槗鍑虹幇瀹夊叏闅愭偅锛屾墍浠ワ紝绫讳腑鐨勫睘鎬ч€氬父琚鏈夊寲锛屽苟瀵瑰鎻愪緵鍏叡鐨勮闂柟娉曘€?br>杩欎釜鏂规硶涓€鑸湁涓や釜锛岃鑼冨啓娉曪細瀵逛簬灞炴€?xxx锛屽彲浠ヤ娇鐢╯etXXX(),getXXX()瀵瑰叾杩涜鎿嶄綔銆?br>
绫讳腑鎬庝箞娌℃湁瀹氫箟涓诲嚱鏁板憿锛?br>娉ㄦ剰锛氫富鍑芥暟鐨勫瓨鍦紝浠呬负璇ョ被鏄惁闇€瑕佺嫭绔嬭繍琛岋紝濡傛灉涓嶉渶瑕侊紝涓诲嚱鏁版槸涓嶇敤瀹氫箟鐨勩€?br>涓诲嚱鏁扮殑瑙i噴锛氫繚璇佹墍鍦ㄧ被鐨勭嫭绔嬭繍琛岋紝鏄▼搴忕殑鍏ュ彛锛岃jvm璋冪敤銆?br>
鎴愬憳鍙橀噺鍜屽眬閮ㄥ彉閲忕殑鍖哄埆锛?/h4>
1锛氭垚鍛樺彉閲忕洿鎺ュ畾涔夊湪绫讳腑銆?br> 灞€閮ㄥ彉閲忓畾涔夊湪鏂规硶涓紝鍙傛暟涓婏紝璇彞涓€?br>2锛氭垚鍛樺彉閲忓湪杩欎釜绫讳腑鏈夋晥銆?br>灞€閮ㄥ彉閲忓彧鍦ㄨ嚜宸辨墍灞炵殑澶ф嫭鍙峰唴鏈夋晥锛屽ぇ鎷彿缁撴潫锛屽眬閮ㄥ彉閲忓け鍘讳綔鐢ㄥ煙銆?br>3锛氭垚鍛樺彉閲忓瓨鍦ㄤ簬鍫嗗唴瀛樹腑锛岄殢鐫€瀵硅薄鐨勪骇鐢熻€屽瓨鍦紝娑堝け鑰屾秷澶便€?br>灞€閮ㄥ彉閲忓瓨鍦ㄤ簬鏍堝唴瀛樹腑锛岄殢鐫€鎵€灞炲尯鍩熺殑杩愯鑰屽瓨鍦紝缁撴潫鑰岄噴鏀俱€?br>
鏋勯€犲嚱鏁帮細鐢ㄤ簬缁欏璞¤繘琛屽垵濮嬪寲锛屾槸缁欎笌涔嬪搴旂殑瀵硅薄杩涜鍒濆鍖栵紝瀹冨叿鏈夐拡瀵规€э紝鍑芥暟涓殑涓€绉嶃€?br>鐗圭偣锛?br>1锛氳鍑芥暟鐨勫悕绉板拰鎵€鍦ㄧ被鐨勫悕绉扮浉鍚屻€?br>2锛氫笉闇€瑕佸畾涔夎繑鍥炲€肩被鍨嬨€?br>3锛氳鍑芥暟娌℃湁鍏蜂綋鐨勮繑鍥炲€笺€?br>璁颁綇锛氭墍鏈夊璞″垱寤烘椂锛岄兘闇€瑕佸垵濮嬪寲鎵嶅彲浠ヤ娇鐢ㄣ€?br>
娉ㄦ剰浜嬮」锛氫竴涓被鍦ㄥ畾涔夋椂锛屽鏋滄病鏈夊畾涔夎繃鏋勯€犲嚱鏁帮紝閭d箞璇ョ被涓細鑷姩鐢熸垚涓€涓┖鍙傛暟鐨勬瀯閫犲嚱鏁帮紝涓轰簡鏂逛究璇ョ被鍒涘缓瀵硅薄锛屽畬鎴愬垵濮嬪寲銆傚鏋滃湪绫讳腑鑷畾涔変簡鏋勯€犲嚱鏁帮紝閭d箞榛樿鐨勬瀯閫犲嚱鏁板氨娌℃湁浜嗐€?br>
涓€涓被涓紝鍙互鏈夊涓瀯閫犲嚱鏁帮紝鍥犱负瀹冧滑鐨勫嚱鏁板悕绉伴兘鐩稿悓锛屾墍浠ュ彧鑳介€氳繃鍙傛暟鍒楄〃鏉ュ尯鍒嗐€傛墍浠ワ紝涓€涓被涓鏋滃嚭鐜板涓瀯閫犲嚱鏁般€傚畠浠殑瀛樺湪鏄互閲嶈浇浣撶幇鐨勩€?br>
鏋勯€犲嚱鏁板拰涓€鑸嚱鏁版湁浠€涔堝尯鍒憿锛?/h4>
1锛氫袱涓嚱鏁板畾涔夋牸寮忎笉鍚屻€?br>2锛氭瀯閫犲嚱鏁版槸鍦ㄥ璞″垱寤烘椂锛屽氨琚皟鐢紝鐢ㄤ簬鍒濆鍖栵紝鑰屼笖鍒濆鍖栧姩浣滃彧鎵ц涓€娆°€?br> 涓€鑸嚱鏁帮紝鏄璞″垱寤哄悗锛岄渶瑕佽皟鐢ㄦ墠鎵ц锛屽彲浠ヨ璋冪敤澶氭銆?br>
浠€涔堟椂鍊欎娇鐢ㄦ瀯閫犲嚱鏁板憿锛?/h4>
鍒嗘瀽浜嬬墿鏃讹紝鍙戠幇鍏蜂綋浜嬬墿涓€鍑虹幇锛屽氨鍏峰浜嗕竴浜涚壒寰侊紝閭e氨灏嗚繖浜涚壒寰佸畾涔夊埌鏋勯€犲嚱鏁板唴銆?br>
鏋勯€犱唬鐮佸潡鍜屾瀯閫犲嚱鏁版湁浠€涔堝尯鍒紵
鏋勯€犱唬鐮佸潡锛氭槸缁欐墍鏈夌殑瀵硅薄杩涜鍒濆鍖栵紝涔熷氨鏄锛屾墍鏈夌殑瀵硅薄閮戒細璋冪敤涓€涓唬鐮佸潡銆傚彧瑕佸璞′竴寤虹珛銆傚氨浼氳皟鐢ㄨ繖涓唬鐮佸潡銆?br>鏋勯€犲嚱鏁帮細鏄粰涓庝箣瀵瑰簲鐨勫璞¤繘琛屽垵濮嬪寲銆傚畠鍏锋湁閽堝鎬с€?br>
Person p = new Person();
鍒涘缓涓€涓璞¢兘鍦ㄥ唴瀛樹腑鍋氫簡浠€涔堜簨鎯咃紵
1锛氬厛灏嗙‖鐩樹笂鎸囧畾浣嶇疆鐨凱erson.class鏂囦欢鍔犺浇杩涘唴瀛樸€?br>2锛氭墽琛宮ain鏂规硶鏃讹紝鍦ㄦ爤鍐呭瓨涓紑杈熶簡main鏂规硶鐨勭┖闂?鍘嬫爤-杩涙爤)锛岀劧鍚庡湪main鏂规硶鐨勬爤鍖哄垎閰嶄簡涓€涓彉閲弍銆?br>3锛氬湪鍫嗗唴瀛樹腑寮€杈熶竴涓疄浣撶┖闂达紝鍒嗛厤浜嗕竴涓唴瀛橀鍦板潃鍊笺€俷ew
4锛氬湪璇ュ疄浣撶┖闂翠腑杩涜灞炴€х殑绌洪棿鍒嗛厤锛屽苟杩涜浜嗛粯璁ゅ垵濮嬪寲銆?br>5锛氬绌洪棿涓殑灞炴€ц繘琛屾樉绀哄垵濮嬪寲銆?br>6锛氳繘琛屽疄浣撶殑鏋勯€犱唬鐮佸潡鍒濆鍖栥€?br>7锛氳皟鐢ㄨ瀹炰綋瀵瑰簲鐨勬瀯閫犲嚱鏁帮紝杩涜鏋勯€犲嚱鏁板垵濮嬪寲銆傦紙锛?br>8锛氬皢棣栧湴鍧€璧嬪€肩粰p 锛宲鍙橀噺灏卞紩鐢ㄤ簡璇ュ疄浣撱€?鎸囧悜浜嗚瀵硅薄)
--------------------------------------------------------------------------------------------
灏?瑁呪槄鈽呪槄
锛堥潰鍚戝璞$壒寰佷箣涓€锛夛細鏄寚闅愯棌瀵硅薄鐨勫睘鎬у拰瀹炵幇缁嗚妭锛屼粎瀵瑰鎻愪緵鍏叡璁块棶鏂瑰紡銆?br>濂藉锛氬皢鍙樺寲闅旂锛涗究浜庝娇鐢紱鎻愰珮閲嶇敤鎬э紱瀹夊叏鎬с€?br>灏佽鍘熷垯锛氬皢涓嶉渶瑕佸澶栨彁渚涚殑鍐呭閮介殣钘忚捣鏉ワ紝鎶婂睘鎬ч兘闅愯棌锛屾彁渚涘叕鍏辨柟娉曞鍏惰闂€?this锛氣槄鈽呪槄
this:浠h〃瀵硅薄銆傚氨鏄墍鍦ㄥ嚱鏁版墍灞炲璞$殑寮曠敤銆?br>this鍒板簳浠h〃浠€涔堝憿锛熷摢涓璞¤皟鐢ㄤ簡this鎵€鍦ㄧ殑鍑芥暟锛宼his灏变唬琛ㄥ摢涓璞★紝灏辨槸鍝釜瀵硅薄鐨勫紩鐢ㄣ€?br>寮€鍙戞椂锛屼粈涔堟椂鍊欎娇鐢╰his鍛紵
鍦ㄥ畾涔夊姛鑳芥椂锛屽鏋滆鍔熻兘鍐呴儴浣跨敤鍒颁簡璋冪敤璇ュ姛鑳界殑瀵硅薄锛岃繖鏃跺氨鐢╰his鏉ヨ〃绀鸿繖涓璞°€?br>
this 杩樺彲浠ョ敤浜庢瀯閫犲嚱鏁伴棿鐨勮皟鐢ㄣ€?br>璋冪敤鏍煎紡锛歵his(瀹為檯鍙傛暟)锛?br>this瀵硅薄鍚庨潰璺熶笂 . 璋冪敤鐨勬槸鎴愬憳灞炴€у拰鎴愬憳鏂规硶(涓€鑸柟娉?锛?br>this瀵硅薄鍚庨潰璺熶笂 () 璋冪敤鐨勬槸鏈被涓殑瀵瑰簲鍙傛暟鐨勬瀯閫犲嚱鏁般€?br>
娉ㄦ剰锛氱敤this璋冪敤鏋勯€犲嚱鏁帮紝蹇呴』瀹氫箟鍦ㄦ瀯閫犲嚱鏁扮殑绗竴琛屻€傚洜涓烘瀯閫犲嚱鏁版槸鐢ㄤ簬鍒濆鍖栫殑锛屾墍浠ュ垵濮嬪寲鍔ㄤ綔涓€瀹氳鎵ц銆傚惁鍒欑紪璇戝け璐ャ€?br>
static锛氣槄鈽呪槄
鍏抽敭瀛楋紝鏄竴涓慨楗扮锛岀敤浜庝慨楗版垚鍛?鎴愬憳鍙橀噺鍜屾垚鍛樺嚱鏁?銆?br>鐗圭偣锛?br>1锛屾兂瑕佸疄鐜板璞′腑鐨勫叡鎬ф暟鎹殑瀵硅薄鍏变韩銆傚彲浠ュ皢杩欎釜鏁版嵁杩涜闈欐€佷慨楗般€?br>2锛岃闈欐€佷慨楗扮殑鎴愬憳锛屽彲浠ョ洿鎺ヨ绫诲悕鎵€璋冪敤銆備篃灏辨槸璇达紝闈欐€佺殑鎴愬憳澶氫簡涓€绉嶈皟鐢ㄦ柟寮忋€傜被鍚?闈欐€佹柟寮忋€?br>3锛岄潤鎬侀殢鐫€绫荤殑鍔犺浇鑰屽姞杞姐€傝€屼笖浼樺厛浜庡璞″瓨鍦ㄣ€?br>
寮婄锛?br>1锛屾湁浜涙暟鎹槸瀵硅薄鐗规湁鐨勬暟鎹紝鏄笉鍙互琚潤鎬佷慨楗扮殑銆傚洜涓洪偅鏍风殑璇濓紝鐗规湁鏁版嵁浼氬彉鎴愬璞$殑鍏变韩鏁版嵁銆傝繖鏍峰浜嬬墿鐨勬弿杩板氨鍑轰簡闂銆傛墍浠ワ紝鍦ㄥ畾涔夐潤鎬佹椂锛屽繀椤昏鏄庣‘锛岃繖涓暟鎹槸鍚︽槸琚璞℃墍鍏变韩鐨勩€?br>2锛岄潤鎬佹柟娉曞彧鑳借闂潤鎬佹垚鍛橈紝涓嶅彲浠ヨ闂潪闈欐€佹垚鍛樸€?br>鍥犱负闈欐€佹柟娉曞姞杞芥椂锛屼紭鍏堜簬瀵硅薄瀛樺湪锛屾墍浠ユ病鏈夊姙娉曡闂璞′腑鐨勬垚鍛樸€?br>3锛岄潤鎬佹柟娉曚腑涓嶈兘浣跨敤this锛宻uper鍏抽敭瀛椼€?br>鍥犱负this浠h〃瀵硅薄锛岃€岄潤鎬佸湪鏃讹紝鏈夊彲鑳芥病鏈夊璞★紝鎵€浠his鏃犳硶浣跨敤銆?br>4锛屼富鍑芥暟鏄潤鎬佺殑銆?br>
浠€涔堟椂鍊欏畾涔夐潤鎬佹垚鍛樺憿锛熸垨鑰呰锛氬畾涔夋垚鍛樻椂锛屽埌搴曢渶涓嶉渶瑕佽闈欐€佷慨楗板憿锛?br>鎴愬憳鍒嗕袱绉嶏細
1锛屾垚鍛樺彉閲忋€傦紙鏁版嵁鍏变韩鏃堕潤鎬佸寲锛?br>璇ユ垚鍛樺彉閲忕殑鏁版嵁鏄惁鏄墍鏈夊璞¢兘涓€鏍凤細
濡傛灉鏄紝閭d箞璇ュ彉閲忛渶瑕佽闈欐€佷慨楗帮紝鍥犱负鏄叡浜殑鏁版嵁銆?br>濡傛灉涓嶆槸锛岄偅涔堝氨璇磋繖鏄璞$殑鐗规湁鏁版嵁锛岃瀛樺偍鍒板璞′腑銆?br>2锛屾垚鍛樺嚱鏁般€傦紙鏂规硶涓病鏈夎皟鐢ㄧ壒鏈夋暟鎹椂灏卞畾涔夋垚闈欐€侊級
濡傛灉鍒ゆ柇鎴愬憳鍑芥暟鏄惁闇€瑕佽闈欐€佷慨楗板憿锛?br> 鍙鍙傝€冿紝璇ュ嚱鏁板唴鏄惁璁块棶浜嗗璞′腑鐨勭壒鏈夋暟鎹細
濡傛灉鏈夎闂壒鏈夋暟鎹紝閭f柟娉曚笉鑳借闈欐€佷慨楗般€?br> 濡傛灉娌℃湁璁块棶杩囩壒鏈夋暟鎹紝閭d箞杩欎釜鏂规硶闇€瑕佽闈欐€佷慨楗般€?br>
鎴愬憳鍙橀噺鍜岄潤鎬佸彉閲忕殑鍖哄埆锛?/h3>
1锛屾垚鍛樺彉閲忔墍灞炰簬瀵硅薄銆傛墍浠ヤ篃绉颁负瀹炰緥鍙橀噺銆?br>闈欐€佸彉閲忔墍灞炰簬绫汇€傛墍浠ヤ篃绉颁负绫诲彉閲忋€?br>2锛屾垚鍛樺彉閲忓瓨鍦ㄤ簬鍫嗗唴瀛樹腑銆?br>闈欐€佸彉閲忓瓨鍦ㄤ簬鏂规硶鍖轰腑銆?br>3锛屾垚鍛樺彉閲忛殢鐫€瀵硅薄鍒涘缓鑰屽瓨鍦ㄣ€傞殢鐫€瀵硅薄琚洖鏀惰€屾秷澶便€?br>闈欐€佸彉閲忛殢鐫€绫荤殑鍔犺浇鑰屽瓨鍦ㄣ€傞殢鐫€绫荤殑娑堝け鑰屾秷澶便€?br>4锛屾垚鍛樺彉閲忓彧鑳借瀵硅薄鎵€璋冪敤 銆?br>闈欐€佸彉閲忓彲浠ヨ瀵硅薄璋冪敤锛屼篃鍙互琚被鍚嶈皟鐢ㄣ€?br>鎵€浠ワ紝鎴愬憳鍙橀噺鍙互绉颁负瀵硅薄鐨勭壒鏈夋暟鎹紝闈欐€佸彉閲忕О涓哄璞$殑鍏变韩鏁版嵁銆?br>
闈欐€佺殑娉ㄦ剰锛氶潤鎬佺殑鐢熷懡鍛ㄦ湡寰堥暱銆?br>闈欐€佷唬鐮佸潡锛氬氨鏄竴涓湁闈欐€佸叧閿瓧鏍囩ず鐨勪竴涓唬鐮佸潡鍖哄煙銆傚畾涔夊湪绫讳腑銆?br>浣滅敤锛氬彲浠ュ畬鎴愮被鐨勫垵濮嬪寲銆傞潤鎬佷唬鐮佸潡闅忕潃绫荤殑鍔犺浇鑰屾墽琛岋紝鑰屼笖鍙墽琛屼竴娆★紙new 澶氫釜瀵硅薄灏卞彧鎵ц涓€娆★級銆傚鏋滃拰涓诲嚱鏁板湪鍚屼竴绫讳腑锛屼紭鍏堜簬涓诲嚱鏁版墽琛屻€?br>
Public锛氳闂潈闄愭渶澶с€?br>static锛氫笉闇€瑕佸璞★紝鐩存帴绫诲悕鍗冲彲銆?br>void锛氫富鍑芥暟娌℃湁杩斿洖鍊笺€?br>Main锛氫富鍑芥暟鐗瑰畾鐨勫悕绉般€?br>(String[] args)锛氫富鍑芥暟鐨勫弬鏁帮紝鏄竴涓瓧绗︿覆鏁扮粍绫诲瀷鐨勫弬鏁帮紝jvm璋冪敤main鏂规硶鏃讹紝浼犻€掔殑瀹為檯鍙傛暟鏄?new String[0]銆?br>
jvm榛樿浼犻€掔殑鏄暱搴︿负0鐨勫瓧绗︿覆鏁扮粍锛屾垜浠湪杩愯璇ョ被鏃讹紝涔熷彲浠ユ寚瀹氬叿浣撶殑鍙傛暟杩涜浼犻€掋€傚彲浠ュ湪鎺у埗鍙帮紝杩愯璇ョ被鏃讹紝鍦ㄥ悗闈㈠姞鍏ュ弬鏁般€傚弬鏁颁箣闂撮€氳繃绌烘牸闅斿紑銆俲vm浼氳嚜鍔ㄥ皢杩欎簺瀛楃涓插弬鏁颁綔涓篴rgs鏁扮粍涓殑鍏冪礌锛岃繘琛屽瓨鍌ㄣ€?br>
闈欐€佷唬鐮佸潡銆佹瀯閫犱唬鐮佸潡銆佹瀯閫犲嚱鏁板悓鏃跺瓨鍦ㄦ椂鐨勬墽琛岄『搴忥細闈欐€佷唬鐮佸潡 脿 鏋勯€犱唬鐮佸潡 脿 鏋勯€犲嚱鏁帮紱
鐢熸垚Java甯姪鏂囨。锛氬懡浠ゆ牸寮忥細javadoc 鈥揹 鏂囦欢澶瑰悕 鈥揳uther 鈥搗ersion *.java
/** //鏍煎紡
*绫绘弿杩?br>*@author 浣滆€呭悕
*@version 鐗堟湰鍙?br>*/
/**
*鏂规硶鎻忚堪
*@param 鍙傛暟鎻忚堪
*@return 杩斿洖鍊兼弿杩?br>*/
---------------------------------------------------------------------------------------------
璁捐妯″紡锛氳В鍐抽棶棰樻渶琛屼箣鏈夋晥鐨勬€濇兂銆傛槸涓€濂楄鍙嶅浣跨敤銆佸鏁颁汉鐭ユ檽鐨勩€佺粡杩囧垎绫荤紪鐩殑銆佷唬鐮佽璁$粡楠岀殑鎬荤粨銆備娇鐢ㄨ璁℃ā寮忔槸涓轰簡鍙噸鐢ㄤ唬鐮併€佽浠g爜鏇村鏄撹浠栦汉鐞嗚В銆佷繚璇佷唬鐮佸彲闈犳€с€?br>
java涓湁23绉嶈璁℃ā寮忥細
鍗曚緥璁捐妯″紡锛氣槄鈽呪槄鈽呪槄
瑙e喅鐨勯棶棰橈細淇濊瘉涓€涓被鍦ㄥ唴瀛樹腑鐨勫璞″敮涓€鎬с€?br>姣斿锛氬绋嬪簭璇诲彇涓€涓厤缃枃浠舵椂锛屽缓璁厤缃枃浠跺皝瑁呮垚瀵硅薄銆備細鏂逛究鎿嶄綔鍏朵腑鏁版嵁锛屽張瑕佷繚璇佸涓▼搴忚鍒扮殑鏄悓涓€涓厤缃枃浠跺璞★紝灏遍渶瑕佽閰嶇疆鏂囦欢瀵硅薄鍦ㄥ唴瀛樹腑鏄敮涓€鐨勩€?br>
Runtime()鏂规硶灏辨槸鍗曚緥璁捐妯″紡杩涜璁捐鐨勩€?br>
濡備綍淇濊瘉瀵硅薄鍞竴鎬у憿锛?br>鎬濇兂锛?br>1锛屼笉璁╁叾浠栫▼搴忓垱寤鸿绫诲璞°€?br>2锛屽湪鏈被涓垱寤轰竴涓湰绫诲璞°€?br>3锛屽澶栨彁渚涙柟娉曪紝璁╁叾浠栫▼搴忚幏鍙栬繖涓璞°€?br>
姝ラ锛?br>1锛屽洜涓哄垱寤哄璞¢兘闇€瑕佹瀯閫犲嚱鏁板垵濮嬪寲锛屽彧瑕佸皢鏈被涓殑鏋勯€犲嚱鏁扮鏈夊寲锛屽叾浠栫▼搴忓氨鏃犳硶鍐嶅垱寤鸿绫诲璞★紱
2锛屽氨鍦ㄧ被涓垱寤轰竴涓湰绫荤殑瀵硅薄锛?br>3锛屽畾涔変竴涓柟娉曪紝杩斿洖璇ュ璞★紝璁╁叾浠栫▼搴忓彲浠ラ€氳繃鏂规硶灏卞緱鍒版湰绫诲璞°€傦紙浣滅敤锛氬彲鎺э級
浠g爜浣撶幇锛?br>1锛岀鏈夊寲鏋勯€犲嚱鏁帮紱
2锛屽垱寤虹鏈夊苟闈欐€佺殑鏈被瀵硅薄锛?br>3锛屽畾涔夊叕鏈夊苟闈欐€佺殑鏂规硶锛岃繑鍥炶瀵硅薄銆?br>---------------------------------------------
//楗挎眽寮?br>class Single{
private Single(){} //绉佹湁鍖栨瀯閫犲嚱鏁般€?br>private static Single s = new Single(); //鍒涘缓绉佹湁骞堕潤鎬佺殑鏈被瀵硅薄銆?br> public static Single getInstance(){ //瀹氫箟鍏湁骞堕潤鎬佺殑鏂规硶锛岃繑鍥炶瀵硅薄銆?br> return s;
}
}
---------------------------------------------
//鎳掓眽寮?寤惰繜鍔犺浇鏂瑰紡銆?br>class Single2{
private Single2(){}
private static Single2 s = null;
public static Single2 getInstance(){
if(s==null)
s = new Single2();
return s;
}
}
-------------------------------------------------------------------------------------------------
缁?鎵匡紙闈㈠悜瀵硅薄鐗瑰緛涔嬩竴锛?/h2>
濂藉锛?br>1锛氭彁楂樹簡浠g爜鐨勫鐢ㄦ€с€?br>2锛氳绫讳笌绫讳箣闂翠骇鐢熶簡鍏崇郴锛屾彁渚涗簡鍙︿竴涓壒寰佸鎬佺殑鍓嶆彁銆?br>
鐖剁被鐨勭敱鏉ワ細鍏跺疄鏄敱澶氫釜绫讳笉鏂悜涓婃娊鍙栧叡鎬у唴瀹硅€屾潵鐨勩€?br>java涓浜庣户鎵匡紝java鍙敮鎸佸崟缁ф壙銆俲ava铏界劧涓嶇洿鎺ユ敮鎸佸缁ф壙锛屼絾鏄繚鐣欎簡杩欑澶氱户鎵挎満鍒讹紝杩涜鏀硅壇銆?br>
鍗曠户鎵匡細涓€涓被鍙兘鏈変竴涓埗绫汇€?br>澶氱户鎵匡細涓€涓被鍙互鏈夊涓埗绫汇€?br>
涓轰粈涔堜笉鏀寔澶氱户鎵垮憿锛?br>鍥犱负褰撲竴涓被鍚屾椂缁ф壙涓や釜鐖剁被鏃讹紝涓や釜鐖剁被涓湁鐩稿悓鐨勫姛鑳斤紝閭d箞瀛愮被瀵硅薄璋冪敤璇ュ姛鑳芥椂锛岃繍琛屽摢涓€涓憿锛熷洜涓虹埗绫讳腑鐨勬柟娉曚腑瀛樺湪鏂规硶浣撱€?br>浣嗘槸java鏀寔澶氶噸缁ф壙銆侫缁ф壙B B缁ф壙C C缁ф壙D銆?br>澶氶噸缁ф壙鐨勫嚭鐜帮紝灏辨湁浜嗙户鎵夸綋绯汇€備綋绯讳腑鐨勯《灞傜埗绫绘槸閫氳繃涓嶆柇鍚戜笂鎶藉彇鑰屾潵鐨勩€傚畠閲岄潰瀹氫箟鐨勮浣撶郴鏈€鍩烘湰鏈€鍏辨€у唴瀹圭殑鍔熻兘銆?br>鎵€浠ワ紝涓€涓綋绯昏鎯宠浣跨敤锛岀洿鎺ユ煡闃呰绯荤粺涓殑鐖剁被鐨勫姛鑳藉嵆鍙煡閬撹浣撶郴鐨勫熀鏈敤娉曘€傞偅涔堟兂瑕佷娇鐢ㄤ竴涓綋绯绘椂锛岄渶瑕佸缓绔嬪璞°€傚缓璁缓绔嬫渶瀛愮被瀵硅薄锛屽洜涓烘渶瀛愮被涓嶄粎鍙互浣跨敤鐖剁被涓殑鍔熻兘銆傝繕鍙互浣跨敤瀛愮被鐗规湁鐨勪竴浜涘姛鑳姐€?br>
绠€鍗曡锛氬浜庝竴涓户鎵夸綋绯荤殑浣跨敤锛屾煡闃呴《灞傜埗绫讳腑鐨勫唴瀹癸紝鍒涘缓鏈€搴曞眰瀛愮被鐨勫璞°€?
瀛愮埗绫诲嚭鐜板悗锛岀被涓殑鎴愬憳閮芥湁浜嗗摢浜涚壒鐐癸細
1锛氭垚鍛樺彉閲忋€?br> 褰撳瓙鐖剁被涓嚭鐜颁竴鏍风殑灞炴€ф椂锛屽瓙绫荤被鍨嬬殑瀵硅薄锛岃皟鐢ㄨ灞炴€э紝鍊兼槸瀛愮被鐨勫睘鎬у€笺€?br> 濡傛灉鎯宠璋冪敤鐖剁被涓殑灞炴€у€硷紝闇€瑕佷娇鐢ㄤ竴涓叧閿瓧锛歴uper
This锛氫唬琛ㄦ槸鏈被绫诲瀷鐨勫璞″紩鐢ㄣ€?br> Super锛氫唬琛ㄦ槸瀛愮被鎵€灞炵殑鐖剁被涓殑鍐呭瓨绌洪棿寮曠敤銆?br> 娉ㄦ剰锛氬瓙鐖剁被涓€氬父鏄笉浼氬嚭鐜板悓鍚嶆垚鍛樺彉閲忕殑锛屽洜涓虹埗绫讳腑鍙瀹氫箟浜嗭紝瀛愮被灏变笉鐢ㄥ湪瀹氫箟浜嗭紝鐩存帴缁ф壙杩囨潵鐢ㄥ氨鍙互浜嗐€?br>2锛氭垚鍛樺嚱鏁般€?br>褰撳瓙鐖剁被涓嚭鐜颁簡涓€妯′竴鏍风殑鏂规硶鏃讹紝寤虹珛瀛愮被瀵硅薄浼氳繍琛屽瓙绫讳腑鐨勬柟娉曘€傚ソ鍍忕埗绫讳腑鐨勬柟娉曡瑕嗙洊鎺変竴鏍枫€傛墍浠ヨ繖绉嶆儏鍐碉紝鏄嚱鏁扮殑鍙︿竴涓壒鎬э細瑕嗙洊(澶嶅啓锛岄噸鍐?
浠€涔堟椂鍊欎娇鐢ㄨ鐩栧憿锛熷綋涓€涓被鐨勫姛鑳藉唴瀹归渶瑕佷慨鏀规椂锛屽彲浠ラ€氳繃瑕嗙洊鏉ュ疄鐜般€?br>3锛氭瀯閫犲嚱鏁般€?br>鍙戠幇瀛愮被鏋勯€犲嚱鏁拌繍琛屾椂锛屽厛杩愯浜嗙埗绫荤殑鏋勯€犲嚱鏁般€備负浠€涔堝憿?
鍘熷洜锛氬瓙绫荤殑鎵€鏈夋瀯閫犲嚱鏁颁腑鐨勭涓€琛岋紝鍏跺疄閮芥湁涓€鏉¢殣韬殑璇彞super();
super(): 琛ㄧず鐖剁被鐨勬瀯閫犲嚱鏁帮紝骞朵細璋冪敤浜庡弬鏁扮浉瀵瑰簲鐨勭埗绫讳腑鐨勬瀯閫犲嚱鏁般€傝€宻uper():鏄湪璋冪敤鐖剁被涓┖鍙傛暟鐨勬瀯閫犲嚱鏁般€?br>涓轰粈涔堝瓙绫诲璞″垵濮嬪寲鏃讹紝閮介渶瑕佽皟鐢ㄧ埗绫讳腑鐨勫嚱鏁帮紵(涓轰粈涔堣鍦ㄥ瓙绫绘瀯閫犲嚱鏁扮殑绗竴琛屽姞鍏ヨ繖涓猻uper()?)
鍥犱负瀛愮被缁ф壙鐖剁被锛屼細缁ф壙鍒扮埗绫讳腑鐨勬暟鎹紝鎵€浠ュ繀椤昏鐪嬬埗绫绘槸濡備綍瀵硅嚜宸辩殑鏁版嵁杩涜鍒濆鍖栫殑銆傛墍浠ュ瓙绫诲湪杩涜瀵硅薄鍒濆鍖栨椂锛屽厛璋冪敤鐖剁被鐨勬瀯閫犲嚱鏁帮紝杩欏氨鏄瓙绫荤殑瀹炰緥鍖栬繃绋嬨€?br>
娉ㄦ剰锛氬瓙绫讳腑鎵€鏈夌殑鏋勯€犲嚱鏁伴兘浼氶粯璁よ闂埗绫讳腑鐨勭┖鍙傛暟鐨勬瀯閫犲嚱鏁帮紝鍥犱负姣忎竴涓瓙绫绘瀯閫犲唴绗竴琛岄兘鏈夐粯璁ょ殑璇彞super();
濡傛灉鐖剁被涓病鏈夌┖鍙傛暟鐨勬瀯閫犲嚱鏁帮紝閭d箞瀛愮被鐨勬瀯閫犲嚱鏁板唴锛屽繀椤婚€氳繃super璇彞鎸囧畾瑕佽闂殑鐖剁被涓殑鏋勯€犲嚱鏁般€?br>濡傛灉瀛愮被鏋勯€犲嚱鏁颁腑鐢╰his鏉ユ寚瀹氳皟鐢ㄥ瓙绫昏嚜宸辩殑鏋勯€犲嚱鏁帮紝閭d箞琚皟鐢ㄧ殑鏋勯€犲嚱鏁颁篃涓€鏍蜂細璁块棶鐖剁被涓殑鏋勯€犲嚱鏁般€?br>
闂锛歴uper()鍜宼his()鏄惁鍙互鍚屾椂鍑虹幇鐨勬瀯閫犲嚱鏁颁腑銆?br>涓や釜璇彞鍙兘鏈変竴涓畾涔夊湪绗竴琛岋紝鎵€浠ュ彧鑳藉嚭鐜板叾涓竴涓€?br>
super()鎴栬€卼his():涓轰粈涔堜竴瀹氳瀹氫箟鍦ㄧ涓€琛岋紵
鍥犱负super()鎴栬€卼his()閮芥槸璋冪敤鏋勯€犲嚱鏁帮紝鏋勯€犲嚱鏁扮敤浜庡垵濮嬪寲锛屾墍浠ュ垵濮嬪寲鐨勫姩浣滆鍏堝畬鎴愩€?br>
缁ф壙鐨勭粏鑺傦細
浠€涔堟椂鍊欎娇鐢ㄧ户鎵垮憿锛?br>褰撶被涓庣被涔嬮棿瀛樺湪鐫€鎵€灞炲叧绯绘椂锛屾墠鍏峰浜嗙户鎵跨殑鍓嶆彁銆俛鏄痓涓殑涓€绉嶃€俛缁ф壙b銆傜嫾鏄姮绉戜腑鐨勪竴绉嶃€?br>鑻辨枃涔︿腑锛屾墍灞炲叧绯伙細" is a "
娉ㄦ剰锛氫笉瑕佷粎浠呬负浜嗚幏鍙栧叾浠栫被涓殑宸叉湁鎴愬憳杩涜缁ф壙銆?br>
鎵€浠ュ垽鏂墍灞炲叧绯伙紝鍙互绠€鍗曠湅锛屽鏋滅户鎵垮悗锛岃缁ф壙鐨勭被涓殑鍔熻兘锛岄兘鍙互琚瀛愮被鎵€鍏峰锛岄偅涔堢户鎵挎垚绔嬨€傚鏋滀笉鏄紝涓嶅彲浠ョ户鎵裤€?br>
缁嗚妭浜岋細
鍦ㄦ柟娉曡鐩栨椂锛屾敞鎰忎袱鐐癸細
1锛氬瓙绫昏鐩栫埗绫绘椂锛屽繀椤昏淇濊瘉锛屽瓙绫绘柟娉曠殑鏉冮檺蹇呴』澶т簬绛変簬鐖剁被鏂规硶鏉冮檺鍙互瀹炵幇缁ф壙銆傚惁鍒欙紝缂栬瘧澶辫触銆?br>2锛氳鐩栨椂锛岃涔堥兘闈欐€侊紝瑕佷箞閮戒笉闈欐€併€?(闈欐€佸彧鑳借鐩栭潤鎬侊紝鎴栬€呰闈欐€佽鐩?
缁ф壙鐨勪竴涓紛绔細鎵撶牬浜嗗皝瑁呮€с€傚浜庝竴浜涚被锛屾垨鑰呯被涓姛鑳斤紝鏄渶瑕佽缁ф壙锛屾垨鑰呭鍐欑殑銆?br>杩欐椂濡備綍瑙e喅闂鍛紵浠嬬粛涓€涓叧閿瓧锛宖inal:鏈€缁堛€?br>
final鐗圭偣锛?br>1锛氳繖涓叧閿瓧鏄竴涓慨楗扮锛屽彲浠ヤ慨楗扮被锛屾柟娉曪紝鍙橀噺銆?br>2锛氳final淇グ鐨勭被鏄竴涓渶缁堢被锛屼笉鍙互琚户鎵裤€?br>3锛氳final淇グ鐨勬柟娉曟槸涓€涓渶缁堟柟娉曪紝涓嶅彲浠ヨ瑕嗙洊銆?br>4锛氳final淇グ鐨勫彉閲忔槸涓€涓父閲忥紝鍙兘璧嬪€间竴娆°€?br>
鍏跺疄杩欐牱鐨勫師鍥犵殑灏辨槸缁欎竴浜涘浐瀹氱殑鏁版嵁璧蜂釜闃呰鎬ц緝寮虹殑鍚嶇О銆?br> 涓嶅姞final淇グ涓嶆槸涔熷彲浠ヤ娇鐢ㄥ悧锛熼偅涔堣繖涓€兼槸涓€涓彉閲忥紝鏄彲浠ユ洿鏀圭殑銆傚姞浜唂inal锛岀▼搴忔洿涓轰弗璋ㄣ€傚父閲忓悕绉板畾涔夋椂锛屾湁瑙勮寖锛屾墍鏈夊瓧姣嶉兘澶у啓锛屽鏋滅敱澶氫釜鍗曡瘝缁勬垚锛屼腑闂寸敤 _ 杩炴帴銆?br>
鎶借薄绫? abstract
鎶借薄锛氫笉鍏蜂綋锛岀湅涓嶆槑鐧姐€傛娊璞$被琛ㄨ薄浣撶幇銆?br>鍦ㄤ笉鏂娊鍙栬繃绋嬩腑锛屽皢鍏辨€у唴瀹逛腑鐨勬柟娉曞0鏄庢娊鍙栵紝浣嗘槸鏂规硶涓嶄竴鏍凤紝娌℃湁鎶藉彇锛岃繖鏃舵娊鍙栧埌鐨勬柟娉曪紝骞朵笉鍏蜂綋锛岄渶瑕佽鎸囧畾鍏抽敭瀛梐bstract鎵€鏍囩ず锛屽0鏄庝负鎶借薄鏂规硶銆?br>鎶借薄鏂规硶鎵€鍦ㄧ被涓€瀹氳鏍囩ず涓烘娊璞$被锛屼篃灏辨槸璇磋绫婚渶瑕佽abstract鍏抽敭瀛楁墍淇グ銆?br>
鎶借薄绫荤殑鐗圭偣锛?/h3>
1锛氭娊璞℃柟娉曞彧鑳藉畾涔夊湪鎶借薄绫讳腑锛屾娊璞$被鍜屾娊璞℃柟娉曞繀椤荤敱abstract鍏抽敭瀛椾慨楗帮紙鍙互鎻忚堪绫诲拰鏂规硶锛屼笉鍙互鎻忚堪鍙橀噺锛夈€?br>2锛氭娊璞℃柟娉曞彧瀹氫箟鏂规硶澹版槑锛屽苟涓嶅畾涔夋柟娉曞疄鐜般€?br>3锛氭娊璞$被涓嶅彲浠ヨ鍒涘缓瀵硅薄(瀹炰緥鍖?銆?br>4锛氬彧鏈夐€氳繃瀛愮被缁ф壙鎶借薄绫诲苟瑕嗙洊浜嗘娊璞$被涓殑鎵€鏈夋娊璞℃柟娉曞悗锛岃瀛愮被鎵嶅彲浠ュ疄渚嬪寲銆傚惁鍒欙紝璇ュ瓙绫昏繕鏄竴涓娊璞$被銆?br>
鎶借薄绫荤殑缁嗚妭锛?/h4>
1锛氭娊璞$被涓槸鍚︽湁鏋勯€犲嚱鏁帮紵鏈夛紝鐢ㄤ簬缁欏瓙绫诲璞¤繘琛屽垵濮嬪寲銆?br>2锛氭娊璞$被涓槸鍚﹀彲浠ュ畾涔夐潪鎶借薄鏂规硶锛?br> 鍙互銆傚叾瀹烇紝鎶借薄绫诲拰涓€鑸被娌℃湁澶ぇ鐨勫尯鍒紝閮芥槸鍦ㄦ弿杩颁簨鐗╋紝鍙笉杩囨娊璞$被鍦ㄦ弿杩颁簨鐗╂椂锛屾湁浜涘姛鑳戒笉鍏蜂綋銆傛墍浠ユ娊璞$被鍜屼竴鑸被鍦ㄥ畾涔変笂锛岄兘鏄渶瑕佸畾涔夊睘鎬у拰琛屼负鐨勩€傚彧涓嶈繃锛屾瘮涓€鑸被澶氫簡涓€涓娊璞″嚱鏁般€傝€屼笖姣斾竴鑸被灏戜簡涓€涓垱寤哄璞$殑閮ㄥ垎銆?br>3锛氭娊璞″叧閿瓧abstract鍜屽摢浜涗笉鍙互鍏卞瓨锛焒inal , private , static
4锛氭娊璞$被涓彲涓嶅彲浠ヤ笉瀹氫箟鎶借薄鏂规硶锛熷彲浠ャ€傛娊璞℃柟娉曠洰鐨勪粎浠呬负浜嗕笉璁╄绫诲垱寤哄璞°€?br>-----------------------------------------------------------------------------------------------
妯℃澘鏂规硶璁捐妯″紡锛?/h3>
瑙e喅鐨勯棶棰橈細褰撳姛鑳藉唴閮ㄤ竴閮ㄥ垎瀹炵幇鏃剁‘瀹氾紝涓€閮ㄥ垎瀹炵幇鏄笉纭畾鐨勩€傝繖鏃跺彲浠ユ妸涓嶇‘瀹氱殑閮ㄥ垎鏆撮湶鍑哄幓锛岃瀛愮被鍘诲疄鐜般€?br>abstract class GetTime{
public final void getTime(){ //姝ゅ姛鑳藉鏋滀笉闇€瑕佸鍐欙紝鍙姞final闄愬畾
long start = System.currentTimeMillis();
code(); //涓嶇‘瀹氱殑鍔熻兘閮ㄥ垎锛屾彁鍙栧嚭鏉ワ紝閫氳繃鎶借薄鏂规硶瀹炵幇
long end = System.currentTimeMillis();
System.out.println("姣鏄細"+(end-start));
}
public abstract void code(); //鎶借薄涓嶇‘瀹氱殑鍔熻兘锛岃瀛愮被澶嶅啓瀹炵幇
}
class SubDemo extends GetTime{
public void code(){ //瀛愮被澶嶅啓鍔熻兘鏂规硶
for(int y=0; y<1000; y++){
System.out.println("y");
}
}
}
---------------------------------------------------------------------------------------------
鎺?鍙o細鈽呪槄鈽呪槄鈽?/h2>
1锛氭槸鐢ㄥ叧閿瓧interface瀹氫箟鐨勩€?br>2锛氭帴鍙d腑鍖呭惈鐨勬垚鍛橈紝鏈€甯歌鐨勬湁鍏ㄥ眬甯搁噺銆佹娊璞℃柟娉曘€?br>娉ㄦ剰锛氭帴鍙d腑鐨勬垚鍛橀兘鏈夊浐瀹氱殑淇グ绗︺€?br> 鎴愬憳鍙橀噺锛歱ublic static final
鎴愬憳鏂规硶锛歱ublic abstract
interface Inter{
public static final int x = 3;
public abstract void show();
}
3锛氭帴鍙d腑鏈夋娊璞℃柟娉曪紝璇存槑鎺ュ彛涓嶅彲浠ュ疄渚嬪寲銆傛帴鍙g殑瀛愮被蹇呴』瀹炵幇浜嗘帴鍙d腑鎵€鏈夌殑鎶借薄鏂规硶鍚庯紝璇ュ瓙绫绘墠鍙互瀹炰緥鍖栥€傚惁鍒欙紝璇ュ瓙绫昏繕鏄竴涓娊璞$被銆?br>4锛氱被涓庣被涔嬮棿瀛樺湪鐫€缁ф壙鍏崇郴锛岀被涓庢帴鍙d腑闂村瓨鍦ㄧ殑鏄疄鐜板叧绯汇€?br> 缁ф壙鐢╡xtends 锛涘疄鐜扮敤implements 锛?br>5锛氭帴鍙e拰绫讳笉涓€鏍风殑鍦版柟锛屽氨鏄紝鎺ュ彛鍙互琚瀹炵幇锛岃繖灏辨槸澶氱户鎵挎敼鑹悗鐨勭粨鏋溿€俲ava灏嗗缁ф壙鏈哄埗閫氳繃澶氱幇瀹炴潵浣撶幇銆?br>6锛氫竴涓被鍦ㄧ户鎵垮彟涓€涓被鐨勫悓鏃讹紝杩樺彲浠ュ疄鐜板涓帴鍙c€傛墍浠ユ帴鍙g殑鍑虹幇閬垮厤浜嗗崟缁ф壙鐨勫眬闄愭€с€傝繕鍙互灏嗙被杩涜鍔熻兘鐨勬墿灞曘€?br>7锛氬叾瀹瀓ava涓槸鏈夊缁ф壙鐨勩€傛帴鍙d笌鎺ュ彛涔嬮棿瀛樺湪鐫€缁ф壙鍏崇郴锛屾帴鍙e彲浠ュ缁ф壙鎺ュ彛銆?br>
鎺ュ彛閮界敤浜庤璁′笂锛岃璁′笂鐨勭壒鐐癸細锛堝彲浠ョ悊瑙d富鏉夸笂鎻愪緵鐨勬帴鍙o級
1锛氭帴鍙f槸瀵瑰鎻愪緵鐨勮鍒欍€?br>2锛氭帴鍙f槸鍔熻兘鐨勬墿灞曘€?br>3锛氭帴鍙g殑鍑虹幇闄嶄綆浜嗚€﹀悎鎬с€?br>
鎶借薄绫讳笌鎺ュ彛锛?/h3>
鎶借薄绫伙細涓€鑸敤浜庢弿杩颁竴涓綋绯诲崟鍏冿紝灏嗕竴缁勫叡鎬у唴瀹硅繘琛屾娊鍙栵紝鐗圭偣锛氬彲浠ュ湪绫讳腑瀹氫箟鎶借薄鍐呭璁╁瓙绫诲疄鐜帮紝鍙互瀹氫箟闈炴娊璞″唴瀹硅瀛愮被鐩存帴浣跨敤銆傚畠閲岄潰瀹氫箟鐨勯兘鏄竴浜涗綋绯讳腑鐨勫熀鏈唴瀹广€?br>鎺ュ彛锛氫竴鑸敤浜庡畾涔夊璞$殑鎵╁睍鍔熻兘锛屾槸鍦ㄧ户鎵夸箣澶栬繕闇€杩欎釜瀵硅薄鍏峰鐨勪竴浜涘姛鑳姐€?br>
鎶借薄绫诲拰鎺ュ彛鐨勫叡鎬э細閮芥槸涓嶆柇鍚戜笂鎶藉彇鐨勭粨鏋溿€?br>
鎶借薄绫诲拰鎺ュ彛鐨勫尯鍒細
1锛氭娊璞$被鍙兘琚户鎵匡紝鑰屼笖鍙兘鍗曠户鎵裤€?br>鎺ュ彛闇€瑕佽瀹炵幇锛岃€屼笖鍙互澶氬疄鐜般€?
2锛氭娊璞$被涓彲浠ュ畾涔夐潪鎶借薄鏂规硶锛屽瓙绫诲彲浠ョ洿鎺ョ户鎵夸娇鐢ㄣ€?br>鎺ュ彛涓兘鏈夋娊璞℃柟娉曪紝闇€瑕佸瓙绫诲幓瀹炵幇銆?br>3锛氭娊璞$被浣跨敤鐨勬槸 is a 鍏崇郴銆?br>鎺ュ彛浣跨敤鐨?like a 鍏崇郴銆?
4锛氭娊璞$被鐨勬垚鍛樹慨楗扮鍙互鑷畾涔夈€?br>鎺ュ彛涓殑鎴愬憳淇グ绗︽槸鍥哄畾鐨勩€傚叏閮芥槸public鐨勩€?br>
鍦ㄥ紑鍙戜箣鍓嶏紝鍏堝畾涔夎鍒欙紝A鍜孊鍒嗗埆寮€鍙戯紝A璐熻矗瀹炵幇杩欎釜瑙勫垯锛孊璐熻矗浣跨敤杩欎釜瑙勫垯銆傝嚦浜嶢鏄浣曞瑙勫垯鍏蜂綋瀹炵幇鐨勶紝B鏄笉闇€瑕佺煡閬撶殑銆傝繖鏍疯繖涓帴鍙g殑鍑虹幇灏遍檷浣庝簡A鍜孊鐩存帴鑰﹀悎鎬с€?br>
------------------------------------------------------------------------------------------------
澶?鎬佲槄鈽呪槄鈽呪槄
锛堥潰鍚戝璞$壒寰佷箣涓€锛夛細鍑芥暟鏈韩灏卞叿澶囧鎬佹€э紝鏌愪竴绉嶄簨鐗╂湁涓嶅悓鐨勫叿浣撶殑浣撶幇銆?br>
浣撶幇锛氱埗绫诲紩鐢ㄦ垨鑰呮帴鍙g殑寮曠敤鎸囧悜浜嗚嚜宸辩殑瀛愮被瀵硅薄銆?/Animal a = new Cat();
澶氭€佺殑濂藉锛氭彁楂樹簡绋嬪簭鐨勬墿灞曟€с€?br>澶氭€佺殑寮婄锛氬綋鐖剁被寮曠敤鎸囧悜瀛愮被瀵硅薄鏃讹紝铏界劧鎻愰珮浜嗘墿灞曟€э紝浣嗘槸鍙兘璁块棶鐖剁被涓叿澶囩殑鏂规硶锛屼笉鍙互璁块棶瀛愮被涓壒鏈夌殑鏂规硶銆?鍓嶆湡涓嶈兘浣跨敤鍚庢湡浜х敓鐨勫姛鑳斤紝鍗宠闂殑灞€闄愭€?
澶氭€佺殑鍓嶆彁锛?br> 1锛氬繀椤昏鏈夊叧绯伙紝姣斿缁ф壙銆佹垨鑰呭疄鐜般€?br> 2锛氶€氬父浼氭湁瑕嗙洊鎿嶄綔銆?br>
澶氭€佺殑鍑虹幇鎬濇兂涓婁篃鍋氱潃鍙樺寲锛氫互鍓嶆槸鍒涘缓瀵硅薄骞舵寚鎸ュ璞″仛浜嬫儏銆傛湁浜嗗鎬佷互鍚庯紝鎴戜滑鍙互鎵惧埌瀵硅薄鐨勫叡鎬х被鍨嬶紝鐩存帴鎿嶄綔鍏辨€х被鍨嬪仛浜嬫儏鍗冲彲锛岃繖鏍峰彲浠ユ寚鎸ヤ竴鎵瑰璞″仛浜嬫儏锛屽嵆閫氳繃鎿嶄綔鐖剁被鎴栨帴鍙e疄鐜般€?br>--------------------------------------------------------------
class teacher{
void teachs(){
System.out.println("浼佷笟绠$悊");
}
void play(){
System.out.println("閽撻奔");
}
}
class teacher extends teachers{
void 璁茶(){
System.out.println("JAVA");
}
void 鐪嬬數褰?){
System.out.println("鐪嬬數褰?);
}
}
class {
public static void main(String[] args) {
teachers x = new teacher(); //teacher瀵硅薄琚彁鍗囦负浜唗eahcers绫诲瀷銆?br>// x.璁茶();
// x.鐪嬬數褰?); //閿欒.
teacher y = (teachers)x; //灏唗eachers绫诲瀷寮哄埗杞崲鎴恡eacher绫诲瀷銆?br> y.鐪嬬數褰?);//鍦ㄥ鎬佷腑锛岃嚜濮嬭嚜缁堥兘鏄瓙绫诲璞″湪鍋氱潃绫诲瀷鐨勫彉鍖栥€?br> }
}
---------------------------------------------------------------
濡傛灉鎯崇敤瀛愮被瀵硅薄鐨勭壒鏈夋柟娉曪紝濡備綍鍒ゆ柇瀵硅薄鏄摢涓叿浣撶殑瀛愮被绫诲瀷鍛紵
鍙互鍙互閫氳繃涓€涓叧閿瓧 instanceof ;//鍒ゆ柇瀵硅薄鏄惁瀹炵幇浜嗘寚瀹氱殑鎺ュ彛鎴栫户鎵夸簡鎸囧畾鐨勭被
鏍煎紡锛?lt;瀵硅薄 instanceof 绫诲瀷> 锛屽垽鏂竴涓璞℃槸鍚︽墍灞炰簬鎸囧畾鐨勭被鍨嬨€?br>Student instanceof Person = true;//student缁ф壙浜唒erson绫?br>
澶氭€佸湪瀛愮埗绫讳腑鐨勬垚鍛樹笂鐨勪綋鐜扮殑鐗圭偣锛?/h3>
1锛屾垚鍛樺彉閲忥細鍦ㄥ鎬佷腑锛屽瓙鐖剁被鎴愬憳鍙橀噺鍚屽悕銆?br> 鍦ㄧ紪璇戞椂鏈燂細鍙傝€冪殑鏄紩鐢ㄥ瀷鍙橀噺鎵€灞炵殑绫讳腑鏄惁鏈夎皟鐢ㄧ殑鎴愬憳銆傦紙缂栬瘧鏃朵笉浜х敓瀵硅薄锛屽彧妫€鏌ヨ娉曢敊璇級
杩愯鏃舵湡锛氫篃鏄弬鑰冨紩鐢ㄥ瀷鍙橀噺鎵€灞炵殑绫讳腑鏄惁鏈夎皟鐢ㄧ殑鎴愬憳銆?br> 绠€鍗曚竴鍙ヨ瘽锛氭棤璁虹紪璇戝拰杩愯锛屾垚鍛樺彉閲忓弬鑰冪殑閮芥槸寮曠敤鍙橀噺鎵€灞炵殑绫讳腑鐨勬垚鍛樺彉閲忋€?br> 鍐嶈鐨勬洿瀹规槗璁板繂涓€浜涳細鎴愬憳鍙橀噺 --- 缂栬瘧杩愯閮界湅 = 宸﹁竟銆?br>2锛屾垚鍛樺嚱鏁般€?br> 缂栬瘧鏃舵湡锛氬弬鑰冨紩鐢ㄥ瀷鍙橀噺鎵€灞炵殑绫讳腑鏄惁鏈夎皟鐢ㄧ殑鏂规硶銆?br> 杩愯浜嬫儏锛氬弬鑰冪殑鏄璞℃墍灞炵殑绫讳腑鏄惁鏈夎皟鐢ㄧ殑鏂规硶銆?br> 涓轰粈涔堟槸杩欐牱鐨勫憿锛熷洜涓哄湪瀛愮埗绫讳腑锛屽浜庝竴妯′竴鏍风殑鎴愬憳鍑芥暟锛屾湁涓€涓壒鎬э細瑕嗙洊銆?br> 绠€鍗曚竴鍙ワ細鎴愬憳鍑芥暟锛岀紪璇戠湅寮曠敤鍨嬪彉閲忔墍灞炵殑绫伙紝杩愯鐪嬪璞℃墍灞炵殑绫汇€?br> 鏇寸畝鍗曪細鎴愬憳鍑芥暟 --- 缂栬瘧鐪?= 宸﹁竟锛岃繍琛岀湅 = 鍙宠竟銆?br>3锛岄潤鎬佸嚱鏁般€?
缂栬瘧鏃舵湡锛氬弬鑰冪殑鏄紩鐢ㄥ瀷鍙橀噺鎵€灞炵殑绫讳腑鏄惁鏈夎皟鐢ㄧ殑鎴愬憳銆?br> 杩愯鏃舵湡锛氫篃鏄弬鑰冨紩鐢ㄥ瀷鍙橀噺鎵€灞炵殑绫讳腑鏄惁鏈夎皟鐢ㄧ殑鎴愬憳銆?br> 涓轰粈涔堟槸杩欐牱鐨勫憿锛熷洜涓洪潤鎬佹柟娉曪紝鍏跺疄涓嶆墍灞炰簬瀵硅薄锛岃€屾槸鎵€灞炰簬璇ユ柟娉曟墍鍦ㄧ殑绫汇€?br> 璋冪敤闈欐€佺殑鏂规硶寮曠敤鏄摢涓被鐨勫紩鐢ㄨ皟鐢ㄧ殑灏辨槸鍝釜绫讳腑鐨勯潤鎬佹柟娉曘€?br> 绠€鍗曡锛氶潤鎬佸嚱鏁?--- 缂栬瘧杩愯閮界湅 = 宸﹁竟銆?br>-----------------------------------------------------------------------------------------------
java.lang.Object
Object锛氭墍鏈夌被鐨勭洿鎺ユ垨鑰呴棿鎺ョ埗绫伙紝Java璁や负鎵€鏈夌殑瀵硅薄閮藉叿澶囦竴浜涘熀鏈殑鍏辨€у唴瀹癸紝杩欎簺鍐呭鍙互涓嶆柇鐨勫悜涓婃娊鍙栵紝鏈€缁堝氨鎶藉彇鍒颁簡涓€涓渶椤跺眰鐨勭被涓殑锛岃绫讳腑瀹氫箟鐨勫氨鏄墍鏈夊璞¢兘鍏峰鐨勫姛鑳姐€?br>
鍏蜂綋鏂规硶锛?br>1锛宐oolean equals(Object obj)锛氱敤浜庢瘮杈冧袱涓璞℃槸鍚︾浉绛夛紝鍏跺疄鍐呴儴姣旇緝鐨勫氨鏄袱涓璞″湴鍧€銆?br>鑰屾牴鎹璞$殑灞炴€т笉鍚岋紝鍒ゆ柇瀵硅薄鏄惁鐩稿悓鐨勫叿浣撳唴瀹逛篃涓嶄竴鏍枫€傛墍浠ュ湪瀹氫箟绫绘椂锛屼竴鑸兘浼氬鍐檈quals鏂规硶锛屽缓绔嬫湰绫荤壒鏈夌殑鍒ゆ柇瀵硅薄鏄惁鐩稿悓鐨勪緷鎹€?br> public boolean equals(Object obj){
if(!(obj instanceof Person))
return false;
Person p = (Person)obj;
return this.age == p.age;
}
2锛孲tring toString()锛氬皢瀵硅薄鍙樻垚瀛楃涓诧紱榛樿杩斿洖鐨勬牸寮忥細绫诲悕@鍝堝笇鍊?= getClass().getName() + 鈥楡鈥?+ Integer.toHexString(hashCode())
涓轰簡瀵硅薄瀵瑰簲鐨勫瓧绗︿覆鍐呭鏈夋剰涔夛紝鍙互閫氳繃澶嶅啓锛屽缓绔嬭绫诲璞¤嚜宸辩壒鏈夌殑瀛楃涓茶〃鐜板舰寮忋€?br> public String toString(){
return "person : "+age;
}
3锛孋lass getClass()锛氳幏鍙栦换鎰忓璞¤繍琛屾椂鐨勬墍灞炲瓧鑺傜爜鏂囦欢瀵硅薄銆?br>4锛宨nt hashCode()锛氳繑鍥炶瀵硅薄鐨勫搱甯岀爜鍊笺€傛敮鎸佹鏂规硶鏄负浜嗘彁楂樺搱甯岃〃鐨勬€ц兘銆?br>
閫氬父equals锛宼oString锛宧ashCode锛屽湪搴旂敤涓兘浼氳澶嶅啓锛屽缓绔嬪叿浣撳璞$殑鐗规湁鐨勫唴瀹广€?br>------------------------------------------------------------------------------------------------
浠€涔堟椂鍊欎娇鐢ㄦ瀯閫犲嚱鏁板憿锛?/h4>
鍒嗘瀽浜嬬墿鏃讹紝鍙戠幇鍏蜂綋浜嬬墿涓€鍑虹幇锛屽氨鍏峰浜嗕竴浜涚壒寰侊紝閭e氨灏嗚繖浜涚壒寰佸畾涔夊埌鏋勯€犲嚱鏁板唴銆?br>
鏋勯€犱唬鐮佸潡鍜屾瀯閫犲嚱鏁版湁浠€涔堝尯鍒紵
鏋勯€犱唬鐮佸潡锛氭槸缁欐墍鏈夌殑瀵硅薄杩涜鍒濆鍖栵紝涔熷氨鏄锛屾墍鏈夌殑瀵硅薄閮戒細璋冪敤涓€涓唬鐮佸潡銆傚彧瑕佸璞′竴寤虹珛銆傚氨浼氳皟鐢ㄨ繖涓唬鐮佸潡銆?br>鏋勯€犲嚱鏁帮細鏄粰涓庝箣瀵瑰簲鐨勫璞¤繘琛屽垵濮嬪寲銆傚畠鍏锋湁閽堝鎬с€?br>
Person p = new Person();
鍒涘缓涓€涓璞¢兘鍦ㄥ唴瀛樹腑鍋氫簡浠€涔堜簨鎯咃紵
1锛氬厛灏嗙‖鐩樹笂鎸囧畾浣嶇疆鐨凱erson.class鏂囦欢鍔犺浇杩涘唴瀛樸€?br>2锛氭墽琛宮ain鏂规硶鏃讹紝鍦ㄦ爤鍐呭瓨涓紑杈熶簡main鏂规硶鐨勭┖闂?鍘嬫爤-杩涙爤)锛岀劧鍚庡湪main鏂规硶鐨勬爤鍖哄垎閰嶄簡涓€涓彉閲弍銆?br>3锛氬湪鍫嗗唴瀛樹腑寮€杈熶竴涓疄浣撶┖闂达紝鍒嗛厤浜嗕竴涓唴瀛橀鍦板潃鍊笺€俷ew
4锛氬湪璇ュ疄浣撶┖闂翠腑杩涜灞炴€х殑绌洪棿鍒嗛厤锛屽苟杩涜浜嗛粯璁ゅ垵濮嬪寲銆?br>5锛氬绌洪棿涓殑灞炴€ц繘琛屾樉绀哄垵濮嬪寲銆?br>6锛氳繘琛屽疄浣撶殑鏋勯€犱唬鐮佸潡鍒濆鍖栥€?br>7锛氳皟鐢ㄨ瀹炰綋瀵瑰簲鐨勬瀯閫犲嚱鏁帮紝杩涜鏋勯€犲嚱鏁板垵濮嬪寲銆傦紙锛?br>8锛氬皢棣栧湴鍧€璧嬪€肩粰p 锛宲鍙橀噺灏卞紩鐢ㄤ簡璇ュ疄浣撱€?鎸囧悜浜嗚瀵硅薄)
--------------------------------------------------------------------------------------------
灏?瑁呪槄鈽呪槄
锛堥潰鍚戝璞$壒寰佷箣涓€锛夛細鏄寚闅愯棌瀵硅薄鐨勫睘鎬у拰瀹炵幇缁嗚妭锛屼粎瀵瑰鎻愪緵鍏叡璁块棶鏂瑰紡銆?br>濂藉锛氬皢鍙樺寲闅旂锛涗究浜庝娇鐢紱鎻愰珮閲嶇敤鎬э紱瀹夊叏鎬с€?br>灏佽鍘熷垯锛氬皢涓嶉渶瑕佸澶栨彁渚涚殑鍐呭閮介殣钘忚捣鏉ワ紝鎶婂睘鎬ч兘闅愯棌锛屾彁渚涘叕鍏辨柟娉曞鍏惰闂€?this锛氣槄鈽呪槄
this:浠h〃瀵硅薄銆傚氨鏄墍鍦ㄥ嚱鏁版墍灞炲璞$殑寮曠敤銆?br>this鍒板簳浠h〃浠€涔堝憿锛熷摢涓璞¤皟鐢ㄤ簡this鎵€鍦ㄧ殑鍑芥暟锛宼his灏变唬琛ㄥ摢涓璞★紝灏辨槸鍝釜瀵硅薄鐨勫紩鐢ㄣ€?br>寮€鍙戞椂锛屼粈涔堟椂鍊欎娇鐢╰his鍛紵
鍦ㄥ畾涔夊姛鑳芥椂锛屽鏋滆鍔熻兘鍐呴儴浣跨敤鍒颁簡璋冪敤璇ュ姛鑳界殑瀵硅薄锛岃繖鏃跺氨鐢╰his鏉ヨ〃绀鸿繖涓璞°€?br>
this 杩樺彲浠ョ敤浜庢瀯閫犲嚱鏁伴棿鐨勮皟鐢ㄣ€?br>璋冪敤鏍煎紡锛歵his(瀹為檯鍙傛暟)锛?br>this瀵硅薄鍚庨潰璺熶笂 . 璋冪敤鐨勬槸鎴愬憳灞炴€у拰鎴愬憳鏂规硶(涓€鑸柟娉?锛?br>this瀵硅薄鍚庨潰璺熶笂 () 璋冪敤鐨勬槸鏈被涓殑瀵瑰簲鍙傛暟鐨勬瀯閫犲嚱鏁般€?br>
娉ㄦ剰锛氱敤this璋冪敤鏋勯€犲嚱鏁帮紝蹇呴』瀹氫箟鍦ㄦ瀯閫犲嚱鏁扮殑绗竴琛屻€傚洜涓烘瀯閫犲嚱鏁版槸鐢ㄤ簬鍒濆鍖栫殑锛屾墍浠ュ垵濮嬪寲鍔ㄤ綔涓€瀹氳鎵ц銆傚惁鍒欑紪璇戝け璐ャ€?br>
static锛氣槄鈽呪槄
鍏抽敭瀛楋紝鏄竴涓慨楗扮锛岀敤浜庝慨楗版垚鍛?鎴愬憳鍙橀噺鍜屾垚鍛樺嚱鏁?銆?br>鐗圭偣锛?br>1锛屾兂瑕佸疄鐜板璞′腑鐨勫叡鎬ф暟鎹殑瀵硅薄鍏变韩銆傚彲浠ュ皢杩欎釜鏁版嵁杩涜闈欐€佷慨楗般€?br>2锛岃闈欐€佷慨楗扮殑鎴愬憳锛屽彲浠ョ洿鎺ヨ绫诲悕鎵€璋冪敤銆備篃灏辨槸璇达紝闈欐€佺殑鎴愬憳澶氫簡涓€绉嶈皟鐢ㄦ柟寮忋€傜被鍚?闈欐€佹柟寮忋€?br>3锛岄潤鎬侀殢鐫€绫荤殑鍔犺浇鑰屽姞杞姐€傝€屼笖浼樺厛浜庡璞″瓨鍦ㄣ€?br>
寮婄锛?br>1锛屾湁浜涙暟鎹槸瀵硅薄鐗规湁鐨勬暟鎹紝鏄笉鍙互琚潤鎬佷慨楗扮殑銆傚洜涓洪偅鏍风殑璇濓紝鐗规湁鏁版嵁浼氬彉鎴愬璞$殑鍏变韩鏁版嵁銆傝繖鏍峰浜嬬墿鐨勬弿杩板氨鍑轰簡闂銆傛墍浠ワ紝鍦ㄥ畾涔夐潤鎬佹椂锛屽繀椤昏鏄庣‘锛岃繖涓暟鎹槸鍚︽槸琚璞℃墍鍏变韩鐨勩€?br>2锛岄潤鎬佹柟娉曞彧鑳借闂潤鎬佹垚鍛橈紝涓嶅彲浠ヨ闂潪闈欐€佹垚鍛樸€?br>鍥犱负闈欐€佹柟娉曞姞杞芥椂锛屼紭鍏堜簬瀵硅薄瀛樺湪锛屾墍浠ユ病鏈夊姙娉曡闂璞′腑鐨勬垚鍛樸€?br>3锛岄潤鎬佹柟娉曚腑涓嶈兘浣跨敤this锛宻uper鍏抽敭瀛椼€?br>鍥犱负this浠h〃瀵硅薄锛岃€岄潤鎬佸湪鏃讹紝鏈夊彲鑳芥病鏈夊璞★紝鎵€浠his鏃犳硶浣跨敤銆?br>4锛屼富鍑芥暟鏄潤鎬佺殑銆?br>
浠€涔堟椂鍊欏畾涔夐潤鎬佹垚鍛樺憿锛熸垨鑰呰锛氬畾涔夋垚鍛樻椂锛屽埌搴曢渶涓嶉渶瑕佽闈欐€佷慨楗板憿锛?br>鎴愬憳鍒嗕袱绉嶏細
1锛屾垚鍛樺彉閲忋€傦紙鏁版嵁鍏变韩鏃堕潤鎬佸寲锛?br>璇ユ垚鍛樺彉閲忕殑鏁版嵁鏄惁鏄墍鏈夊璞¢兘涓€鏍凤細
濡傛灉鏄紝閭d箞璇ュ彉閲忛渶瑕佽闈欐€佷慨楗帮紝鍥犱负鏄叡浜殑鏁版嵁銆?br>濡傛灉涓嶆槸锛岄偅涔堝氨璇磋繖鏄璞$殑鐗规湁鏁版嵁锛岃瀛樺偍鍒板璞′腑銆?br>2锛屾垚鍛樺嚱鏁般€傦紙鏂规硶涓病鏈夎皟鐢ㄧ壒鏈夋暟鎹椂灏卞畾涔夋垚闈欐€侊級
濡傛灉鍒ゆ柇鎴愬憳鍑芥暟鏄惁闇€瑕佽闈欐€佷慨楗板憿锛?br> 鍙鍙傝€冿紝璇ュ嚱鏁板唴鏄惁璁块棶浜嗗璞′腑鐨勭壒鏈夋暟鎹細
濡傛灉鏈夎闂壒鏈夋暟鎹紝閭f柟娉曚笉鑳借闈欐€佷慨楗般€?br> 濡傛灉娌℃湁璁块棶杩囩壒鏈夋暟鎹紝閭d箞杩欎釜鏂规硶闇€瑕佽闈欐€佷慨楗般€?br>
鎴愬憳鍙橀噺鍜岄潤鎬佸彉閲忕殑鍖哄埆锛?/h3>
1锛屾垚鍛樺彉閲忔墍灞炰簬瀵硅薄銆傛墍浠ヤ篃绉颁负瀹炰緥鍙橀噺銆?br>闈欐€佸彉閲忔墍灞炰簬绫汇€傛墍浠ヤ篃绉颁负绫诲彉閲忋€?br>2锛屾垚鍛樺彉閲忓瓨鍦ㄤ簬鍫嗗唴瀛樹腑銆?br>闈欐€佸彉閲忓瓨鍦ㄤ簬鏂规硶鍖轰腑銆?br>3锛屾垚鍛樺彉閲忛殢鐫€瀵硅薄鍒涘缓鑰屽瓨鍦ㄣ€傞殢鐫€瀵硅薄琚洖鏀惰€屾秷澶便€?br>闈欐€佸彉閲忛殢鐫€绫荤殑鍔犺浇鑰屽瓨鍦ㄣ€傞殢鐫€绫荤殑娑堝け鑰屾秷澶便€?br>4锛屾垚鍛樺彉閲忓彧鑳借瀵硅薄鎵€璋冪敤 銆?br>闈欐€佸彉閲忓彲浠ヨ瀵硅薄璋冪敤锛屼篃鍙互琚被鍚嶈皟鐢ㄣ€?br>鎵€浠ワ紝鎴愬憳鍙橀噺鍙互绉颁负瀵硅薄鐨勭壒鏈夋暟鎹紝闈欐€佸彉閲忕О涓哄璞$殑鍏变韩鏁版嵁銆?br>
闈欐€佺殑娉ㄦ剰锛氶潤鎬佺殑鐢熷懡鍛ㄦ湡寰堥暱銆?br>闈欐€佷唬鐮佸潡锛氬氨鏄竴涓湁闈欐€佸叧閿瓧鏍囩ず鐨勪竴涓唬鐮佸潡鍖哄煙銆傚畾涔夊湪绫讳腑銆?br>浣滅敤锛氬彲浠ュ畬鎴愮被鐨勫垵濮嬪寲銆傞潤鎬佷唬鐮佸潡闅忕潃绫荤殑鍔犺浇鑰屾墽琛岋紝鑰屼笖鍙墽琛屼竴娆★紙new 澶氫釜瀵硅薄灏卞彧鎵ц涓€娆★級銆傚鏋滃拰涓诲嚱鏁板湪鍚屼竴绫讳腑锛屼紭鍏堜簬涓诲嚱鏁版墽琛屻€?br>
Public锛氳闂潈闄愭渶澶с€?br>static锛氫笉闇€瑕佸璞★紝鐩存帴绫诲悕鍗冲彲銆?br>void锛氫富鍑芥暟娌℃湁杩斿洖鍊笺€?br>Main锛氫富鍑芥暟鐗瑰畾鐨勫悕绉般€?br>(String[] args)锛氫富鍑芥暟鐨勫弬鏁帮紝鏄竴涓瓧绗︿覆鏁扮粍绫诲瀷鐨勫弬鏁帮紝jvm璋冪敤main鏂规硶鏃讹紝浼犻€掔殑瀹為檯鍙傛暟鏄?new String[0]銆?br>
jvm榛樿浼犻€掔殑鏄暱搴︿负0鐨勫瓧绗︿覆鏁扮粍锛屾垜浠湪杩愯璇ョ被鏃讹紝涔熷彲浠ユ寚瀹氬叿浣撶殑鍙傛暟杩涜浼犻€掋€傚彲浠ュ湪鎺у埗鍙帮紝杩愯璇ョ被鏃讹紝鍦ㄥ悗闈㈠姞鍏ュ弬鏁般€傚弬鏁颁箣闂撮€氳繃绌烘牸闅斿紑銆俲vm浼氳嚜鍔ㄥ皢杩欎簺瀛楃涓插弬鏁颁綔涓篴rgs鏁扮粍涓殑鍏冪礌锛岃繘琛屽瓨鍌ㄣ€?br>
闈欐€佷唬鐮佸潡銆佹瀯閫犱唬鐮佸潡銆佹瀯閫犲嚱鏁板悓鏃跺瓨鍦ㄦ椂鐨勬墽琛岄『搴忥細闈欐€佷唬鐮佸潡 脿 鏋勯€犱唬鐮佸潡 脿 鏋勯€犲嚱鏁帮紱
鐢熸垚Java甯姪鏂囨。锛氬懡浠ゆ牸寮忥細javadoc 鈥揹 鏂囦欢澶瑰悕 鈥揳uther 鈥搗ersion *.java
/** //鏍煎紡
*绫绘弿杩?br>*@author 浣滆€呭悕
*@version 鐗堟湰鍙?br>*/
/**
*鏂规硶鎻忚堪
*@param 鍙傛暟鎻忚堪
*@return 杩斿洖鍊兼弿杩?br>*/
---------------------------------------------------------------------------------------------
璁捐妯″紡锛氳В鍐抽棶棰樻渶琛屼箣鏈夋晥鐨勬€濇兂銆傛槸涓€濂楄鍙嶅浣跨敤銆佸鏁颁汉鐭ユ檽鐨勩€佺粡杩囧垎绫荤紪鐩殑銆佷唬鐮佽璁$粡楠岀殑鎬荤粨銆備娇鐢ㄨ璁℃ā寮忔槸涓轰簡鍙噸鐢ㄤ唬鐮併€佽浠g爜鏇村鏄撹浠栦汉鐞嗚В銆佷繚璇佷唬鐮佸彲闈犳€с€?br>
java涓湁23绉嶈璁℃ā寮忥細
鍗曚緥璁捐妯″紡锛氣槄鈽呪槄鈽呪槄
瑙e喅鐨勯棶棰橈細淇濊瘉涓€涓被鍦ㄥ唴瀛樹腑鐨勫璞″敮涓€鎬с€?br>姣斿锛氬绋嬪簭璇诲彇涓€涓厤缃枃浠舵椂锛屽缓璁厤缃枃浠跺皝瑁呮垚瀵硅薄銆備細鏂逛究鎿嶄綔鍏朵腑鏁版嵁锛屽張瑕佷繚璇佸涓▼搴忚鍒扮殑鏄悓涓€涓厤缃枃浠跺璞★紝灏遍渶瑕佽閰嶇疆鏂囦欢瀵硅薄鍦ㄥ唴瀛樹腑鏄敮涓€鐨勩€?br>
Runtime()鏂规硶灏辨槸鍗曚緥璁捐妯″紡杩涜璁捐鐨勩€?br>
濡備綍淇濊瘉瀵硅薄鍞竴鎬у憿锛?br>鎬濇兂锛?br>1锛屼笉璁╁叾浠栫▼搴忓垱寤鸿绫诲璞°€?br>2锛屽湪鏈被涓垱寤轰竴涓湰绫诲璞°€?br>3锛屽澶栨彁渚涙柟娉曪紝璁╁叾浠栫▼搴忚幏鍙栬繖涓璞°€?br>
姝ラ锛?br>1锛屽洜涓哄垱寤哄璞¢兘闇€瑕佹瀯閫犲嚱鏁板垵濮嬪寲锛屽彧瑕佸皢鏈被涓殑鏋勯€犲嚱鏁扮鏈夊寲锛屽叾浠栫▼搴忓氨鏃犳硶鍐嶅垱寤鸿绫诲璞★紱
2锛屽氨鍦ㄧ被涓垱寤轰竴涓湰绫荤殑瀵硅薄锛?br>3锛屽畾涔変竴涓柟娉曪紝杩斿洖璇ュ璞★紝璁╁叾浠栫▼搴忓彲浠ラ€氳繃鏂规硶灏卞緱鍒版湰绫诲璞°€傦紙浣滅敤锛氬彲鎺э級
浠g爜浣撶幇锛?br>1锛岀鏈夊寲鏋勯€犲嚱鏁帮紱
2锛屽垱寤虹鏈夊苟闈欐€佺殑鏈被瀵硅薄锛?br>3锛屽畾涔夊叕鏈夊苟闈欐€佺殑鏂规硶锛岃繑鍥炶瀵硅薄銆?br>---------------------------------------------
//楗挎眽寮?br>class Single{
private Single(){} //绉佹湁鍖栨瀯閫犲嚱鏁般€?br>private static Single s = new Single(); //鍒涘缓绉佹湁骞堕潤鎬佺殑鏈被瀵硅薄銆?br> public static Single getInstance(){ //瀹氫箟鍏湁骞堕潤鎬佺殑鏂规硶锛岃繑鍥炶瀵硅薄銆?br> return s;
}
}
---------------------------------------------
//鎳掓眽寮?寤惰繜鍔犺浇鏂瑰紡銆?br>class Single2{
private Single2(){}
private static Single2 s = null;
public static Single2 getInstance(){
if(s==null)
s = new Single2();
return s;
}
}
-------------------------------------------------------------------------------------------------
缁?鎵匡紙闈㈠悜瀵硅薄鐗瑰緛涔嬩竴锛?/h2>
濂藉锛?br>1锛氭彁楂樹簡浠g爜鐨勫鐢ㄦ€с€?br>2锛氳绫讳笌绫讳箣闂翠骇鐢熶簡鍏崇郴锛屾彁渚涗簡鍙︿竴涓壒寰佸鎬佺殑鍓嶆彁銆?br>
鐖剁被鐨勭敱鏉ワ細鍏跺疄鏄敱澶氫釜绫讳笉鏂悜涓婃娊鍙栧叡鎬у唴瀹硅€屾潵鐨勩€?br>java涓浜庣户鎵匡紝java鍙敮鎸佸崟缁ф壙銆俲ava铏界劧涓嶇洿鎺ユ敮鎸佸缁ф壙锛屼絾鏄繚鐣欎簡杩欑澶氱户鎵挎満鍒讹紝杩涜鏀硅壇銆?br>
鍗曠户鎵匡細涓€涓被鍙兘鏈変竴涓埗绫汇€?br>澶氱户鎵匡細涓€涓被鍙互鏈夊涓埗绫汇€?br>
涓轰粈涔堜笉鏀寔澶氱户鎵垮憿锛?br>鍥犱负褰撲竴涓被鍚屾椂缁ф壙涓や釜鐖剁被鏃讹紝涓や釜鐖剁被涓湁鐩稿悓鐨勫姛鑳斤紝閭d箞瀛愮被瀵硅薄璋冪敤璇ュ姛鑳芥椂锛岃繍琛屽摢涓€涓憿锛熷洜涓虹埗绫讳腑鐨勬柟娉曚腑瀛樺湪鏂规硶浣撱€?br>浣嗘槸java鏀寔澶氶噸缁ф壙銆侫缁ф壙B B缁ф壙C C缁ф壙D銆?br>澶氶噸缁ф壙鐨勫嚭鐜帮紝灏辨湁浜嗙户鎵夸綋绯汇€備綋绯讳腑鐨勯《灞傜埗绫绘槸閫氳繃涓嶆柇鍚戜笂鎶藉彇鑰屾潵鐨勩€傚畠閲岄潰瀹氫箟鐨勮浣撶郴鏈€鍩烘湰鏈€鍏辨€у唴瀹圭殑鍔熻兘銆?br>鎵€浠ワ紝涓€涓綋绯昏鎯宠浣跨敤锛岀洿鎺ユ煡闃呰绯荤粺涓殑鐖剁被鐨勫姛鑳藉嵆鍙煡閬撹浣撶郴鐨勫熀鏈敤娉曘€傞偅涔堟兂瑕佷娇鐢ㄤ竴涓綋绯绘椂锛岄渶瑕佸缓绔嬪璞°€傚缓璁缓绔嬫渶瀛愮被瀵硅薄锛屽洜涓烘渶瀛愮被涓嶄粎鍙互浣跨敤鐖剁被涓殑鍔熻兘銆傝繕鍙互浣跨敤瀛愮被鐗规湁鐨勪竴浜涘姛鑳姐€?br>
绠€鍗曡锛氬浜庝竴涓户鎵夸綋绯荤殑浣跨敤锛屾煡闃呴《灞傜埗绫讳腑鐨勫唴瀹癸紝鍒涘缓鏈€搴曞眰瀛愮被鐨勫璞°€?
瀛愮埗绫诲嚭鐜板悗锛岀被涓殑鎴愬憳閮芥湁浜嗗摢浜涚壒鐐癸細
1锛氭垚鍛樺彉閲忋€?br> 褰撳瓙鐖剁被涓嚭鐜颁竴鏍风殑灞炴€ф椂锛屽瓙绫荤被鍨嬬殑瀵硅薄锛岃皟鐢ㄨ灞炴€э紝鍊兼槸瀛愮被鐨勫睘鎬у€笺€?br> 濡傛灉鎯宠璋冪敤鐖剁被涓殑灞炴€у€硷紝闇€瑕佷娇鐢ㄤ竴涓叧閿瓧锛歴uper
This锛氫唬琛ㄦ槸鏈被绫诲瀷鐨勫璞″紩鐢ㄣ€?br> Super锛氫唬琛ㄦ槸瀛愮被鎵€灞炵殑鐖剁被涓殑鍐呭瓨绌洪棿寮曠敤銆?br> 娉ㄦ剰锛氬瓙鐖剁被涓€氬父鏄笉浼氬嚭鐜板悓鍚嶆垚鍛樺彉閲忕殑锛屽洜涓虹埗绫讳腑鍙瀹氫箟浜嗭紝瀛愮被灏变笉鐢ㄥ湪瀹氫箟浜嗭紝鐩存帴缁ф壙杩囨潵鐢ㄥ氨鍙互浜嗐€?br>2锛氭垚鍛樺嚱鏁般€?br>褰撳瓙鐖剁被涓嚭鐜颁簡涓€妯′竴鏍风殑鏂规硶鏃讹紝寤虹珛瀛愮被瀵硅薄浼氳繍琛屽瓙绫讳腑鐨勬柟娉曘€傚ソ鍍忕埗绫讳腑鐨勬柟娉曡瑕嗙洊鎺変竴鏍枫€傛墍浠ヨ繖绉嶆儏鍐碉紝鏄嚱鏁扮殑鍙︿竴涓壒鎬э細瑕嗙洊(澶嶅啓锛岄噸鍐?
浠€涔堟椂鍊欎娇鐢ㄨ鐩栧憿锛熷綋涓€涓被鐨勫姛鑳藉唴瀹归渶瑕佷慨鏀规椂锛屽彲浠ラ€氳繃瑕嗙洊鏉ュ疄鐜般€?br>3锛氭瀯閫犲嚱鏁般€?br>鍙戠幇瀛愮被鏋勯€犲嚱鏁拌繍琛屾椂锛屽厛杩愯浜嗙埗绫荤殑鏋勯€犲嚱鏁般€備负浠€涔堝憿?
鍘熷洜锛氬瓙绫荤殑鎵€鏈夋瀯閫犲嚱鏁颁腑鐨勭涓€琛岋紝鍏跺疄閮芥湁涓€鏉¢殣韬殑璇彞super();
super(): 琛ㄧず鐖剁被鐨勬瀯閫犲嚱鏁帮紝骞朵細璋冪敤浜庡弬鏁扮浉瀵瑰簲鐨勭埗绫讳腑鐨勬瀯閫犲嚱鏁般€傝€宻uper():鏄湪璋冪敤鐖剁被涓┖鍙傛暟鐨勬瀯閫犲嚱鏁般€?br>涓轰粈涔堝瓙绫诲璞″垵濮嬪寲鏃讹紝閮介渶瑕佽皟鐢ㄧ埗绫讳腑鐨勫嚱鏁帮紵(涓轰粈涔堣鍦ㄥ瓙绫绘瀯閫犲嚱鏁扮殑绗竴琛屽姞鍏ヨ繖涓猻uper()?)
鍥犱负瀛愮被缁ф壙鐖剁被锛屼細缁ф壙鍒扮埗绫讳腑鐨勬暟鎹紝鎵€浠ュ繀椤昏鐪嬬埗绫绘槸濡備綍瀵硅嚜宸辩殑鏁版嵁杩涜鍒濆鍖栫殑銆傛墍浠ュ瓙绫诲湪杩涜瀵硅薄鍒濆鍖栨椂锛屽厛璋冪敤鐖剁被鐨勬瀯閫犲嚱鏁帮紝杩欏氨鏄瓙绫荤殑瀹炰緥鍖栬繃绋嬨€?br>
娉ㄦ剰锛氬瓙绫讳腑鎵€鏈夌殑鏋勯€犲嚱鏁伴兘浼氶粯璁よ闂埗绫讳腑鐨勭┖鍙傛暟鐨勬瀯閫犲嚱鏁帮紝鍥犱负姣忎竴涓瓙绫绘瀯閫犲唴绗竴琛岄兘鏈夐粯璁ょ殑璇彞super();
濡傛灉鐖剁被涓病鏈夌┖鍙傛暟鐨勬瀯閫犲嚱鏁帮紝閭d箞瀛愮被鐨勬瀯閫犲嚱鏁板唴锛屽繀椤婚€氳繃super璇彞鎸囧畾瑕佽闂殑鐖剁被涓殑鏋勯€犲嚱鏁般€?br>濡傛灉瀛愮被鏋勯€犲嚱鏁颁腑鐢╰his鏉ユ寚瀹氳皟鐢ㄥ瓙绫昏嚜宸辩殑鏋勯€犲嚱鏁帮紝閭d箞琚皟鐢ㄧ殑鏋勯€犲嚱鏁颁篃涓€鏍蜂細璁块棶鐖剁被涓殑鏋勯€犲嚱鏁般€?br>
闂锛歴uper()鍜宼his()鏄惁鍙互鍚屾椂鍑虹幇鐨勬瀯閫犲嚱鏁颁腑銆?br>涓や釜璇彞鍙兘鏈変竴涓畾涔夊湪绗竴琛岋紝鎵€浠ュ彧鑳藉嚭鐜板叾涓竴涓€?br>
super()鎴栬€卼his():涓轰粈涔堜竴瀹氳瀹氫箟鍦ㄧ涓€琛岋紵
鍥犱负super()鎴栬€卼his()閮芥槸璋冪敤鏋勯€犲嚱鏁帮紝鏋勯€犲嚱鏁扮敤浜庡垵濮嬪寲锛屾墍浠ュ垵濮嬪寲鐨勫姩浣滆鍏堝畬鎴愩€?br>
缁ф壙鐨勭粏鑺傦細
浠€涔堟椂鍊欎娇鐢ㄧ户鎵垮憿锛?br>褰撶被涓庣被涔嬮棿瀛樺湪鐫€鎵€灞炲叧绯绘椂锛屾墠鍏峰浜嗙户鎵跨殑鍓嶆彁銆俛鏄痓涓殑涓€绉嶃€俛缁ф壙b銆傜嫾鏄姮绉戜腑鐨勪竴绉嶃€?br>鑻辨枃涔︿腑锛屾墍灞炲叧绯伙細" is a "
娉ㄦ剰锛氫笉瑕佷粎浠呬负浜嗚幏鍙栧叾浠栫被涓殑宸叉湁鎴愬憳杩涜缁ф壙銆?br>
鎵€浠ュ垽鏂墍灞炲叧绯伙紝鍙互绠€鍗曠湅锛屽鏋滅户鎵垮悗锛岃缁ф壙鐨勭被涓殑鍔熻兘锛岄兘鍙互琚瀛愮被鎵€鍏峰锛岄偅涔堢户鎵挎垚绔嬨€傚鏋滀笉鏄紝涓嶅彲浠ョ户鎵裤€?br>
缁嗚妭浜岋細
鍦ㄦ柟娉曡鐩栨椂锛屾敞鎰忎袱鐐癸細
1锛氬瓙绫昏鐩栫埗绫绘椂锛屽繀椤昏淇濊瘉锛屽瓙绫绘柟娉曠殑鏉冮檺蹇呴』澶т簬绛変簬鐖剁被鏂规硶鏉冮檺鍙互瀹炵幇缁ф壙銆傚惁鍒欙紝缂栬瘧澶辫触銆?br>2锛氳鐩栨椂锛岃涔堥兘闈欐€侊紝瑕佷箞閮戒笉闈欐€併€?(闈欐€佸彧鑳借鐩栭潤鎬侊紝鎴栬€呰闈欐€佽鐩?
缁ф壙鐨勪竴涓紛绔細鎵撶牬浜嗗皝瑁呮€с€傚浜庝竴浜涚被锛屾垨鑰呯被涓姛鑳斤紝鏄渶瑕佽缁ф壙锛屾垨鑰呭鍐欑殑銆?br>杩欐椂濡備綍瑙e喅闂鍛紵浠嬬粛涓€涓叧閿瓧锛宖inal:鏈€缁堛€?br>
final鐗圭偣锛?br>1锛氳繖涓叧閿瓧鏄竴涓慨楗扮锛屽彲浠ヤ慨楗扮被锛屾柟娉曪紝鍙橀噺銆?br>2锛氳final淇グ鐨勭被鏄竴涓渶缁堢被锛屼笉鍙互琚户鎵裤€?br>3锛氳final淇グ鐨勬柟娉曟槸涓€涓渶缁堟柟娉曪紝涓嶅彲浠ヨ瑕嗙洊銆?br>4锛氳final淇グ鐨勫彉閲忔槸涓€涓父閲忥紝鍙兘璧嬪€间竴娆°€?br>
鍏跺疄杩欐牱鐨勫師鍥犵殑灏辨槸缁欎竴浜涘浐瀹氱殑鏁版嵁璧蜂釜闃呰鎬ц緝寮虹殑鍚嶇О銆?br> 涓嶅姞final淇グ涓嶆槸涔熷彲浠ヤ娇鐢ㄥ悧锛熼偅涔堣繖涓€兼槸涓€涓彉閲忥紝鏄彲浠ユ洿鏀圭殑銆傚姞浜唂inal锛岀▼搴忔洿涓轰弗璋ㄣ€傚父閲忓悕绉板畾涔夋椂锛屾湁瑙勮寖锛屾墍鏈夊瓧姣嶉兘澶у啓锛屽鏋滅敱澶氫釜鍗曡瘝缁勬垚锛屼腑闂寸敤 _ 杩炴帴銆?br>
鎶借薄绫? abstract
鎶借薄锛氫笉鍏蜂綋锛岀湅涓嶆槑鐧姐€傛娊璞$被琛ㄨ薄浣撶幇銆?br>鍦ㄤ笉鏂娊鍙栬繃绋嬩腑锛屽皢鍏辨€у唴瀹逛腑鐨勬柟娉曞0鏄庢娊鍙栵紝浣嗘槸鏂规硶涓嶄竴鏍凤紝娌℃湁鎶藉彇锛岃繖鏃舵娊鍙栧埌鐨勬柟娉曪紝骞朵笉鍏蜂綋锛岄渶瑕佽鎸囧畾鍏抽敭瀛梐bstract鎵€鏍囩ず锛屽0鏄庝负鎶借薄鏂规硶銆?br>鎶借薄鏂规硶鎵€鍦ㄧ被涓€瀹氳鏍囩ず涓烘娊璞$被锛屼篃灏辨槸璇磋绫婚渶瑕佽abstract鍏抽敭瀛楁墍淇グ銆?br>
鎶借薄绫荤殑鐗圭偣锛?/h3>
1锛氭娊璞℃柟娉曞彧鑳藉畾涔夊湪鎶借薄绫讳腑锛屾娊璞$被鍜屾娊璞℃柟娉曞繀椤荤敱abstract鍏抽敭瀛椾慨楗帮紙鍙互鎻忚堪绫诲拰鏂规硶锛屼笉鍙互鎻忚堪鍙橀噺锛夈€?br>2锛氭娊璞℃柟娉曞彧瀹氫箟鏂规硶澹版槑锛屽苟涓嶅畾涔夋柟娉曞疄鐜般€?br>3锛氭娊璞$被涓嶅彲浠ヨ鍒涘缓瀵硅薄(瀹炰緥鍖?銆?br>4锛氬彧鏈夐€氳繃瀛愮被缁ф壙鎶借薄绫诲苟瑕嗙洊浜嗘娊璞$被涓殑鎵€鏈夋娊璞℃柟娉曞悗锛岃瀛愮被鎵嶅彲浠ュ疄渚嬪寲銆傚惁鍒欙紝璇ュ瓙绫昏繕鏄竴涓娊璞$被銆?br>
鎶借薄绫荤殑缁嗚妭锛?/h4>
1锛氭娊璞$被涓槸鍚︽湁鏋勯€犲嚱鏁帮紵鏈夛紝鐢ㄤ簬缁欏瓙绫诲璞¤繘琛屽垵濮嬪寲銆?br>2锛氭娊璞$被涓槸鍚﹀彲浠ュ畾涔夐潪鎶借薄鏂规硶锛?br> 鍙互銆傚叾瀹烇紝鎶借薄绫诲拰涓€鑸被娌℃湁澶ぇ鐨勫尯鍒紝閮芥槸鍦ㄦ弿杩颁簨鐗╋紝鍙笉杩囨娊璞$被鍦ㄦ弿杩颁簨鐗╂椂锛屾湁浜涘姛鑳戒笉鍏蜂綋銆傛墍浠ユ娊璞$被鍜屼竴鑸被鍦ㄥ畾涔変笂锛岄兘鏄渶瑕佸畾涔夊睘鎬у拰琛屼负鐨勩€傚彧涓嶈繃锛屾瘮涓€鑸被澶氫簡涓€涓娊璞″嚱鏁般€傝€屼笖姣斾竴鑸被灏戜簡涓€涓垱寤哄璞$殑閮ㄥ垎銆?br>3锛氭娊璞″叧閿瓧abstract鍜屽摢浜涗笉鍙互鍏卞瓨锛焒inal , private , static
4锛氭娊璞$被涓彲涓嶅彲浠ヤ笉瀹氫箟鎶借薄鏂规硶锛熷彲浠ャ€傛娊璞℃柟娉曠洰鐨勪粎浠呬负浜嗕笉璁╄绫诲垱寤哄璞°€?br>-----------------------------------------------------------------------------------------------
妯℃澘鏂规硶璁捐妯″紡锛?/h3>
瑙e喅鐨勯棶棰橈細褰撳姛鑳藉唴閮ㄤ竴閮ㄥ垎瀹炵幇鏃剁‘瀹氾紝涓€閮ㄥ垎瀹炵幇鏄笉纭畾鐨勩€傝繖鏃跺彲浠ユ妸涓嶇‘瀹氱殑閮ㄥ垎鏆撮湶鍑哄幓锛岃瀛愮被鍘诲疄鐜般€?br>abstract class GetTime{
public final void getTime(){ //姝ゅ姛鑳藉鏋滀笉闇€瑕佸鍐欙紝鍙姞final闄愬畾
long start = System.currentTimeMillis();
code(); //涓嶇‘瀹氱殑鍔熻兘閮ㄥ垎锛屾彁鍙栧嚭鏉ワ紝閫氳繃鎶借薄鏂规硶瀹炵幇
long end = System.currentTimeMillis();
System.out.println("姣鏄細"+(end-start));
}
public abstract void code(); //鎶借薄涓嶇‘瀹氱殑鍔熻兘锛岃瀛愮被澶嶅啓瀹炵幇
}
class SubDemo extends GetTime{
public void code(){ //瀛愮被澶嶅啓鍔熻兘鏂规硶
for(int y=0; y<1000; y++){
System.out.println("y");
}
}
}
---------------------------------------------------------------------------------------------
鎺?鍙o細鈽呪槄鈽呪槄鈽?/h2>
1锛氭槸鐢ㄥ叧閿瓧interface瀹氫箟鐨勩€?br>2锛氭帴鍙d腑鍖呭惈鐨勬垚鍛橈紝鏈€甯歌鐨勬湁鍏ㄥ眬甯搁噺銆佹娊璞℃柟娉曘€?br>娉ㄦ剰锛氭帴鍙d腑鐨勬垚鍛橀兘鏈夊浐瀹氱殑淇グ绗︺€?br> 鎴愬憳鍙橀噺锛歱ublic static final
鎴愬憳鏂规硶锛歱ublic abstract
interface Inter{
public static final int x = 3;
public abstract void show();
}
3锛氭帴鍙d腑鏈夋娊璞℃柟娉曪紝璇存槑鎺ュ彛涓嶅彲浠ュ疄渚嬪寲銆傛帴鍙g殑瀛愮被蹇呴』瀹炵幇浜嗘帴鍙d腑鎵€鏈夌殑鎶借薄鏂规硶鍚庯紝璇ュ瓙绫绘墠鍙互瀹炰緥鍖栥€傚惁鍒欙紝璇ュ瓙绫昏繕鏄竴涓娊璞$被銆?br>4锛氱被涓庣被涔嬮棿瀛樺湪鐫€缁ф壙鍏崇郴锛岀被涓庢帴鍙d腑闂村瓨鍦ㄧ殑鏄疄鐜板叧绯汇€?br> 缁ф壙鐢╡xtends 锛涘疄鐜扮敤implements 锛?br>5锛氭帴鍙e拰绫讳笉涓€鏍风殑鍦版柟锛屽氨鏄紝鎺ュ彛鍙互琚瀹炵幇锛岃繖灏辨槸澶氱户鎵挎敼鑹悗鐨勭粨鏋溿€俲ava灏嗗缁ф壙鏈哄埗閫氳繃澶氱幇瀹炴潵浣撶幇銆?br>6锛氫竴涓被鍦ㄧ户鎵垮彟涓€涓被鐨勫悓鏃讹紝杩樺彲浠ュ疄鐜板涓帴鍙c€傛墍浠ユ帴鍙g殑鍑虹幇閬垮厤浜嗗崟缁ф壙鐨勫眬闄愭€с€傝繕鍙互灏嗙被杩涜鍔熻兘鐨勬墿灞曘€?br>7锛氬叾瀹瀓ava涓槸鏈夊缁ф壙鐨勩€傛帴鍙d笌鎺ュ彛涔嬮棿瀛樺湪鐫€缁ф壙鍏崇郴锛屾帴鍙e彲浠ュ缁ф壙鎺ュ彛銆?br>
鎺ュ彛閮界敤浜庤璁′笂锛岃璁′笂鐨勭壒鐐癸細锛堝彲浠ョ悊瑙d富鏉夸笂鎻愪緵鐨勬帴鍙o級
1锛氭帴鍙f槸瀵瑰鎻愪緵鐨勮鍒欍€?br>2锛氭帴鍙f槸鍔熻兘鐨勬墿灞曘€?br>3锛氭帴鍙g殑鍑虹幇闄嶄綆浜嗚€﹀悎鎬с€?br>
鎶借薄绫讳笌鎺ュ彛锛?/h3>
鎶借薄绫伙細涓€鑸敤浜庢弿杩颁竴涓綋绯诲崟鍏冿紝灏嗕竴缁勫叡鎬у唴瀹硅繘琛屾娊鍙栵紝鐗圭偣锛氬彲浠ュ湪绫讳腑瀹氫箟鎶借薄鍐呭璁╁瓙绫诲疄鐜帮紝鍙互瀹氫箟闈炴娊璞″唴瀹硅瀛愮被鐩存帴浣跨敤銆傚畠閲岄潰瀹氫箟鐨勯兘鏄竴浜涗綋绯讳腑鐨勫熀鏈唴瀹广€?br>鎺ュ彛锛氫竴鑸敤浜庡畾涔夊璞$殑鎵╁睍鍔熻兘锛屾槸鍦ㄧ户鎵夸箣澶栬繕闇€杩欎釜瀵硅薄鍏峰鐨勪竴浜涘姛鑳姐€?br>
鎶借薄绫诲拰鎺ュ彛鐨勫叡鎬э細閮芥槸涓嶆柇鍚戜笂鎶藉彇鐨勭粨鏋溿€?br>
鎶借薄绫诲拰鎺ュ彛鐨勫尯鍒細
1锛氭娊璞$被鍙兘琚户鎵匡紝鑰屼笖鍙兘鍗曠户鎵裤€?br>鎺ュ彛闇€瑕佽瀹炵幇锛岃€屼笖鍙互澶氬疄鐜般€?
2锛氭娊璞$被涓彲浠ュ畾涔夐潪鎶借薄鏂规硶锛屽瓙绫诲彲浠ョ洿鎺ョ户鎵夸娇鐢ㄣ€?br>鎺ュ彛涓兘鏈夋娊璞℃柟娉曪紝闇€瑕佸瓙绫诲幓瀹炵幇銆?br>3锛氭娊璞$被浣跨敤鐨勬槸 is a 鍏崇郴銆?br>鎺ュ彛浣跨敤鐨?like a 鍏崇郴銆?
4锛氭娊璞$被鐨勬垚鍛樹慨楗扮鍙互鑷畾涔夈€?br>鎺ュ彛涓殑鎴愬憳淇グ绗︽槸鍥哄畾鐨勩€傚叏閮芥槸public鐨勩€?br>
鍦ㄥ紑鍙戜箣鍓嶏紝鍏堝畾涔夎鍒欙紝A鍜孊鍒嗗埆寮€鍙戯紝A璐熻矗瀹炵幇杩欎釜瑙勫垯锛孊璐熻矗浣跨敤杩欎釜瑙勫垯銆傝嚦浜嶢鏄浣曞瑙勫垯鍏蜂綋瀹炵幇鐨勶紝B鏄笉闇€瑕佺煡閬撶殑銆傝繖鏍疯繖涓帴鍙g殑鍑虹幇灏遍檷浣庝簡A鍜孊鐩存帴鑰﹀悎鎬с€?br>
------------------------------------------------------------------------------------------------
澶?鎬佲槄鈽呪槄鈽呪槄
锛堥潰鍚戝璞$壒寰佷箣涓€锛夛細鍑芥暟鏈韩灏卞叿澶囧鎬佹€э紝鏌愪竴绉嶄簨鐗╂湁涓嶅悓鐨勫叿浣撶殑浣撶幇銆?br>
浣撶幇锛氱埗绫诲紩鐢ㄦ垨鑰呮帴鍙g殑寮曠敤鎸囧悜浜嗚嚜宸辩殑瀛愮被瀵硅薄銆?/Animal a = new Cat();
澶氭€佺殑濂藉锛氭彁楂樹簡绋嬪簭鐨勬墿灞曟€с€?br>澶氭€佺殑寮婄锛氬綋鐖剁被寮曠敤鎸囧悜瀛愮被瀵硅薄鏃讹紝铏界劧鎻愰珮浜嗘墿灞曟€э紝浣嗘槸鍙兘璁块棶鐖剁被涓叿澶囩殑鏂规硶锛屼笉鍙互璁块棶瀛愮被涓壒鏈夌殑鏂规硶銆?鍓嶆湡涓嶈兘浣跨敤鍚庢湡浜х敓鐨勫姛鑳斤紝鍗宠闂殑灞€闄愭€?
澶氭€佺殑鍓嶆彁锛?br> 1锛氬繀椤昏鏈夊叧绯伙紝姣斿缁ф壙銆佹垨鑰呭疄鐜般€?br> 2锛氶€氬父浼氭湁瑕嗙洊鎿嶄綔銆?br>
澶氭€佺殑鍑虹幇鎬濇兂涓婁篃鍋氱潃鍙樺寲锛氫互鍓嶆槸鍒涘缓瀵硅薄骞舵寚鎸ュ璞″仛浜嬫儏銆傛湁浜嗗鎬佷互鍚庯紝鎴戜滑鍙互鎵惧埌瀵硅薄鐨勫叡鎬х被鍨嬶紝鐩存帴鎿嶄綔鍏辨€х被鍨嬪仛浜嬫儏鍗冲彲锛岃繖鏍峰彲浠ユ寚鎸ヤ竴鎵瑰璞″仛浜嬫儏锛屽嵆閫氳繃鎿嶄綔鐖剁被鎴栨帴鍙e疄鐜般€?br>--------------------------------------------------------------
class teacher{
void teachs(){
System.out.println("浼佷笟绠$悊");
}
void play(){
System.out.println("閽撻奔");
}
}
class teacher extends teachers{
void 璁茶(){
System.out.println("JAVA");
}
void 鐪嬬數褰?){
System.out.println("鐪嬬數褰?);
}
}
class {
public static void main(String[] args) {
teachers x = new teacher(); //teacher瀵硅薄琚彁鍗囦负浜唗eahcers绫诲瀷銆?br>// x.璁茶();
// x.鐪嬬數褰?); //閿欒.
teacher y = (teachers)x; //灏唗eachers绫诲瀷寮哄埗杞崲鎴恡eacher绫诲瀷銆?br> y.鐪嬬數褰?);//鍦ㄥ鎬佷腑锛岃嚜濮嬭嚜缁堥兘鏄瓙绫诲璞″湪鍋氱潃绫诲瀷鐨勫彉鍖栥€?br> }
}
---------------------------------------------------------------
濡傛灉鎯崇敤瀛愮被瀵硅薄鐨勭壒鏈夋柟娉曪紝濡備綍鍒ゆ柇瀵硅薄鏄摢涓叿浣撶殑瀛愮被绫诲瀷鍛紵
鍙互鍙互閫氳繃涓€涓叧閿瓧 instanceof ;//鍒ゆ柇瀵硅薄鏄惁瀹炵幇浜嗘寚瀹氱殑鎺ュ彛鎴栫户鎵夸簡鎸囧畾鐨勭被
鏍煎紡锛?lt;瀵硅薄 instanceof 绫诲瀷> 锛屽垽鏂竴涓璞℃槸鍚︽墍灞炰簬鎸囧畾鐨勭被鍨嬨€?br>Student instanceof Person = true;//student缁ф壙浜唒erson绫?br>
澶氭€佸湪瀛愮埗绫讳腑鐨勬垚鍛樹笂鐨勪綋鐜扮殑鐗圭偣锛?/h3>
1锛屾垚鍛樺彉閲忥細鍦ㄥ鎬佷腑锛屽瓙鐖剁被鎴愬憳鍙橀噺鍚屽悕銆?br> 鍦ㄧ紪璇戞椂鏈燂細鍙傝€冪殑鏄紩鐢ㄥ瀷鍙橀噺鎵€灞炵殑绫讳腑鏄惁鏈夎皟鐢ㄧ殑鎴愬憳銆傦紙缂栬瘧鏃朵笉浜х敓瀵硅薄锛屽彧妫€鏌ヨ娉曢敊璇級
杩愯鏃舵湡锛氫篃鏄弬鑰冨紩鐢ㄥ瀷鍙橀噺鎵€灞炵殑绫讳腑鏄惁鏈夎皟鐢ㄧ殑鎴愬憳銆?br> 绠€鍗曚竴鍙ヨ瘽锛氭棤璁虹紪璇戝拰杩愯锛屾垚鍛樺彉閲忓弬鑰冪殑閮芥槸寮曠敤鍙橀噺鎵€灞炵殑绫讳腑鐨勬垚鍛樺彉閲忋€?br> 鍐嶈鐨勬洿瀹规槗璁板繂涓€浜涳細鎴愬憳鍙橀噺 --- 缂栬瘧杩愯閮界湅 = 宸﹁竟銆?br>2锛屾垚鍛樺嚱鏁般€?br> 缂栬瘧鏃舵湡锛氬弬鑰冨紩鐢ㄥ瀷鍙橀噺鎵€灞炵殑绫讳腑鏄惁鏈夎皟鐢ㄧ殑鏂规硶銆?br> 杩愯浜嬫儏锛氬弬鑰冪殑鏄璞℃墍灞炵殑绫讳腑鏄惁鏈夎皟鐢ㄧ殑鏂规硶銆?br> 涓轰粈涔堟槸杩欐牱鐨勫憿锛熷洜涓哄湪瀛愮埗绫讳腑锛屽浜庝竴妯′竴鏍风殑鎴愬憳鍑芥暟锛屾湁涓€涓壒鎬э細瑕嗙洊銆?br> 绠€鍗曚竴鍙ワ細鎴愬憳鍑芥暟锛岀紪璇戠湅寮曠敤鍨嬪彉閲忔墍灞炵殑绫伙紝杩愯鐪嬪璞℃墍灞炵殑绫汇€?br> 鏇寸畝鍗曪細鎴愬憳鍑芥暟 --- 缂栬瘧鐪?= 宸﹁竟锛岃繍琛岀湅 = 鍙宠竟銆?br>3锛岄潤鎬佸嚱鏁般€?
缂栬瘧鏃舵湡锛氬弬鑰冪殑鏄紩鐢ㄥ瀷鍙橀噺鎵€灞炵殑绫讳腑鏄惁鏈夎皟鐢ㄧ殑鎴愬憳銆?br> 杩愯鏃舵湡锛氫篃鏄弬鑰冨紩鐢ㄥ瀷鍙橀噺鎵€灞炵殑绫讳腑鏄惁鏈夎皟鐢ㄧ殑鎴愬憳銆?br> 涓轰粈涔堟槸杩欐牱鐨勫憿锛熷洜涓洪潤鎬佹柟娉曪紝鍏跺疄涓嶆墍灞炰簬瀵硅薄锛岃€屾槸鎵€灞炰簬璇ユ柟娉曟墍鍦ㄧ殑绫汇€?br> 璋冪敤闈欐€佺殑鏂规硶寮曠敤鏄摢涓被鐨勫紩鐢ㄨ皟鐢ㄧ殑灏辨槸鍝釜绫讳腑鐨勯潤鎬佹柟娉曘€?br> 绠€鍗曡锛氶潤鎬佸嚱鏁?--- 缂栬瘧杩愯閮界湅 = 宸﹁竟銆?br>-----------------------------------------------------------------------------------------------
java.lang.Object
Object锛氭墍鏈夌被鐨勭洿鎺ユ垨鑰呴棿鎺ョ埗绫伙紝Java璁や负鎵€鏈夌殑瀵硅薄閮藉叿澶囦竴浜涘熀鏈殑鍏辨€у唴瀹癸紝杩欎簺鍐呭鍙互涓嶆柇鐨勫悜涓婃娊鍙栵紝鏈€缁堝氨鎶藉彇鍒颁簡涓€涓渶椤跺眰鐨勭被涓殑锛岃绫讳腑瀹氫箟鐨勫氨鏄墍鏈夊璞¢兘鍏峰鐨勫姛鑳姐€?br>
鍏蜂綋鏂规硶锛?br>1锛宐oolean equals(Object obj)锛氱敤浜庢瘮杈冧袱涓璞℃槸鍚︾浉绛夛紝鍏跺疄鍐呴儴姣旇緝鐨勫氨鏄袱涓璞″湴鍧€銆?br>鑰屾牴鎹璞$殑灞炴€т笉鍚岋紝鍒ゆ柇瀵硅薄鏄惁鐩稿悓鐨勫叿浣撳唴瀹逛篃涓嶄竴鏍枫€傛墍浠ュ湪瀹氫箟绫绘椂锛屼竴鑸兘浼氬鍐檈quals鏂规硶锛屽缓绔嬫湰绫荤壒鏈夌殑鍒ゆ柇瀵硅薄鏄惁鐩稿悓鐨勪緷鎹€?br> public boolean equals(Object obj){
if(!(obj instanceof Person))
return false;
Person p = (Person)obj;
return this.age == p.age;
}
2锛孲tring toString()锛氬皢瀵硅薄鍙樻垚瀛楃涓诧紱榛樿杩斿洖鐨勬牸寮忥細绫诲悕@鍝堝笇鍊?= getClass().getName() + 鈥楡鈥?+ Integer.toHexString(hashCode())
涓轰簡瀵硅薄瀵瑰簲鐨勫瓧绗︿覆鍐呭鏈夋剰涔夛紝鍙互閫氳繃澶嶅啓锛屽缓绔嬭绫诲璞¤嚜宸辩壒鏈夌殑瀛楃涓茶〃鐜板舰寮忋€?br> public String toString(){
return "person : "+age;
}
3锛孋lass getClass()锛氳幏鍙栦换鎰忓璞¤繍琛屾椂鐨勬墍灞炲瓧鑺傜爜鏂囦欢瀵硅薄銆?br>4锛宨nt hashCode()锛氳繑鍥炶瀵硅薄鐨勫搱甯岀爜鍊笺€傛敮鎸佹鏂规硶鏄负浜嗘彁楂樺搱甯岃〃鐨勬€ц兘銆?br>
閫氬父equals锛宼oString锛宧ashCode锛屽湪搴旂敤涓兘浼氳澶嶅啓锛屽缓绔嬪叿浣撳璞$殑鐗规湁鐨勫唴瀹广€?br>------------------------------------------------------------------------------------------------
闈欐€佺殑娉ㄦ剰锛氶潤鎬佺殑鐢熷懡鍛ㄦ湡寰堥暱銆?br>闈欐€佷唬鐮佸潡锛氬氨鏄竴涓湁闈欐€佸叧閿瓧鏍囩ず鐨勪竴涓唬鐮佸潡鍖哄煙銆傚畾涔夊湪绫讳腑銆?br>浣滅敤锛氬彲浠ュ畬鎴愮被鐨勫垵濮嬪寲銆傞潤鎬佷唬鐮佸潡闅忕潃绫荤殑鍔犺浇鑰屾墽琛岋紝鑰屼笖鍙墽琛屼竴娆★紙new 澶氫釜瀵硅薄灏卞彧鎵ц涓€娆★級銆傚鏋滃拰涓诲嚱鏁板湪鍚屼竴绫讳腑锛屼紭鍏堜簬涓诲嚱鏁版墽琛屻€?br>
Public锛氳闂潈闄愭渶澶с€?br>static锛氫笉闇€瑕佸璞★紝鐩存帴绫诲悕鍗冲彲銆?br>void锛氫富鍑芥暟娌℃湁杩斿洖鍊笺€?br>Main锛氫富鍑芥暟鐗瑰畾鐨勫悕绉般€?br>(String[] args)锛氫富鍑芥暟鐨勫弬鏁帮紝鏄竴涓瓧绗︿覆鏁扮粍绫诲瀷鐨勫弬鏁帮紝jvm璋冪敤main鏂规硶鏃讹紝浼犻€掔殑瀹為檯鍙傛暟鏄?new String[0]銆?br>
jvm榛樿浼犻€掔殑鏄暱搴︿负0鐨勫瓧绗︿覆鏁扮粍锛屾垜浠湪杩愯璇ョ被鏃讹紝涔熷彲浠ユ寚瀹氬叿浣撶殑鍙傛暟杩涜浼犻€掋€傚彲浠ュ湪鎺у埗鍙帮紝杩愯璇ョ被鏃讹紝鍦ㄥ悗闈㈠姞鍏ュ弬鏁般€傚弬鏁颁箣闂撮€氳繃绌烘牸闅斿紑銆俲vm浼氳嚜鍔ㄥ皢杩欎簺瀛楃涓插弬鏁颁綔涓篴rgs鏁扮粍涓殑鍏冪礌锛岃繘琛屽瓨鍌ㄣ€?br>
闈欐€佷唬鐮佸潡銆佹瀯閫犱唬鐮佸潡銆佹瀯閫犲嚱鏁板悓鏃跺瓨鍦ㄦ椂鐨勬墽琛岄『搴忥細闈欐€佷唬鐮佸潡 脿 鏋勯€犱唬鐮佸潡 脿 鏋勯€犲嚱鏁帮紱
鐢熸垚Java甯姪鏂囨。锛氬懡浠ゆ牸寮忥細javadoc 鈥揹 鏂囦欢澶瑰悕 鈥揳uther 鈥搗ersion *.java
/** //鏍煎紡
*绫绘弿杩?br>*@author 浣滆€呭悕
*@version 鐗堟湰鍙?br>*/
/**
*鏂规硶鎻忚堪
*@param 鍙傛暟鎻忚堪
*@return 杩斿洖鍊兼弿杩?br>*/
---------------------------------------------------------------------------------------------
璁捐妯″紡锛氳В鍐抽棶棰樻渶琛屼箣鏈夋晥鐨勬€濇兂銆傛槸涓€濂楄鍙嶅浣跨敤銆佸鏁颁汉鐭ユ檽鐨勩€佺粡杩囧垎绫荤紪鐩殑銆佷唬鐮佽璁$粡楠岀殑鎬荤粨銆備娇鐢ㄨ璁℃ā寮忔槸涓轰簡鍙噸鐢ㄤ唬鐮併€佽浠g爜鏇村鏄撹浠栦汉鐞嗚В銆佷繚璇佷唬鐮佸彲闈犳€с€?br>
java涓湁23绉嶈璁℃ā寮忥細
鍗曚緥璁捐妯″紡锛氣槄鈽呪槄鈽呪槄
瑙e喅鐨勯棶棰橈細淇濊瘉涓€涓被鍦ㄥ唴瀛樹腑鐨勫璞″敮涓€鎬с€?br>姣斿锛氬绋嬪簭璇诲彇涓€涓厤缃枃浠舵椂锛屽缓璁厤缃枃浠跺皝瑁呮垚瀵硅薄銆備細鏂逛究鎿嶄綔鍏朵腑鏁版嵁锛屽張瑕佷繚璇佸涓▼搴忚鍒扮殑鏄悓涓€涓厤缃枃浠跺璞★紝灏遍渶瑕佽閰嶇疆鏂囦欢瀵硅薄鍦ㄥ唴瀛樹腑鏄敮涓€鐨勩€?br>
Runtime()鏂规硶灏辨槸鍗曚緥璁捐妯″紡杩涜璁捐鐨勩€?br>
濡備綍淇濊瘉瀵硅薄鍞竴鎬у憿锛?br>鎬濇兂锛?br>1锛屼笉璁╁叾浠栫▼搴忓垱寤鸿绫诲璞°€?br>2锛屽湪鏈被涓垱寤轰竴涓湰绫诲璞°€?br>3锛屽澶栨彁渚涙柟娉曪紝璁╁叾浠栫▼搴忚幏鍙栬繖涓璞°€?br>
姝ラ锛?br>1锛屽洜涓哄垱寤哄璞¢兘闇€瑕佹瀯閫犲嚱鏁板垵濮嬪寲锛屽彧瑕佸皢鏈被涓殑鏋勯€犲嚱鏁扮鏈夊寲锛屽叾浠栫▼搴忓氨鏃犳硶鍐嶅垱寤鸿绫诲璞★紱
2锛屽氨鍦ㄧ被涓垱寤轰竴涓湰绫荤殑瀵硅薄锛?br>3锛屽畾涔変竴涓柟娉曪紝杩斿洖璇ュ璞★紝璁╁叾浠栫▼搴忓彲浠ラ€氳繃鏂规硶灏卞緱鍒版湰绫诲璞°€傦紙浣滅敤锛氬彲鎺э級
浠g爜浣撶幇锛?br>1锛岀鏈夊寲鏋勯€犲嚱鏁帮紱
2锛屽垱寤虹鏈夊苟闈欐€佺殑鏈被瀵硅薄锛?br>3锛屽畾涔夊叕鏈夊苟闈欐€佺殑鏂规硶锛岃繑鍥炶瀵硅薄銆?br>---------------------------------------------
//楗挎眽寮?br>class Single{
private Single(){} //绉佹湁鍖栨瀯閫犲嚱鏁般€?br>private static Single s = new Single(); //鍒涘缓绉佹湁骞堕潤鎬佺殑鏈被瀵硅薄銆?br> public static Single getInstance(){ //瀹氫箟鍏湁骞堕潤鎬佺殑鏂规硶锛岃繑鍥炶瀵硅薄銆?br> return s;
}
}
---------------------------------------------
//鎳掓眽寮?寤惰繜鍔犺浇鏂瑰紡銆?br>class Single2{
private Single2(){}
private static Single2 s = null;
public static Single2 getInstance(){
if(s==null)
s = new Single2();
return s;
}
}
-------------------------------------------------------------------------------------------------
缁?鎵匡紙闈㈠悜瀵硅薄鐗瑰緛涔嬩竴锛?/h2>
濂藉锛?br>1锛氭彁楂樹簡浠g爜鐨勫鐢ㄦ€с€?br>2锛氳绫讳笌绫讳箣闂翠骇鐢熶簡鍏崇郴锛屾彁渚涗簡鍙︿竴涓壒寰佸鎬佺殑鍓嶆彁銆?br>
鐖剁被鐨勭敱鏉ワ細鍏跺疄鏄敱澶氫釜绫讳笉鏂悜涓婃娊鍙栧叡鎬у唴瀹硅€屾潵鐨勩€?br>java涓浜庣户鎵匡紝java鍙敮鎸佸崟缁ф壙銆俲ava铏界劧涓嶇洿鎺ユ敮鎸佸缁ф壙锛屼絾鏄繚鐣欎簡杩欑澶氱户鎵挎満鍒讹紝杩涜鏀硅壇銆?br>
鍗曠户鎵匡細涓€涓被鍙兘鏈変竴涓埗绫汇€?br>澶氱户鎵匡細涓€涓被鍙互鏈夊涓埗绫汇€?br>
涓轰粈涔堜笉鏀寔澶氱户鎵垮憿锛?br>鍥犱负褰撲竴涓被鍚屾椂缁ф壙涓や釜鐖剁被鏃讹紝涓や釜鐖剁被涓湁鐩稿悓鐨勫姛鑳斤紝閭d箞瀛愮被瀵硅薄璋冪敤璇ュ姛鑳芥椂锛岃繍琛屽摢涓€涓憿锛熷洜涓虹埗绫讳腑鐨勬柟娉曚腑瀛樺湪鏂规硶浣撱€?br>浣嗘槸java鏀寔澶氶噸缁ф壙銆侫缁ф壙B B缁ф壙C C缁ф壙D銆?br>澶氶噸缁ф壙鐨勫嚭鐜帮紝灏辨湁浜嗙户鎵夸綋绯汇€備綋绯讳腑鐨勯《灞傜埗绫绘槸閫氳繃涓嶆柇鍚戜笂鎶藉彇鑰屾潵鐨勩€傚畠閲岄潰瀹氫箟鐨勮浣撶郴鏈€鍩烘湰鏈€鍏辨€у唴瀹圭殑鍔熻兘銆?br>鎵€浠ワ紝涓€涓綋绯昏鎯宠浣跨敤锛岀洿鎺ユ煡闃呰绯荤粺涓殑鐖剁被鐨勫姛鑳藉嵆鍙煡閬撹浣撶郴鐨勫熀鏈敤娉曘€傞偅涔堟兂瑕佷娇鐢ㄤ竴涓綋绯绘椂锛岄渶瑕佸缓绔嬪璞°€傚缓璁缓绔嬫渶瀛愮被瀵硅薄锛屽洜涓烘渶瀛愮被涓嶄粎鍙互浣跨敤鐖剁被涓殑鍔熻兘銆傝繕鍙互浣跨敤瀛愮被鐗规湁鐨勪竴浜涘姛鑳姐€?br>
绠€鍗曡锛氬浜庝竴涓户鎵夸綋绯荤殑浣跨敤锛屾煡闃呴《灞傜埗绫讳腑鐨勫唴瀹癸紝鍒涘缓鏈€搴曞眰瀛愮被鐨勫璞°€?
瀛愮埗绫诲嚭鐜板悗锛岀被涓殑鎴愬憳閮芥湁浜嗗摢浜涚壒鐐癸細
1锛氭垚鍛樺彉閲忋€?br> 褰撳瓙鐖剁被涓嚭鐜颁竴鏍风殑灞炴€ф椂锛屽瓙绫荤被鍨嬬殑瀵硅薄锛岃皟鐢ㄨ灞炴€э紝鍊兼槸瀛愮被鐨勫睘鎬у€笺€?br> 濡傛灉鎯宠璋冪敤鐖剁被涓殑灞炴€у€硷紝闇€瑕佷娇鐢ㄤ竴涓叧閿瓧锛歴uper
This锛氫唬琛ㄦ槸鏈被绫诲瀷鐨勫璞″紩鐢ㄣ€?br> Super锛氫唬琛ㄦ槸瀛愮被鎵€灞炵殑鐖剁被涓殑鍐呭瓨绌洪棿寮曠敤銆?br> 娉ㄦ剰锛氬瓙鐖剁被涓€氬父鏄笉浼氬嚭鐜板悓鍚嶆垚鍛樺彉閲忕殑锛屽洜涓虹埗绫讳腑鍙瀹氫箟浜嗭紝瀛愮被灏变笉鐢ㄥ湪瀹氫箟浜嗭紝鐩存帴缁ф壙杩囨潵鐢ㄥ氨鍙互浜嗐€?br>2锛氭垚鍛樺嚱鏁般€?br>褰撳瓙鐖剁被涓嚭鐜颁簡涓€妯′竴鏍风殑鏂规硶鏃讹紝寤虹珛瀛愮被瀵硅薄浼氳繍琛屽瓙绫讳腑鐨勬柟娉曘€傚ソ鍍忕埗绫讳腑鐨勬柟娉曡瑕嗙洊鎺変竴鏍枫€傛墍浠ヨ繖绉嶆儏鍐碉紝鏄嚱鏁扮殑鍙︿竴涓壒鎬э細瑕嗙洊(澶嶅啓锛岄噸鍐?
浠€涔堟椂鍊欎娇鐢ㄨ鐩栧憿锛熷綋涓€涓被鐨勫姛鑳藉唴瀹归渶瑕佷慨鏀规椂锛屽彲浠ラ€氳繃瑕嗙洊鏉ュ疄鐜般€?br>3锛氭瀯閫犲嚱鏁般€?br>鍙戠幇瀛愮被鏋勯€犲嚱鏁拌繍琛屾椂锛屽厛杩愯浜嗙埗绫荤殑鏋勯€犲嚱鏁般€備负浠€涔堝憿?
鍘熷洜锛氬瓙绫荤殑鎵€鏈夋瀯閫犲嚱鏁颁腑鐨勭涓€琛岋紝鍏跺疄閮芥湁涓€鏉¢殣韬殑璇彞super();
super(): 琛ㄧず鐖剁被鐨勬瀯閫犲嚱鏁帮紝骞朵細璋冪敤浜庡弬鏁扮浉瀵瑰簲鐨勭埗绫讳腑鐨勬瀯閫犲嚱鏁般€傝€宻uper():鏄湪璋冪敤鐖剁被涓┖鍙傛暟鐨勬瀯閫犲嚱鏁般€?br>涓轰粈涔堝瓙绫诲璞″垵濮嬪寲鏃讹紝閮介渶瑕佽皟鐢ㄧ埗绫讳腑鐨勫嚱鏁帮紵(涓轰粈涔堣鍦ㄥ瓙绫绘瀯閫犲嚱鏁扮殑绗竴琛屽姞鍏ヨ繖涓猻uper()?)
鍥犱负瀛愮被缁ф壙鐖剁被锛屼細缁ф壙鍒扮埗绫讳腑鐨勬暟鎹紝鎵€浠ュ繀椤昏鐪嬬埗绫绘槸濡備綍瀵硅嚜宸辩殑鏁版嵁杩涜鍒濆鍖栫殑銆傛墍浠ュ瓙绫诲湪杩涜瀵硅薄鍒濆鍖栨椂锛屽厛璋冪敤鐖剁被鐨勬瀯閫犲嚱鏁帮紝杩欏氨鏄瓙绫荤殑瀹炰緥鍖栬繃绋嬨€?br>
娉ㄦ剰锛氬瓙绫讳腑鎵€鏈夌殑鏋勯€犲嚱鏁伴兘浼氶粯璁よ闂埗绫讳腑鐨勭┖鍙傛暟鐨勬瀯閫犲嚱鏁帮紝鍥犱负姣忎竴涓瓙绫绘瀯閫犲唴绗竴琛岄兘鏈夐粯璁ょ殑璇彞super();
濡傛灉鐖剁被涓病鏈夌┖鍙傛暟鐨勬瀯閫犲嚱鏁帮紝閭d箞瀛愮被鐨勬瀯閫犲嚱鏁板唴锛屽繀椤婚€氳繃super璇彞鎸囧畾瑕佽闂殑鐖剁被涓殑鏋勯€犲嚱鏁般€?br>濡傛灉瀛愮被鏋勯€犲嚱鏁颁腑鐢╰his鏉ユ寚瀹氳皟鐢ㄥ瓙绫昏嚜宸辩殑鏋勯€犲嚱鏁帮紝閭d箞琚皟鐢ㄧ殑鏋勯€犲嚱鏁颁篃涓€鏍蜂細璁块棶鐖剁被涓殑鏋勯€犲嚱鏁般€?br>
闂锛歴uper()鍜宼his()鏄惁鍙互鍚屾椂鍑虹幇鐨勬瀯閫犲嚱鏁颁腑銆?br>涓や釜璇彞鍙兘鏈変竴涓畾涔夊湪绗竴琛岋紝鎵€浠ュ彧鑳藉嚭鐜板叾涓竴涓€?br>
super()鎴栬€卼his():涓轰粈涔堜竴瀹氳瀹氫箟鍦ㄧ涓€琛岋紵
鍥犱负super()鎴栬€卼his()閮芥槸璋冪敤鏋勯€犲嚱鏁帮紝鏋勯€犲嚱鏁扮敤浜庡垵濮嬪寲锛屾墍浠ュ垵濮嬪寲鐨勫姩浣滆鍏堝畬鎴愩€?br>
缁ф壙鐨勭粏鑺傦細
浠€涔堟椂鍊欎娇鐢ㄧ户鎵垮憿锛?br>褰撶被涓庣被涔嬮棿瀛樺湪鐫€鎵€灞炲叧绯绘椂锛屾墠鍏峰浜嗙户鎵跨殑鍓嶆彁銆俛鏄痓涓殑涓€绉嶃€俛缁ф壙b銆傜嫾鏄姮绉戜腑鐨勪竴绉嶃€?br>鑻辨枃涔︿腑锛屾墍灞炲叧绯伙細" is a "
娉ㄦ剰锛氫笉瑕佷粎浠呬负浜嗚幏鍙栧叾浠栫被涓殑宸叉湁鎴愬憳杩涜缁ф壙銆?br>
鎵€浠ュ垽鏂墍灞炲叧绯伙紝鍙互绠€鍗曠湅锛屽鏋滅户鎵垮悗锛岃缁ф壙鐨勭被涓殑鍔熻兘锛岄兘鍙互琚瀛愮被鎵€鍏峰锛岄偅涔堢户鎵挎垚绔嬨€傚鏋滀笉鏄紝涓嶅彲浠ョ户鎵裤€?br>
缁嗚妭浜岋細
鍦ㄦ柟娉曡鐩栨椂锛屾敞鎰忎袱鐐癸細
1锛氬瓙绫昏鐩栫埗绫绘椂锛屽繀椤昏淇濊瘉锛屽瓙绫绘柟娉曠殑鏉冮檺蹇呴』澶т簬绛変簬鐖剁被鏂规硶鏉冮檺鍙互瀹炵幇缁ф壙銆傚惁鍒欙紝缂栬瘧澶辫触銆?br>2锛氳鐩栨椂锛岃涔堥兘闈欐€侊紝瑕佷箞閮戒笉闈欐€併€?(闈欐€佸彧鑳借鐩栭潤鎬侊紝鎴栬€呰闈欐€佽鐩?
缁ф壙鐨勪竴涓紛绔細鎵撶牬浜嗗皝瑁呮€с€傚浜庝竴浜涚被锛屾垨鑰呯被涓姛鑳斤紝鏄渶瑕佽缁ф壙锛屾垨鑰呭鍐欑殑銆?br>杩欐椂濡備綍瑙e喅闂鍛紵浠嬬粛涓€涓叧閿瓧锛宖inal:鏈€缁堛€?br>
final鐗圭偣锛?br>1锛氳繖涓叧閿瓧鏄竴涓慨楗扮锛屽彲浠ヤ慨楗扮被锛屾柟娉曪紝鍙橀噺銆?br>2锛氳final淇グ鐨勭被鏄竴涓渶缁堢被锛屼笉鍙互琚户鎵裤€?br>3锛氳final淇グ鐨勬柟娉曟槸涓€涓渶缁堟柟娉曪紝涓嶅彲浠ヨ瑕嗙洊銆?br>4锛氳final淇グ鐨勫彉閲忔槸涓€涓父閲忥紝鍙兘璧嬪€间竴娆°€?br>
鍏跺疄杩欐牱鐨勫師鍥犵殑灏辨槸缁欎竴浜涘浐瀹氱殑鏁版嵁璧蜂釜闃呰鎬ц緝寮虹殑鍚嶇О銆?br> 涓嶅姞final淇グ涓嶆槸涔熷彲浠ヤ娇鐢ㄥ悧锛熼偅涔堣繖涓€兼槸涓€涓彉閲忥紝鏄彲浠ユ洿鏀圭殑銆傚姞浜唂inal锛岀▼搴忔洿涓轰弗璋ㄣ€傚父閲忓悕绉板畾涔夋椂锛屾湁瑙勮寖锛屾墍鏈夊瓧姣嶉兘澶у啓锛屽鏋滅敱澶氫釜鍗曡瘝缁勬垚锛屼腑闂寸敤 _ 杩炴帴銆?br>
鎶借薄绫? abstract
鎶借薄锛氫笉鍏蜂綋锛岀湅涓嶆槑鐧姐€傛娊璞$被琛ㄨ薄浣撶幇銆?br>鍦ㄤ笉鏂娊鍙栬繃绋嬩腑锛屽皢鍏辨€у唴瀹逛腑鐨勬柟娉曞0鏄庢娊鍙栵紝浣嗘槸鏂规硶涓嶄竴鏍凤紝娌℃湁鎶藉彇锛岃繖鏃舵娊鍙栧埌鐨勬柟娉曪紝骞朵笉鍏蜂綋锛岄渶瑕佽鎸囧畾鍏抽敭瀛梐bstract鎵€鏍囩ず锛屽0鏄庝负鎶借薄鏂规硶銆?br>鎶借薄鏂规硶鎵€鍦ㄧ被涓€瀹氳鏍囩ず涓烘娊璞$被锛屼篃灏辨槸璇磋绫婚渶瑕佽abstract鍏抽敭瀛楁墍淇グ銆?br>
鎶借薄绫荤殑鐗圭偣锛?/h3>
1锛氭娊璞℃柟娉曞彧鑳藉畾涔夊湪鎶借薄绫讳腑锛屾娊璞$被鍜屾娊璞℃柟娉曞繀椤荤敱abstract鍏抽敭瀛椾慨楗帮紙鍙互鎻忚堪绫诲拰鏂规硶锛屼笉鍙互鎻忚堪鍙橀噺锛夈€?br>2锛氭娊璞℃柟娉曞彧瀹氫箟鏂规硶澹版槑锛屽苟涓嶅畾涔夋柟娉曞疄鐜般€?br>3锛氭娊璞$被涓嶅彲浠ヨ鍒涘缓瀵硅薄(瀹炰緥鍖?銆?br>4锛氬彧鏈夐€氳繃瀛愮被缁ф壙鎶借薄绫诲苟瑕嗙洊浜嗘娊璞$被涓殑鎵€鏈夋娊璞℃柟娉曞悗锛岃瀛愮被鎵嶅彲浠ュ疄渚嬪寲銆傚惁鍒欙紝璇ュ瓙绫昏繕鏄竴涓娊璞$被銆?br>
鎶借薄绫荤殑缁嗚妭锛?/h4>
1锛氭娊璞$被涓槸鍚︽湁鏋勯€犲嚱鏁帮紵鏈夛紝鐢ㄤ簬缁欏瓙绫诲璞¤繘琛屽垵濮嬪寲銆?br>2锛氭娊璞$被涓槸鍚﹀彲浠ュ畾涔夐潪鎶借薄鏂规硶锛?br> 鍙互銆傚叾瀹烇紝鎶借薄绫诲拰涓€鑸被娌℃湁澶ぇ鐨勫尯鍒紝閮芥槸鍦ㄦ弿杩颁簨鐗╋紝鍙笉杩囨娊璞$被鍦ㄦ弿杩颁簨鐗╂椂锛屾湁浜涘姛鑳戒笉鍏蜂綋銆傛墍浠ユ娊璞$被鍜屼竴鑸被鍦ㄥ畾涔変笂锛岄兘鏄渶瑕佸畾涔夊睘鎬у拰琛屼负鐨勩€傚彧涓嶈繃锛屾瘮涓€鑸被澶氫簡涓€涓娊璞″嚱鏁般€傝€屼笖姣斾竴鑸被灏戜簡涓€涓垱寤哄璞$殑閮ㄥ垎銆?br>3锛氭娊璞″叧閿瓧abstract鍜屽摢浜涗笉鍙互鍏卞瓨锛焒inal , private , static
4锛氭娊璞$被涓彲涓嶅彲浠ヤ笉瀹氫箟鎶借薄鏂规硶锛熷彲浠ャ€傛娊璞℃柟娉曠洰鐨勪粎浠呬负浜嗕笉璁╄绫诲垱寤哄璞°€?br>-----------------------------------------------------------------------------------------------
妯℃澘鏂规硶璁捐妯″紡锛?/h3>
瑙e喅鐨勯棶棰橈細褰撳姛鑳藉唴閮ㄤ竴閮ㄥ垎瀹炵幇鏃剁‘瀹氾紝涓€閮ㄥ垎瀹炵幇鏄笉纭畾鐨勩€傝繖鏃跺彲浠ユ妸涓嶇‘瀹氱殑閮ㄥ垎鏆撮湶鍑哄幓锛岃瀛愮被鍘诲疄鐜般€?br>abstract class GetTime{
public final void getTime(){ //姝ゅ姛鑳藉鏋滀笉闇€瑕佸鍐欙紝鍙姞final闄愬畾
long start = System.currentTimeMillis();
code(); //涓嶇‘瀹氱殑鍔熻兘閮ㄥ垎锛屾彁鍙栧嚭鏉ワ紝閫氳繃鎶借薄鏂规硶瀹炵幇
long end = System.currentTimeMillis();
System.out.println("姣鏄細"+(end-start));
}
public abstract void code(); //鎶借薄涓嶇‘瀹氱殑鍔熻兘锛岃瀛愮被澶嶅啓瀹炵幇
}
class SubDemo extends GetTime{
public void code(){ //瀛愮被澶嶅啓鍔熻兘鏂规硶
for(int y=0; y<1000; y++){
System.out.println("y");
}
}
}
---------------------------------------------------------------------------------------------
鎺?鍙o細鈽呪槄鈽呪槄鈽?/h2>
1锛氭槸鐢ㄥ叧閿瓧interface瀹氫箟鐨勩€?br>2锛氭帴鍙d腑鍖呭惈鐨勬垚鍛橈紝鏈€甯歌鐨勬湁鍏ㄥ眬甯搁噺銆佹娊璞℃柟娉曘€?br>娉ㄦ剰锛氭帴鍙d腑鐨勬垚鍛橀兘鏈夊浐瀹氱殑淇グ绗︺€?br> 鎴愬憳鍙橀噺锛歱ublic static final
鎴愬憳鏂规硶锛歱ublic abstract
interface Inter{
public static final int x = 3;
public abstract void show();
}
3锛氭帴鍙d腑鏈夋娊璞℃柟娉曪紝璇存槑鎺ュ彛涓嶅彲浠ュ疄渚嬪寲銆傛帴鍙g殑瀛愮被蹇呴』瀹炵幇浜嗘帴鍙d腑鎵€鏈夌殑鎶借薄鏂规硶鍚庯紝璇ュ瓙绫绘墠鍙互瀹炰緥鍖栥€傚惁鍒欙紝璇ュ瓙绫昏繕鏄竴涓娊璞$被銆?br>4锛氱被涓庣被涔嬮棿瀛樺湪鐫€缁ф壙鍏崇郴锛岀被涓庢帴鍙d腑闂村瓨鍦ㄧ殑鏄疄鐜板叧绯汇€?br> 缁ф壙鐢╡xtends 锛涘疄鐜扮敤implements 锛?br>5锛氭帴鍙e拰绫讳笉涓€鏍风殑鍦版柟锛屽氨鏄紝鎺ュ彛鍙互琚瀹炵幇锛岃繖灏辨槸澶氱户鎵挎敼鑹悗鐨勭粨鏋溿€俲ava灏嗗缁ф壙鏈哄埗閫氳繃澶氱幇瀹炴潵浣撶幇銆?br>6锛氫竴涓被鍦ㄧ户鎵垮彟涓€涓被鐨勫悓鏃讹紝杩樺彲浠ュ疄鐜板涓帴鍙c€傛墍浠ユ帴鍙g殑鍑虹幇閬垮厤浜嗗崟缁ф壙鐨勫眬闄愭€с€傝繕鍙互灏嗙被杩涜鍔熻兘鐨勬墿灞曘€?br>7锛氬叾瀹瀓ava涓槸鏈夊缁ф壙鐨勩€傛帴鍙d笌鎺ュ彛涔嬮棿瀛樺湪鐫€缁ф壙鍏崇郴锛屾帴鍙e彲浠ュ缁ф壙鎺ュ彛銆?br>
鎺ュ彛閮界敤浜庤璁′笂锛岃璁′笂鐨勭壒鐐癸細锛堝彲浠ョ悊瑙d富鏉夸笂鎻愪緵鐨勬帴鍙o級
1锛氭帴鍙f槸瀵瑰鎻愪緵鐨勮鍒欍€?br>2锛氭帴鍙f槸鍔熻兘鐨勬墿灞曘€?br>3锛氭帴鍙g殑鍑虹幇闄嶄綆浜嗚€﹀悎鎬с€?br>
鎶借薄绫讳笌鎺ュ彛锛?/h3>
鎶借薄绫伙細涓€鑸敤浜庢弿杩颁竴涓綋绯诲崟鍏冿紝灏嗕竴缁勫叡鎬у唴瀹硅繘琛屾娊鍙栵紝鐗圭偣锛氬彲浠ュ湪绫讳腑瀹氫箟鎶借薄鍐呭璁╁瓙绫诲疄鐜帮紝鍙互瀹氫箟闈炴娊璞″唴瀹硅瀛愮被鐩存帴浣跨敤銆傚畠閲岄潰瀹氫箟鐨勯兘鏄竴浜涗綋绯讳腑鐨勫熀鏈唴瀹广€?br>鎺ュ彛锛氫竴鑸敤浜庡畾涔夊璞$殑鎵╁睍鍔熻兘锛屾槸鍦ㄧ户鎵夸箣澶栬繕闇€杩欎釜瀵硅薄鍏峰鐨勪竴浜涘姛鑳姐€?br>
鎶借薄绫诲拰鎺ュ彛鐨勫叡鎬э細閮芥槸涓嶆柇鍚戜笂鎶藉彇鐨勭粨鏋溿€?br>
鎶借薄绫诲拰鎺ュ彛鐨勫尯鍒細
1锛氭娊璞$被鍙兘琚户鎵匡紝鑰屼笖鍙兘鍗曠户鎵裤€?br>鎺ュ彛闇€瑕佽瀹炵幇锛岃€屼笖鍙互澶氬疄鐜般€?
2锛氭娊璞$被涓彲浠ュ畾涔夐潪鎶借薄鏂规硶锛屽瓙绫诲彲浠ョ洿鎺ョ户鎵夸娇鐢ㄣ€?br>鎺ュ彛涓兘鏈夋娊璞℃柟娉曪紝闇€瑕佸瓙绫诲幓瀹炵幇銆?br>3锛氭娊璞$被浣跨敤鐨勬槸 is a 鍏崇郴銆?br>鎺ュ彛浣跨敤鐨?like a 鍏崇郴銆?
4锛氭娊璞$被鐨勬垚鍛樹慨楗扮鍙互鑷畾涔夈€?br>鎺ュ彛涓殑鎴愬憳淇グ绗︽槸鍥哄畾鐨勩€傚叏閮芥槸public鐨勩€?br>
鍦ㄥ紑鍙戜箣鍓嶏紝鍏堝畾涔夎鍒欙紝A鍜孊鍒嗗埆寮€鍙戯紝A璐熻矗瀹炵幇杩欎釜瑙勫垯锛孊璐熻矗浣跨敤杩欎釜瑙勫垯銆傝嚦浜嶢鏄浣曞瑙勫垯鍏蜂綋瀹炵幇鐨勶紝B鏄笉闇€瑕佺煡閬撶殑銆傝繖鏍疯繖涓帴鍙g殑鍑虹幇灏遍檷浣庝簡A鍜孊鐩存帴鑰﹀悎鎬с€?br>
------------------------------------------------------------------------------------------------
澶?鎬佲槄鈽呪槄鈽呪槄
锛堥潰鍚戝璞$壒寰佷箣涓€锛夛細鍑芥暟鏈韩灏卞叿澶囧鎬佹€э紝鏌愪竴绉嶄簨鐗╂湁涓嶅悓鐨勫叿浣撶殑浣撶幇銆?br>
浣撶幇锛氱埗绫诲紩鐢ㄦ垨鑰呮帴鍙g殑寮曠敤鎸囧悜浜嗚嚜宸辩殑瀛愮被瀵硅薄銆?/Animal a = new Cat();
澶氭€佺殑濂藉锛氭彁楂樹簡绋嬪簭鐨勬墿灞曟€с€?br>澶氭€佺殑寮婄锛氬綋鐖剁被寮曠敤鎸囧悜瀛愮被瀵硅薄鏃讹紝铏界劧鎻愰珮浜嗘墿灞曟€э紝浣嗘槸鍙兘璁块棶鐖剁被涓叿澶囩殑鏂规硶锛屼笉鍙互璁块棶瀛愮被涓壒鏈夌殑鏂规硶銆?鍓嶆湡涓嶈兘浣跨敤鍚庢湡浜х敓鐨勫姛鑳斤紝鍗宠闂殑灞€闄愭€?
澶氭€佺殑鍓嶆彁锛?br> 1锛氬繀椤昏鏈夊叧绯伙紝姣斿缁ф壙銆佹垨鑰呭疄鐜般€?br> 2锛氶€氬父浼氭湁瑕嗙洊鎿嶄綔銆?br>
澶氭€佺殑鍑虹幇鎬濇兂涓婁篃鍋氱潃鍙樺寲锛氫互鍓嶆槸鍒涘缓瀵硅薄骞舵寚鎸ュ璞″仛浜嬫儏銆傛湁浜嗗鎬佷互鍚庯紝鎴戜滑鍙互鎵惧埌瀵硅薄鐨勫叡鎬х被鍨嬶紝鐩存帴鎿嶄綔鍏辨€х被鍨嬪仛浜嬫儏鍗冲彲锛岃繖鏍峰彲浠ユ寚鎸ヤ竴鎵瑰璞″仛浜嬫儏锛屽嵆閫氳繃鎿嶄綔鐖剁被鎴栨帴鍙e疄鐜般€?br>--------------------------------------------------------------
class teacher{
void teachs(){
System.out.println("浼佷笟绠$悊");
}
void play(){
System.out.println("閽撻奔");
}
}
class teacher extends teachers{
void 璁茶(){
System.out.println("JAVA");
}
void 鐪嬬數褰?){
System.out.println("鐪嬬數褰?);
}
}
class {
public static void main(String[] args) {
teachers x = new teacher(); //teacher瀵硅薄琚彁鍗囦负浜唗eahcers绫诲瀷銆?br>// x.璁茶();
// x.鐪嬬數褰?); //閿欒.
teacher y = (teachers)x; //灏唗eachers绫诲瀷寮哄埗杞崲鎴恡eacher绫诲瀷銆?br> y.鐪嬬數褰?);//鍦ㄥ鎬佷腑锛岃嚜濮嬭嚜缁堥兘鏄瓙绫诲璞″湪鍋氱潃绫诲瀷鐨勫彉鍖栥€?br> }
}
---------------------------------------------------------------
濡傛灉鎯崇敤瀛愮被瀵硅薄鐨勭壒鏈夋柟娉曪紝濡備綍鍒ゆ柇瀵硅薄鏄摢涓叿浣撶殑瀛愮被绫诲瀷鍛紵
鍙互鍙互閫氳繃涓€涓叧閿瓧 instanceof ;//鍒ゆ柇瀵硅薄鏄惁瀹炵幇浜嗘寚瀹氱殑鎺ュ彛鎴栫户鎵夸簡鎸囧畾鐨勭被
鏍煎紡锛?lt;瀵硅薄 instanceof 绫诲瀷> 锛屽垽鏂竴涓璞℃槸鍚︽墍灞炰簬鎸囧畾鐨勭被鍨嬨€?br>Student instanceof Person = true;//student缁ф壙浜唒erson绫?br>
澶氭€佸湪瀛愮埗绫讳腑鐨勬垚鍛樹笂鐨勪綋鐜扮殑鐗圭偣锛?/h3>
1锛屾垚鍛樺彉閲忥細鍦ㄥ鎬佷腑锛屽瓙鐖剁被鎴愬憳鍙橀噺鍚屽悕銆?br> 鍦ㄧ紪璇戞椂鏈燂細鍙傝€冪殑鏄紩鐢ㄥ瀷鍙橀噺鎵€灞炵殑绫讳腑鏄惁鏈夎皟鐢ㄧ殑鎴愬憳銆傦紙缂栬瘧鏃朵笉浜х敓瀵硅薄锛屽彧妫€鏌ヨ娉曢敊璇級
杩愯鏃舵湡锛氫篃鏄弬鑰冨紩鐢ㄥ瀷鍙橀噺鎵€灞炵殑绫讳腑鏄惁鏈夎皟鐢ㄧ殑鎴愬憳銆?br> 绠€鍗曚竴鍙ヨ瘽锛氭棤璁虹紪璇戝拰杩愯锛屾垚鍛樺彉閲忓弬鑰冪殑閮芥槸寮曠敤鍙橀噺鎵€灞炵殑绫讳腑鐨勬垚鍛樺彉閲忋€?br> 鍐嶈鐨勬洿瀹规槗璁板繂涓€浜涳細鎴愬憳鍙橀噺 --- 缂栬瘧杩愯閮界湅 = 宸﹁竟銆?br>2锛屾垚鍛樺嚱鏁般€?br> 缂栬瘧鏃舵湡锛氬弬鑰冨紩鐢ㄥ瀷鍙橀噺鎵€灞炵殑绫讳腑鏄惁鏈夎皟鐢ㄧ殑鏂规硶銆?br> 杩愯浜嬫儏锛氬弬鑰冪殑鏄璞℃墍灞炵殑绫讳腑鏄惁鏈夎皟鐢ㄧ殑鏂规硶銆?br> 涓轰粈涔堟槸杩欐牱鐨勫憿锛熷洜涓哄湪瀛愮埗绫讳腑锛屽浜庝竴妯′竴鏍风殑鎴愬憳鍑芥暟锛屾湁涓€涓壒鎬э細瑕嗙洊銆?br> 绠€鍗曚竴鍙ワ細鎴愬憳鍑芥暟锛岀紪璇戠湅寮曠敤鍨嬪彉閲忔墍灞炵殑绫伙紝杩愯鐪嬪璞℃墍灞炵殑绫汇€?br> 鏇寸畝鍗曪細鎴愬憳鍑芥暟 --- 缂栬瘧鐪?= 宸﹁竟锛岃繍琛岀湅 = 鍙宠竟銆?br>3锛岄潤鎬佸嚱鏁般€?
缂栬瘧鏃舵湡锛氬弬鑰冪殑鏄紩鐢ㄥ瀷鍙橀噺鎵€灞炵殑绫讳腑鏄惁鏈夎皟鐢ㄧ殑鎴愬憳銆?br> 杩愯鏃舵湡锛氫篃鏄弬鑰冨紩鐢ㄥ瀷鍙橀噺鎵€灞炵殑绫讳腑鏄惁鏈夎皟鐢ㄧ殑鎴愬憳銆?br> 涓轰粈涔堟槸杩欐牱鐨勫憿锛熷洜涓洪潤鎬佹柟娉曪紝鍏跺疄涓嶆墍灞炰簬瀵硅薄锛岃€屾槸鎵€灞炰簬璇ユ柟娉曟墍鍦ㄧ殑绫汇€?br> 璋冪敤闈欐€佺殑鏂规硶寮曠敤鏄摢涓被鐨勫紩鐢ㄨ皟鐢ㄧ殑灏辨槸鍝釜绫讳腑鐨勯潤鎬佹柟娉曘€?br> 绠€鍗曡锛氶潤鎬佸嚱鏁?--- 缂栬瘧杩愯閮界湅 = 宸﹁竟銆?br>-----------------------------------------------------------------------------------------------
java.lang.Object
Object锛氭墍鏈夌被鐨勭洿鎺ユ垨鑰呴棿鎺ョ埗绫伙紝Java璁や负鎵€鏈夌殑瀵硅薄閮藉叿澶囦竴浜涘熀鏈殑鍏辨€у唴瀹癸紝杩欎簺鍐呭鍙互涓嶆柇鐨勫悜涓婃娊鍙栵紝鏈€缁堝氨鎶藉彇鍒颁簡涓€涓渶椤跺眰鐨勭被涓殑锛岃绫讳腑瀹氫箟鐨勫氨鏄墍鏈夊璞¢兘鍏峰鐨勫姛鑳姐€?br>
鍏蜂綋鏂规硶锛?br>1锛宐oolean equals(Object obj)锛氱敤浜庢瘮杈冧袱涓璞℃槸鍚︾浉绛夛紝鍏跺疄鍐呴儴姣旇緝鐨勫氨鏄袱涓璞″湴鍧€銆?br>鑰屾牴鎹璞$殑灞炴€т笉鍚岋紝鍒ゆ柇瀵硅薄鏄惁鐩稿悓鐨勫叿浣撳唴瀹逛篃涓嶄竴鏍枫€傛墍浠ュ湪瀹氫箟绫绘椂锛屼竴鑸兘浼氬鍐檈quals鏂规硶锛屽缓绔嬫湰绫荤壒鏈夌殑鍒ゆ柇瀵硅薄鏄惁鐩稿悓鐨勪緷鎹€?br> public boolean equals(Object obj){
if(!(obj instanceof Person))
return false;
Person p = (Person)obj;
return this.age == p.age;
}
2锛孲tring toString()锛氬皢瀵硅薄鍙樻垚瀛楃涓诧紱榛樿杩斿洖鐨勬牸寮忥細绫诲悕@鍝堝笇鍊?= getClass().getName() + 鈥楡鈥?+ Integer.toHexString(hashCode())
涓轰簡瀵硅薄瀵瑰簲鐨勫瓧绗︿覆鍐呭鏈夋剰涔夛紝鍙互閫氳繃澶嶅啓锛屽缓绔嬭绫诲璞¤嚜宸辩壒鏈夌殑瀛楃涓茶〃鐜板舰寮忋€?br> public String toString(){
return "person : "+age;
}
3锛孋lass getClass()锛氳幏鍙栦换鎰忓璞¤繍琛屾椂鐨勬墍灞炲瓧鑺傜爜鏂囦欢瀵硅薄銆?br>4锛宨nt hashCode()锛氳繑鍥炶瀵硅薄鐨勫搱甯岀爜鍊笺€傛敮鎸佹鏂规硶鏄负浜嗘彁楂樺搱甯岃〃鐨勬€ц兘銆?br>
閫氬父equals锛宼oString锛宧ashCode锛屽湪搴旂敤涓兘浼氳澶嶅啓锛屽缓绔嬪叿浣撳璞$殑鐗规湁鐨勫唴瀹广€?br>------------------------------------------------------------------------------------------------
鎶借薄绫荤殑缁嗚妭锛?/h4>
1锛氭娊璞$被涓槸鍚︽湁鏋勯€犲嚱鏁帮紵鏈夛紝鐢ㄤ簬缁欏瓙绫诲璞¤繘琛屽垵濮嬪寲銆?br>2锛氭娊璞$被涓槸鍚﹀彲浠ュ畾涔夐潪鎶借薄鏂规硶锛?br> 鍙互銆傚叾瀹烇紝鎶借薄绫诲拰涓€鑸被娌℃湁澶ぇ鐨勫尯鍒紝閮芥槸鍦ㄦ弿杩颁簨鐗╋紝鍙笉杩囨娊璞$被鍦ㄦ弿杩颁簨鐗╂椂锛屾湁浜涘姛鑳戒笉鍏蜂綋銆傛墍浠ユ娊璞$被鍜屼竴鑸被鍦ㄥ畾涔変笂锛岄兘鏄渶瑕佸畾涔夊睘鎬у拰琛屼负鐨勩€傚彧涓嶈繃锛屾瘮涓€鑸被澶氫簡涓€涓娊璞″嚱鏁般€傝€屼笖姣斾竴鑸被灏戜簡涓€涓垱寤哄璞$殑閮ㄥ垎銆?br>3锛氭娊璞″叧閿瓧abstract鍜屽摢浜涗笉鍙互鍏卞瓨锛焒inal , private , static
4锛氭娊璞$被涓彲涓嶅彲浠ヤ笉瀹氫箟鎶借薄鏂规硶锛熷彲浠ャ€傛娊璞℃柟娉曠洰鐨勪粎浠呬负浜嗕笉璁╄绫诲垱寤哄璞°€?br>-----------------------------------------------------------------------------------------------
妯℃澘鏂规硶璁捐妯″紡锛?/h3>
瑙e喅鐨勯棶棰橈細褰撳姛鑳藉唴閮ㄤ竴閮ㄥ垎瀹炵幇鏃剁‘瀹氾紝涓€閮ㄥ垎瀹炵幇鏄笉纭畾鐨勩€傝繖鏃跺彲浠ユ妸涓嶇‘瀹氱殑閮ㄥ垎鏆撮湶鍑哄幓锛岃瀛愮被鍘诲疄鐜般€?br>abstract class GetTime{
public final void getTime(){ //姝ゅ姛鑳藉鏋滀笉闇€瑕佸鍐欙紝鍙姞final闄愬畾
long start = System.currentTimeMillis();
code(); //涓嶇‘瀹氱殑鍔熻兘閮ㄥ垎锛屾彁鍙栧嚭鏉ワ紝閫氳繃鎶借薄鏂规硶瀹炵幇
long end = System.currentTimeMillis();
System.out.println("姣鏄細"+(end-start));
}
public abstract void code(); //鎶借薄涓嶇‘瀹氱殑鍔熻兘锛岃瀛愮被澶嶅啓瀹炵幇
}
class SubDemo extends GetTime{
public void code(){ //瀛愮被澶嶅啓鍔熻兘鏂规硶
for(int y=0; y<1000; y++){
System.out.println("y");
}
}
}
---------------------------------------------------------------------------------------------
鎺?鍙o細鈽呪槄鈽呪槄鈽?/h2>
1锛氭槸鐢ㄥ叧閿瓧interface瀹氫箟鐨勩€?br>2锛氭帴鍙d腑鍖呭惈鐨勬垚鍛橈紝鏈€甯歌鐨勬湁鍏ㄥ眬甯搁噺銆佹娊璞℃柟娉曘€?br>娉ㄦ剰锛氭帴鍙d腑鐨勬垚鍛橀兘鏈夊浐瀹氱殑淇グ绗︺€?br> 鎴愬憳鍙橀噺锛歱ublic static final
鎴愬憳鏂规硶锛歱ublic abstract
interface Inter{
public static final int x = 3;
public abstract void show();
}
3锛氭帴鍙d腑鏈夋娊璞℃柟娉曪紝璇存槑鎺ュ彛涓嶅彲浠ュ疄渚嬪寲銆傛帴鍙g殑瀛愮被蹇呴』瀹炵幇浜嗘帴鍙d腑鎵€鏈夌殑鎶借薄鏂规硶鍚庯紝璇ュ瓙绫绘墠鍙互瀹炰緥鍖栥€傚惁鍒欙紝璇ュ瓙绫昏繕鏄竴涓娊璞$被銆?br>4锛氱被涓庣被涔嬮棿瀛樺湪鐫€缁ф壙鍏崇郴锛岀被涓庢帴鍙d腑闂村瓨鍦ㄧ殑鏄疄鐜板叧绯汇€?br> 缁ф壙鐢╡xtends 锛涘疄鐜扮敤implements 锛?br>5锛氭帴鍙e拰绫讳笉涓€鏍风殑鍦版柟锛屽氨鏄紝鎺ュ彛鍙互琚瀹炵幇锛岃繖灏辨槸澶氱户鎵挎敼鑹悗鐨勭粨鏋溿€俲ava灏嗗缁ф壙鏈哄埗閫氳繃澶氱幇瀹炴潵浣撶幇銆?br>6锛氫竴涓被鍦ㄧ户鎵垮彟涓€涓被鐨勫悓鏃讹紝杩樺彲浠ュ疄鐜板涓帴鍙c€傛墍浠ユ帴鍙g殑鍑虹幇閬垮厤浜嗗崟缁ф壙鐨勫眬闄愭€с€傝繕鍙互灏嗙被杩涜鍔熻兘鐨勬墿灞曘€?br>7锛氬叾瀹瀓ava涓槸鏈夊缁ф壙鐨勩€傛帴鍙d笌鎺ュ彛涔嬮棿瀛樺湪鐫€缁ф壙鍏崇郴锛屾帴鍙e彲浠ュ缁ф壙鎺ュ彛銆?br>
鎺ュ彛閮界敤浜庤璁′笂锛岃璁′笂鐨勭壒鐐癸細锛堝彲浠ョ悊瑙d富鏉夸笂鎻愪緵鐨勬帴鍙o級
1锛氭帴鍙f槸瀵瑰鎻愪緵鐨勮鍒欍€?br>2锛氭帴鍙f槸鍔熻兘鐨勬墿灞曘€?br>3锛氭帴鍙g殑鍑虹幇闄嶄綆浜嗚€﹀悎鎬с€?br>
鎶借薄绫讳笌鎺ュ彛锛?/h3>
鎶借薄绫伙細涓€鑸敤浜庢弿杩颁竴涓綋绯诲崟鍏冿紝灏嗕竴缁勫叡鎬у唴瀹硅繘琛屾娊鍙栵紝鐗圭偣锛氬彲浠ュ湪绫讳腑瀹氫箟鎶借薄鍐呭璁╁瓙绫诲疄鐜帮紝鍙互瀹氫箟闈炴娊璞″唴瀹硅瀛愮被鐩存帴浣跨敤銆傚畠閲岄潰瀹氫箟鐨勯兘鏄竴浜涗綋绯讳腑鐨勫熀鏈唴瀹广€?br>鎺ュ彛锛氫竴鑸敤浜庡畾涔夊璞$殑鎵╁睍鍔熻兘锛屾槸鍦ㄧ户鎵夸箣澶栬繕闇€杩欎釜瀵硅薄鍏峰鐨勪竴浜涘姛鑳姐€?br>
鎶借薄绫诲拰鎺ュ彛鐨勫叡鎬э細閮芥槸涓嶆柇鍚戜笂鎶藉彇鐨勭粨鏋溿€?br>
鎶借薄绫诲拰鎺ュ彛鐨勫尯鍒細
1锛氭娊璞$被鍙兘琚户鎵匡紝鑰屼笖鍙兘鍗曠户鎵裤€?br>鎺ュ彛闇€瑕佽瀹炵幇锛岃€屼笖鍙互澶氬疄鐜般€?
2锛氭娊璞$被涓彲浠ュ畾涔夐潪鎶借薄鏂规硶锛屽瓙绫诲彲浠ョ洿鎺ョ户鎵夸娇鐢ㄣ€?br>鎺ュ彛涓兘鏈夋娊璞℃柟娉曪紝闇€瑕佸瓙绫诲幓瀹炵幇銆?br>3锛氭娊璞$被浣跨敤鐨勬槸 is a 鍏崇郴銆?br>鎺ュ彛浣跨敤鐨?like a 鍏崇郴銆?
4锛氭娊璞$被鐨勬垚鍛樹慨楗扮鍙互鑷畾涔夈€?br>鎺ュ彛涓殑鎴愬憳淇グ绗︽槸鍥哄畾鐨勩€傚叏閮芥槸public鐨勩€?br>
鍦ㄥ紑鍙戜箣鍓嶏紝鍏堝畾涔夎鍒欙紝A鍜孊鍒嗗埆寮€鍙戯紝A璐熻矗瀹炵幇杩欎釜瑙勫垯锛孊璐熻矗浣跨敤杩欎釜瑙勫垯銆傝嚦浜嶢鏄浣曞瑙勫垯鍏蜂綋瀹炵幇鐨勶紝B鏄笉闇€瑕佺煡閬撶殑銆傝繖鏍疯繖涓帴鍙g殑鍑虹幇灏遍檷浣庝簡A鍜孊鐩存帴鑰﹀悎鎬с€?br>
------------------------------------------------------------------------------------------------
澶?鎬佲槄鈽呪槄鈽呪槄
锛堥潰鍚戝璞$壒寰佷箣涓€锛夛細鍑芥暟鏈韩灏卞叿澶囧鎬佹€э紝鏌愪竴绉嶄簨鐗╂湁涓嶅悓鐨勫叿浣撶殑浣撶幇銆?br>
浣撶幇锛氱埗绫诲紩鐢ㄦ垨鑰呮帴鍙g殑寮曠敤鎸囧悜浜嗚嚜宸辩殑瀛愮被瀵硅薄銆?/Animal a = new Cat();
澶氭€佺殑濂藉锛氭彁楂樹簡绋嬪簭鐨勬墿灞曟€с€?br>澶氭€佺殑寮婄锛氬綋鐖剁被寮曠敤鎸囧悜瀛愮被瀵硅薄鏃讹紝铏界劧鎻愰珮浜嗘墿灞曟€э紝浣嗘槸鍙兘璁块棶鐖剁被涓叿澶囩殑鏂规硶锛屼笉鍙互璁块棶瀛愮被涓壒鏈夌殑鏂规硶銆?鍓嶆湡涓嶈兘浣跨敤鍚庢湡浜х敓鐨勫姛鑳斤紝鍗宠闂殑灞€闄愭€?
澶氭€佺殑鍓嶆彁锛?br> 1锛氬繀椤昏鏈夊叧绯伙紝姣斿缁ф壙銆佹垨鑰呭疄鐜般€?br> 2锛氶€氬父浼氭湁瑕嗙洊鎿嶄綔銆?br>
澶氭€佺殑鍑虹幇鎬濇兂涓婁篃鍋氱潃鍙樺寲锛氫互鍓嶆槸鍒涘缓瀵硅薄骞舵寚鎸ュ璞″仛浜嬫儏銆傛湁浜嗗鎬佷互鍚庯紝鎴戜滑鍙互鎵惧埌瀵硅薄鐨勫叡鎬х被鍨嬶紝鐩存帴鎿嶄綔鍏辨€х被鍨嬪仛浜嬫儏鍗冲彲锛岃繖鏍峰彲浠ユ寚鎸ヤ竴鎵瑰璞″仛浜嬫儏锛屽嵆閫氳繃鎿嶄綔鐖剁被鎴栨帴鍙e疄鐜般€?br>--------------------------------------------------------------
class teacher{
void teachs(){
System.out.println("浼佷笟绠$悊");
}
void play(){
System.out.println("閽撻奔");
}
}
class teacher extends teachers{
void 璁茶(){
System.out.println("JAVA");
}
void 鐪嬬數褰?){
System.out.println("鐪嬬數褰?);
}
}
class {
public static void main(String[] args) {
teachers x = new teacher(); //teacher瀵硅薄琚彁鍗囦负浜唗eahcers绫诲瀷銆?br>// x.璁茶();
// x.鐪嬬數褰?); //閿欒.
teacher y = (teachers)x; //灏唗eachers绫诲瀷寮哄埗杞崲鎴恡eacher绫诲瀷銆?br> y.鐪嬬數褰?);//鍦ㄥ鎬佷腑锛岃嚜濮嬭嚜缁堥兘鏄瓙绫诲璞″湪鍋氱潃绫诲瀷鐨勫彉鍖栥€?br> }
}
---------------------------------------------------------------
濡傛灉鎯崇敤瀛愮被瀵硅薄鐨勭壒鏈夋柟娉曪紝濡備綍鍒ゆ柇瀵硅薄鏄摢涓叿浣撶殑瀛愮被绫诲瀷鍛紵
鍙互鍙互閫氳繃涓€涓叧閿瓧 instanceof ;//鍒ゆ柇瀵硅薄鏄惁瀹炵幇浜嗘寚瀹氱殑鎺ュ彛鎴栫户鎵夸簡鎸囧畾鐨勭被
鏍煎紡锛?lt;瀵硅薄 instanceof 绫诲瀷> 锛屽垽鏂竴涓璞℃槸鍚︽墍灞炰簬鎸囧畾鐨勭被鍨嬨€?br>Student instanceof Person = true;//student缁ф壙浜唒erson绫?br>
澶氭€佸湪瀛愮埗绫讳腑鐨勬垚鍛樹笂鐨勪綋鐜扮殑鐗圭偣锛?/h3>
1锛屾垚鍛樺彉閲忥細鍦ㄥ鎬佷腑锛屽瓙鐖剁被鎴愬憳鍙橀噺鍚屽悕銆?br> 鍦ㄧ紪璇戞椂鏈燂細鍙傝€冪殑鏄紩鐢ㄥ瀷鍙橀噺鎵€灞炵殑绫讳腑鏄惁鏈夎皟鐢ㄧ殑鎴愬憳銆傦紙缂栬瘧鏃朵笉浜х敓瀵硅薄锛屽彧妫€鏌ヨ娉曢敊璇級
杩愯鏃舵湡锛氫篃鏄弬鑰冨紩鐢ㄥ瀷鍙橀噺鎵€灞炵殑绫讳腑鏄惁鏈夎皟鐢ㄧ殑鎴愬憳銆?br> 绠€鍗曚竴鍙ヨ瘽锛氭棤璁虹紪璇戝拰杩愯锛屾垚鍛樺彉閲忓弬鑰冪殑閮芥槸寮曠敤鍙橀噺鎵€灞炵殑绫讳腑鐨勬垚鍛樺彉閲忋€?br> 鍐嶈鐨勬洿瀹规槗璁板繂涓€浜涳細鎴愬憳鍙橀噺 --- 缂栬瘧杩愯閮界湅 = 宸﹁竟銆?br>2锛屾垚鍛樺嚱鏁般€?br> 缂栬瘧鏃舵湡锛氬弬鑰冨紩鐢ㄥ瀷鍙橀噺鎵€灞炵殑绫讳腑鏄惁鏈夎皟鐢ㄧ殑鏂规硶銆?br> 杩愯浜嬫儏锛氬弬鑰冪殑鏄璞℃墍灞炵殑绫讳腑鏄惁鏈夎皟鐢ㄧ殑鏂规硶銆?br> 涓轰粈涔堟槸杩欐牱鐨勫憿锛熷洜涓哄湪瀛愮埗绫讳腑锛屽浜庝竴妯′竴鏍风殑鎴愬憳鍑芥暟锛屾湁涓€涓壒鎬э細瑕嗙洊銆?br> 绠€鍗曚竴鍙ワ細鎴愬憳鍑芥暟锛岀紪璇戠湅寮曠敤鍨嬪彉閲忔墍灞炵殑绫伙紝杩愯鐪嬪璞℃墍灞炵殑绫汇€?br> 鏇寸畝鍗曪細鎴愬憳鍑芥暟 --- 缂栬瘧鐪?= 宸﹁竟锛岃繍琛岀湅 = 鍙宠竟銆?br>3锛岄潤鎬佸嚱鏁般€?
缂栬瘧鏃舵湡锛氬弬鑰冪殑鏄紩鐢ㄥ瀷鍙橀噺鎵€灞炵殑绫讳腑鏄惁鏈夎皟鐢ㄧ殑鎴愬憳銆?br> 杩愯鏃舵湡锛氫篃鏄弬鑰冨紩鐢ㄥ瀷鍙橀噺鎵€灞炵殑绫讳腑鏄惁鏈夎皟鐢ㄧ殑鎴愬憳銆?br> 涓轰粈涔堟槸杩欐牱鐨勫憿锛熷洜涓洪潤鎬佹柟娉曪紝鍏跺疄涓嶆墍灞炰簬瀵硅薄锛岃€屾槸鎵€灞炰簬璇ユ柟娉曟墍鍦ㄧ殑绫汇€?br> 璋冪敤闈欐€佺殑鏂规硶寮曠敤鏄摢涓被鐨勫紩鐢ㄨ皟鐢ㄧ殑灏辨槸鍝釜绫讳腑鐨勯潤鎬佹柟娉曘€?br> 绠€鍗曡锛氶潤鎬佸嚱鏁?--- 缂栬瘧杩愯閮界湅 = 宸﹁竟銆?br>-----------------------------------------------------------------------------------------------
java.lang.Object
Object锛氭墍鏈夌被鐨勭洿鎺ユ垨鑰呴棿鎺ョ埗绫伙紝Java璁や负鎵€鏈夌殑瀵硅薄閮藉叿澶囦竴浜涘熀鏈殑鍏辨€у唴瀹癸紝杩欎簺鍐呭鍙互涓嶆柇鐨勫悜涓婃娊鍙栵紝鏈€缁堝氨鎶藉彇鍒颁簡涓€涓渶椤跺眰鐨勭被涓殑锛岃绫讳腑瀹氫箟鐨勫氨鏄墍鏈夊璞¢兘鍏峰鐨勫姛鑳姐€?br>
鍏蜂綋鏂规硶锛?br>1锛宐oolean equals(Object obj)锛氱敤浜庢瘮杈冧袱涓璞℃槸鍚︾浉绛夛紝鍏跺疄鍐呴儴姣旇緝鐨勫氨鏄袱涓璞″湴鍧€銆?br>鑰屾牴鎹璞$殑灞炴€т笉鍚岋紝鍒ゆ柇瀵硅薄鏄惁鐩稿悓鐨勫叿浣撳唴瀹逛篃涓嶄竴鏍枫€傛墍浠ュ湪瀹氫箟绫绘椂锛屼竴鑸兘浼氬鍐檈quals鏂规硶锛屽缓绔嬫湰绫荤壒鏈夌殑鍒ゆ柇瀵硅薄鏄惁鐩稿悓鐨勪緷鎹€?br> public boolean equals(Object obj){
if(!(obj instanceof Person))
return false;
Person p = (Person)obj;
return this.age == p.age;
}
2锛孲tring toString()锛氬皢瀵硅薄鍙樻垚瀛楃涓诧紱榛樿杩斿洖鐨勬牸寮忥細绫诲悕@鍝堝笇鍊?= getClass().getName() + 鈥楡鈥?+ Integer.toHexString(hashCode())
涓轰簡瀵硅薄瀵瑰簲鐨勫瓧绗︿覆鍐呭鏈夋剰涔夛紝鍙互閫氳繃澶嶅啓锛屽缓绔嬭绫诲璞¤嚜宸辩壒鏈夌殑瀛楃涓茶〃鐜板舰寮忋€?br> public String toString(){
return "person : "+age;
}
3锛孋lass getClass()锛氳幏鍙栦换鎰忓璞¤繍琛屾椂鐨勬墍灞炲瓧鑺傜爜鏂囦欢瀵硅薄銆?br>4锛宨nt hashCode()锛氳繑鍥炶瀵硅薄鐨勫搱甯岀爜鍊笺€傛敮鎸佹鏂规硶鏄负浜嗘彁楂樺搱甯岃〃鐨勬€ц兘銆?br>
閫氬父equals锛宼oString锛宧ashCode锛屽湪搴旂敤涓兘浼氳澶嶅啓锛屽缓绔嬪叿浣撳璞$殑鐗规湁鐨勫唴瀹广€?br>------------------------------------------------------------------------------------------------
public final void getTime(){ //姝ゅ姛鑳藉鏋滀笉闇€瑕佸鍐欙紝鍙姞final闄愬畾
long start = System.currentTimeMillis();
code(); //涓嶇‘瀹氱殑鍔熻兘閮ㄥ垎锛屾彁鍙栧嚭鏉ワ紝閫氳繃鎶借薄鏂规硶瀹炵幇
long end = System.currentTimeMillis();
System.out.println("姣鏄細"+(end-start));
}
public abstract void code(); //鎶借薄涓嶇‘瀹氱殑鍔熻兘锛岃瀛愮被澶嶅啓瀹炵幇
}
class SubDemo extends GetTime{
public void code(){ //瀛愮被澶嶅啓鍔熻兘鏂规硶
for(int y=0; y<1000; y++){
System.out.println("y");
}
}
}
---------------------------------------------------------------------------------------------
鎺?鍙o細鈽呪槄鈽呪槄鈽?/h2>
1锛氭槸鐢ㄥ叧閿瓧interface瀹氫箟鐨勩€?br>2锛氭帴鍙d腑鍖呭惈鐨勬垚鍛橈紝鏈€甯歌鐨勬湁鍏ㄥ眬甯搁噺銆佹娊璞℃柟娉曘€?br>娉ㄦ剰锛氭帴鍙d腑鐨勬垚鍛橀兘鏈夊浐瀹氱殑淇グ绗︺€?br> 鎴愬憳鍙橀噺锛歱ublic static final
鎴愬憳鏂规硶锛歱ublic abstract
interface Inter{
public static final int x = 3;
public abstract void show();
}
3锛氭帴鍙d腑鏈夋娊璞℃柟娉曪紝璇存槑鎺ュ彛涓嶅彲浠ュ疄渚嬪寲銆傛帴鍙g殑瀛愮被蹇呴』瀹炵幇浜嗘帴鍙d腑鎵€鏈夌殑鎶借薄鏂规硶鍚庯紝璇ュ瓙绫绘墠鍙互瀹炰緥鍖栥€傚惁鍒欙紝璇ュ瓙绫昏繕鏄竴涓娊璞$被銆?br>4锛氱被涓庣被涔嬮棿瀛樺湪鐫€缁ф壙鍏崇郴锛岀被涓庢帴鍙d腑闂村瓨鍦ㄧ殑鏄疄鐜板叧绯汇€?br> 缁ф壙鐢╡xtends 锛涘疄鐜扮敤implements 锛?br>5锛氭帴鍙e拰绫讳笉涓€鏍风殑鍦版柟锛屽氨鏄紝鎺ュ彛鍙互琚瀹炵幇锛岃繖灏辨槸澶氱户鎵挎敼鑹悗鐨勭粨鏋溿€俲ava灏嗗缁ф壙鏈哄埗閫氳繃澶氱幇瀹炴潵浣撶幇銆?br>6锛氫竴涓被鍦ㄧ户鎵垮彟涓€涓被鐨勫悓鏃讹紝杩樺彲浠ュ疄鐜板涓帴鍙c€傛墍浠ユ帴鍙g殑鍑虹幇閬垮厤浜嗗崟缁ф壙鐨勫眬闄愭€с€傝繕鍙互灏嗙被杩涜鍔熻兘鐨勬墿灞曘€?br>7锛氬叾瀹瀓ava涓槸鏈夊缁ф壙鐨勩€傛帴鍙d笌鎺ュ彛涔嬮棿瀛樺湪鐫€缁ф壙鍏崇郴锛屾帴鍙e彲浠ュ缁ф壙鎺ュ彛銆?br>
鎺ュ彛閮界敤浜庤璁′笂锛岃璁′笂鐨勭壒鐐癸細锛堝彲浠ョ悊瑙d富鏉夸笂鎻愪緵鐨勬帴鍙o級
1锛氭帴鍙f槸瀵瑰鎻愪緵鐨勮鍒欍€?br>2锛氭帴鍙f槸鍔熻兘鐨勬墿灞曘€?br>3锛氭帴鍙g殑鍑虹幇闄嶄綆浜嗚€﹀悎鎬с€?br>
鎶借薄绫讳笌鎺ュ彛锛?/h3>
鎶借薄绫伙細涓€鑸敤浜庢弿杩颁竴涓綋绯诲崟鍏冿紝灏嗕竴缁勫叡鎬у唴瀹硅繘琛屾娊鍙栵紝鐗圭偣锛氬彲浠ュ湪绫讳腑瀹氫箟鎶借薄鍐呭璁╁瓙绫诲疄鐜帮紝鍙互瀹氫箟闈炴娊璞″唴瀹硅瀛愮被鐩存帴浣跨敤銆傚畠閲岄潰瀹氫箟鐨勯兘鏄竴浜涗綋绯讳腑鐨勫熀鏈唴瀹广€?br>鎺ュ彛锛氫竴鑸敤浜庡畾涔夊璞$殑鎵╁睍鍔熻兘锛屾槸鍦ㄧ户鎵夸箣澶栬繕闇€杩欎釜瀵硅薄鍏峰鐨勪竴浜涘姛鑳姐€?br>
鎶借薄绫诲拰鎺ュ彛鐨勫叡鎬э細閮芥槸涓嶆柇鍚戜笂鎶藉彇鐨勭粨鏋溿€?br>
鎶借薄绫诲拰鎺ュ彛鐨勫尯鍒細
1锛氭娊璞$被鍙兘琚户鎵匡紝鑰屼笖鍙兘鍗曠户鎵裤€?br>鎺ュ彛闇€瑕佽瀹炵幇锛岃€屼笖鍙互澶氬疄鐜般€?
2锛氭娊璞$被涓彲浠ュ畾涔夐潪鎶借薄鏂规硶锛屽瓙绫诲彲浠ョ洿鎺ョ户鎵夸娇鐢ㄣ€?br>鎺ュ彛涓兘鏈夋娊璞℃柟娉曪紝闇€瑕佸瓙绫诲幓瀹炵幇銆?br>3锛氭娊璞$被浣跨敤鐨勬槸 is a 鍏崇郴銆?br>鎺ュ彛浣跨敤鐨?like a 鍏崇郴銆?
4锛氭娊璞$被鐨勬垚鍛樹慨楗扮鍙互鑷畾涔夈€?br>鎺ュ彛涓殑鎴愬憳淇グ绗︽槸鍥哄畾鐨勩€傚叏閮芥槸public鐨勩€?br>
鍦ㄥ紑鍙戜箣鍓嶏紝鍏堝畾涔夎鍒欙紝A鍜孊鍒嗗埆寮€鍙戯紝A璐熻矗瀹炵幇杩欎釜瑙勫垯锛孊璐熻矗浣跨敤杩欎釜瑙勫垯銆傝嚦浜嶢鏄浣曞瑙勫垯鍏蜂綋瀹炵幇鐨勶紝B鏄笉闇€瑕佺煡閬撶殑銆傝繖鏍疯繖涓帴鍙g殑鍑虹幇灏遍檷浣庝簡A鍜孊鐩存帴鑰﹀悎鎬с€?br>
------------------------------------------------------------------------------------------------
澶?鎬佲槄鈽呪槄鈽呪槄
锛堥潰鍚戝璞$壒寰佷箣涓€锛夛細鍑芥暟鏈韩灏卞叿澶囧鎬佹€э紝鏌愪竴绉嶄簨鐗╂湁涓嶅悓鐨勫叿浣撶殑浣撶幇銆?br>
浣撶幇锛氱埗绫诲紩鐢ㄦ垨鑰呮帴鍙g殑寮曠敤鎸囧悜浜嗚嚜宸辩殑瀛愮被瀵硅薄銆?/Animal a = new Cat();
澶氭€佺殑濂藉锛氭彁楂樹簡绋嬪簭鐨勬墿灞曟€с€?br>澶氭€佺殑寮婄锛氬綋鐖剁被寮曠敤鎸囧悜瀛愮被瀵硅薄鏃讹紝铏界劧鎻愰珮浜嗘墿灞曟€э紝浣嗘槸鍙兘璁块棶鐖剁被涓叿澶囩殑鏂规硶锛屼笉鍙互璁块棶瀛愮被涓壒鏈夌殑鏂规硶銆?鍓嶆湡涓嶈兘浣跨敤鍚庢湡浜х敓鐨勫姛鑳斤紝鍗宠闂殑灞€闄愭€?
澶氭€佺殑鍓嶆彁锛?br> 1锛氬繀椤昏鏈夊叧绯伙紝姣斿缁ф壙銆佹垨鑰呭疄鐜般€?br> 2锛氶€氬父浼氭湁瑕嗙洊鎿嶄綔銆?br>
澶氭€佺殑鍑虹幇鎬濇兂涓婁篃鍋氱潃鍙樺寲锛氫互鍓嶆槸鍒涘缓瀵硅薄骞舵寚鎸ュ璞″仛浜嬫儏銆傛湁浜嗗鎬佷互鍚庯紝鎴戜滑鍙互鎵惧埌瀵硅薄鐨勫叡鎬х被鍨嬶紝鐩存帴鎿嶄綔鍏辨€х被鍨嬪仛浜嬫儏鍗冲彲锛岃繖鏍峰彲浠ユ寚鎸ヤ竴鎵瑰璞″仛浜嬫儏锛屽嵆閫氳繃鎿嶄綔鐖剁被鎴栨帴鍙e疄鐜般€?br>--------------------------------------------------------------
class teacher{
void teachs(){
System.out.println("浼佷笟绠$悊");
}
void play(){
System.out.println("閽撻奔");
}
}
class teacher extends teachers{
void 璁茶(){
System.out.println("JAVA");
}
void 鐪嬬數褰?){
System.out.println("鐪嬬數褰?);
}
}
class {
public static void main(String[] args) {
teachers x = new teacher(); //teacher瀵硅薄琚彁鍗囦负浜唗eahcers绫诲瀷銆?br>// x.璁茶();
// x.鐪嬬數褰?); //閿欒.
teacher y = (teachers)x; //灏唗eachers绫诲瀷寮哄埗杞崲鎴恡eacher绫诲瀷銆?br> y.鐪嬬數褰?);//鍦ㄥ鎬佷腑锛岃嚜濮嬭嚜缁堥兘鏄瓙绫诲璞″湪鍋氱潃绫诲瀷鐨勫彉鍖栥€?br> }
}
---------------------------------------------------------------
濡傛灉鎯崇敤瀛愮被瀵硅薄鐨勭壒鏈夋柟娉曪紝濡備綍鍒ゆ柇瀵硅薄鏄摢涓叿浣撶殑瀛愮被绫诲瀷鍛紵
鍙互鍙互閫氳繃涓€涓叧閿瓧 instanceof ;//鍒ゆ柇瀵硅薄鏄惁瀹炵幇浜嗘寚瀹氱殑鎺ュ彛鎴栫户鎵夸簡鎸囧畾鐨勭被
鏍煎紡锛?lt;瀵硅薄 instanceof 绫诲瀷> 锛屽垽鏂竴涓璞℃槸鍚︽墍灞炰簬鎸囧畾鐨勭被鍨嬨€?br>Student instanceof Person = true;//student缁ф壙浜唒erson绫?br>
澶氭€佸湪瀛愮埗绫讳腑鐨勬垚鍛樹笂鐨勪綋鐜扮殑鐗圭偣锛?/h3>
1锛屾垚鍛樺彉閲忥細鍦ㄥ鎬佷腑锛屽瓙鐖剁被鎴愬憳鍙橀噺鍚屽悕銆?br> 鍦ㄧ紪璇戞椂鏈燂細鍙傝€冪殑鏄紩鐢ㄥ瀷鍙橀噺鎵€灞炵殑绫讳腑鏄惁鏈夎皟鐢ㄧ殑鎴愬憳銆傦紙缂栬瘧鏃朵笉浜х敓瀵硅薄锛屽彧妫€鏌ヨ娉曢敊璇級
杩愯鏃舵湡锛氫篃鏄弬鑰冨紩鐢ㄥ瀷鍙橀噺鎵€灞炵殑绫讳腑鏄惁鏈夎皟鐢ㄧ殑鎴愬憳銆?br> 绠€鍗曚竴鍙ヨ瘽锛氭棤璁虹紪璇戝拰杩愯锛屾垚鍛樺彉閲忓弬鑰冪殑閮芥槸寮曠敤鍙橀噺鎵€灞炵殑绫讳腑鐨勬垚鍛樺彉閲忋€?br> 鍐嶈鐨勬洿瀹规槗璁板繂涓€浜涳細鎴愬憳鍙橀噺 --- 缂栬瘧杩愯閮界湅 = 宸﹁竟銆?br>2锛屾垚鍛樺嚱鏁般€?br> 缂栬瘧鏃舵湡锛氬弬鑰冨紩鐢ㄥ瀷鍙橀噺鎵€灞炵殑绫讳腑鏄惁鏈夎皟鐢ㄧ殑鏂规硶銆?br> 杩愯浜嬫儏锛氬弬鑰冪殑鏄璞℃墍灞炵殑绫讳腑鏄惁鏈夎皟鐢ㄧ殑鏂规硶銆?br> 涓轰粈涔堟槸杩欐牱鐨勫憿锛熷洜涓哄湪瀛愮埗绫讳腑锛屽浜庝竴妯′竴鏍风殑鎴愬憳鍑芥暟锛屾湁涓€涓壒鎬э細瑕嗙洊銆?br> 绠€鍗曚竴鍙ワ細鎴愬憳鍑芥暟锛岀紪璇戠湅寮曠敤鍨嬪彉閲忔墍灞炵殑绫伙紝杩愯鐪嬪璞℃墍灞炵殑绫汇€?br> 鏇寸畝鍗曪細鎴愬憳鍑芥暟 --- 缂栬瘧鐪?= 宸﹁竟锛岃繍琛岀湅 = 鍙宠竟銆?br>3锛岄潤鎬佸嚱鏁般€?
缂栬瘧鏃舵湡锛氬弬鑰冪殑鏄紩鐢ㄥ瀷鍙橀噺鎵€灞炵殑绫讳腑鏄惁鏈夎皟鐢ㄧ殑鎴愬憳銆?br> 杩愯鏃舵湡锛氫篃鏄弬鑰冨紩鐢ㄥ瀷鍙橀噺鎵€灞炵殑绫讳腑鏄惁鏈夎皟鐢ㄧ殑鎴愬憳銆?br> 涓轰粈涔堟槸杩欐牱鐨勫憿锛熷洜涓洪潤鎬佹柟娉曪紝鍏跺疄涓嶆墍灞炰簬瀵硅薄锛岃€屾槸鎵€灞炰簬璇ユ柟娉曟墍鍦ㄧ殑绫汇€?br> 璋冪敤闈欐€佺殑鏂规硶寮曠敤鏄摢涓被鐨勫紩鐢ㄨ皟鐢ㄧ殑灏辨槸鍝釜绫讳腑鐨勯潤鎬佹柟娉曘€?br> 绠€鍗曡锛氶潤鎬佸嚱鏁?--- 缂栬瘧杩愯閮界湅 = 宸﹁竟銆?br>-----------------------------------------------------------------------------------------------
java.lang.Object
Object锛氭墍鏈夌被鐨勭洿鎺ユ垨鑰呴棿鎺ョ埗绫伙紝Java璁や负鎵€鏈夌殑瀵硅薄閮藉叿澶囦竴浜涘熀鏈殑鍏辨€у唴瀹癸紝杩欎簺鍐呭鍙互涓嶆柇鐨勫悜涓婃娊鍙栵紝鏈€缁堝氨鎶藉彇鍒颁簡涓€涓渶椤跺眰鐨勭被涓殑锛岃绫讳腑瀹氫箟鐨勫氨鏄墍鏈夊璞¢兘鍏峰鐨勫姛鑳姐€?br>
鍏蜂綋鏂规硶锛?br>1锛宐oolean equals(Object obj)锛氱敤浜庢瘮杈冧袱涓璞℃槸鍚︾浉绛夛紝鍏跺疄鍐呴儴姣旇緝鐨勫氨鏄袱涓璞″湴鍧€銆?br>鑰屾牴鎹璞$殑灞炴€т笉鍚岋紝鍒ゆ柇瀵硅薄鏄惁鐩稿悓鐨勫叿浣撳唴瀹逛篃涓嶄竴鏍枫€傛墍浠ュ湪瀹氫箟绫绘椂锛屼竴鑸兘浼氬鍐檈quals鏂规硶锛屽缓绔嬫湰绫荤壒鏈夌殑鍒ゆ柇瀵硅薄鏄惁鐩稿悓鐨勪緷鎹€?br> public boolean equals(Object obj){
if(!(obj instanceof Person))
return false;
Person p = (Person)obj;
return this.age == p.age;
}
2锛孲tring toString()锛氬皢瀵硅薄鍙樻垚瀛楃涓诧紱榛樿杩斿洖鐨勬牸寮忥細绫诲悕@鍝堝笇鍊?= getClass().getName() + 鈥楡鈥?+ Integer.toHexString(hashCode())
涓轰簡瀵硅薄瀵瑰簲鐨勫瓧绗︿覆鍐呭鏈夋剰涔夛紝鍙互閫氳繃澶嶅啓锛屽缓绔嬭绫诲璞¤嚜宸辩壒鏈夌殑瀛楃涓茶〃鐜板舰寮忋€?br> public String toString(){
return "person : "+age;
}
3锛孋lass getClass()锛氳幏鍙栦换鎰忓璞¤繍琛屾椂鐨勬墍灞炲瓧鑺傜爜鏂囦欢瀵硅薄銆?br>4锛宨nt hashCode()锛氳繑鍥炶瀵硅薄鐨勫搱甯岀爜鍊笺€傛敮鎸佹鏂规硶鏄负浜嗘彁楂樺搱甯岃〃鐨勬€ц兘銆?br>
閫氬父equals锛宼oString锛宧ashCode锛屽湪搴旂敤涓兘浼氳澶嶅啓锛屽缓绔嬪叿浣撳璞$殑鐗规湁鐨勫唴瀹广€?br>------------------------------------------------------------------------------------------------
鎶借薄绫诲拰鎺ュ彛鐨勫叡鎬э細閮芥槸涓嶆柇鍚戜笂鎶藉彇鐨勭粨鏋溿€?br>
鎶借薄绫诲拰鎺ュ彛鐨勫尯鍒細
1锛氭娊璞$被鍙兘琚户鎵匡紝鑰屼笖鍙兘鍗曠户鎵裤€?br>鎺ュ彛闇€瑕佽瀹炵幇锛岃€屼笖鍙互澶氬疄鐜般€?2锛氭娊璞$被涓彲浠ュ畾涔夐潪鎶借薄鏂规硶锛屽瓙绫诲彲浠ョ洿鎺ョ户鎵夸娇鐢ㄣ€?br>鎺ュ彛涓兘鏈夋娊璞℃柟娉曪紝闇€瑕佸瓙绫诲幓瀹炵幇銆?br>3锛氭娊璞$被浣跨敤鐨勬槸 is a 鍏崇郴銆?br>鎺ュ彛浣跨敤鐨?like a 鍏崇郴銆?
4锛氭娊璞$被鐨勬垚鍛樹慨楗扮鍙互鑷畾涔夈€?br>鎺ュ彛涓殑鎴愬憳淇グ绗︽槸鍥哄畾鐨勩€傚叏閮芥槸public鐨勩€?br>
鍦ㄥ紑鍙戜箣鍓嶏紝鍏堝畾涔夎鍒欙紝A鍜孊鍒嗗埆寮€鍙戯紝A璐熻矗瀹炵幇杩欎釜瑙勫垯锛孊璐熻矗浣跨敤杩欎釜瑙勫垯銆傝嚦浜嶢鏄浣曞瑙勫垯鍏蜂綋瀹炵幇鐨勶紝B鏄笉闇€瑕佺煡閬撶殑銆傝繖鏍疯繖涓帴鍙g殑鍑虹幇灏遍檷浣庝簡A鍜孊鐩存帴鑰﹀悎鎬с€?br>
------------------------------------------------------------------------------------------------
澶?鎬佲槄鈽呪槄鈽呪槄
锛堥潰鍚戝璞$壒寰佷箣涓€锛夛細鍑芥暟鏈韩灏卞叿澶囧鎬佹€э紝鏌愪竴绉嶄簨鐗╂湁涓嶅悓鐨勫叿浣撶殑浣撶幇銆?br>浣撶幇锛氱埗绫诲紩鐢ㄦ垨鑰呮帴鍙g殑寮曠敤鎸囧悜浜嗚嚜宸辩殑瀛愮被瀵硅薄銆?/Animal a = new Cat();
澶氭€佺殑濂藉锛氭彁楂樹簡绋嬪簭鐨勬墿灞曟€с€?br>澶氭€佺殑寮婄锛氬綋鐖剁被寮曠敤鎸囧悜瀛愮被瀵硅薄鏃讹紝铏界劧鎻愰珮浜嗘墿灞曟€э紝浣嗘槸鍙兘璁块棶鐖剁被涓叿澶囩殑鏂规硶锛屼笉鍙互璁块棶瀛愮被涓壒鏈夌殑鏂规硶銆?鍓嶆湡涓嶈兘浣跨敤鍚庢湡浜х敓鐨勫姛鑳斤紝鍗宠闂殑灞€闄愭€?
澶氭€佺殑鍓嶆彁锛?br> 1锛氬繀椤昏鏈夊叧绯伙紝姣斿缁ф壙銆佹垨鑰呭疄鐜般€?br> 2锛氶€氬父浼氭湁瑕嗙洊鎿嶄綔銆?br>
澶氭€佺殑鍑虹幇鎬濇兂涓婁篃鍋氱潃鍙樺寲锛氫互鍓嶆槸鍒涘缓瀵硅薄骞舵寚鎸ュ璞″仛浜嬫儏銆傛湁浜嗗鎬佷互鍚庯紝鎴戜滑鍙互鎵惧埌瀵硅薄鐨勫叡鎬х被鍨嬶紝鐩存帴鎿嶄綔鍏辨€х被鍨嬪仛浜嬫儏鍗冲彲锛岃繖鏍峰彲浠ユ寚鎸ヤ竴鎵瑰璞″仛浜嬫儏锛屽嵆閫氳繃鎿嶄綔鐖剁被鎴栨帴鍙e疄鐜般€?br>--------------------------------------------------------------
class teacher{
void teachs(){
System.out.println("浼佷笟绠$悊");
}
void play(){
System.out.println("閽撻奔");
}
}
class teacher extends teachers{
void 璁茶(){
System.out.println("JAVA");
}
void 鐪嬬數褰?){
System.out.println("鐪嬬數褰?);
}
}
class {
public static void main(String[] args) {
teachers x = new teacher(); //teacher瀵硅薄琚彁鍗囦负浜唗eahcers绫诲瀷銆?br>// x.璁茶();
// x.鐪嬬數褰?); //閿欒.
teacher y = (teachers)x; //灏唗eachers绫诲瀷寮哄埗杞崲鎴恡eacher绫诲瀷銆?br> y.鐪嬬數褰?);//鍦ㄥ鎬佷腑锛岃嚜濮嬭嚜缁堥兘鏄瓙绫诲璞″湪鍋氱潃绫诲瀷鐨勫彉鍖栥€?br> }
}
---------------------------------------------------------------
濡傛灉鎯崇敤瀛愮被瀵硅薄鐨勭壒鏈夋柟娉曪紝濡備綍鍒ゆ柇瀵硅薄鏄摢涓叿浣撶殑瀛愮被绫诲瀷鍛紵
鍙互鍙互閫氳繃涓€涓叧閿瓧 instanceof ;//鍒ゆ柇瀵硅薄鏄惁瀹炵幇浜嗘寚瀹氱殑鎺ュ彛鎴栫户鎵夸簡鎸囧畾鐨勭被鏍煎紡锛?lt;瀵硅薄 instanceof 绫诲瀷> 锛屽垽鏂竴涓璞℃槸鍚︽墍灞炰簬鎸囧畾鐨勭被鍨嬨€?br>Student instanceof Person = true;//student缁ф壙浜唒erson绫?br>
澶氭€佸湪瀛愮埗绫讳腑鐨勬垚鍛樹笂鐨勪綋鐜扮殑鐗圭偣锛?/h3>
1锛屾垚鍛樺彉閲忥細鍦ㄥ鎬佷腑锛屽瓙鐖剁被鎴愬憳鍙橀噺鍚屽悕銆?br> 鍦ㄧ紪璇戞椂鏈燂細鍙傝€冪殑鏄紩鐢ㄥ瀷鍙橀噺鎵€灞炵殑绫讳腑鏄惁鏈夎皟鐢ㄧ殑鎴愬憳銆傦紙缂栬瘧鏃朵笉浜х敓瀵硅薄锛屽彧妫€鏌ヨ娉曢敊璇級
杩愯鏃舵湡锛氫篃鏄弬鑰冨紩鐢ㄥ瀷鍙橀噺鎵€灞炵殑绫讳腑鏄惁鏈夎皟鐢ㄧ殑鎴愬憳銆?br> 绠€鍗曚竴鍙ヨ瘽锛氭棤璁虹紪璇戝拰杩愯锛屾垚鍛樺彉閲忓弬鑰冪殑閮芥槸寮曠敤鍙橀噺鎵€灞炵殑绫讳腑鐨勬垚鍛樺彉閲忋€?br> 鍐嶈鐨勬洿瀹规槗璁板繂涓€浜涳細鎴愬憳鍙橀噺 --- 缂栬瘧杩愯閮界湅 = 宸﹁竟銆?br>2锛屾垚鍛樺嚱鏁般€?br> 缂栬瘧鏃舵湡锛氬弬鑰冨紩鐢ㄥ瀷鍙橀噺鎵€灞炵殑绫讳腑鏄惁鏈夎皟鐢ㄧ殑鏂规硶銆?br> 杩愯浜嬫儏锛氬弬鑰冪殑鏄璞℃墍灞炵殑绫讳腑鏄惁鏈夎皟鐢ㄧ殑鏂规硶銆?br> 涓轰粈涔堟槸杩欐牱鐨勫憿锛熷洜涓哄湪瀛愮埗绫讳腑锛屽浜庝竴妯′竴鏍风殑鎴愬憳鍑芥暟锛屾湁涓€涓壒鎬э細瑕嗙洊銆?br> 绠€鍗曚竴鍙ワ細鎴愬憳鍑芥暟锛岀紪璇戠湅寮曠敤鍨嬪彉閲忔墍灞炵殑绫伙紝杩愯鐪嬪璞℃墍灞炵殑绫汇€?br> 鏇寸畝鍗曪細鎴愬憳鍑芥暟 --- 缂栬瘧鐪?= 宸﹁竟锛岃繍琛岀湅 = 鍙宠竟銆?br>3锛岄潤鎬佸嚱鏁般€?
缂栬瘧鏃舵湡锛氬弬鑰冪殑鏄紩鐢ㄥ瀷鍙橀噺鎵€灞炵殑绫讳腑鏄惁鏈夎皟鐢ㄧ殑鎴愬憳銆?br> 杩愯鏃舵湡锛氫篃鏄弬鑰冨紩鐢ㄥ瀷鍙橀噺鎵€灞炵殑绫讳腑鏄惁鏈夎皟鐢ㄧ殑鎴愬憳銆?br> 涓轰粈涔堟槸杩欐牱鐨勫憿锛熷洜涓洪潤鎬佹柟娉曪紝鍏跺疄涓嶆墍灞炰簬瀵硅薄锛岃€屾槸鎵€灞炰簬璇ユ柟娉曟墍鍦ㄧ殑绫汇€?br> 璋冪敤闈欐€佺殑鏂规硶寮曠敤鏄摢涓被鐨勫紩鐢ㄨ皟鐢ㄧ殑灏辨槸鍝釜绫讳腑鐨勯潤鎬佹柟娉曘€?br> 绠€鍗曡锛氶潤鎬佸嚱鏁?--- 缂栬瘧杩愯閮界湅 = 宸﹁竟銆?br>-----------------------------------------------------------------------------------------------
java.lang.Object
Object锛氭墍鏈夌被鐨勭洿鎺ユ垨鑰呴棿鎺ョ埗绫伙紝Java璁や负鎵€鏈夌殑瀵硅薄閮藉叿澶囦竴浜涘熀鏈殑鍏辨€у唴瀹癸紝杩欎簺鍐呭鍙互涓嶆柇鐨勫悜涓婃娊鍙栵紝鏈€缁堝氨鎶藉彇鍒颁簡涓€涓渶椤跺眰鐨勭被涓殑锛岃绫讳腑瀹氫箟鐨勫氨鏄墍鏈夊璞¢兘鍏峰鐨勫姛鑳姐€?br>
鍏蜂綋鏂规硶锛?br>1锛宐oolean equals(Object obj)锛氱敤浜庢瘮杈冧袱涓璞℃槸鍚︾浉绛夛紝鍏跺疄鍐呴儴姣旇緝鐨勫氨鏄袱涓璞″湴鍧€銆?br>鑰屾牴鎹璞$殑灞炴€т笉鍚岋紝鍒ゆ柇瀵硅薄鏄惁鐩稿悓鐨勫叿浣撳唴瀹逛篃涓嶄竴鏍枫€傛墍浠ュ湪瀹氫箟绫绘椂锛屼竴鑸兘浼氬鍐檈quals鏂规硶锛屽缓绔嬫湰绫荤壒鏈夌殑鍒ゆ柇瀵硅薄鏄惁鐩稿悓鐨勪緷鎹€?br> public boolean equals(Object obj){
if(!(obj instanceof Person))
return false;
Person p = (Person)obj;
return this.age == p.age;
}
2锛孲tring toString()锛氬皢瀵硅薄鍙樻垚瀛楃涓诧紱榛樿杩斿洖鐨勬牸寮忥細绫诲悕@鍝堝笇鍊?= getClass().getName() + 鈥楡鈥?+ Integer.toHexString(hashCode())
涓轰簡瀵硅薄瀵瑰簲鐨勫瓧绗︿覆鍐呭鏈夋剰涔夛紝鍙互閫氳繃澶嶅啓锛屽缓绔嬭绫诲璞¤嚜宸辩壒鏈夌殑瀛楃涓茶〃鐜板舰寮忋€?br> public String toString(){
return "person : "+age;
}
3锛孋lass getClass()锛氳幏鍙栦换鎰忓璞¤繍琛屾椂鐨勬墍灞炲瓧鑺傜爜鏂囦欢瀵硅薄銆?br>4锛宨nt hashCode()锛氳繑鍥炶瀵硅薄鐨勫搱甯岀爜鍊笺€傛敮鎸佹鏂规硶鏄负浜嗘彁楂樺搱甯岃〃鐨勬€ц兘銆?br>
閫氬父equals锛宼oString锛宧ashCode锛屽湪搴旂敤涓兘浼氳澶嶅啓锛屽缓绔嬪叿浣撳璞$殑鐗规湁鐨勫唴瀹广€?br>------------------------------------------------------------------------------------------------
鍐呴儴绫?/h3>
鍐呴儴绫伙細濡傛灉A绫婚渶瑕佺洿鎺ヨ闂瓸绫讳腑鐨勬垚鍛橈紝鑰孊绫诲張闇€瑕佸缓绔婣绫荤殑瀵硅薄銆傝繖鏃?涓轰簡鏂逛究璁捐鍜岃闂紝鐩存帴灏咥绫诲畾涔夊湪B绫讳腑銆傚氨鍙互浜嗐€侫绫诲氨绉颁负鍐呴儴绫汇€傚唴閮ㄧ被鍙互鐩存帴璁块棶澶栭儴绫讳腑鐨勬垚鍛樸€傝€屽閮ㄧ被鎯宠璁块棶鍐呴儴绫伙紝蹇呴』瑕佸缓绔嬪唴閮ㄧ被鐨勫璞°€?br>-----------------------------------------------------
class Outer{
int num = 4;
class Inner {
void show(){
System.out.println("inner show run "+num);
}
}
public void method(){
Inner in = new Inner();//鍒涘缓鍐呴儴绫荤殑瀵硅薄銆?br> in.show();//璋冪敤鍐呴儴绫荤殑鏂规硶銆?br> }
}
-------------------------------------------------------
褰撳唴閮ㄧ被瀹氫箟鍦ㄥ閮ㄧ被涓殑鎴愬憳浣嶇疆涓婏紝鍙互浣跨敤涓€浜涙垚鍛樹慨楗扮淇グ private銆乻tatic銆?br>1锛氶粯璁や慨楗扮銆?br>鐩存帴璁块棶鍐呴儴绫绘牸寮忥細澶栭儴绫诲悕.鍐呴儴绫诲悕 鍙橀噺鍚?= 澶栭儴绫诲璞?鍐呴儴绫诲璞?
Outer.Inner in = new Outer.new Inner();//杩欑褰㈠紡寰堝皯鐢ㄣ€?br> 浣嗘槸杩欑搴旂敤涓嶅瑙侊紝鍥犱负鍐呴儴绫讳箣鎵€浠ュ畾涔夊湪鍐呴儴灏辨槸涓轰簡灏佽銆傛兂瑕佽幏鍙栧唴閮ㄧ被瀵硅薄閫氬父閮介€氳繃澶栭儴绫荤殑鏂规硶鏉ヨ幏鍙栥€傝繖鏍峰彲浠ュ鍐呴儴绫诲璞¤繘琛屾帶鍒躲€?br>2锛氱鏈変慨楗扮銆?br> 閫氬父鍐呴儴绫昏灏佽锛岄兘浼氳绉佹湁鍖栵紝鍥犱负灏佽鎬т笉璁╁叾浠栫▼搴忕洿鎺ヨ闂€?br>3锛氶潤鎬佷慨楗扮銆?br> 濡傛灉鍐呴儴绫昏闈欐€佷慨楗帮紝鐩稿綋浜庡閮ㄧ被锛屼細鍑虹幇璁块棶灞€闄愭€э紝鍙兘璁块棶澶栭儴绫讳腑鐨勯潤鎬佹垚鍛樸€?br> 娉ㄦ剰锛涘鏋滃唴閮ㄧ被涓畾涔変簡闈欐€佹垚鍛橈紝閭d箞璇ュ唴閮ㄧ被蹇呴』鏄潤鎬佺殑銆?br>
鍐呴儴绫荤紪璇戝悗鐨勬枃浠跺悕涓猴細鈥滃閮ㄧ被鍚?鍐呴儴绫诲悕.java鈥濓紱
涓轰粈涔堝唴閮ㄧ被鍙互鐩存帴璁块棶澶栭儴绫讳腑鐨勬垚鍛樺憿锛?br>閭f槸鍥犱负鍐呴儴涓兘鎸佹湁涓€涓閮ㄧ被鐨勫紩鐢ㄣ€傝繖涓槸寮曠敤鏄?澶栭儴绫诲悕.this
鍐呴儴绫诲彲浠ュ畾涔夊湪澶栭儴绫讳腑鐨勬垚鍛樹綅缃笂锛屼篃鍙互瀹氫箟鍦ㄥ閮ㄧ被涓殑灞€閮ㄤ綅缃笂銆?br>褰撳唴閮ㄧ被琚畾涔夊湪灞€閮ㄤ綅缃笂锛屽彧鑳借闂眬閮ㄤ腑琚玣inal淇グ鐨勫眬閮ㄥ彉閲忋€?br>
鍖垮悕鍐呴儴绫伙細娌℃湁鍚嶅瓧鐨勫唴閮ㄧ被銆傚氨鏄唴閮ㄧ被鐨勭畝鍖栧舰寮忋€備竴鑸彧鐢ㄤ竴娆″氨鍙互鐢ㄨ繖绉嶅舰寮忋€傚尶鍚嶅唴閮ㄧ被鍏跺疄灏辨槸涓€涓尶鍚嶅瓙绫诲璞°€傛兂瑕佸畾涔夊尶鍚嶅唴閮ㄧ被锛氶渶瑕佸墠鎻愶紝鍐呴儴绫诲繀椤荤户鎵夸竴涓被鎴栬€呭疄鐜版帴鍙c€?br>
鍖垮悕鍐呴儴绫荤殑鏍煎紡锛歯ew 鐖剁被鍚?amp;鎺ュ彛鍚?){ 瀹氫箟瀛愮被鎴愬憳鎴栬€呰鐩栫埗绫绘柟娉?}.鏂规硶銆?br>
鍖垮悕鍐呴儴绫荤殑浣跨敤鍦烘櫙锛?br>褰撳嚱鏁扮殑鍙傛暟鏄帴鍙g被鍨嬪紩鐢ㄦ椂锛屽鏋滄帴鍙d腑鐨勬柟娉曚笉瓒呰繃3涓€傚彲浠ラ€氳繃鍖垮悕鍐呴儴绫绘潵瀹屾垚鍙傛暟鐨勪紶閫掋€?br>鍏跺疄灏辨槸鍦ㄥ垱寤哄尶鍚嶅唴閮ㄧ被鏃讹紝璇ョ被涓殑灏佽鐨勬柟娉曚笉瑕佽繃澶氾紝鏈€濂戒袱涓垨鑰呬袱涓互鍐呫€?br>--------------------------------------------------------
//闈㈣瘯
//1
new Object(){
void show(){
System.out.println("show run");
}
}.show();
//2
Object obj = new Object(){
void show(){
System.out.println("show run");
}
};
obj.show();
1鍜?鐨勫啓娉曟纭悧锛熸湁鍖哄埆鍚楋紵璇村嚭鍘熷洜銆?br> 鍐欐硶鏄纭紝1鍜?閮芥槸鍦ㄩ€氳繃鍖垮悕鍐呴儴绫诲缓绔嬩竴涓狾bject绫荤殑瀛愮被瀵硅薄銆?br> 鍖哄埆锛?br> 绗竴涓彲鏄紪璇戦€氳繃锛屽苟杩愯銆?br> 绗簩涓紪璇戝け璐ワ紝鍥犱负鍖垮悕鍐呴儴绫绘槸涓€涓瓙绫诲璞★紝褰撶敤Object鐨刼bj寮曠敤鎸囧悜鏃讹紝灏辫鎻愬崌涓轰簡
Object绫诲瀷锛岃€岀紪璇戞椂妫€鏌bject绫讳腑鏄惁鏈塻how鏂规硶锛屾墍浠ョ紪璇戝け璐ャ€?br>-------------------------------------------------------
class InnerClassDemo6 {
+锛坰tatic锛塩lass Inner{
void show(){}
}
public void method(){
this.new Inner().show();//鍙互
}
public static void main(String[] args) {//static涓嶅厑璁竧his
This.new Inner().show();//閿欒锛孖nner绫婚渶瑕佸畾涔夋垚static
}
}
------------------------------------------------------
interface Inter{
void show();
}
class Outer{//閫氳繃鍖垮悕鍐呴儴绫昏ˉ瓒砄uter绫讳腑鐨勪唬鐮併€?br> public static Inter method(){
return new Inter(){
public void show(){}
};
}
}
class InnerClassDemo7 {
public static void main(String[] args) {
Outer.method().show();
/*
Outer.method():鎰忔€濇槸锛歄uter涓湁涓€涓悕绉颁负method鐨勬柟娉曪紝鑰屼笖杩欎釜鏂规硶鏄潤鎬佺殑銆?br> Outer.method().show():褰揙uter绫昏皟鐢ㄩ潤鎬佺殑method鏂规硶杩愮畻缁撴潫鍚庣殑缁撴灉鍙堣皟鐢ㄤ簡show鏂规硶锛屾剰鍛崇潃锛歮ethod()鏂规硶杩愮畻瀹屼竴涓槸瀵硅薄锛岃€屼笖杩欎釜瀵硅薄鏄疘nter绫诲瀷鐨勩€?br> */
function (new Inter(){
public void show(){}
}); //鍖垮悕鍐呴儴绫讳綔涓烘柟娉曠殑鍙傛暟杩涜浼犻€掋€?br> }
public static void function(Inter in){
in.show();
}
}
------------------------------------------------------------------------------------------------
寮?甯革細鈽呪槄鈽呪槄
寮傚父锛氬氨鏄笉姝e父銆傜▼搴忓湪杩愯鏃跺嚭鐜扮殑涓嶆甯告儏鍐点€傚叾瀹炲氨鏄▼搴忎腑鍑虹幇鐨勯棶棰樸€傝繖涓棶棰樻寜鐓ч潰鍚戝璞℃€濇兂杩涜鎻忚堪锛屽苟灏佽鎴愪簡瀵硅薄銆傚洜涓洪棶棰樼殑浜х敓鏈変骇鐢熺殑鍘熷洜銆佹湁闂鐨勫悕绉般€佹湁闂鐨勬弿杩扮瓑澶氫釜灞炴€т俊鎭瓨鍦ㄣ€傚綋鍑虹幇澶氬睘鎬т俊鎭渶鏂逛究鐨勬柟寮忓氨鏄皢杩欎簺淇℃伅杩涜灏佽銆傚紓甯稿氨鏄痡ava鎸夌収闈㈠悜瀵硅薄鐨勬€濇兂灏嗛棶棰樿繘琛屽璞″皝瑁呫€傝繖鏍峰氨鏂逛究浜庢搷浣滈棶棰樹互鍙婂鐞嗛棶棰樸€?br>
鍑虹幇鐨勯棶棰樻湁寰堝绉嶏紝姣斿瑙掓爣瓒婄晫锛岀┖鎸囬拡绛夐兘鏄€傚氨瀵硅繖浜涢棶棰樿繘琛屽垎绫汇€傝€屼笖杩欎簺闂閮芥湁鍏辨€у唴瀹规瘮濡傦細姣忎竴涓棶棰橀兘鏈夊悕绉帮紝鍚屾椂杩樻湁闂鎻忚堪鐨勪俊鎭紝闂鍑虹幇鐨勪綅缃紝鎵€浠ュ彲浠ヤ笉鏂殑鍚戜笂鎶藉彇銆傚舰鎴愪簡寮傚父浣撶郴銆?br>
--------java.lang.Throwable锛?br>Throwable锛氬彲鎶涘嚭鐨勩€?br> |--Error锛氶敊璇紝涓€鑸儏鍐典笅锛屼笉缂栧啓閽堝鎬х殑浠g爜杩涜澶勭悊锛岄€氬父鏄痡vm鍙戠敓鐨勶紝闇€瑕佸绋嬪簭杩涜淇銆?br> |--Exception锛氬紓甯革紝鍙互鏈夐拡瀵规€х殑澶勭悊鏂瑰紡
鏃犺鏄敊璇繕鏄紓甯革紝瀹冧滑閮芥湁鍏蜂綋鐨勫瓙绫讳綋鐜版瘡涓€涓棶棰橈紝瀹冧滑鐨勫瓙绫婚兘鏈変竴涓叡鎬э紝灏辨槸閮戒互鐖剁被鍚嶆墠浣滀负瀛愮被鐨勫悗缂€鍚嶃€?br>
杩欎釜浣撶郴涓殑鎵€鏈夌被鍜屽璞¢兘鍏峰涓€涓嫭鏈夌殑鐗圭偣锛涘氨鏄彲鎶涙€с€?br>鍙姏鎬х殑浣撶幇锛氬氨鏄繖涓綋绯讳腑鐨勭被鍜屽璞¢兘鍙互琚玹hrows鍜宼hrow涓や釜鍏抽敭瀛楁墍鎿嶄綔銆?br>------------------------------------------------------
class ExceptionDemo{
public static void main(String[] args) {
// byte[] buf = new byte[1024*1024*700];//java.lang.OutOfMemoryError鍐呭瓨婧㈠嚭閿欒
}
}
------------------------------------------------------
鍦ㄥ紑鍙戞椂锛屽鏋滃畾涔夊姛鑳芥椂锛屽彂鐜拌鍔熻兘浼氬嚭鐜颁竴浜涢棶棰橈紝搴旇灏嗛棶棰樺湪瀹氫箟鍔熻兘鏃舵爣绀哄嚭鏉ワ紝杩欐牱璋冪敤鑰呭氨鍙互鍦ㄤ娇鐢ㄨ繖涓姛鑳界殑鏃跺€欙紝棰勫厛缁欏嚭澶勭悊鏂瑰紡銆?br>
濡備綍鏍囩ず鍛紵閫氳繃throws鍏抽敭瀛楀畬鎴愶紝鏍煎紡锛歵hrows 寮傚父绫诲悕,寮傚父绫诲悕...
杩欐牱鏍囩ず鍚庯紝璋冪敤鑰咃紝鍦ㄤ娇鐢ㄨ鍔熻兘鏃讹紝灏卞繀椤昏澶勭悊锛屽惁鍒欑紪璇戝け璐ャ€?br>
澶勭悊鏂瑰紡鏈変袱绉嶏細1銆佹崟鎹夛紱2銆佹姏鍑恒€?br>瀵逛簬鎹曟崏锛歫ava鏈夐拡瀵规€х殑璇彞鍧楄繘琛屽鐞嗐€?br>try {
闇€瑕佽妫€娴嬬殑浠g爜锛?br>}
catch(寮傚父绫?鍙橀噺鍚?{
寮傚父澶勭悊浠g爜锛?br>}
fianlly{
涓€瀹氫細鎵ц鐨勪唬鐮侊紱
}
--------------------------------------------------------
catch (Exception e) { //e鐢ㄤ簬鎺ユ敹try妫€娴嬪埌鐨勫紓甯稿璞°€?br> System.out.println("message:"+e.getMessage());//鑾峰彇鐨勬槸寮傚父鐨勪俊鎭€?br> System.out.println("toString:"+e.toString());//鑾峰彇鐨勬槸寮傚父鐨勫悕瀛?寮傚父鐨勪俊鎭€?br> e.printStackTrace();//鎵撳嵃寮傚父鍦ㄥ爢鏍堜腑淇℃伅锛涘紓甯稿悕绉?寮傚父淇℃伅+寮傚父鐨勪綅缃€?br>}
---------------------------------------------------------
寮傚父澶勭悊鍘熷垯锛氬姛鑳芥姏鍑哄嚑涓紓甯革紝鍔熻兘璋冪敤濡傛灉杩涜try澶勭悊锛岄渶瑕佷笌涔嬪搴旂殑catch澶勭悊浠g爜鍧楋紝杩欐牱鐨勫鐞嗘湁閽堝鎬э紝鎶涘嚑涓氨澶勭悊鍑犱釜銆?br>
鐗规畩鎯呭喌锛歵ry瀵瑰簲澶氫釜catch鏃讹紝濡傛灉鏈夌埗绫荤殑catch璇彞鍧楋紝涓€瀹氳鏀惧湪涓嬮潰銆?br>
throw 鍜宼hrows鍏抽敭瀛楃殑鍖哄埆锛?br>throw鐢ㄤ簬鎶涘嚭寮傚父瀵硅薄锛屽悗闈㈣窡鐨勬槸寮傚父瀵硅薄锛泃hrow鐢ㄥ湪鍑芥暟鍐呫€?br>throws鐢ㄤ簬鎶涘嚭寮傚父绫伙紝鍚庨潰璺熺殑寮傚父绫诲悕锛屽彲浠ヨ窡澶氫釜锛岀敤閫楀彿闅斿紑銆倀hrows鐢ㄥ湪鍑芥暟涓娿€?br>
閫氬父鎯呭喌锛氬嚱鏁板唴瀹瑰鏋滄湁throw锛屾姏鍑哄紓甯稿璞★紝骞舵病鏈夎繘琛屽鐞嗭紝閭d箞鍑芥暟涓婁竴瀹氳澹版槑锛屽惁鍒欑紪璇戝け璐ャ€備絾鏄篃鏈夌壒娈婃儏鍐点€?br>
寮傚父鍒嗕袱绉嶏細
1锛氱紪璇戞椂琚鏌ョ殑寮傚父锛屽彧瑕佹槸Exception鍙婂叾瀛愮被閮芥槸缂栬瘧鏃惰妫€娴嬬殑寮傚父銆?br>2锛氳繍琛屾椂寮傚父锛屽叾涓璄xception鏈変竴涓壒娈婄殑瀛愮被RuntimeException锛屼互鍙奟untimeException鐨勫瓙绫绘槸杩愯寮傚父锛屼篃灏辫杩欎釜寮傚父鏄紪璇戞椂涓嶈妫€鏌ョ殑寮傚父銆?br>
缂栬瘧鏃惰妫€鏌ョ殑寮傚父鍜岃繍琛屾椂寮傚父鐨勫尯鍒細
缂栬瘧琚鏌ョ殑寮傚父鍦ㄥ嚱鏁板唴琚姏鍑猴紝鍑芥暟蹇呴』瑕佸0鏄庯紝鍚︾紪璇戝け璐ャ€?br>澹版槑鐨勫師鍥狅細鏄渶瑕佽皟鐢ㄨ€呭璇ュ紓甯歌繘琛屽鐞嗐€?br>杩愯鏃跺紓甯稿鏋滃湪鍑芥暟鍐呰鎶涘嚭锛屽湪鍑芥暟涓婁笉闇€瑕佸0鏄庛€?br>涓嶅0鏄庣殑鍘熷洜锛氫笉闇€瑕佽皟鐢ㄨ€呭鐞嗭紝杩愯鏃跺紓甯稿彂鐢燂紝宸茬粡鏃犳硶鍐嶈绋嬪簭缁х画杩愯锛屾墍浠ワ紝涓嶈璋冪敤澶勭悊鐨勶紝鐩存帴璁╃▼搴忓仠姝紝鐢辫皟鐢ㄨ€呭浠g爜杩涜淇銆?br>
瀹氫箟寮傚父澶勭悊鏃讹紝浠€涔堟椂鍊欏畾涔塼ry锛屼粈涔堟椂鍊欏畾涔塼hrows鍛紵
鍔熻兘鍐呴儴濡傛灉鍑虹幇寮傚父锛屽鏋滃唴閮ㄥ彲浠ュ鐞嗭紝灏辩敤try锛?br>濡傛灉鍔熻兘鍐呴儴澶勭悊涓嶄簡锛屽氨蹇呴』澹版槑鍑烘潵锛岃璋冪敤鑰呭鐞嗐€?br>
鑷畾涔夊紓甯革細褰撳紑鍙戞椂锛岄」鐩腑鍑虹幇浜唈ava涓病鏈夊畾涔夎繃鐨勯棶棰樻椂锛岃繖鏃跺氨闇€瑕佹垜浠寜鐓ava寮傚父寤虹珛鎬濇兂锛屽皢椤圭洰鐨勪腑鐨勭壒鏈夐棶棰樹篃杩涜瀵硅薄鐨勫皝瑁呫€傝繖涓紓甯革紝绉颁负鑷畾涔夊紓甯搞€?br>
瀵逛簬闄ゆ硶杩愮畻锛?浣滀负闄ゆ暟鏄笉鍙互鐨勩€俲ava涓杩欑闂鐢ˋrithmeticException绫昏繘琛屾弿杩般€傚浜庤繖涓姛鑳斤紝鍦ㄦ垜浠」鐩腑锛岄櫎鏁伴櫎浜嗕笉鍙互涓?澶栵紝杩樹笉鍙互涓鸿礋鏁般€傚彲鏄礋鏁扮殑閮ㄥ垎java骞舵病鏈夐拡瀵规弿杩般€傛墍浠ユ垜浠氨闇€瑕佽嚜瀹氫箟杩欎釜寮傚父銆?br>
鑷畾涔夊紓甯哥殑姝ラ锛?br>1锛氬畾涔変竴涓瓙绫荤户鎵縀xception鎴朢untimeException锛岃璇ョ被鍏峰鍙姏鎬с€?br>2锛氶€氳繃throw 鎴栬€卼hrows杩涜鎿嶄綔銆?br>
寮傚父鐨勮浆鎹㈡€濇兂锛氬綋鍑虹幇鐨勫紓甯告槸璋冪敤鑰呭鐞嗕笉浜嗙殑锛屽氨闇€瑕佸皢姝ゅ紓甯歌浆鎹负涓€涓皟鐢ㄨ€呭彲浠ュ鐞嗙殑寮傚父鎶涘嚭銆?br>
try catch finally鐨勫嚑绉嶇粨鍚堟柟寮忥細
2,
try
catch
3,
try
finally
1锛?br>try
catch
finally
杩欑鎯呭喌锛屽鏋滃嚭鐜板紓甯革紝骞朵笉澶勭悊锛屼絾鏄祫婧愪竴瀹氬叧闂紝鎵€浠ry finally闆嗗悎鍙负鍏抽棴璧勬簮銆?br>璁颁綇锛歠inally寰堟湁鐢紝涓昏鐢ㄦ埛鍏抽棴璧勬簮銆傛棤璁烘槸鍚﹀彂鐢熷紓甯革紝璧勬簮閮藉繀椤昏繘琛屽叧闂€?br>System.exit(0); //閫€鍑簀vm锛屽彧鏈夎繖绉嶆儏鍐礷inally涓嶆墽琛屻€?br>
褰撳紓甯稿嚭鐜板悗锛屽湪瀛愮埗绫昏繘琛岃鐩栨椂锛屾湁浜嗕竴浜涙柊鐨勭壒鐐癸細
1锛氬綋瀛愮被瑕嗙洊鐖剁被鐨勬柟娉曟椂锛屽鏋滅埗绫荤殑鏂规硶鎶涘嚭浜嗗紓甯革紝閭d箞瀛愮被鐨勬柟娉曡涔堜笉鎶涘嚭寮傚父瑕佷箞鎶涘嚭鐖剁被寮傚父鎴栬€呰寮傚父鐨勫瓙绫伙紝涓嶈兘鎶涘嚭鍏朵粬寮傚父銆?br>2锛氬鏋滅埗绫绘姏鍑轰簡澶氫釜寮傚父锛岄偅涔堝瓙绫诲湪瑕嗙洊鏃跺彧鑳芥姏鍑虹埗绫荤殑寮傚父鐨勫瓙闆嗐€?br>
娉ㄦ剰锛?br>濡傛灉鐖剁被鎴栬€呮帴鍙d腑鐨勬柟娉曟病鏈夋姏鍑鸿繃寮傚父锛岄偅涔堝瓙绫绘槸涓嶅彲浠ユ姏鍑哄紓甯哥殑锛屽鏋滃瓙绫荤殑瑕嗙洊鐨勬柟娉曚腑鍑虹幇浜嗗紓甯革紝鍙兘try涓嶈兘throws銆?br>濡傛灉杩欎釜寮傚父瀛愮被鏃犳硶澶勭悊锛屽凡缁忓奖鍝嶄簡瀛愮被鏂规硶鐨勫叿浣撹繍绠楋紝杩欐椂鍙互鍦ㄥ瓙绫绘柟娉曚腑锛岄€氳繃throw鎶涘嚭RuntimeException寮傚父鎴栬€呭叾瀛愮被锛岃繖鏍凤紝瀛愮被鐨勬柟娉曚笂鏄笉闇€瑕乼hrows澹版槑鐨勩€?br>
甯歌寮傚父锛?br>1銆佽剼鏍囪秺鐣屽紓甯革紙IndexOutOfBoundsException锛夊寘鎷暟缁勩€佸瓧绗︿覆锛?br>绌烘寚閽堝紓甯革紙NullPointerException锛?br>2銆佺被鍨嬭浆鎹㈠紓甯革細ClassCastException
3銆佹病鏈夎繖涓厓绱犲紓甯革細NullPointerException
4銆佷笉鏀寔鎿嶄綔寮傚父锛?br>寮傚父瑕佸敖閲忛伩鍏嶏紝濡傛灉閬垮厤涓嶄簡锛岄渶瑕侀鍏堢粰鍑哄鐞嗘柟寮忋€傛瘮濡傚搴鑽紝姣斿鐏伀鍣ㄣ€?br>-----------------------------------------------------------------------------------------------
鍖咃細瀹氫箟鍖呯敤package鍏抽敭瀛椼€?/h3>
1锛氬绫绘枃浠惰繘琛屽垎绫荤鐞嗐€?br>2锛氱粰绫绘枃浠舵彁渚涘灞傚悕绉扮┖闂淬€?br>
濡傛灉鐢熸垚鐨勫寘涓嶅湪褰撳墠鐩綍涓嬶紝闇€瑕佹渶濂芥墽琛宑lasspath锛屽皢鍖呮墍鍦ㄧ埗鐩綍瀹氫箟鍒癱lasspath鍙橀噺涓嵆鍙€?br>涓€鑸湪瀹氫箟鍖呭悕鏃讹紝鍥犱负鍖呯殑鍑虹幇鏄负浜嗗尯鍒嗛噸鍚嶇殑绫汇€傛墍浠ュ寘鍚嶈灏介噺鍞竴銆傛€庝箞淇濊瘉鍞竴鎬у憿锛熷彲浠ヤ娇鐢╱rl鍩熷悕鏉ヨ繘琛屽寘鍚嶇О鐨勫畾涔夈€?br>package pack;//瀹氫箟浜嗕竴涓寘锛屽悕绉颁负pack銆?娉ㄦ剰锛氬寘鍚嶇殑鍐欐硶瑙勮寖锛氭墍鏈夊瓧姣嶉兘灏忓啓銆?br>//package cn.itcast.pack.demo;
绫荤殑鍏ㄥ悕绉版槸 鍖呭悕.绫诲悕
缂栬瘧鍛戒护锛歫avac 鈥揹 浣嶇疆锛?褰撳墠璺緞锛?java婧愭枃浠?锛堝氨鍙互鑷姩鐢熸垚鍖咃級
鍖呮槸涓€绉嶅皝瑁呭舰寮忥紝鐢ㄤ簬灏佽绫伙紝鎯宠琚寘浠ュ鐨勭▼搴忚闂紝璇ョ被蹇呴』public锛?br>绫讳腑鐨勬垚鍛橈紝濡傛灉琚寘浠ュ璁块棶锛屼篃蹇呴』public锛?br>
鍖呬笌鍖呬箣闂磋闂彲浠ヤ娇鐢ㄧ殑鏉冮檺鏈変袱绉嶏細
1锛歱ublic
2锛歱rotected锛氬彧鑳芥槸涓嶅悓鍖呬腑鐨勫瓙绫诲彲浠ヤ娇鐢ㄧ殑鏉冮檺銆?br>
鎬荤粨java涓殑鍥涚鏉冮檺锛?/h1>
鑼? 鍥?nbsp; public protected default private
鍚屼竴涓被涓?nbsp; ok ok ok ok
鍚?涓€ 鍖?涓?nbsp; ok ok ok
瀛? 绫? ok
涓?鍚屽寘 涓?nbsp; ok
-----------------------------------------------------------------------------------------------
Import - 瀵煎叆锛氱被鍚嶇О鍙橀暱锛屽啓璧锋潵寰堥夯鐑︺€備负浜嗙畝鍖栵紝浣跨敤浜嗕竴涓叧閿瓧锛歩mport锛屽彲浠ヤ娇鐢ㄨ繖涓叧閿瓧瀵煎叆鎸囧畾鍖呬腑鐨勭被銆傝浣忥細瀹為檯寮€鍙戞椂,鍒扮殑鍝釜绫诲氨瀵煎叆鍝釜绫伙紝涓嶅缓璁娇鐢?.
import packa.*;//杩欎釜浠呬粎鏄鍏ヤ簡packa褰撳墠鐩綍涓嬬殑鎵€鏈夌殑绫汇€備笉鍖呭惈瀛愬寘銆?br>import packa.abc.*;//瀵煎叆浜唒acka鍖呬腑鐨勫瓙鍖卆bc涓嬬殑褰撳墠鐨勬墍鏈夌被銆?br>
濡傛灉瀵煎叆鐨勪袱涓寘涓瓨鍦ㄧ潃鐩稿悓鍚嶇О鐨勭被銆傝繖鏃跺鏋滅敤鍒拌绫伙紝蹇呴』鍦ㄤ唬鐮佷腑鎸囧畾鍖呭悕銆?br>
甯歌鐨勮蒋浠跺寘:
java.lang : language java鐨勬牳蹇冨寘锛孫bject System String Throwable jdk1.2鐗堟湰鍚庯紝璇ュ寘涓殑绫昏嚜鍔ㄨ瀵煎叆銆?br>java.awt : 瀹氫箟鐨勯兘鏄敤浜巎ava鍥惧舰鐣岄潰寮€鍙戠殑瀵硅薄銆?br>javax.swing: 鎻愪緵鎵€鏈夌殑windows妗岄潰搴旂敤绋嬪簭鍖呮嫭鐨勬帶浠?姣斿锛欶rame , Dialog, Table, List 绛夌瓑,灏辨槸java鐨勫浘褰㈢晫闈㈠簱銆?br>java.net : 鐢ㄤ簬java缃戠粶缂栫▼鏂归潰鐨勫璞¢兘鍦ㄨ鍖呬腑銆?br>java.io : input output 鐢ㄤ簬鎿嶄綔璁惧涓婃暟鎹殑瀵硅薄閮藉湪璇ュ寘涓€傛瘮濡傦細璇诲彇纭洏鏁版嵁锛屽線纭洏鍐欏叆鏁版嵁銆?br>java.util : java鐨勫伐鍏峰寘锛屾椂闂村璞★紝闆嗗悎妗嗘灦銆?br>java.applet: application+let 瀹㈡埛绔痡ava灏忕▼搴忋€俿erver+let --> servlet 鏈嶅姟绔痡ava灏忕▼搴忋€?br>
jar 锛歫ava鐨勫帇缂╁寘锛屼富瑕佺敤浜庡瓨鍌ㄧ被鏂囦欢锛屾垨鑰呴厤缃枃浠剁瓑銆?br>鍛戒护鏍煎紡锛歫ar 鈥揷f 鍖呭悕.jar 鍖呯洰褰?br> 瑙e帇缂╋細jar 鈥搙vf 鍖呭悕.jar
灏唈ar鍖呯洰褰曞垪琛ㄩ噸瀹氬悜鍒颁竴涓枃浠朵腑锛歫ar 鈥搕f 鍖呭悕.jar >c:1.txt
-----------------------------------------------------------------------------------------------
澶氱嚎绋嬶細鈽呪槄鈽呪槄
杩涚▼锛氭鍦ㄨ繘琛屼腑鐨勭▼搴忋€傚叾瀹炶繘绋嬪氨鏄竴涓簲鐢ㄧ▼搴忚繍琛屾椂鐨勫唴瀛樺垎閰嶇┖闂淬€?br>绾跨▼锛氬叾瀹炲氨鏄繘绋嬩腑涓€涓▼搴忔墽琛屾帶鍒跺崟鍏冿紝涓€鏉℃墽琛岃矾寰勩€傝繘绋嬭礋璐g殑鏄簲鐢ㄧ▼搴忕殑绌洪棿鐨勬爣绀恒€傜嚎绋嬭礋璐g殑鏄簲鐢ㄧ▼搴忕殑鎵ц椤哄簭銆?br>
涓€涓繘绋嬭嚦灏戞湁涓€涓嚎绋嬪湪杩愯锛屽綋涓€涓繘绋嬩腑鍑虹幇澶氫釜绾跨▼鏃讹紝灏辩О杩欎釜搴旂敤绋嬪簭鏄绾跨▼搴旂敤绋嬪簭锛屾瘡涓嚎绋嬪湪鏍堝尯涓兘鏈夎嚜宸辩殑鎵ц绌洪棿锛岃嚜宸辩殑鏂规硶鍖恒€佽嚜宸辩殑鍙橀噺銆?br>jvm鍦ㄥ惎鍔ㄧ殑鏃讹紝棣栧厛鏈変竴涓富绾跨▼锛岃礋璐g▼搴忕殑鎵ц锛岃皟鐢ㄧ殑鏄痬ain鍑芥暟銆備富绾跨▼鎵ц鐨勪唬鐮侀兘鍦╩ain鏂规硶涓€?br>褰撲骇鐢熷瀮鍦炬椂锛屾敹鍨冨溇鐨勫姩浣滐紝鏄笉闇€瑕佷富绾跨▼鏉ュ畬鎴愶紝鍥犱负杩欐牱锛屼細鍑虹幇涓荤嚎绋嬩腑鐨勪唬鐮佹墽琛屼細鍋滄锛屼細鍘昏繍琛屽瀮鍦惧洖鏀跺櫒浠g爜锛屾晥鐜囪緝浣庯紝鎵€浠ョ敱鍗曠嫭涓€涓嚎绋嬫潵璐熻矗鍨冨溇鍥炴敹銆?br>
闅忔満鎬х殑鍘熺悊锛氬洜涓篶pu鐨勫揩閫熷垏鎹㈤€犳垚锛屽摢涓嚎绋嬭幏鍙栧埌浜哻pu鐨勬墽琛屾潈锛屽摢涓嚎绋嬪氨鎵ц銆?br>
杩斿洖褰撳墠绾跨▼鐨勫悕绉帮細Thread.currentThread().getName()
绾跨▼鐨勫悕绉版槸鐢憋細Thread-缂栧彿瀹氫箟鐨勩€傜紪鍙蜂粠0寮€濮嬨€?br>绾跨▼瑕佽繍琛岀殑浠g爜閮界粺涓€瀛樻斁鍦ㄤ簡run鏂规硶涓€?br>
绾跨▼瑕佽繍琛屽繀椤昏閫氳繃绫讳腑鎸囧畾鐨勬柟娉曞紑鍚€俿tart鏂规硶銆傦紙鍚姩鍚庯紝灏卞浜嗕竴鏉℃墽琛岃矾寰勶級
start鏂规硶锛?锛夈€佸惎鍔ㄤ簡绾跨▼锛?锛夈€佽jvm璋冪敤浜唕un鏂规硶銆?br>
鍒涘缓绾跨▼鐨勭涓€绉嶆柟寮忥細
缁ф壙Thread 锛岀敱瀛愮被澶嶅啓run鏂规硶銆?br>姝ラ锛?br>1锛屽畾涔夌被缁ф壙Thread绫伙紱
2锛岀洰鐨勬槸澶嶅啓run鏂规硶锛屽皢瑕佽绾跨▼杩愯鐨勪唬鐮侀兘瀛樺偍鍒皉un鏂规硶涓紱
3锛岄€氳繃鍒涘缓Thread绫荤殑瀛愮被瀵硅薄锛屽垱寤虹嚎绋嬪璞★紱
4锛岃皟鐢ㄧ嚎绋嬬殑start鏂规硶锛屽紑鍚嚎绋嬶紝骞舵墽琛宺un鏂规硶銆?br>
绾跨▼鐘舵€侊細
琚垱寤猴細start()
杩愯锛氬叿澶囨墽琛岃祫鏍硷紝鍚屾椂鍏峰鎵ц鏉冿紱
鍐荤粨锛歴leep(time),wait()鈥攏otify()鍞ら啋锛涚嚎绋嬮噴鏀句簡鎵ц鏉冿紝鍚屾椂閲婃斁鎵ц璧勬牸锛?br>涓存椂闃诲鐘舵€侊細绾跨▼鍏峰cpu鐨勬墽琛岃祫鏍硷紝娌℃湁cpu鐨勬墽琛屾潈锛?br>娑堜骸锛歴top()
鍒涘缓绾跨▼鐨勭浜岀鏂瑰紡锛?/h3>
瀹炵幇涓€涓帴鍙unnable銆?br>姝ラ锛?br>1锛屽畾涔夌被瀹炵幇Runnable鎺ュ彛銆?br>2锛岃鐩栨帴鍙d腑鐨剅un鏂规硶锛堢敤浜庡皝瑁呯嚎绋嬭杩愯鐨勪唬鐮侊級銆?br>3锛岄€氳繃Thread绫诲垱寤虹嚎绋嬪璞★紱
4锛屽皢瀹炵幇浜哛unnable鎺ュ彛鐨勫瓙绫诲璞′綔涓哄疄闄呭弬鏁颁紶閫掔粰Thread绫讳腑鐨勬瀯閫犲嚱鏁般€?br>涓轰粈涔堣浼犻€掑憿锛熷洜涓鸿璁╃嚎绋嬪璞℃槑纭杩愯鐨剅un鏂规硶鎵€灞炵殑瀵硅薄銆?br>5锛岃皟鐢═hread瀵硅薄鐨剆tart鏂规硶銆傚紑鍚嚎绋嬶紝骞惰繍琛孯unnable鎺ュ彛瀛愮被涓殑run鏂规硶銆?br>Ticket t = new Ticket();
/*
鐩存帴鍒涘缓Ticket瀵硅薄锛屽苟涓嶆槸鍒涘缓绾跨▼瀵硅薄銆?br> 鍥犱负鍒涘缓瀵硅薄鍙兘閫氳繃new Thread绫伙紝鎴栬€卬ew Thread绫荤殑瀛愮被鎵嶅彲浠ャ€?br> 鎵€浠ユ渶缁堟兂瑕佸垱寤虹嚎绋嬨€傛棦鐒舵病鏈変簡Thread绫荤殑瀛愮被锛屽氨鍙兘鐢═hread绫汇€?br> */
Thread t1 = new Thread(t); //鍒涘缓绾跨▼銆?br> /*
鍙灏唗浣滀负Thread绫荤殑鏋勯€犲嚱鏁扮殑瀹為檯鍙傛暟浼犲叆鍗冲彲瀹屾垚绾跨▼瀵硅薄鍜宼涔嬮棿鐨勫叧鑱?br> 涓轰粈涔堣灏唗浼犵粰Thread绫荤殑鏋勯€犲嚱鏁板憿锛熷叾瀹炲氨鏄负浜嗘槑纭嚎绋嬭杩愯鐨勪唬鐮乺un鏂规硶銆?br> */
t1.start();
涓轰粈涔堣鏈塕unnable鎺ュ彛鐨勫嚭鐜帮紵
1锛氶€氳繃缁ф壙Thread绫荤殑鏂瑰紡锛屽彲浠ュ畬鎴愬绾跨▼鐨勫缓绔嬨€備絾鏄繖绉嶆柟寮忔湁涓€涓眬闄愭€э紝濡傛灉涓€涓被宸茬粡鏈変簡鑷繁鐨勭埗绫伙紝灏变笉鍙互缁ф壙Thread绫伙紝鍥犱负java鍗曠户鎵跨殑灞€闄愭€с€?br>鍙槸璇ョ被涓殑杩樻湁閮ㄥ垎浠g爜闇€瑕佽澶氫釜绾跨▼鍚屾椂鎵ц銆傝繖鏃舵€庝箞鍔炲憿锛?br>鍙湁瀵硅绫昏繘琛岄澶栫殑鍔熻兘鎵╁睍锛宩ava灏辨彁渚涗簡涓€涓帴鍙unnable銆傝繖涓帴鍙d腑瀹氫箟浜唕un鏂规硶锛屽叾瀹瀝un鏂规硶鐨勫畾涔夊氨鏄负浜嗗瓨鍌ㄥ绾跨▼瑕佽繍琛岀殑浠g爜銆?br>鎵€浠ワ紝閫氬父鍒涘缓绾跨▼閮界敤绗簩绉嶆柟寮忋€?br>鍥犱负瀹炵幇Runnable鎺ュ彛鍙互閬垮厤鍗曠户鎵跨殑灞€闄愭€с€?br>
2锛氬叾瀹炴槸灏嗕笉鍚岀被涓渶瑕佽澶氱嚎绋嬫墽琛岀殑浠g爜杩涜鎶藉彇銆傚皢澶氱嚎绋嬭杩愯鐨勪唬鐮佺殑浣嶇疆鍗曠嫭瀹氫箟鍒版帴鍙d腑銆備负鍏朵粬绫昏繘琛屽姛鑳芥墿灞曟彁渚涗簡鍓嶆彁銆?br>鎵€浠hread绫诲湪鎻忚堪绾跨▼鏃讹紝鍐呴儴瀹氫箟鐨剅un鏂规硶锛屼篃鏉ヨ嚜浜嶳unnable鎺ュ彛銆?br>
瀹炵幇Runnable鎺ュ彛鍙互閬垮厤鍗曠户鎵跨殑灞€闄愭€с€傝€屼笖锛岀户鎵縏hread锛屾槸鍙互瀵筎hread绫讳腑鐨勬柟娉曪紝杩涜瀛愮被澶嶅啓鐨勩€備絾鏄笉闇€瑕佸仛杩欎釜澶嶅啓鍔ㄤ綔鐨勮瘽锛屽彧涓哄畾涔夌嚎绋嬩唬鐮佸瓨鏀句綅缃紝瀹炵幇Runnable鎺ュ彛鏇存柟渚夸竴浜涖€傛墍浠unnable鎺ュ彛灏嗙嚎绋嬭鎵ц鐨勪换鍔″皝瑁呮垚浜嗗璞°€?br>-------------------------------------------------------
//闈㈣瘯
new Thread(new Runnable(){ //鍖垮悕
public void run(){
System.out.println("runnable run");
}
})
{
public void run(){
System.out.println("subthread run");
}
}.start(); //缁撴灉锛歴ubthread run
---------------------------------------------------------
Try {
Thread.sleep(10);
}catch(InterruptedException e){}// 褰撳埢鎰忚绾跨▼绋嶅井鍋滀竴涓嬶紝妯℃嫙cpu 鍒囨崲鎯呭喌銆?br>
澶氱嚎绋嬪畨鍏ㄩ棶棰樼殑鍘熷洜锛?br>閫氳繃鍥捐В锛氬彂鐜颁竴涓嚎绋嬪湪鎵ц澶氭潯璇彞鏃讹紝骞惰繍绠楀悓涓€涓暟鎹椂锛屽湪鎵ц杩囩▼涓紝鍏朵粬绾跨▼鍙備笌杩涙潵锛屽苟鎿嶄綔浜嗚繖涓暟鎹€傚鑷村埌浜嗛敊璇暟鎹殑浜х敓銆?br>
娑夊強鍒颁袱涓洜绱狅細
1锛屽涓嚎绋嬪湪鎿嶄綔鍏变韩鏁版嵁銆?br>2锛屾湁澶氭潯璇彞瀵瑰叡浜暟鎹繘琛岃繍绠椼€?br>鍘熷洜锛氳繖澶氭潯璇彞锛屽湪鏌愪竴涓椂鍒昏涓€涓嚎绋嬫墽琛屾椂锛岃繕娌℃湁鎵ц瀹岋紝灏辫鍏朵粬绾跨▼鎵ц浜嗐€?br>
瑙e喅瀹夊叏闂鐨勫師鐞嗭細
鍙灏嗘搷浣滃叡浜暟鎹殑璇彞鍦ㄦ煇涓€鏃舵璁╀竴涓嚎绋嬫墽琛屽畬锛屽湪鎵ц杩囩▼涓紝鍏朵粬绾跨▼涓嶈兘杩涙潵鎵ц灏卞彲浠ヨВ鍐宠繖涓棶棰樸€?br>
濡備綍杩涜澶氬彞鎿嶄綔鍏变韩鏁版嵁浠g爜鐨勫皝瑁呭憿锛?br>java涓彁渚涗簡涓€涓В鍐虫柟寮忥細灏辨槸鍚屾浠g爜鍧椼€?br>鏍煎紡锛?br>synchronized(瀵硅薄) { // 浠绘剰瀵硅薄閮藉彲浠ャ€傝繖涓璞″氨鏄攣銆?br> 闇€瑕佽鍚屾鐨勪唬鐮侊紱
}
---------------------------------------------------------------
鍚屾锛氣槄鈽呪槄鈽呪槄
濂藉锛氳В鍐充簡绾跨▼瀹夊叏闂銆?br>寮婄锛氱浉瀵归檷浣庢€ц兘锛屽洜涓哄垽鏂攣闇€瑕佹秷鑰楄祫婧愶紝浜х敓浜嗘閿併€?br>
瀹氫箟鍚屾鏄湁鍓嶆彁鐨勶細
1锛屽繀椤昏鏈変袱涓垨鑰呬袱涓互涓婄殑绾跨▼锛屾墠闇€瑕佸悓姝ャ€?br>2锛屽涓嚎绋嬪繀椤讳繚璇佷娇鐢ㄧ殑鏄悓涓€涓攣銆?br>
鍚屾鐨勭浜岀琛ㄧ幇褰㈠紡锛?br>鍚屾鍑芥暟锛氬叾瀹炲氨鏄皢鍚屾鍏抽敭瀛楀畾涔夊湪鍑芥暟涓婏紝璁╁嚱鏁板叿澶囦簡鍚屾鎬с€?br>
鍚屾鍑芥暟鏄敤鐨勫摢涓攣鍛紵
閫氳繃楠岃瘉锛屽嚱鏁伴兘鏈夎嚜宸辨墍灞炵殑瀵硅薄this锛屾墍浠ュ悓姝ュ嚱鏁版墍浣跨敤鐨勯攣灏辨槸this閿併€?br>
褰撳悓姝ュ嚱鏁拌static淇グ鏃讹紝杩欐椂鐨勫悓姝ョ敤鐨勬槸鍝釜閿佸憿锛?br>闈欐€佸嚱鏁板湪鍔犺浇鏃舵墍灞炰簬绫伙紝杩欐椂鏈夊彲鑳借繕娌℃湁璇ョ被浜х敓鐨勫璞★紝浣嗘槸璇ョ被鐨勫瓧鑺傜爜鏂囦欢鍔犺浇杩涘唴瀛樺氨宸茬粡琚皝瑁呮垚浜嗗璞★紝杩欎釜瀵硅薄灏辨槸璇ョ被鐨勫瓧鑺傜爜鏂囦欢瀵硅薄銆?br>鎵€浠ラ潤鎬佸姞杞芥椂锛屽彧鏈変竴涓璞″瓨鍦紝閭d箞闈欐€佸悓姝ュ嚱鏁板氨浣跨敤鐨勮繖涓璞°€?br>杩欎釜瀵硅薄灏辨槸 绫诲悕.class
鍚屾浠g爜鍧楀拰鍚屾鍑芥暟鐨勫尯鍒紵
鍚屾浠g爜鍧椾娇鐢ㄧ殑閿佸彲浠ユ槸浠绘剰瀵硅薄銆?br>鍚屾鍑芥暟浣跨敤鐨勯攣鏄痶his锛岄潤鎬佸悓姝ュ嚱鏁扮殑閿佹槸璇ョ被鐨勫瓧鑺傜爜鏂囦欢瀵硅薄銆?br>
鍦ㄤ竴涓被涓彧鏈変竴涓悓姝ワ紝鍙互浣跨敤鍚屾鍑芥暟銆傚鏋滄湁澶氬悓姝ワ紝蹇呴』浣跨敤鍚屾浠g爜鍧楋紝鏉ョ‘瀹氫笉鍚岀殑閿併€傛墍浠ュ悓姝ヤ唬鐮佸潡鐩稿鐏垫椿涓€浜涖€?br>-------------------------------------------------------
鈽呰€冪偣闂锛?br>璇峰啓涓€涓欢杩熷姞杞界殑鍗曚緥妯″紡锛熷啓鎳掓眽寮忥紱褰撳嚭鐜板绾跨▼璁块棶鏃舵€庝箞瑙e喅锛熷姞鍚屾锛岃В鍐冲畨鍏ㄩ棶棰橈紱鏁堢巼楂樺悧锛熶笉楂橈紱鎬庢牱瑙e喅锛熼€氳繃鍙岄噸鍒ゆ柇鐨勫舰寮忚В鍐炽€?br>//鎳掓眽寮忥細寤惰繜鍔犺浇鏂瑰紡銆?br>褰撳绾跨▼璁块棶鎳掓眽寮忔椂锛屽洜涓烘噿姹夊紡鐨勬柟娉曞唴瀵瑰叡鎬ф暟鎹繘琛屽鏉¤鍙ョ殑鎿嶄綔銆傛墍浠ュ鏄撳嚭鐜扮嚎绋嬪畨鍏ㄩ棶棰樸€備负浜嗚В鍐筹紝鍔犲叆鍚屾鏈哄埗锛岃В鍐冲畨鍏ㄩ棶棰樸€備絾鏄嵈甯︽潵浜嗘晥鐜囬檷浣庛€?br>涓轰簡鏁堢巼闂锛岄€氳繃鍙岄噸鍒ゆ柇鐨勫舰寮忚В鍐炽€?br>class Single{
private static Single s = null;
private Single(){}
public static Single getInstance(){ //閿佹槸璋侊紵瀛楄妭鐮佹枃浠跺璞★紱
if(s == null){
synchronized(Single.class){
if(s == null)
s = new Single();
}
}
return s;
}
}
---------------------------------------------------------
鍚屾姝婚攣锛氶€氬父鍙灏嗗悓姝ヨ繘琛屽祵濂楋紝灏卞彲浠ョ湅鍒扮幇璞°€傚悓姝ュ嚱鏁颁腑鏈夊悓姝ヤ唬鐮佸潡锛屽悓姝ヤ唬鐮佸潡涓繕鏈夊悓姝ュ嚱鏁般€?br>
绾跨▼闂撮€氫俊锛?/h2>
鎬濊矾锛氬涓嚎绋嬪湪鎿嶄綔鍚屼竴涓祫婧愶紝浣嗘槸鎿嶄綔鐨勫姩浣滃嵈涓嶄竴鏍枫€?br>1锛氬皢璧勬簮灏佽鎴愬璞°€?br>2锛氬皢绾跨▼鎵ц鐨勪换鍔?浠诲姟鍏跺疄灏辨槸run鏂规硶銆?涔熷皝瑁呮垚瀵硅薄銆?br>
绛夊緟鍞ら啋鏈哄埗锛氭秹鍙婄殑鏂规硶锛?br>wait:灏嗗悓姝ヤ腑鐨勭嚎绋嬪浜庡喕缁撶姸鎬併€傞噴鏀句簡鎵ц鏉冿紝閲婃斁浜嗚祫鏍笺€傚悓鏃跺皢绾跨▼瀵硅薄瀛樺偍鍒扮嚎绋嬫睜涓€?br>notify锛氬敜閱掔嚎绋嬫睜涓煇涓€涓瓑寰呯嚎绋嬨€?br>notifyAll:鍞ら啋鐨勬槸绾跨▼姹犱腑鐨勬墍鏈夌嚎绋嬨€?br>
娉ㄦ剰锛?br>1锛氳繖浜涙柟娉曢兘闇€瑕佸畾涔夊湪鍚屾涓€?br>2锛氬洜涓鸿繖浜涙柟娉曞繀椤昏鏍囩ず鎵€灞炵殑閿併€?br> 浣犺鐭ラ亾 A閿佷笂鐨勭嚎绋嬭wait浜?閭h繖涓嚎绋嬪氨鐩稿綋浜庡浜嶢閿佺殑绾跨▼姹犱腑锛屽彧鑳紸閿佺殑notify鍞ら啋銆?br>3锛氳繖涓変釜鏂规硶閮藉畾涔夊湪Object绫讳腑銆備负浠€涔堟搷浣滅嚎绋嬬殑鏂规硶瀹氫箟鍦∣bject绫讳腑锛?br> 鍥犱负杩欎笁涓柟娉曢兘闇€瑕佸畾涔夊悓姝ュ唴锛屽苟鏍囩ず鎵€灞炵殑鍚屾閿侊紝鏃㈢劧琚攣璋冪敤锛岃€岄攣鍙堝彲浠ユ槸浠绘剰瀵硅薄锛岄偅涔堣兘琚换鎰忓璞¤皟鐢ㄧ殑鏂规硶涓€瀹氬畾涔夊湪Object绫讳腑銆?br>
wait鍜宻leep鍖哄埆锛?鍒嗘瀽杩欎袱涓柟娉曪細浠庢墽琛屾潈鍜岄攣涓婃潵鍒嗘瀽锛?br>wait锛氬彲浠ユ寚瀹氭椂闂翠篃鍙互涓嶆寚瀹氭椂闂淬€備笉鎸囧畾鏃堕棿锛屽彧鑳界敱瀵瑰簲鐨刵otify鎴栬€卬otifyAll鏉ュ敜閱掋€?br>sleep锛氬繀椤绘寚瀹氭椂闂达紝鏃堕棿鍒拌嚜鍔ㄤ粠鍐荤粨鐘舵€佽浆鎴愯繍琛岀姸鎬?涓存椂闃诲鐘舵€?銆?br>wait锛氱嚎绋嬩細閲婃斁鎵ц鏉冿紝鑰屼笖绾跨▼浼氶噴鏀鹃攣銆?br>Sleep锛氱嚎绋嬩細閲婃斁鎵ц鏉冿紝浣嗘槸涓嶉噴鏀鹃攣銆?br>
绾跨▼鐨勫仠姝細閫氳繃stop鏂规硶灏卞彲浠ュ仠姝㈢嚎绋嬨€備絾鏄繖涓柟寮忚繃鏃朵簡銆?br>鍋滄绾跨▼锛氬師鐞嗗氨鏄細璁╃嚎绋嬭繍琛岀殑浠g爜缁撴潫锛屼篃灏辨槸缁撴潫run鏂规硶銆?br>鎬庝箞缁撴潫run鏂规硶锛熶竴鑸瑀un鏂规硶閲岃偗瀹氬畾涔夊惊鐜€傛墍浠ュ彧瑕佺粨鏉熷惊鐜嵆鍙€?br>绗竴绉嶆柟寮忥細瀹氫箟寰幆鐨勭粨鏉熸爣璁般€?br>绗簩绉嶆柟寮忥細濡傛灉绾跨▼澶勪簬浜嗗喕缁撶姸鎬侊紝鏄笉鍙兘璇诲埌鏍囪鐨勶紝杩欐椂灏遍渶瑕侀€氳繃Thread绫讳腑鐨刬nterrupt鏂规硶锛屽皢鍏跺喕缁撶姸鎬佸己鍒舵竻闄ゃ€傝绾跨▼鎭㈠鍏峰鎵ц璧勬牸鐨勭姸鎬侊紝璁╃嚎绋嬪彲浠ヨ鍒版爣璁帮紝骞剁粨鏉熴€?br>
---------< java.lang.Thread >----------
interrupt()锛氫腑鏂嚎绋嬨€?br>setPriority(int newPriority)锛氭洿鏀圭嚎绋嬬殑浼樺厛绾с€?br>getPriority()锛氳繑鍥炵嚎绋嬬殑浼樺厛绾с€?br>toString()锛氳繑鍥炶绾跨▼鐨勫瓧绗︿覆琛ㄧず褰㈠紡锛屽寘鎷嚎绋嬪悕绉般€佷紭鍏堢骇鍜岀嚎绋嬬粍銆?br>Thread.yield()锛氭殏鍋滃綋鍓嶆鍦ㄦ墽琛岀殑绾跨▼瀵硅薄锛屽苟鎵ц鍏朵粬绾跨▼銆?br>setDaemon(true)锛氬皢璇ョ嚎绋嬫爣璁颁负瀹堟姢绾跨▼鎴栫敤鎴风嚎绋嬨€傚皢璇ョ嚎绋嬫爣璁颁负瀹堟姢绾跨▼鎴栫敤鎴风嚎绋嬨€傚綋姝e湪杩愯鐨勭嚎绋嬮兘鏄畧鎶ょ嚎绋嬫椂锛孞ava 铏氭嫙鏈洪€€鍑恒€傝鏂规硶蹇呴』鍦ㄥ惎鍔ㄧ嚎绋嬪墠璋冪敤銆?br>join锛氫复鏃跺姞鍏ヤ竴涓嚎绋嬬殑鏃跺€欏彲浠ヤ娇鐢╦oin鏂规硶銆?br>褰揂绾跨▼鎵ц鍒颁簡B绾跨▼鐨刯oin鏂瑰紡銆侫绾跨▼澶勪簬鍐荤粨鐘舵€侊紝閲婃斁浜嗘墽琛屾潈锛孊寮€濮嬫墽琛屻€侫浠€涔堟椂鍊欐墽琛屽憿锛熷彧鏈夊綋B绾跨▼杩愯缁撴潫鍚庯紝A鎵嶄粠鍐荤粨鐘舵€佹仮澶嶈繍琛岀姸鎬佹墽琛屻€?br>-----------------------------------------------------------
Lock鎺ュ彛锛氬绾跨▼鍦↗DK1.5鐗堟湰鍗囩骇鏃讹紝鎺ㄥ嚭涓€涓帴鍙ock鎺ュ彛銆?br>瑙e喅绾跨▼瀹夊叏闂浣跨敤鍚屾鐨勫舰寮忥紝(鍚屾浠g爜鍧楋紝瑕佷箞鍚屾鍑芥暟)鍏跺疄鏈€缁堜娇鐢ㄧ殑閮芥槸閿佹満鍒躲€?br>
鍒颁簡鍚庢湡鐗堟湰锛岀洿鎺ュ皢閿佸皝瑁呮垚浜嗗璞°€傜嚎绋嬭繘鍏ュ悓姝ュ氨鏄叿澶囦簡閿侊紝鎵ц瀹岋紝绂诲紑鍚屾锛屽氨鏄噴鏀句簡閿併€?br>鍦ㄥ悗鏈熷閿佺殑鍒嗘瀽杩囩▼涓紝鍙戠幇锛岃幏鍙栭攣锛屾垨鑰呴噴鏀鹃攣鐨勫姩浣滃簲璇ユ槸閿佽繖涓簨鐗╂洿娓呮銆傛墍浠ュ皢杩欎簺鍔ㄤ綔瀹氫箟鍦ㄤ簡閿佸綋涓紝骞舵妸閿佸畾涔夋垚瀵硅薄銆?br>
鎵€浠ュ悓姝ユ槸闅愮ず鐨勯攣鎿嶄綔锛岃€孡ock瀵硅薄鏄樉绀虹殑閿佹搷浣滐紝瀹冪殑鍑虹幇灏辨浛浠d簡鍚屾銆?br>
鍦ㄤ箣鍓嶇殑鐗堟湰涓娇鐢∣bject绫讳腑wait銆乶otify銆乶otifyAll鐨勬柟寮忔潵瀹屾垚鐨勩€傞偅鏄洜涓哄悓姝ヤ腑鐨勯攣鏄换鎰忓璞★紝鎵€浠ユ搷浣滈攣鐨勭瓑寰呭敜閱掔殑鏂规硶閮藉畾涔夊湪Object绫讳腑銆?br>
鑰岀幇鍦ㄩ攣鏄寚瀹氬璞ock銆傛墍浠ユ煡鎵剧瓑寰呭敜閱掓満鍒舵柟寮忛渶瑕侀€氳繃Lock鎺ュ彛鏉ュ畬鎴愩€傝€孡ock鎺ュ彛涓苟娌℃湁鐩存帴鎿嶄綔绛夊緟鍞ら啋鐨勬柟娉曪紝鑰屾槸灏嗚繖浜涙柟寮忓張鍗曠嫭灏佽鍒颁簡涓€涓璞′腑銆傝繖涓璞″氨鏄疌ondition锛屽皢Object涓殑涓変釜鏂规硶杩涜鍗曠嫭鐨勫皝瑁呫€傚苟鎻愪緵浜嗗姛鑳戒竴鑷寸殑鏂规硶 await()銆乻ignal()銆乻ignalAll()浣撶幇鏂扮増鏈璞$殑濂藉銆?br>< java.util.concurrent.locks > Condition鎺ュ彛锛歛wait()銆乻ignal()銆乻ignalAll()锛?br>--------------------------------------------------------
class BoundedBuffer {
final Lock lock = new ReentrantLock();
final Condition notFull = lock.newCondition();
final Condition notEmpty = lock.newCondition();
final Object[] items = new Object[100];
int putptr, takeptr, count;
public void put(Object x) throws InterruptedException {
lock.lock();
try {
while (count == items.length)
notFull.await();
items[putptr] = x;
if (++putptr == items.length) putptr = 0;
++count;
notEmpty.signal();
}
finally {
lock.unlock();
}
}
public Object take() throws InterruptedException {
lock.lock();
try {
while (count == 0)
notEmpty.await();
Object x = items[takeptr];
if (++takeptr == items.length) takeptr = 0;
--count;
notFull.signal();
return x;
}
finally {
lock.unlock();
}
}
}
------------------------------------------------------------------------------------------------
API锛?/h1>
锛圓pplication Programming Interface,搴旂敤绋嬪簭缂栫▼鎺ュ彛锛夋槸涓€浜涢鍏堝畾涔夌殑鍑芥暟锛岀洰鐨勬槸鎻愪緵搴旂敤绋嬪簭涓庡紑鍙戜汉鍛樺熀浜庢煇杞欢鎴栫‖浠剁殑浠ヨ闂竴缁勪緥绋嬬殑鑳藉姏锛岃€屽張鏃犻渶璁块棶婧愮爜锛屾垨鐞嗚В鍐呴儴宸ヤ綔鏈哄埗鐨勭粏鑺傘€?br>
--< java.lang >-- String瀛楃涓诧細鈽呪槄鈽呪槅
java涓敤String绫昏繘琛屾弿杩般€傚瀛楃涓茶繘琛屼簡瀵硅薄鐨勫皝瑁呫€傝繖鏍风殑濂藉鏄彲浠ュ瀛楃涓茶繖绉嶅父瑙佹暟鎹繘琛屾柟渚跨殑鎿嶄綔銆傚璞″皝瑁呭悗锛屽彲浠ュ畾涔塏澶氬睘鎬у拰琛屼负銆?br>
濡備綍瀹氫箟瀛楃涓插璞″憿锛烻tring s = "abc";鍙鏄弻寮曞彿寮曡捣鐨勬暟鎹兘鏄瓧绗︿覆瀵硅薄銆?br>
鐗圭偣锛氬瓧绗︿覆涓€鏃﹁鍒濆鍖栵紝灏变笉鍙互琚敼鍙橈紝瀛樻斁鍦ㄦ柟娉曞尯涓殑甯搁噺姹犱腑銆?br>------------------------------------------------------
String s1 = "abc"; // s1鎸囧悜鐨勫唴瀛樹腑鍙湁涓€涓璞bc銆?br>String s2 = new String("abc"); // s2鎸囧悜鐨勫唴瀹逛腑鏈変袱涓璞bc銆乶ew 銆?br>
System.out.println(s1==s2);//false
System.out.println(s1.equals(s2));//true 锛屽瓧绗︿覆涓璭quals姣旇緝鐨勬槸瀛楃涓插唴瀹规槸鍚︾浉鍚屻€?br>-------------------------------------------------------
瀛楃涓茬殑鏂规硶锛?br>
1锛氭瀯閫犳柟娉曪細
灏嗗瓧鑺傛暟缁勬垨鑰呭瓧绗︽暟缁勮浆鎴愬瓧绗︿覆銆?br>String s1 = new String();//鍒涘缓浜嗕竴涓┖鍐呭鐨勫瓧绗︿覆銆?br>String s2 = null;//s2娌℃湁浠讳綍瀵硅薄鎸囧悜锛屾槸涓€涓猲ull甯搁噺鍊笺€?br>String s3 = "";//s3鎸囧悜涓€涓叿浣撶殑瀛楃涓插璞★紝鍙笉杩囪繖涓瓧绗︿覆涓病鏈夊唴瀹广€?br>//涓€鑸湪瀹氫箟瀛楃涓叉椂锛屼笉鐢╪ew銆?br>String s4 = new String("abc");
String s5 = "abc"; 涓€鑸敤姝ゅ啓娉?br>new String(char[]);//灏嗗瓧绗︽暟缁勮浆鎴愬瓧绗︿覆銆?br>new String(char[],offset,count);//灏嗗瓧绗︽暟缁勪腑鐨勪竴閮ㄥ垎杞垚瀛楃涓层€?br>
2锛氫竴鑸柟娉曪細
鎸夌収闈㈠悜瀵硅薄鐨勬€濇兂锛?br>2.1 鑾峰彇锛?br> 2.1.1锛氳幏鍙栧瓧绗︿覆鐨勯暱搴︺€俵ength();
2.1.2锛氭寚瀹氫綅缃殑瀛楃銆俢har charAt(int index);
2.1.3锛氳幏鍙栨寚瀹氬瓧绗︾殑浣嶇疆銆傚鏋滀笉瀛樺湪杩斿洖-1锛屾墍浠ュ彲浠ラ€氳繃杩斿洖鍊?1鏉ュ垽鏂煇涓€涓瓧绗︿笉瀛樺湪鐨勬儏鍐点€?br> int indexOf(int ch);//杩斿洖绗竴娆℃壘鍒扮殑瀛楃瑙掓爣
int indexOf(int ch,int fromIndex); //杩斿洖浠庢寚瀹氫綅缃紑濮嬬涓€娆℃壘鍒扮殑瑙掓爣
int indexOf(String str); //杩斿洖绗竴娆℃壘鍒扮殑瀛楃涓茶鏍?br> int indexOf(String str,int fromIndex);
int lastIndexOf(int ch);
int lastIndexOf(int ch,int fromIndex);
int lastIndexOf(String str);
int lastIndexOf(String str,int fromIndex);
2.1.4锛氳幏鍙栧瓙涓层€?br> String substring(int start);//浠巗tart浣嶅紑濮嬶紝鍒發ength()-1涓烘.
String substring(int start,int end);//浠巗tart寮€濮嬪埌end涓烘銆?/鍖呭惈start浣嶏紝涓嶅寘鍚玡nd浣嶃€?br> substring(0,str.length());//鑾峰彇鏁翠覆
2.2 鍒ゆ柇锛?br> 2.2.1锛氬瓧绗︿覆涓寘鍚寚瀹氱殑瀛楃涓插悧锛?br> boolean contains(String substring);
2.2.2锛氬瓧绗︿覆鏄惁浠ユ寚瀹氬瓧绗︿覆寮€澶村晩锛?br> boolean startsWith(string);
2.2.3锛氬瓧绗︿覆鏄惁浠ユ寚瀹氬瓧绗︿覆缁撳熬鍟婏紵
boolean endsWith(string);
2.2.4锛氬垽鏂瓧绗︿覆鏄惁鐩稿悓
boolean equals(string);//瑕嗙洊浜哋bject涓殑鏂规硶锛屽垽鏂瓧绗︿覆鍐呭鏄惁鐩稿悓銆?br> 2.2.5锛氬垽鏂瓧绗︿覆鍐呭鏄惁鐩稿悓锛屽拷鐣ュぇ灏忓啓銆?br> boolean equalsIgnoreCase(string) ;
2.3 杞崲锛?br> 2.3.1锛氶€氳繃鏋勯€犲嚱鏁板彲浠ュ皢瀛楃鏁扮粍鎴栬€呭瓧鑺傛暟缁勮浆鎴愬瓧绗︿覆銆?br> 2.3.2锛氬彲浠ラ€氳繃瀛楃涓蹭腑鐨勯潤鎬佹柟娉曪紝灏嗗瓧绗︽暟缁勮浆鎴愬瓧绗︿覆銆?br> static String copyValueOf(char[] );
static String copyValueOf(char[],int offset,int count);
static String valueOf(char[]);
static String valueOf(char[],int offset,int count);
2.3.3锛氬皢鍩烘湰鏁版嵁绫诲瀷鎴栬€呭璞¤浆鎴愬瓧绗︿覆銆?br> static String valueOf(char);
static String valueOf(boolean);
static String valueOf(double);
static String valueOf(float);
static String valueOf(int);
static String valueOf(long);
static String valueOf(Object);
2.3.4锛氬皢瀛楃涓茶浆鎴愬ぇ灏忓啓銆?br> String toLowerCase();
String toUpperCase();
2.3.5锛氬皢瀛楃涓茶浆鎴愭暟缁勩€?br> char[] toCharArray();//杞垚瀛楃鏁扮粍銆?br> byte[] getBytes();//鍙互鍔犲叆缂栫爜琛ㄣ€傝浆鎴愬瓧鑺傛暟缁勩€?br> 2.3.6锛氬皢瀛楃涓茶浆鎴愬瓧绗︿覆鏁扮粍銆傚垏鍓叉柟娉曘€?br> String[] split(鍒嗗壊鐨勮鍒?瀛楃涓?;
2.3.7锛氬皢瀛楃涓茶繘琛屽唴瀹规浛鎹€傛敞鎰忥細淇敼鍚庡彉鎴愭柊瀛楃涓诧紝骞朵笉鏄皢鍘熷瓧绗︿覆鐩存帴淇敼銆?br> String replace(oldChar,newChar);
String replace(oldstring,newstring);
2.3.8锛?String concat(string); //瀵瑰瓧绗︿覆杩涜杩藉姞銆?br> String trim();//鍘婚櫎瀛楃涓蹭袱绔殑绌烘牸
int compareTo();//濡傛灉鍙傛暟瀛楃涓茬瓑浜庢瀛楃涓诧紝鍒欒繑鍥炲€?0锛涘鏋滄瀛楃涓叉寜瀛楀吀椤哄簭灏忎簬瀛楃涓插弬鏁帮紝鍒欒繑鍥炰竴涓皬浜?0 鐨勫€硷紱濡傛灉姝ゅ瓧绗︿覆鎸夊瓧鍏搁『搴忓ぇ浜庡瓧绗︿覆鍙傛暟锛屽垯杩斿洖涓€涓ぇ浜?0 鐨勫€笺€?br>------------------------------------------------------------------------------------------------
--< java.lang >-- StringBuffer瀛楃涓茬紦鍐插尯锛氣槄鈽呪槄鈽?/h3>
鏋勯€犱竴涓叾涓笉甯﹀瓧绗︾殑瀛楃涓茬紦鍐插尯锛屽垵濮嬪閲忎负 16 涓瓧绗︺€?br>鐗圭偣锛?br>1锛氬彲浠ュ瀛楃涓插唴瀹硅繘琛屼慨鏀广€?br>2锛氭槸涓€涓鍣ㄣ€?br>3锛氭槸鍙彉闀垮害鐨勩€?br>4锛氱紦鍐插尯涓彲浠ュ瓨鍌ㄤ换鎰忕被鍨嬬殑鏁版嵁銆?br>5锛氭渶缁堥渶瑕佸彉鎴愬瓧绗︿覆銆?br>
瀹瑰櫒閫氬父鍏峰涓€浜涘浐瀹氱殑鏂规硶锛?br>1锛屾坊鍔犮€?br> StringBuffer append(data):鍦ㄧ紦鍐插尯涓拷鍔犳暟鎹€傝拷鍔犲埌灏鹃儴銆?br> StringBuffer insert(index,data):鍦ㄦ寚瀹氫綅缃彃鍏ユ暟鎹€?br>2锛屽垹闄ゃ€?br> StringBuffer delete(start,end);鍒犻櫎浠巗tart鑷砮nd-1鑼冨洿鐨勫厓绱?br> StringBuffer deleteCharAt(index);鍒犻櫎鎸囧畾浣嶇疆鐨勫厓绱?br>//sb.delete(0,sb.length());//娓呯┖缂撳啿鍖恒€?br>3锛屼慨鏀广€?br> StringBuffer replace(start,end,string);灏唖tart鑷砮nd-1鏇挎崲鎴恠tring
void setCharAt(index,char);鏇挎崲鎸囧畾浣嶇疆鐨勫瓧绗?br> void setLength(len);灏嗗師瀛楃涓茬疆涓烘寚瀹氶暱搴︾殑瀛楃涓?br>4锛屾煡鎵俱€傦紙鏌ヤ笉鍒拌繑鍥?1锛?br> int indexOf(string); 杩斿洖鎸囧畾瀛愬瓧绗︿覆鍦ㄦ瀛楃涓蹭腑绗竴娆″嚭鐜板鐨勭储寮曘€?br> int indexOf(string,int fromIndex);浠庢寚瀹氫綅缃紑濮嬫煡鎵惧瓧绗︿覆
int lastIndexOf(string); 杩斿洖鎸囧畾瀛愬瓧绗︿覆鍦ㄦ瀛楃涓蹭腑鏈€鍙宠竟鍑虹幇澶勭殑绱㈠紩銆?br> int lastIndexOf(string,int fromIndex); 浠庢寚瀹氱殑绱㈠紩寮€濮嬪弽鍚戞悳绱?br>5锛岃幏鍙栧瓙涓层€?br> string substring(start); 杩斿洖start鍒扮粨灏剧殑瀛愪覆
string substring(start,end); 杩斿洖start鑷砮nd-1鐨勫瓙涓?br>6锛屽弽杞€?br> StringBuffer reverse();瀛楃涓插弽杞?br>------------------------------------------------------------------------------------------------
--< java.lang >-- StringBuilder瀛楃涓茬紦鍐插尯锛氣槄鈽呪槄鈽?/h3>
JDK1.5鍑虹幇StringBuiler锛涙瀯閫犱竴涓叾涓笉甯﹀瓧绗︾殑瀛楃涓茬敓鎴愬櫒锛屽垵濮嬪閲忎负 16 涓瓧绗︺€傝绫昏璁捐鐢ㄤ綔 StringBuffer 鐨勪竴涓畝鏄撴浛鎹紝鐢ㄥ湪瀛楃涓茬紦鍐插尯琚崟涓嚎绋嬩娇鐢ㄧ殑鏃跺€欙紙杩欑鎯呭喌寰堟櫘閬嶏級銆?br>鏂规硶鍜孲tringBuffer涓€鏍凤紱
StringBuffer 鍜?StringBuilder 鐨勫尯鍒細
StringBuffer绾跨▼瀹夊叏銆?br>StringBuilder绾跨▼涓嶅畨鍏ㄣ€?br>
鍗曠嚎绋嬫搷浣滐紝浣跨敤StringBuilder 鏁堢巼楂樸€?br>澶氱嚎绋嬫搷浣滐紝浣跨敤StringBuffer 瀹夊叏銆?br>---------------------------------------------------------
StringBuilder sb = new StringBuilder("abcdefg");
sb.append("ak"); //abcdefgak
sb.insert(1,"et");//aetbcdefg
sb.deleteCharAt(2);//abdefg
sb.delete(2,4);//abefg
sb.setLength(4);//abcd
sb.setCharAt(0,鈥榢鈥?;//kbcdefg
sb.replace(0,2,"hhhh");//hhhhcdefg
//鎯宠浣跨敤缂撳啿鍖猴紝鍏堣寤虹珛瀵硅薄銆?br> StringBuffer sb = new StringBuffer();
sb.append(12).append("haha");//鏂规硶璋冪敤閾俱€?br> String s = "abc"+4+鈥榪鈥?
s = new StringBuffer().append("abc").append(4).append(鈥榪鈥?.toString();
---------------------------------------------------------
class Test{
public static void main(String[] args) {
String s1 = "java";
String s2 = "hello";
method_1(s1,s2);
System.out.println(s1+"...."+s2); //java....hello
StringBuilder s11 = new StringBuilder("java");
StringBuilder s22 = new StringBuilder("hello");
method_2(s11,s22);
System.out.println(s11+"-----"+s22); //javahello-----hello
}
public static void method_1(String s1,String s2){
s1.replace(鈥榓鈥?鈥榢鈥?;
s1 = s2;
}
public static void method_2(StringBuilder s1,StringBuilder s2){
s1.append(s2);
s1 = s2;
}
}
---------------------------------------------------------
鍩烘湰鏁版嵁绫诲瀷瀵硅薄鍖呰绫伙細
鏄寜鐓ч潰鍚戝璞℃€濇兂灏嗗熀鏈暟鎹被鍨嬪皝瑁呮垚浜嗗璞°€?br>濂藉锛?br>1锛氬彲浠ラ€氳繃瀵硅薄涓殑灞炴€у拰琛屼负鎿嶄綔鍩烘湰鏁版嵁銆?br>2锛氬彲浠ュ疄鐜板熀鏈暟鎹被鍨嬪拰瀛楃涓蹭箣闂寸殑杞崲銆?br>
鍏抽敭瀛?nbsp; 瀵瑰簲鐨勭被鍚?br>byte Byte
short Short parseShort(numstring);
int Integer 闈欐€佹柟娉曪細parseInt(numstring)
long Long
float Float
double Double
char Character
Boolean Boolean
鍩烘湰鏁版嵁绫诲瀷瀵硅薄鍖呰绫伙細閮芥湁 XXX parseXXX 鏂规硶
鍙湁涓€涓被鍨嬫病鏈塸arse鏂规硶锛欳haracter 锛?br>--------------------------------------------------------
Integer瀵硅薄锛?鈽呪槄鈽呪槅
鏁板瓧鏍煎紡鐨勫瓧绗︿覆杞垚鍩烘湰鏁版嵁绫诲瀷鐨勬柟娉曪細
1锛氬皢璇ュ瓧绗︿覆灏佽鎴愪簡Integer瀵硅薄锛屽苟璋冪敤瀵硅薄鐨勬柟娉昳ntValue();
2锛氫娇鐢↖nteger.parseInt(numstring):涓嶇敤寤虹珛瀵硅薄锛岀洿鎺ョ被鍚嶈皟鐢紱
灏嗗熀鏈被鍨嬭浆鎴愬瓧绗︿覆锛?br>1锛欼nteger涓殑闈欐€佹柟娉?String toString(int);
2锛歩nt+"";
灏嗕竴涓崄杩涘埗鏁存暟杞垚鍏朵粬杩涘埗锛?/h2>
杞垚浜岃繘鍒讹細toBinaryString
杞垚鍏繘鍒讹細toOctalString
杞垚鍗佸叚杩涘埗锛歵oHexString
toString(int num,int radix);
灏嗗叾浠栬繘鍒惰浆鎹㈠崄杩涘埗锛?/h2>
parseInt(string,radix); //灏嗙粰瀹氱殑鏁拌浆鎴愭寚瀹氱殑鍩烘暟杩涘埗锛?br>
鍦╦dk1.5鐗堟湰鍚庯紝瀵瑰熀鏈暟鎹被鍨嬪璞″寘瑁呯被杩涜鍗囩骇銆傚湪鍗囩骇涓紝浣跨敤鍩烘湰鏁版嵁绫诲瀷瀵硅薄鍖呰绫诲彲浠ュ儚浣跨敤鍩烘湰鏁版嵁绫诲瀷涓€鏍凤紝杩涜杩愮畻銆?br> Integer i = new Integer(4); //1.5鐗堟湰涔嬪墠鐨勫啓娉曪紱
Integer i = 4; //鑷姩瑁呯锛?.5鐗堟湰鍚庣殑鍐欐硶锛?br> i = i + 5;
//i瀵硅薄鏄笉鑳界洿鎺ュ拰5鐩稿姞鐨勶紝鍏跺疄搴曞眰鍏堝皢i杞垚int绫诲瀷锛屽湪鍜?鐩稿姞銆傝€岃浆鎴恑nt绫诲瀷鐨勬搷浣滄槸闅愬紡鐨勩€傝嚜鍔ㄦ媶绠憋細鎷嗙鐨勫師鐞嗗氨鏄痠.intValue();i+5杩愮畻瀹屾槸涓€涓猧nt鏁存暟銆傚浣曡祴鍊肩粰寮曠敤绫诲瀷i鍛紵鍏跺疄鏈夊缁撴灉杩涜瑁呯銆?br>Integer c = 127;
Integer d = 127;
System.out.println(c = = d); //true
//鍦ㄨ绠辨椂锛屽鏋滄暟鍊煎湪byte鑼冨洿涔嬪唴锛岄偅涔堟暟鍊肩浉鍚岋紝涓嶄細浜х敓鏂扮殑瀵硅薄锛屼篃灏辨槸璇村涓暟鍊肩浉鍚岀殑寮曠敤鎸囧悜鐨勬槸鍚屼竴涓璞°€?br>------------------------------------------------------------------------------------------------
API--- java.lang.System:
灞炴€у拰琛屼负閮芥槸闈欐€佺殑銆?br>long currentTimeMillis(); // 杩斿洖褰撳墠鏃堕棿姣鍊?br>exit(); // 閫€鍑鸿櫄鎷熸満
Properties getProperties() ; // 鑾峰彇褰撳墠绯荤粺鐨勫睘鎬т俊鎭?br>
Properties prop = System.getProperties(); //鑾峰彇绯荤粺鐨勫睘鎬т俊鎭紝骞跺皢杩欎簺淇℃伅瀛樺偍鍒癙roperties闆嗗悎涓€?
System.setProperty("myname","鍑摜"); //缁欑郴缁熷睘鎬т俊鎭泦娣诲姞鍏蜂綋鐨勫睘鎬т俊鎭?br>//涓存椂璁剧疆鏂瑰紡锛氳繍琛宩vm鏃讹紝鍙互閫氳繃jvm鐨勫弬鏁拌繘琛岀郴缁熷睘鎬х殑涓存椂璁剧疆锛屽彲浠ュ湪java鍛戒护鐨勫悗闈㈠姞鍏?鈥揇<name>=<value> 鐢ㄦ硶锛歫ava 鈥揇myname=灏忔槑 绫诲悕銆?br>
String name = System.getProperty("os.name");//鑾峰彇鎸囧畾灞炴€х殑淇℃伅
//鎯宠鐭ラ亾璇ョ郴缁熸槸鍚︽槸璇ヨ蒋浠舵墍鏀寔鐨勭郴缁熶腑鐨勪竴涓€?br>Set<String> hs = new HashSet<String>();
hs.add("Windows XP");
hs.add("Windows 7");
if(hs.contains(name))
System.out.println("鍙互鏀寔");
else
System.out.println("涓嶆敮鎸?);
--------------------------------------------------------------------------------------------------------------------
API--- java.util.Math:
鐢ㄤ簬鏁板杩愮畻鐨勫伐鍏风被锛屽睘鎬у拰琛屼负閮芥槸闈欐€佺殑銆傝绫绘槸final涓嶅厑璁哥户鎵裤€?br>
static double ceil(double a) ; //杩斿洖澶т簬鎸囧畾鏁板€肩殑鏈€灏忔暣鏁?br>static double floor(double a) ; //杩斿洖灏忎簬鎸囧畾鏁板€肩殑鏈€澶ф暣鏁?br>static long round(double a) ; //鍥涜垗浜斿叆鎴愭暣鏁?br>static double pow(double a, double b) ; //a鐨刡娆″箓
static double random(); //杩斿洖0~1鐨勪吉闅忔満鏁?br>
public static void main(String[] args) {
Random r = new Random();
for(int x=0; x<10; x++) {
//double d = Math.floor(Math.random()*10+1);
//int d = (int)(Math.random()*10+1);
int d = r.nextInt(10)+1;
System.out.println(d);
}
}
--------------------------------------------------------------------------------------------------------------------
API--- java.lang.Runtime:
绫讳腑娌℃湁鏋勯€犳柟娉曪紝涓嶈兘鍒涘缓瀵硅薄銆?br>浣嗘槸鏈夐潪闈欐€佹柟娉曘€傝鏄庤绫讳腑搴旇瀹氫箟濂戒簡瀵硅薄锛屽苟鍙互閫氳繃涓€涓猻tatic鏂规硶鑾峰彇杩欎釜瀵硅薄銆傜敤杩欎釜瀵硅薄鏉ヨ皟鐢ㄩ潪闈欐€佹柟娉曘€傝繖涓柟娉曞氨鏄?static Runtime getRuntime();
杩欎釜Runtime鍏跺疄浣跨敤鍗曚緥璁捐妯″紡杩涜璁捐銆?br>
class RuntimeDemo {
public static void main(String[] args) throws Exception {
Runtime r = Runtime.getRuntime();
Process p = r.exec("notepad.exe SystemDemo.java"); //杩愯鎸囧畾鐨勭▼搴?br> Thread.sleep(4000);
p.destroy(); //鏉€鎺夎繘绋?br> }
}
--------------------------------------------------------------------------------------------------------------------
API--- java.util.Date锛?/h2>
鏃ユ湡绫伙紝鏈堜唤浠?-11锛?br> /*
鏃ユ湡瀵硅薄鍜屾绉掑€间箣闂寸殑杞崲銆?br> 1锛屾棩鏈熷璞¤浆鎴愭绉掑€笺€侱ate绫讳腑鐨刧etTime鏂规硶銆?br> 2锛屽浣曞皢鑾峰彇鍒扮殑姣鍊艰浆鎴愬叿浣撶殑鏃ユ湡鍛紵
Date绫讳腑鐨剆etTime鏂规硶銆備篃鍙互閫氳繃鏋勯€犲嚱鏁般€?
*/
//鏃ユ湡瀵硅薄杞垚姣鍊?br> Date d = new Date();
long time1 = d.getTime();
long time2 = System.currentTimeMillis(); / /姣鍊笺€?br>
//姣鍊艰浆鎴愬叿浣撶殑鏃ユ湡
long time = 1322709921312l;
Date d = new Date();
d.setTime(time);
/*
灏嗘棩鏈熷瓧绗︿覆杞崲鎴愭棩鏈熷璞★細浣跨敤鐨勫氨鏄疍ateFormat鏂规硶涓殑 Date parse(String source) 锛?br> */
public static void method() throws Exception {
String str_time = "2011/10/25";
DateFormat df = new SimpleDateFormat("yyyy/MM/dd"); //SimpleDateFormat浣滀负鍙互鎸囧畾鐢ㄦ埛鑷畾涔夌殑鏍煎紡鏉ュ畬鎴愭牸寮忓寲銆?br> Date d = df.parse(str_time);
}
/*
濡傛灉涓嶉渶瑕佷娇鐢ㄧ壒瀹氱殑鏍煎紡鍖栭鏍硷紝瀹屽叏鍙互浣跨敤DateFormat绫讳腑鐨勯潤鎬佸伐鍘傛柟娉曡幏鍙栧叿浣撶殑宸茬粡灏佽濂介鏍肩殑瀵硅薄銆俫etDateInstance();getDateTimeInstance();
*/
Date d = new Date();
DateFormat df = DateFormat.getDateInstance(DateFormat.LONG);
df = DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.LONG);
String str_time = df.format(d);
//灏嗘棩鏈熷璞¤浆鎹㈡垚瀛楃涓茬殑鏂瑰紡锛欴ateFormat绫讳腑鐨刦ormat鏂规硶銆?br> //鍒涘缓鏃ユ湡鏍煎紡瀵硅薄銆?
DateFormat df = new SimpleDateFormat(); //璇ュ璞$殑寤虹珛鍐呴儴浼氬皝瑁呬竴涓粯璁ょ殑鏃ユ湡鏍煎紡銆?1-12-1 涓嬪崍1:48
//濡傛灉鎯宠鑷畾涔夋棩鏈熸牸寮忕殑璇濄€傚彲浣跨敤SimpleDateFormat鐨勬瀯閫犲嚱鏁般€傚皢鍏蜂綋鐨勬牸寮忎綔涓哄弬鏁颁紶鍏ュ埌鏋勯€犲嚱鏁颁腑銆傚浣曡〃绀烘棩鏈熶腑骞寸殑閮ㄥ垎鍛紵鍙互蹇呴』瑕佸弬涓庢牸寮忓璞℃枃妗c€?
df = new SimpleDateFormat("yyyy骞碝M鏈坉d鏃?HH:mm:ss");
//璋冪敤DateFormat涓殑format鏂规硶銆傚宸叉湁鐨勬棩鏈熷璞¤繘琛屾牸寮忓寲銆?br> String str_time = df.format(d);
--------------------------------------------------------------------------------------------------------------------
API--- java.util. Calendar锛氭棩鍘嗙被
public static void method(){
Calendar c = Calendar.getInstance();
System.out.println(c.get(Calendar.YEAR)+"骞?+(c.get(Calendar.MONTH)+1)+"鏈?
+getNum(c.get(Calendar.DAY_OF_MONTH))+"鏃?
+"鏄熸湡"+getWeek(c.get(Calendar.DAY_OF_WEEK)));
}
public static String getNum(int num){
return num>9 ? num+"" : "0"+num;
}
public static String getWeek(int index){
/*
鏌ヨ〃娉曪細寤虹珛鏁版嵁鐨勫搴斿叧绯?
鏈€濂斤細鏁版嵁涓暟鏄‘瀹氱殑锛岃€屼笖鏈夊搴斿叧绯汇€傚鏋滃搴斿叧绯荤殑涓€鏂癸紝鏄暟瀛楋紝鑰屼笖鍙互浣滀负瑙掓爣锛岄偅涔堝彲浠ラ€氳繃鏁扮粍鏉ヤ綔涓鸿〃銆?br> */
String[] weeks = {"","鏃?,"涓€","浜?,"涓?,"鍥?,"浜?,"鍏?};
return weeks[index];
}
------------------------------------------------------------------------------------------------------------------------------------------------
姝e垯琛ㄨ揪寮忥細鈽呪槄鈽呪槅锛?/h2>
鍏跺疄鏄敤鏉ユ搷浣滃瓧绗︿覆鐨勪竴浜涜鍒欍€?br>濂藉锛氭鍒欑殑鍑虹幇锛屽瀛楃涓茬殑澶嶆潅鎿嶄綔鍙樺緱鏇翠负绠€鍗曘€?br>鐗圭偣锛氬皢瀵瑰瓧绗︿覆鎿嶄綔鐨勪唬鐮佺敤涓€浜涚鍙锋潵琛ㄧず銆傚彧瑕佷娇鐢ㄤ簡鎸囧畾绗﹀彿锛屽氨鍙互璋冪敤搴曞眰鐨勪唬鐮佸瀛楃涓茶繘琛屾搷浣溿€傜鍙风殑鍑虹幇锛岀畝鍖栦簡浠g爜鐨勪功鍐欍€?br>寮婄锛氱鍙风殑鍑虹幇铏界劧绠€鍖栦簡涔﹀啓锛屼絾鏄嵈闄嶄綆浜嗛槄璇绘€с€?br>鍏跺疄鏇村鏄敤姝e垯瑙e喅瀛楃涓叉搷浣滅殑闂銆?br>
缁勶細鐢ㄥ皬鎷彿鏍囩ず锛屾瘡瀹氫箟涓€涓皬鎷彿锛屽氨鏄竴涓粍锛岃€屼笖鏈夎嚜鍔ㄧ紪鍙凤紝浠?寮€濮嬨€?br> 鍙浣跨敤缁勶紝瀵瑰簲鐨勬暟瀛楀氨鏄娇鐢ㄨ缁勭殑鍐呭銆傚埆蹇樹簡锛屾暟缁勮鍔燶銆?br> (aaa(wwww(ccc))(eee))鎶€宸э紝浠庡乏鎷彿寮€濮嬫暟鍗冲彲銆傛湁鍑犱釜宸︽嫭鍙峰氨鏄嚑缁勩€?br>
甯歌鎿嶄綔锛?br>1锛屽尮閰嶏細鍏跺疄鐢ㄧ殑灏辨槸String绫讳腑鐨刴atches鏂规硶銆?br>String reg = "[1-9][0-9]{4,14}";
boolean b = qq.matches(reg);//灏嗘鍒欏拰瀛楃涓插叧鑱斿瀛楃涓茶繘琛屽尮閰嶃€?br>2锛屽垏鍓诧細鍏跺疄鐢ㄧ殑灏辨槸String绫讳腑鐨剆plit鏂规硶銆?br>3锛屾浛鎹細鍏跺疄鐢ㄧ殑灏辨槸String绫讳腑鐨剅eplaceAll();
4锛岃幏鍙栵細
1锛夛紝鍏堣灏嗘鍒欒〃杈惧紡缂栬瘧鎴愭鍒欏璞°€備娇鐢ㄧ殑鏄疨attern涓潤鎬佹柟娉?compile(regex);
2锛夛紝閫氳繃Pattern瀵硅薄鑾峰彇Matcher瀵硅薄銆?br> Pattern鐢ㄤ簬鎻忚堪姝e垯琛ㄨ揪寮忥紝鍙互瀵规鍒欒〃杈惧紡杩涜瑙f瀽銆?br> 鑰屽皢瑙勫垯鎿嶄綔瀛楃涓诧紝闇€瑕佷粠鏂板皝瑁呭埌鍖归厤鍣ㄥ璞atcher涓€?br> 鐒跺悗浣跨敤Matcher瀵硅薄鐨勬柟娉曟潵鎿嶄綔瀛楃涓层€?br> 濡備綍鑾峰彇鍖归厤鍣ㄥ璞″憿锛?br> 閫氳繃Pattern瀵硅薄涓殑matcher鏂规硶銆傝鏂规硶鍙互姝e垯瑙勫垯鍜屽瓧绗︿覆鎯冲叧鑱斻€傚苟杩斿洖鍖归厤鍣ㄥ璞°€?br> 3锛夛紝浣跨敤Matcher瀵硅薄涓殑鏂规硶鍗冲彲瀵瑰瓧绗︿覆杩涜鍚勭姝e垯鎿嶄綔銆?br>----------------------------------------------------------------------------------------------
闆嗗悎妗嗘灦锛氣槄鈽呪槄鈽呪槄锛?/h1>
鐢ㄤ簬瀛樺偍鏁版嵁鐨勫鍣ㄣ€?br>鐗圭偣锛?br>1锛氬璞″皝瑁呮暟鎹紝瀵硅薄澶氫簡涔熼渶瑕佸瓨鍌ㄣ€傞泦鍚堢敤浜庡瓨鍌ㄥ璞°€?br>2锛氬璞$殑涓暟纭畾鍙互浣跨敤鏁扮粍锛屼絾鏄笉纭畾鎬庝箞鍔烇紵鍙互鐢ㄩ泦鍚堛€傚洜涓洪泦鍚堟槸鍙彉闀垮害鐨勩€?br>
闆嗗悎鍜屾暟缁勭殑鍖哄埆锛?br>1锛氭暟缁勬槸鍥哄畾闀垮害鐨勶紱闆嗗悎鍙彉闀垮害鐨勩€?br>2锛氭暟缁勫彲浠ュ瓨鍌ㄥ熀鏈暟鎹被鍨嬶紝涔熷彲浠ュ瓨鍌ㄥ紩鐢ㄦ暟鎹被鍨嬶紱闆嗗悎鍙兘瀛樺偍寮曠敤鏁版嵁绫诲瀷銆?br>3锛氭暟缁勫瓨鍌ㄧ殑鍏冪礌蹇呴』鏄悓涓€涓暟鎹被鍨嬶紱闆嗗悎瀛樺偍鐨勫璞″彲浠ユ槸涓嶅悓鏁版嵁绫诲瀷銆?br>
鏁版嵁缁撴瀯锛氬氨鏄鍣ㄤ腑瀛樺偍鏁版嵁鐨勬柟寮忋€?br>
瀵逛簬闆嗗悎瀹瑰櫒锛屾湁寰堝绉嶃€傚洜涓烘瘡涓€涓鍣ㄧ殑鑷韩鐗圭偣涓嶅悓锛屽叾瀹炲師鐞嗗湪浜庢瘡涓鍣ㄧ殑鍐呴儴鏁版嵁缁撴瀯涓嶅悓銆?br>闆嗗悎瀹瑰櫒鍦ㄤ笉鏂悜涓婃娊鍙栬繃绋嬩腑銆傚嚭鐜颁簡闆嗗悎浣撶郴銆?br>鍦ㄤ娇鐢ㄤ竴涓綋绯绘椂锛屽師鍒欙細鍙傞槄椤跺眰鍐呭銆傚缓绔嬪簳灞傚璞°€?br>
------------------------------------------------------------
--< java.util >-- Collection鎺ュ彛锛?br>Collection锛?br> |--List锛氭湁搴?鍏冪礌瀛樺叆闆嗗悎鐨勯『搴忓拰鍙栧嚭鐨勯『搴忎竴鑷?锛屽厓绱犻兘鏈夌储寮曘€傚厓绱犲彲浠ラ噸澶嶃€?br> |--Set锛氭棤搴?瀛樺叆鍜屽彇鍑洪『搴忔湁鍙兘涓嶄竴鑷?锛屼笉鍙互瀛樺偍閲嶅鍏冪礌銆傚繀椤讳繚璇佸厓绱犲敮涓€鎬с€?br>
1锛屾坊鍔狅細
add(object)锛氭坊鍔犱竴涓厓绱?br> addAll(Collection) 锛氭坊鍔犱竴涓泦鍚堜腑鐨勬墍鏈夊厓绱犮€?br>2锛屽垹闄わ細
clear()锛氬皢闆嗗悎涓殑鍏冪礌鍏ㄥ垹闄わ紝娓呯┖闆嗗悎銆?br> remove(obj) 锛氬垹闄ら泦鍚堜腑鎸囧畾鐨勫璞°€傛敞鎰忥細鍒犻櫎鎴愬姛锛岄泦鍚堢殑闀垮害浼氭敼鍙樸€?br> removeAll(collection) 锛氬垹闄ら儴鍒嗗厓绱犮€傞儴鍒嗗厓绱犲拰浼犲叆Collection涓€鑷淬€?br>3锛屽垽鏂細
boolean contains(obj) 锛氶泦鍚堜腑鏄惁鍖呭惈鎸囧畾鍏冪礌 銆?br> boolean containsAll(Collection) 锛氶泦鍚堜腑鏄惁鍖呭惈鎸囧畾鐨勫涓厓绱犮€?br> boolean isEmpty()锛氶泦鍚堜腑鏄惁鏈夊厓绱犮€?br>4锛岃幏鍙栵細
int size()锛氶泦鍚堜腑鏈夊嚑涓厓绱犮€?br>5锛屽彇浜ら泦锛?br> boolean retainAll(Collection) 锛氬褰撳墠闆嗗悎涓繚鐣欏拰鎸囧畾闆嗗悎涓殑鐩稿悓鐨勫厓绱犮€傚鏋滀袱涓泦鍚堝厓绱犵浉鍚岋紝杩斿洖flase锛涘鏋渞etainAll淇敼浜嗗綋鍓嶉泦鍚堬紝杩斿洖true銆?br>6锛岃幏鍙栭泦鍚堜腑鎵€鏈夊厓绱狅細
Iterator iterator()锛氳凯浠e櫒
7锛屽皢闆嗗悎鍙樻垚鏁扮粍锛?br> toArray();
------------------------------------------------------------
--< java.util >-- Iterator鎺ュ彛锛?br>杩唬鍣細鏄竴涓帴鍙c€備綔鐢細鐢ㄤ簬鍙栭泦鍚堜腑鐨勫厓绱犮€?br> boolean hasNext() 濡傛灉浠嶆湁鍏冪礌鍙互杩唬锛屽垯杩斿洖 true銆?br> E next() 杩斿洖杩唬鐨勪笅涓€涓厓绱犮€?br> void remove() 浠庤凯浠e櫒鎸囧悜鐨?collection 涓Щ闄よ凯浠e櫒杩斿洖鐨勬渶鍚庝竴涓厓绱狅紙鍙€夋搷浣滐級銆?br>
姣忎竴涓泦鍚堥兘鏈夎嚜宸辩殑鏁版嵁缁撴瀯锛岄兘鏈夌壒瀹氱殑鍙栧嚭鑷繁鍐呴儴鍏冪礌鐨勬柟寮忋€備负浜嗕究浜庢搷浣滄墍鏈夌殑瀹瑰櫒锛屽彇鍑哄厓绱犮€傚皢瀹瑰櫒鍐呴儴鐨勫彇鍑烘柟寮忔寜鐓т竴涓粺涓€鐨勮鍒欏悜澶栨彁渚涳紝杩欎釜瑙勫垯灏辨槸Iterator鎺ュ彛銆?br>涔熷氨璇达紝鍙閫氳繃璇ユ帴鍙e氨鍙互鍙栧嚭Collection闆嗗悎涓殑鍏冪礌锛岃嚦浜庢瘡涓€涓叿浣撶殑瀹瑰櫒渚濇嵁鑷繁鐨勬暟鎹粨鏋勶紝濡備綍瀹炵幇鐨勫叿浣撳彇鍑虹粏鑺傦紝杩欎釜涓嶇敤鍏冲績锛岃繖鏍峰氨闄嶄綆浜嗗彇鍑哄厓绱犲拰鍏蜂綋闆嗗悎鐨勮€﹀悎鎬с€?br>
Iterator it = coll.iterator();//鑾峰彇瀹瑰櫒涓殑杩唬鍣ㄥ璞★紝鑷充簬杩欎釜瀵硅薄鏄槸浠€涔堜笉閲嶈銆傝繖瀵硅薄鑲畾绗﹀悎涓€涓鍒橧terator鎺ュ彛銆?br>-----------------------------------------------------------------------------
public static void main(String[] args) {
Collection coll = new ArrayList();
coll.add("abc0");
coll.add("abc1");
coll.add("abc2");
//--------------鏂瑰紡1----------------------
Iterator it = coll.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
//---------------鏂瑰紡2鐢ㄦ绉?---------------------
for(Iterator it = coll.iterator();it.hasNext(); ){
System.out.println(it.next());
}
}
-----------------------------------------------------------------------------
--< java.util >-- List鎺ュ彛锛?/h2>
List鏈韩鏄疌ollection鎺ュ彛鐨勫瓙鎺ュ彛锛屽叿澶囦簡Collection鐨勬墍鏈夋柟娉曘€傜幇鍦ㄥ涔燣ist浣撶郴鐗规湁鐨勫叡鎬ф柟娉曪紝鏌ラ槄鏂规硶鍙戠幇List鐨勭壒鏈夋柟娉曢兘鏈夌储寮曪紝杩欐槸璇ラ泦鍚堟渶澶х殑鐗圭偣銆?br>
List锛氭湁搴?鍏冪礌瀛樺叆闆嗗悎鐨勯『搴忓拰鍙栧嚭鐨勯『搴忎竴鑷?锛屽厓绱犻兘鏈夌储寮曘€傚厓绱犲彲浠ラ噸澶嶃€?br> |--ArrayList锛氬簳灞傜殑鏁版嵁缁撴瀯鏄暟缁?绾跨▼涓嶅悓姝ワ紝ArrayList鏇夸唬浜哣ector锛屾煡璇㈠厓绱犵殑閫熷害闈炲父蹇€?br> |--LinkedList锛氬簳灞傜殑鏁版嵁缁撴瀯鏄摼琛紝绾跨▼涓嶅悓姝ワ紝澧炲垹鍏冪礌鐨勯€熷害闈炲父蹇€?br> |--Vector锛氬簳灞傜殑鏁版嵁缁撴瀯灏辨槸鏁扮粍锛岀嚎绋嬪悓姝ョ殑锛孷ector鏃犺鏌ヨ鍜屽鍒犻兘宸ㄦ參銆?br>
1锛屾坊鍔狅細
add(index,element) 锛氬湪鎸囧畾鐨勭储寮曚綅鎻掑叆鍏冪礌銆?br> addAll(index,collection) 锛氬湪鎸囧畾鐨勭储寮曚綅鎻掑叆涓€鍫嗗厓绱犮€?br>2锛屽垹闄わ細
remove(index) 锛氬垹闄ゆ寚瀹氱储寮曚綅鐨勫厓绱犮€?杩斿洖琚垹鐨勫厓绱犮€?br>3锛岃幏鍙栵細
Object get(index) 锛氶€氳繃绱㈠紩鑾峰彇鎸囧畾鍏冪礌銆?br> int indexOf(obj) 锛氳幏鍙栨寚瀹氬厓绱犵涓€娆″嚭鐜扮殑绱㈠紩浣嶏紝濡傛灉璇ュ厓绱犱笉瀛樺湪杩斿洖-1锛?br> 鎵€浠ワ紝閫氳繃-1锛屽彲浠ュ垽鏂竴涓厓绱犳槸鍚﹀瓨鍦ㄣ€?br> int lastIndexOf(Object o) 锛氬弽鍚戠储寮曟寚瀹氬厓绱犵殑浣嶇疆銆?br> List subList(start,end) 锛氳幏鍙栧瓙鍒楄〃銆?br>4锛屼慨鏀癸細
Object set(index,element) 锛氬鎸囧畾绱㈠紩浣嶈繘琛屽厓绱犵殑淇敼銆?br>5锛岃幏鍙栨墍鏈夊厓绱狅細
ListIterator listIterator()锛歭ist闆嗗悎鐗规湁鐨勮凯浠e櫒銆?br>
List闆嗗悎鏀寔瀵瑰厓绱犵殑澧炪€佸垹銆佹敼銆佹煡銆?br>
List闆嗗悎鍥犱负瑙掓爣鏈変簡鑷繁鐨勮幏鍙栧厓绱犵殑鏂瑰紡锛?閬嶅巻銆?br>for(int x=0; x<list.size(); x++){
sop("get:"+list.get(x));
}
鍦ㄨ繘琛宭ist鍒楄〃鍏冪礌杩唬鐨勬椂鍊欙紝濡傛灉鎯宠鍦ㄨ凯浠h繃绋嬩腑锛屾兂瑕佸鍏冪礌杩涜鎿嶄綔鐨勬椂鍊欙紝姣斿婊¤冻鏉′欢娣诲姞鏂板厓绱犮€備細鍙戠敓.ConcurrentModificationException骞跺彂淇敼寮傚父銆?br>瀵艰嚧鐨勫師鍥犳槸锛?br>闆嗗悎寮曠敤鍜岃凯浠e櫒寮曠敤鍦ㄥ悓鏃舵搷浣滃厓绱狅紝閫氳繃闆嗗悎鑾峰彇鍒板搴旂殑杩唬鍣ㄥ悗锛屽湪杩唬涓紝杩涜闆嗗悎寮曠敤鐨勫厓绱犳坊鍔狅紝杩唬鍣ㄥ苟涓嶇煡閬擄紝鎵€浠ヤ細鍑虹幇寮傚父鎯呭喌銆?br>濡備綍瑙e喅鍛紵
鏃㈢劧鏄湪杩唬涓鍏冪礌杩涜鎿嶄綔,鎵捐凯浠e櫒鐨勬柟娉曟渶涓哄悎閫?鍙槸Iterator涓彧鏈塰asNext,next,remove鏂规硶.閫氳繃鏌ラ槄鐨勫畠鐨勫瓙鎺ュ彛,ListIterator,鍙戠幇璇ュ垪琛ㄨ凯浠e櫒鎺ュ彛鍏峰浜嗗鍏冪礌鐨勫銆佸垹銆佹敼銆佹煡鐨勫姩浣溿€?br>
ListIterator鏄疞ist闆嗗悎鐗规湁鐨勮凯浠e櫒銆?br>ListIterator it = list.listIterator;//鍙栦唬Iterator it = list.iterator;
鏂规硶鎽樿
void add(E e) 灏嗘寚瀹氱殑鍏冪礌鎻掑叆鍒楄〃锛堝彲閫夋搷浣滐級銆?br> boolean hasNext() 浠ユ鍚戦亶鍘嗗垪琛ㄦ椂锛屽鏋滃垪琛ㄨ凯浠e櫒鏈夊涓厓绱狅紝鍒欒繑鍥?true锛堟崲鍙ヨ瘽璇达紝濡傛灉 next 杩斿洖涓€涓厓绱犺€屼笉鏄姏鍑哄紓甯革紝鍒欒繑鍥?true锛夈€?br> boolean hasPrevious() 濡傛灉浠ラ€嗗悜閬嶅巻鍒楄〃锛屽垪琛ㄨ凯浠e櫒鏈夊涓厓绱狅紝鍒欒繑鍥?true銆?br> E next() 杩斿洖鍒楄〃涓殑涓嬩竴涓厓绱犮€?br> int nextIndex() 杩斿洖瀵?next 鐨勫悗缁皟鐢ㄦ墍杩斿洖鍏冪礌鐨勭储寮曘€?br> E previous() 杩斿洖鍒楄〃涓殑鍓嶄竴涓厓绱犮€?br> int previousIndex() 杩斿洖瀵?previous 鐨勫悗缁皟鐢ㄦ墍杩斿洖鍏冪礌鐨勭储寮曘€?br> void remove() 浠庡垪琛ㄤ腑绉婚櫎鐢?next 鎴?previous 杩斿洖鐨勬渶鍚庝竴涓厓绱狅紙鍙€夋搷浣滐級銆?br> void set(E e) 鐢ㄦ寚瀹氬厓绱犳浛鎹?next 鎴?previous 杩斿洖鐨勬渶鍚庝竴涓厓绱狅紙鍙€夋搷浣滐級銆?br>
鍙彉闀垮害鏁扮粍鐨勫師鐞嗭細
褰撳厓绱犺秴鍑烘暟缁勯暱搴︼紝浼氫骇鐢熶竴涓柊鏁扮粍锛屽皢鍘熸暟缁勭殑鏁版嵁澶嶅埗鍒版柊鏁扮粍涓紝鍐嶅皢鏂扮殑鍏冪礌娣诲姞鍒版柊鏁扮粍涓€?br>ArrayList锛?br>鏄寜鐓у師鏁扮粍鐨?0%寤堕暱銆傛瀯閫犱竴涓垵濮嬪閲忎负 10 鐨勭┖鍒楄〃銆?br>Vector锛?br>鏄寜鐓у師鏁扮粍鐨?00%寤堕暱銆?br>
娉ㄦ剰锛氬浜巐ist闆嗗悎锛屽簳灞傚垽鏂厓绱犳槸鍚︾浉鍚岋紝鍏跺疄鐢ㄧ殑鏄厓绱犺嚜韬殑equals鏂规硶瀹屾垚鐨勩€傛墍浠ュ缓璁厓绱犻兘瑕佸鍐檈quals鏂规硶锛屽缓绔嬪厓绱犲璞¤嚜宸辩殑姣旇緝鐩稿悓鐨勬潯浠朵緷鎹€?br>
LinkedList锛氱殑鐗规湁鏂规硶銆?br>addFirst();
addLast();
鍦╦dk1.6浠ュ悗銆?br>offerFirst();
offerLast();
getFirst():鑾峰彇閾捐〃涓殑绗竴涓厓绱犮€傚鏋滈摼琛ㄤ负绌猴紝鎶涘嚭NoSuchElementException;
getLast();
鍦╦dk1.6浠ュ悗銆?br>peekFirst();鑾峰彇閾捐〃涓殑绗竴涓厓绱犮€傚鏋滈摼琛ㄤ负绌猴紝杩斿洖null銆?br>peekLast();
removeFirst()锛氳幏鍙栭摼琛ㄤ腑鐨勭涓€涓厓绱狅紝浣嗘槸浼氬垹闄ら摼琛ㄤ腑鐨勭涓€涓厓绱犮€傚鏋滈摼琛ㄤ负绌猴紝鎶涘嚭NoSuchElementException
removeLast();
鍦╦dk1.6浠ュ悗銆?br>pollFirst();鑾峰彇閾捐〃涓殑绗竴涓厓绱狅紝浣嗘槸浼氬垹闄ら摼琛ㄤ腑鐨勭涓€涓厓绱犮€傚鏋滈摼琛ㄤ负绌猴紝杩斿洖null銆?br>pollLast();
------------------------------------------------------------
--< java.util >-- Set鎺ュ彛锛?/h2>
Set鎺ュ彛涓殑鏂规硶鍜孋ollection涓柟娉曚竴鑷寸殑銆係et鎺ュ彛鍙栧嚭鏂瑰紡鍙湁涓€绉嶏紝杩唬鍣ㄣ€?br> |--HashSet锛氬簳灞傛暟鎹粨鏋勬槸鍝堝笇琛紝绾跨▼鏄笉鍚屾鐨勩€傛棤搴忥紝楂樻晥锛?br> HashSet闆嗗悎淇濊瘉鍏冪礌鍞竴鎬э細閫氳繃鍏冪礌鐨刪ashCode鏂规硶锛屽拰equals鏂规硶瀹屾垚鐨勩€?br> 褰撳厓绱犵殑hashCode鍊肩浉鍚屾椂锛屾墠缁х画鍒ゆ柇鍏冪礌鐨別quals鏄惁涓簍rue銆?br> 濡傛灉涓簍rue锛岄偅涔堣涓虹浉鍚屽厓绱狅紝涓嶅瓨銆傚鏋滀负false锛岄偅涔堝瓨鍌ㄣ€?br> 濡傛灉hashCode鍊间笉鍚岋紝閭d箞涓嶅垽鏂璭quals锛屼粠鑰屾彁楂樺璞℃瘮杈冪殑閫熷害銆?br> |--LinkedHashSet锛氭湁搴忥紝hashset鐨勫瓙绫汇€?br> |--TreeSet锛氬Set闆嗗悎涓殑鍏冪礌鐨勮繘琛屾寚瀹氶『搴忕殑鎺掑簭銆備笉鍚屾銆俆reeSet搴曞眰鐨勬暟鎹粨鏋勫氨鏄簩鍙夋爲銆?br>
鍝堝笇琛ㄧ殑鍘熺悊锛?br>1锛屽瀵硅薄鍏冪礌涓殑鍏抽敭瀛?瀵硅薄涓殑鐗规湁鏁版嵁)锛岃繘琛屽搱甯岀畻娉曠殑杩愮畻锛屽苟寰楀嚭涓€涓叿浣撶殑绠楁硶鍊硷紝杩欎釜鍊?绉颁负鍝堝笇鍊笺€?br>2锛屽搱甯屽€煎氨鏄繖涓厓绱犵殑浣嶇疆銆?br>3锛屽鏋滃搱甯屽€煎嚭鐜板啿绐侊紝鍐嶆鍒ゆ柇杩欎釜鍏抽敭瀛楀搴旂殑瀵硅薄鏄惁鐩稿悓銆傚鏋滃璞$浉鍚岋紝灏变笉瀛樺偍锛屽洜涓哄厓绱犻噸澶嶃€傚鏋滃璞′笉鍚岋紝灏卞瓨鍌紝鍦ㄥ師鏉ュ璞$殑鍝堝笇鍊煎熀纭€ +1椤哄欢銆?br>4锛屽瓨鍌ㄥ搱甯屽€肩殑缁撴瀯锛屾垜浠О涓哄搱甯岃〃銆?br>5锛屾棦鐒跺搱甯岃〃鏄牴鎹搱甯屽€煎瓨鍌ㄧ殑锛屼负浜嗘彁楂樻晥鐜囷紝鏈€濂戒繚璇佸璞$殑鍏抽敭瀛楁槸鍞竴鐨勩€?br> 杩欐牱鍙互灏介噺灏戠殑鍒ゆ柇鍏抽敭瀛楀搴旂殑瀵硅薄鏄惁鐩稿悓锛屾彁楂樹簡鍝堝笇琛ㄧ殑鎿嶄綔鏁堢巼銆?br>
瀵逛簬ArrayList闆嗗悎锛屽垽鏂厓绱犳槸鍚﹀瓨鍦紝鎴栬€呭垹鍏冪礌搴曞眰渚濇嵁閮芥槸equals鏂规硶銆?br>瀵逛簬HashSet闆嗗悎锛屽垽鏂厓绱犳槸鍚﹀瓨鍦紝鎴栬€呭垹闄ゅ厓绱狅紝搴曞眰渚濇嵁鐨勬槸hashCode鏂规硶鍜宔quals鏂规硶銆?br>
TreeSet:
鐢ㄤ簬瀵筍et闆嗗悎杩涜鍏冪礌鐨勬寚瀹氶『搴忔帓搴忥紝鎺掑簭闇€瑕佷緷鎹厓绱犺嚜韬叿澶囩殑姣旇緝鎬с€?br> 濡傛灉鍏冪礌涓嶅叿澶囨瘮杈冩€э紝鍦ㄨ繍琛屾椂浼氬彂鐢烠lassCastException寮傚父銆?br> 鎵€浠ラ渶瑕佸厓绱犲疄鐜癈omparable鎺ュ彛锛屽己鍒惰鍏冪礌鍏峰姣旇緝鎬э紝澶嶅啓compareTo鏂规硶銆?br> 渚濇嵁compareTo鏂规硶鐨勮繑鍥炲€硷紝纭畾鍏冪礌鍦═reeSet鏁版嵁缁撴瀯涓殑浣嶇疆銆?br> TreeSet鏂规硶淇濊瘉鍏冪礌鍞竴鎬х殑鏂瑰紡锛氬氨鏄弬鑰冩瘮杈冩柟娉曠殑缁撴灉鏄惁涓?锛屽鏋渞eturn 0锛岃涓轰袱涓璞¢噸澶嶏紝涓嶅瓨銆?br>
娉ㄦ剰锛氬湪杩涜姣旇緝鏃讹紝濡傛灉鍒ゆ柇鍏冪礌涓嶅敮涓€锛屾瘮濡傦紝鍚屽鍚嶏紝鍚屽勾榫勶紝鎵嶈涓哄悓涓€涓汉銆?br> 鍦ㄥ垽鏂椂锛岄渶瑕佸垎涓昏鏉′欢鍜屾瑕佹潯浠讹紝褰撲富瑕佹潯浠剁浉鍚屾椂锛屽啀鍒ゆ柇娆¤鏉′欢锛屾寜鐓ф瑕佹潯浠舵帓搴忋€?br>
TreeSet闆嗗悎鎺掑簭鏈変袱绉嶆柟寮忥紝Comparable鍜孋omparator鍖哄埆锛?br>1锛氳鍏冪礌鑷韩鍏峰姣旇緝鎬э紝闇€瑕佸厓绱犲璞″疄鐜癈omparable鎺ュ彛锛岃鐩朿ompareTo鏂规硶銆?br>2锛氳闆嗗悎鑷韩鍏峰姣旇緝鎬э紝闇€瑕佸畾涔変竴涓疄鐜颁簡Comparator鎺ュ彛鐨勬瘮杈冨櫒锛屽苟瑕嗙洊compare鏂规硶锛屽苟灏嗚绫诲璞′綔涓哄疄闄呭弬鏁颁紶閫掔粰TreeSet闆嗗悎鐨勬瀯閫犲嚱鏁般€?br>绗簩绉嶆柟寮忚緝涓虹伒娲汇€?br>
------------------------------------------------------------
Map闆嗗悎锛?/h2>
|--Hashtable锛氬簳灞傛槸鍝堝笇琛ㄦ暟鎹粨鏋勶紝鏄嚎绋嬪悓姝ョ殑銆備笉鍙互瀛樺偍null閿紝null鍊笺€?br>|--HashMap锛氬簳灞傛槸鍝堝笇琛ㄦ暟鎹粨鏋勶紝鏄嚎绋嬩笉鍚屾鐨勩€傚彲浠ュ瓨鍌╪ull閿紝null鍊笺€傛浛浠d簡Hashtable.
|--TreeMap锛氬簳灞傛槸浜屽弶鏍戠粨鏋勶紝鍙互瀵筸ap闆嗗悎涓殑閿繘琛屾寚瀹氶『搴忕殑鎺掑簭銆?br>
Map闆嗗悎瀛樺偍鍜孋ollection鏈夌潃寰堝ぇ涓嶅悓锛?br>Collection涓€娆″瓨涓€涓厓绱狅紱Map涓€娆″瓨涓€瀵瑰厓绱犮€?br>Collection鏄崟鍒楅泦鍚堬紱Map鏄弻鍒楅泦鍚堛€?br>Map涓殑瀛樺偍鐨勪竴瀵瑰厓绱狅細涓€涓槸閿紝涓€涓槸鍊硷紝閿笌鍊间箣闂存湁瀵瑰簲(鏄犲皠)鍏崇郴銆?br>鐗圭偣锛氳淇濊瘉map闆嗗悎涓敭鐨勫敮涓€鎬с€?br>
1锛屾坊鍔犮€?br> put(key,value)锛氬綋瀛樺偍鐨勯敭鐩稿悓鏃讹紝鏂扮殑鍊间細鏇挎崲鑰佺殑鍊硷紝骞跺皢鑰佸€艰繑鍥炪€傚鏋滈敭娌℃湁閲嶅锛岃繑鍥瀗ull銆?br> void putAll(Map);
2锛屽垹闄ゃ€?br> void clear()锛氭竻绌?br> value remove(key) 锛氬垹闄ゆ寚瀹氶敭銆?br>3锛屽垽鏂€?br> boolean isEmpty()锛?br> boolean containsKey(key)锛氭槸鍚﹀寘鍚玨ey
boolean containsValue(value) 锛氭槸鍚﹀寘鍚玽alue
4锛屽彇鍑恒€?br> int size()锛氳繑鍥為暱搴?br> value get(key) 锛氶€氳繃鎸囧畾閿幏鍙栧搴旂殑鍊笺€傚鏋滆繑鍥瀗ull锛屽彲浠ュ垽鏂閿笉瀛樺湪銆傚綋鐒舵湁鐗规畩鎯呭喌锛屽氨鏄湪hashmap闆嗗悎涓紝鏄彲浠ュ瓨鍌╪ull閿畁ull鍊肩殑銆?br> Collection values()锛氳幏鍙杕ap闆嗗悎涓殑鎵€鏈夌殑鍊笺€?br>5锛屾兂瑕佽幏鍙杕ap涓殑鎵€鏈夊厓绱狅細
鍘熺悊锛歮ap涓槸娌℃湁杩唬鍣ㄧ殑锛宑ollection鍏峰杩唬鍣紝鍙灏唌ap闆嗗悎杞垚Set闆嗗悎锛屽彲浠ヤ娇鐢ㄨ凯浠e櫒浜嗐€備箣鎵€浠ヨ浆鎴恠et锛屾槸鍥犱负map闆嗗悎鍏峰鐫€閿殑鍞竴鎬э紝鍏跺疄set闆嗗悎灏辨潵鑷簬map锛宻et闆嗗悎搴曞眰鍏跺疄鐢ㄧ殑灏辨槸map鐨勬柟娉曘€?br>鎶妋ap闆嗗悎杞垚set鐨勬柟娉曪細
Set keySet();
Set entrySet();//鍙栫殑鏄敭鍜屽€肩殑鏄犲皠鍏崇郴銆?br>Entry灏辨槸Map鎺ュ彛涓殑鍐呴儴鎺ュ彛锛?br>涓轰粈涔堣瀹氫箟鍦╩ap鍐呴儴鍛紵entry鏄闂敭鍊煎叧绯荤殑鍏ュ彛锛屾槸map鐨勫叆鍙o紝璁块棶鐨勬槸map涓殑閿€煎銆?br>---------------------------------------------------------
鍙栧嚭map闆嗗悎涓墍鏈夊厓绱犵殑鏂瑰紡涓€锛歬eySet()鏂规硶銆?br>鍙互灏唌ap闆嗗悎涓殑閿兘鍙栧嚭瀛樻斁鍒皊et闆嗗悎涓€傚set闆嗗悎杩涜杩唬銆傝凯浠e畬鎴愶紝鍐嶉€氳繃get鏂规硶瀵硅幏鍙栧埌鐨勯敭杩涜鍊肩殑鑾峰彇銆?br> Set keySet = map.keySet();
Iterator it = keySet.iterator();
while(it.hasNext()) {
Object key = it.next();
Object value = map.get(key);
System.out.println(key+":"+value);
}
--------------------------------------------------------
鍙栧嚭map闆嗗悎涓墍鏈夊厓绱犵殑鏂瑰紡浜岋細entrySet()鏂规硶銆?br>Set entrySet = map.entrySet();
Iterator it = entrySet.iterator();
while(it.hasNext()) {
Map.Entry me = (Map.Entry)it.next();
System.out.println(me.getKey()+"::::"+me.getValue());
}
--------------------------------------------------------
浣跨敤闆嗗悎鐨勬妧宸э細
鐪嬪埌Array灏辨槸鏁扮粍缁撴瀯锛屾湁瑙掓爣锛屾煡璇㈤€熷害寰堝揩銆?br>鐪嬪埌link灏辨槸閾捐〃缁撴瀯锛氬鍒犻€熷害蹇紝鑰屼笖鏈夌壒鏈夋柟娉曘€俛ddFirst锛?addLast锛?removeFirst()锛?removeLast()锛?getFirst()锛沢etLast()锛?br>鐪嬪埌hash灏辨槸鍝堝笇琛紝灏辫鎯宠鍝堝笇鍊硷紝灏辫鎯冲埌鍞竴鎬э紝灏辫鎯冲埌瀛樺叆鍒拌缁撴瀯鐨勪腑鐨勫厓绱犲繀椤昏鐩杊ashCode锛宔quals鏂规硶銆?br>鐪嬪埌tree灏辨槸浜屽弶鏍戯紝灏辫鎯冲埌鎺掑簭锛屽氨鎯宠鐢ㄥ埌姣旇緝銆?br>姣旇緝鐨勪袱绉嶆柟寮忥細
涓€涓槸Comparable锛氳鐩朿ompareTo鏂规硶锛?br>涓€涓槸Comparator锛氳鐩朿ompare鏂规硶銆?br>LinkedHashSet锛孡inkedHashMap:杩欎袱涓泦鍚堝彲浠ヤ繚璇佸搱甯岃〃鏈夊瓨鍏ラ『搴忓拰鍙栧嚭椤哄簭涓€鑷达紝淇濊瘉鍝堝笇琛ㄦ湁搴忋€?br>
闆嗗悎浠€涔堟椂鍊欑敤锛?br>褰撳瓨鍌ㄧ殑鏄竴涓厓绱犳椂锛屽氨鐢–ollection銆傚綋瀛樺偍瀵硅薄涔嬮棿瀛樺湪鐫€鏄犲皠鍏崇郴鏃讹紝灏变娇鐢∕ap闆嗗悎銆?br>
淇濊瘉鍞竴锛屽氨鐢⊿et銆備笉淇濊瘉鍞竴锛屽氨鐢↙ist銆?br>------------------------------------------------------------------------------------------------
Collections锛氬畠鐨勫嚭鐜扮粰闆嗗悎鎿嶄綔鎻愪緵浜嗘洿澶氱殑鍔熻兘銆傝繖涓被涓嶉渶瑕佸垱寤哄璞★紝鍐呴儴鎻愪緵鐨勯兘鏄潤鎬佹柟娉曘€?br>闈欐€佹柟娉曪細
Collections.sort(list);//list闆嗗悎杩涜鍏冪礌鐨勮嚜鐒堕『搴忔帓搴忋€?br>Collections.sort(list,new ComparatorByLen());//鎸夋寚瀹氱殑姣旇緝鍣ㄦ柟娉曟帓搴忋€?br>class ComparatorByLen implements Comparator<String>{
public int compare(String s1,String s2){
int temp = s1.length()-s2.length();
return temp==0?s1.compareTo(s2):temp;
}
}
Collections.max(list); //杩斿洖list涓瓧鍏搁『搴忔渶澶х殑鍏冪礌銆?br>int index = Collections.binarySearch(list,"zz");//浜屽垎鏌ユ壘锛岃繑鍥炶鏍囥€?br>Collections.reverseOrder();//閫嗗悜鍙嶈浆鎺掑簭銆?br>Collections.shuffle(list);//闅忔満瀵筶ist涓殑鍏冪礌杩涜浣嶇疆鐨勭疆鎹€?br>
灏嗛潪鍚屾闆嗗悎杞垚鍚屾闆嗗悎鐨勬柟娉曪細Collections涓殑 XXX synchronizedXXX(XXX);
List synchronizedList(list);
Map synchronizedMap(map);
鍘熺悊锛氬畾涔変竴涓被锛屽皢闆嗗悎鎵€鏈夌殑鏂规硶鍔犲悓涓€鎶婇攣鍚庤繑鍥炪€?br>
Collection 鍜?Collections鐨勫尯鍒細
Collections鏄釜java.util涓嬬殑绫伙紝鏄拡瀵归泦鍚堢被鐨勪竴涓伐鍏风被,鎻愪緵涓€绯诲垪闈欐€佹柟娉?瀹炵幇瀵归泦鍚堢殑鏌ユ壘銆佹帓搴忋€佹浛鎹€佺嚎绋嬪畨鍏ㄥ寲锛堝皢闈炲悓姝ョ殑闆嗗悎杞崲鎴愬悓姝ョ殑锛夌瓑鎿嶄綔銆?br>Collection鏄釜java.util涓嬬殑鎺ュ彛锛屽畠鏄悇绉嶉泦鍚堢粨鏋勭殑鐖舵帴鍙o紝缁ф壙浜庡畠鐨勬帴鍙d富瑕佹湁Set鍜孡ist,鎻愪緵浜嗗叧浜庨泦鍚堢殑涓€浜涙搷浣?濡傛彃鍏ャ€佸垹闄ゃ€佸垽鏂竴涓厓绱犳槸鍚﹀叾鎴愬憳銆侀亶鍘嗙瓑銆?br>-------------------------------------------------------
Arrays锛?br>鐢ㄤ簬鎿嶄綔鏁扮粍瀵硅薄鐨勫伐鍏风被锛岄噷闈㈤兘鏄潤鎬佹柟娉曘€?br>asList鏂规硶锛氬皢鏁扮粍杞崲鎴恖ist闆嗗悎銆?br>String[] arr = {"abc","kk","qq"};
List<String> list = Arrays.asList(arr);//灏哸rr鏁扮粍杞垚list闆嗗悎銆?br>灏嗘暟缁勮浆鎹㈡垚闆嗗悎锛屾湁浠€涔堝ソ澶勫憿锛熺敤aslist鏂规硶锛屽皢鏁扮粍鍙樻垚闆嗗悎锛?br>鍙互閫氳繃list闆嗗悎涓殑鏂规硶鏉ユ搷浣滄暟缁勪腑鐨勫厓绱狅細isEmpty()銆乧ontains銆乮ndexOf銆乻et锛?br>娉ㄦ剰锛堝眬闄愭€э級锛氭暟缁勬槸鍥哄畾闀垮害锛屼笉鍙互浣跨敤闆嗗悎瀵硅薄澧炲姞鎴栬€呭垹闄ょ瓑锛屼細鏀瑰彉鏁扮粍闀垮害鐨勫姛鑳芥柟娉曘€傛瘮濡俛dd銆乺emove銆乧lear銆傦紙浼氭姤涓嶆敮鎸佹搷浣滃紓甯窾nsupportedOperationException锛夛紱
濡傛灉鏁扮粍涓瓨鍌ㄧ殑寮曠敤鏁版嵁绫诲瀷锛岀洿鎺ヤ綔涓洪泦鍚堢殑鍏冪礌鍙互鐩存帴鐢ㄩ泦鍚堟柟娉曟搷浣溿€?br>濡傛灉鏁扮粍涓瓨鍌ㄧ殑鏄熀鏈暟鎹被鍨嬶紝asList浼氬皢鏁扮粍瀹炰綋浣滀负闆嗗悎鍏冪礌瀛樺湪銆?br>
闆嗗悎鍙樻暟缁勶細鐢ㄧ殑鏄疌ollection鎺ュ彛涓殑鏂规硶锛歵oArray();
濡傛灉缁檛oArray浼犻€掔殑鎸囧畾绫诲瀷鐨勬暟鎹暱搴﹀皬浜庝簡闆嗗悎鐨剆ize锛岄偅涔坱oArray鏂规硶锛屼細鑷畾鍐嶅垱寤轰竴涓绫诲瀷鐨勬暟鎹紝闀垮害涓洪泦鍚堢殑size銆?br> 濡傛灉浼犻€掔殑鎸囧畾鐨勭被鍨嬬殑鏁扮粍鐨勯暱搴﹀ぇ浜庝簡闆嗗悎鐨剆ize锛岄偅涔坱oArray鏂规硶锛屽氨涓嶄細鍒涘缓鏂版暟缁勶紝鐩存帴浣跨敤璇ユ暟缁勫嵆鍙紝骞跺皢闆嗗悎涓殑鍏冪礌瀛樺偍鍒版暟缁勪腑锛屽叾浠栦负瀛樺偍鍏冪礌鐨勪綅缃粯璁ゅ€糿ull銆?br> 鎵€浠ワ紝鍦ㄤ紶閫掓寚瀹氱被鍨嬫暟缁勬椂锛屾渶濂界殑鏂瑰紡灏辨槸鎸囧畾鐨勯暱搴﹀拰size鐩哥瓑鐨勬暟缁勩€?br>灏嗛泦鍚堝彉鎴愭暟缁勫悗鏈変粈涔堝ソ澶勶紵闄愬畾浜嗗闆嗗悎涓殑鍏冪礌杩涜澧炲垹鎿嶄綔锛屽彧瑕佽幏鍙栬繖浜涘厓绱犲嵆鍙€?br>------------------------------------------------------------------------------------------------
Jdk5.0鏂扮壒鎬э細
Collection鍦╦dk1.5浠ュ悗锛屾湁浜嗕竴涓埗鎺ュ彛Iterable锛岃繖涓帴鍙g殑鍑虹幇鐨勫皢iterator鏂规硶杩涜鎶藉彇锛屾彁楂樹簡鎵╁睍鎬с€?br>--------------------------------------------------
澧炲己for寰幆锛歠oreach璇彞锛宖oreach绠€鍖栦簡杩唬鍣ㄣ€?br>鏍煎紡锛?/ 澧炲己for寰幆鎷彿閲屽啓涓や釜鍙傛暟锛岀涓€涓槸澹版槑涓€涓彉閲忥紝绗簩涓氨鏄渶瑕佽凯浠g殑瀹瑰櫒
for( 鍏冪礌绫诲瀷 鍙橀噺鍚?: Collection闆嗗悎 & 鏁扮粍 ) {
鈥?br>}
楂樼骇for寰幆鍜屼紶缁焒or寰幆鐨勫尯鍒細
楂樼骇for寰幆鍦ㄤ娇鐢ㄦ椂锛屽繀椤昏鏄庣‘琚亶鍘嗙殑鐩爣銆傝繖涓洰鏍囷紝鍙互鏄疌ollection闆嗗悎鎴栬€呮暟缁勶紝濡傛灉閬嶅巻Collection闆嗗悎锛屽湪閬嶅巻杩囩▼涓繕闇€瑕佸鍏冪礌杩涜鎿嶄綔锛屾瘮濡傚垹闄わ紝闇€瑕佷娇鐢ㄨ凯浠e櫒銆?br>濡傛灉閬嶅巻鏁扮粍锛岃繕闇€瑕佸鏁扮粍鍏冪礌杩涜鎿嶄綔锛屽缓璁敤浼犵粺for寰幆鍥犱负鍙互瀹氫箟瑙掓爣閫氳繃瑙掓爣鎿嶄綔鍏冪礌銆傚鏋滃彧涓洪亶鍘嗚幏鍙栵紝鍙互绠€鍖栨垚楂樼骇for寰幆锛屽畠鐨勫嚭鐜颁负浜嗙畝鍖栦功鍐欍€?br>
楂樼骇for寰幆鍙互閬嶅巻map闆嗗悎鍚楋紵涓嶅彲浠ャ€備絾鏄彲浠ュ皢map杞垚set鍚庡啀浣跨敤foreach璇彞銆?br>
1)銆佷綔鐢細瀵瑰瓨鍌ㄥ璞$殑瀹瑰櫒杩涜杩唬锛?鏁扮粍 collection map
2)銆佸寮篺or寰幆杩唬鏁扮粍锛?br>String [] arr = {"a", "b", "c"};//鏁扮粍鐨勯潤鎬佸畾涔夋柟寮忥紝鍙瘯鐢ㄤ簬鏁扮粍棣栨瀹氫箟鐨勬椂鍊?br>for(String s : arr) {
System.out.println(s);
}
3)銆佸崟鍒楅泦鍚?Collection锛?br>List list = new ArrayList();
list.add("aaa");
// 澧炲己for寰幆, 娌℃湁浣跨敤娉涘瀷鐨勯泦鍚堣兘涓嶈兘浣跨敤澧炲己for寰幆杩唬锛熻兘
for(Object obj : list) {
String s = (String) obj;
System.out.println(s);
}
4)銆佸弻鍒楅泦鍚?Map锛?br>Map map = new HashMap();
map.put("a", "aaa");
// 浼犵粺鏂瑰紡锛氬繀椤绘帉鎻¤繖绉嶆柟寮?br>Set entrys = map.entrySet(); // 1.鑾峰緱鎵€鏈夌殑閿€煎Entry瀵硅薄
iter = entrys.iterator(); // 2.杩唬鍑烘墍鏈夌殑entry
while(iter.hasNext()) {
Map.Entry entry = (Entry) iter.next();
String key = (String) entry.getKey(); // 鍒嗗埆鑾峰緱key鍜寁alue
String value = (String) entry.getValue();
System.out.println(key + "=" + value);
}
// 澧炲己for寰幆杩唬锛氬師鍒欎笂map闆嗗悎鏄棤娉曚娇鐢ㄥ寮篺or寰幆鏉ヨ凯浠g殑锛屽洜涓哄寮篺or寰幆鍙兘閽堝瀹炵幇浜咺terable鎺ュ彛鐨勯泦鍚堣繘琛岃凯浠o紱Iterable鏄痡dk5涓柊瀹氫箟鐨勬帴鍙o紝灏变竴涓柟娉昳terator鏂规硶锛屽彧鏈夊疄鐜颁簡Iterable鎺ュ彛鐨勭被锛屾墠鑳戒繚璇佷竴瀹氭湁iterator鏂规硶锛宩ava鏈夎繖鏍风殑闄愬畾鏄洜涓哄寮篺or寰幆鍐呴儴杩樻槸鐢ㄨ凯浠e櫒瀹炵幇鐨勶紝鑰屽疄闄呬笂锛屾垜浠彲浠ラ€氳繃鏌愮鏂瑰紡鏉ヤ娇鐢ㄥ寮篺or寰幆銆?br>for(Object obj : map.entrySet()) {
Map.Entry entry = (Entry) obj; // obj 渚濇琛ㄧずEntry
System.out.println(entry.getKey() + "=" + entry.getValue());
}
5锛夈€侀泦鍚堣凯浠f敞鎰忛棶棰橈細鍦ㄨ凯浠i泦鍚堢殑杩囩▼涓紝涓嶈兘瀵归泦鍚堣繘琛屽鍒犳搷浣滐紙浼氭姤骞跺彂璁块棶寮傚父锛夛紱鍙互鐢ㄨ凯浠e櫒鐨勬柟娉曡繘琛屾搷浣滐紙瀛愮被listIterator锛氭湁澧炲垹鐨勬柟娉曪級銆?br>6)銆佸寮篺or寰幆娉ㄦ剰闂锛氬湪浣跨敤澧炲己for寰幆鏃讹紝涓嶈兘瀵瑰厓绱犺繘琛岃祴鍊硷紱
int[] arr = {1,2,3};
for(int num : arr) {
num = 0; //涓嶈兘鏀瑰彉鏁扮粍鐨勫€?br>}
System.out.println(arr[1]); //2
--------------------------------------------------
鍙彉鍙傛暟锛?..锛夛細鐢ㄥ埌鍑芥暟鐨勫弬鏁颁笂锛屽綋瑕佹搷浣滅殑鍚屼竴涓被鍨嬪厓绱犱釜鏁颁笉纭畾鐨勬椂鍊欙紝鍙槸鐢ㄨ繖涓柟寮忥紝杩欎釜鍙傛暟鍙互鎺ュ彈浠绘剰涓暟鐨勫悓涓€绫诲瀷鐨勬暟鎹€?br>
鍜屼互鍓嶆帴鏀舵暟缁勪笉涓€鏍风殑鏄細
浠ュ墠瀹氫箟鏁扮粍绫诲瀷锛岄渶瑕佸厛鍒涘缓涓€涓暟缁勫璞★紝鍐嶅皢杩欎釜鏁扮粍瀵硅薄浣滀负鍙傛暟浼犻€掔粰鍑芥暟銆傜幇鍦紝鐩存帴灏嗘暟缁勪腑鐨勫厓绱犱綔涓哄弬鏁颁紶閫掑嵆鍙€傚簳灞傚叾瀹炴槸灏嗚繖浜涘厓绱犺繘琛屾暟缁勭殑灏佽锛岃€岃繖涓皝瑁呭姩浣滐紝鏄湪搴曞眰瀹屾垚鐨勶紝琚殣钘忎簡銆傛墍浠ョ畝鍖栦簡鐢ㄦ埛鐨勪功鍐欙紝灏戜簡璋冪敤鑰呭畾涔夋暟缁勭殑鍔ㄤ綔銆?br>濡傛灉鍦ㄥ弬鏁板垪琛ㄤ腑浣跨敤浜嗗彲鍙樺弬鏁帮紝鍙彉鍙傛暟蹇呴』瀹氫箟鍦ㄥ弬鏁板垪琛ㄧ粨灏?涔熷氨鏄繀椤绘槸鏈€鍚庝竴涓弬鏁帮紝鍚﹀垯缂栬瘧浼氬け璐ャ€?銆?br>濡傛灉瑕佽幏鍙栧涓猧nt鏁扮殑鍜屽憿锛熷彲浠ヤ娇鐢ㄥ皢澶氫釜int鏁板皝瑁呭埌鏁扮粍涓紝鐩存帴瀵规暟缁勬眰鍜屽嵆鍙€?br>---------------------------------------------------
闈欐€佸鍏ワ細瀵煎叆浜嗙被涓殑鎵€鏈夐潤鎬佹垚鍛橈紝绠€鍖栭潤鎬佹垚鍛樼殑涔﹀啓銆?br>import static java.util.Collections.*; //瀵煎叆浜咰ollections绫讳腑鐨勬墍鏈夐潤鎬佹垚鍛?br>---------------------------------------------------
鏋氫妇锛氬叧閿瓧 enum
闂锛氬璞$殑鏌愪釜灞炴€х殑鍊间笉鑳芥槸浠绘剰鐨勶紝蹇呴』涓哄浐瀹氱殑涓€缁勫彇鍊煎叾涓殑鏌愪竴涓紱
瑙e喅鍔炴硶锛?br>1锛夈€佸湪setGrade鏂规硶涓仛鍒ゆ柇锛屼笉绗﹀悎鏍煎紡瑕佹眰灏辨姏鍑哄紓甯革紱
2锛夈€佺洿鎺ラ檺瀹氱敤鎴风殑閫夋嫨锛岄€氳繃鑷畾涔夌被妯℃嫙鏋氫妇鐨勬柟寮忔潵闄愬畾鐢ㄦ埛鐨勮緭鍏ワ紝鍐欎竴涓狦rade绫伙紝绉佹湁鏋勯€犲嚱鏁帮紝瀵瑰鎻愪緵5涓潤鎬佺殑甯搁噺琛ㄧず绫荤殑瀹炰緥锛?br>3)銆乯dk5涓柊瀹氫箟浜嗘灇涓剧被鍨嬶紝涓撻棬鐢ㄤ簬瑙e喅姝ょ被闂锛?br>4)銆佹灇涓惧氨鏄竴涓壒娈婄殑java绫伙紝鍙互瀹氫箟灞炴€с€佹柟娉曘€佹瀯閫犲嚱鏁般€佸疄鐜版帴鍙c€佺户鎵跨被锛?br>------------------------------------------------------------------------------
鑷姩鎷嗚绠憋細java涓暟鎹被鍨嬪垎涓轰袱绉?锛?鍩烘湰鏁版嵁绫诲瀷 寮曠敤鏁版嵁绫诲瀷(瀵硅薄)
鍦?java绋嬪簭涓墍鏈夌殑鏁版嵁閮介渶瑕佸綋鍋氬璞℃潵澶勭悊锛岄拡瀵?绉嶅熀鏈暟鎹被鍨嬫彁渚涗簡鍖呰绫伙紝濡備笅锛?br>int --> Integer
byte --> Byte
short --> Short
long --> Long
char --> Character
double --> Double
float --> Float
boolean --> Boolean
jdk5浠ュ墠鍩烘湰鏁版嵁绫诲瀷鍜屽寘瑁呯被涔嬮棿闇€瑕佷簰杞細
鍩烘湰---寮曠敤 Integer x = new Integer(x);
寮曠敤---鍩烘湰 int num = x.intValue();
1)銆両nteger x = 1; x = x + 1; 缁忓巻浜嗕粈涔堣繃绋嬶紵瑁呯 脿 鎷嗙 脿 瑁呯锛?br>2)銆佷负浜嗕紭鍖栵紝铏氭嫙鏈轰负鍖呰绫绘彁渚涗簡缂撳啿姹狅紝Integer姹犵殑澶у皬 -128~127 涓€涓瓧鑺傜殑澶у皬锛?br>3)銆丼tring姹狅細Java涓轰簡浼樺寲瀛楃涓叉搷浣?鎻愪緵浜嗕竴涓紦鍐叉睜锛?br>----------------------------------------------------------
娉涘瀷锛歫dk1.5鐗堟湰浠ュ悗鍑虹幇鐨勪竴涓畨鍏ㄦ満鍒躲€傝〃鐜版牸寮忥細< >
濂藉锛?br>1锛氬皢杩愯鏃舵湡鐨勯棶棰楥lassCastException闂杞崲鎴愪簡缂栬瘧澶辫触锛屼綋鐜板湪缂栬瘧鏃舵湡锛岀▼搴忓憳灏卞彲浠ヨВ鍐抽棶棰樸€?br>2锛氶伩鍏嶄簡寮哄埗杞崲鐨勯夯鐑︺€?br>
鍙甯︽湁<>鐨勭被鎴栬€呮帴鍙o紝閮藉睘浜庡甫鏈夌被鍨嬪弬鏁扮殑绫绘垨鑰呮帴鍙o紝鍦ㄤ娇鐢ㄨ繖浜涚被鎴栬€呮帴鍙f椂锛屽繀椤荤粰<>涓紶閫掍竴涓叿浣撶殑寮曠敤鏁版嵁绫诲瀷銆?br>
娉涘瀷鎶€鏈細鍏跺疄搴旂敤鍦ㄧ紪璇戞椂鏈燂紝鏄粰缂栬瘧鍣ㄤ娇鐢ㄧ殑鎶€鏈紝鍒颁簡杩愯鏃舵湡锛屾硾鍨嬪氨涓嶅瓨鍦ㄤ簡銆?br>涓轰粈涔? 鍥犱负娉涘瀷鐨勬摝闄わ細涔熷氨鏄锛岀紪杈戝櫒妫€鏌ヤ簡娉涘瀷鐨勭被鍨嬫纭悗锛屽湪鐢熸垚鐨勭被鏂囦欢涓槸娌℃湁娉涘瀷鐨勩€?br>鍦ㄨ繍琛屾椂锛屽浣曠煡閬撹幏鍙栫殑鍏冪礌绫诲瀷鑰屼笉鐢ㄥ己杞憿锛?br>娉涘瀷鐨勮ˉ鍋匡細鍥犱负瀛樺偍鐨勬椂鍊欙紝绫诲瀷宸茬粡纭畾浜嗘槸鍚屼竴涓被鍨嬬殑鍏冪礌锛屾墍浠ュ湪杩愯鏃讹紝鍙鑾峰彇鍒拌鍏冪礌鐨勭被鍨嬶紝鍦ㄥ唴閮ㄨ繘琛屼竴娆¤浆鎹㈠嵆鍙紝鎵€浠ヤ娇鐢ㄨ€呬笉鐢ㄥ啀鍋氳浆鎹㈠姩浣滀簡銆?br>
浠€涔堟椂鍊欑敤娉涘瀷绫诲憿锛?br>褰撶被涓殑鎿嶄綔鐨勫紩鐢ㄦ暟鎹被鍨嬩笉纭畾鐨勬椂鍊欙紝浠ュ墠鐢ㄧ殑Object鏉ヨ繘琛屾墿灞曠殑锛岀幇鍦ㄥ彲浠ョ敤娉涘瀷鏉ヨ〃绀恒€傝繖鏍峰彲浠ラ伩鍏嶅己杞殑楹荤儲锛岃€屼笖灏嗚繍琛岄棶棰樿浆绉诲埌鐨勭紪璇戞椂鏈熴€?br>----------------------------------------------------------
娉涘瀷鍦ㄧ▼搴忓畾涔変笂鐨勪綋鐜帮細
//娉涘瀷绫伙細灏嗘硾鍨嬪畾涔夊湪绫讳笂銆?br>class Tool<Q> {
private Q obj;
public void setObject(Q obj) {
this.obj = obj;
}
public Q getObject() {
return obj;
}
}
//褰撴柟娉曟搷浣滅殑寮曠敤鏁版嵁绫诲瀷涓嶇‘瀹氱殑鏃跺€欙紝鍙互灏嗘硾鍨嬪畾涔夊湪鏂规硶涓娿€?br> public <W> void method(W w) {
System.out.println("method:"+w);
}
//闈欐€佹柟娉曚笂鐨勬硾鍨嬶細闈欐€佹柟娉曟棤娉曡闂被涓婂畾涔夌殑娉涘瀷銆傚鏋滈潤鎬佹柟娉曟搷浣滅殑寮曠敤鏁版嵁绫诲瀷涓嶇‘瀹氱殑鏃跺€欙紝蹇呴』瑕佸皢娉涘瀷瀹氫箟鍦ㄦ柟娉曚笂銆?br> public static <Q> void function(Q t) {
System.out.println("function:"+t);
}
//娉涘瀷鎺ュ彛.
interface Inter<T> {
void show(T t);
}
class InterImpl<R> implements Inter<R> {
public void show(R r) {
System.out.println("show:"+r);
}
}
------------------------------------------------------------
娉涘瀷涓殑閫氶厤绗︼細鍙互瑙e喅褰撳叿浣撶被鍨嬩笉纭畾鐨勬椂鍊欙紝杩欎釜閫氶厤绗﹀氨鏄?? 锛涘綋鎿嶄綔绫诲瀷鏃讹紝涓嶉渶瑕佷娇鐢ㄧ被鍨嬬殑鍏蜂綋鍔熻兘鏃讹紝鍙娇鐢∣bject绫讳腑鐨勫姛鑳姐€傞偅涔堝彲浠ョ敤 ? 閫氶厤绗︽潵琛ㄦ湭鐭ョ被鍨嬨€?br>
娉涘瀷闄愬畾锛?br> 涓婇檺锛氾紵extends E锛氬彲浠ユ帴鏀禘绫诲瀷鎴栬€匛鐨勫瓙绫诲瀷瀵硅薄銆?br> 涓嬮檺锛氾紵super E锛氬彲浠ユ帴鏀禘绫诲瀷鎴栬€匛鐨勭埗绫诲瀷瀵硅薄銆?br>
涓婇檺浠€涔堟椂鍊欑敤锛氬線闆嗗悎涓坊鍔犲厓绱犳椂锛屾棦鍙互娣诲姞E绫诲瀷瀵硅薄锛屽張鍙互娣诲姞E鐨勫瓙绫诲瀷瀵硅薄銆備负浠€涔堬紵鍥犱负鍙栫殑鏃跺€欙紝E绫诲瀷鏃㈠彲浠ユ帴鏀禘绫诲璞★紝鍙堝彲浠ユ帴鏀禘鐨勫瓙绫诲瀷瀵硅薄銆?br>
涓嬮檺浠€涔堟椂鍊欑敤锛氬綋浠庨泦鍚堜腑鑾峰彇鍏冪礌杩涜鎿嶄綔鐨勬椂鍊欙紝鍙互鐢ㄥ綋鍓嶅厓绱犵殑绫诲瀷鎺ユ敹锛屼篃鍙互鐢ㄥ綋鍓嶅厓绱犵殑鐖剁被鍨嬫帴鏀躲€?br>
娉涘瀷鐨勭粏鑺傦細
1锛夈€佹硾鍨嬪埌搴曚唬琛ㄤ粈涔堢被鍨嬪彇鍐充簬璋冪敤鑰呬紶鍏ョ殑绫诲瀷锛屽鏋滄病浼狅紝榛樿鏄疧bject绫诲瀷锛?br>2锛夈€佷娇鐢ㄥ甫娉涘瀷鐨勭被鍒涘缓瀵硅薄鏃讹紝绛夊紡涓よ竟鎸囧畾鐨勬硾鍨嬪繀椤讳竴鑷达紱
鍘熷洜锛氱紪璇戝櫒妫€鏌ュ璞¤皟鐢ㄦ柟娉曟椂鍙湅鍙橀噺锛岀劧鑰岀▼搴忚繍琛屾湡闂磋皟鐢ㄦ柟娉曟椂灏辫鑰冭檻瀵硅薄鍏蜂綋绫诲瀷浜嗭紱
3锛夈€佺瓑寮忎袱杈瑰彲浠ュ湪浠绘剰涓€杈逛娇鐢ㄦ硾鍨嬶紝鍦ㄥ彟涓€杈逛笉浣跨敤(鑰冭檻鍚戝悗鍏煎)锛?br>ArrayList<String> al = new ArrayList<Object>(); //閿?br>//瑕佷繚璇佸乏鍙充袱杈圭殑娉涘瀷鍏蜂綋绫诲瀷涓€鑷村氨鍙互浜嗭紝杩欐牱涓嶅鏄撳嚭閿欍€?br>ArrayList<? extends Object> al = new ArrayList<String>();
al.add("aa"); //閿?br>//鍥犱负闆嗗悎鍏蜂綋瀵硅薄涓棦鍙瓨鍌⊿tring锛屼篃鍙互瀛樺偍Object鐨勫叾浠栧瓙绫伙紝鎵€浠ユ坊鍔犲叿浣撶殑绫诲瀷瀵硅薄涓嶅悎閫傦紝绫诲瀷妫€鏌ヤ細鍑虹幇瀹夊叏闂銆?锛焑xtends Object 浠h〃Object鐨勫瓙绫诲瀷涓嶇‘瀹氾紝鎬庝箞鑳芥坊鍔犲叿浣撶被鍨嬬殑瀵硅薄鍛紵
public static void method(ArrayList<? extends Object> al) {
al.add("abc"); //閿?br> //鍙兘瀵筧l闆嗗悎涓殑鍏冪礌璋冪敤Object绫讳腑鐨勬柟娉曪紝鍏蜂綋瀛愮被鍨嬬殑鏂规硶閮戒笉鑳界敤锛屽洜涓哄瓙绫诲瀷涓嶇‘瀹氥€?br>}
------------------------------------------------------------------------------------------------------------------------------------------------
IO娴侊細鈽呪槄鈽呪槄鈽咃紝
鐢ㄤ簬澶勭悊璁惧涓婃暟鎹€?br>娴侊細鍙互鐞嗚В鏁版嵁鐨勬祦鍔紝灏辨槸涓€涓暟鎹祦銆侷O娴佹渶缁堣浠ュ璞℃潵浣撶幇锛屽璞¢兘瀛樺湪IO鍖呬腑銆?br>娴佷篃杩涜鍒嗙被锛?br>1锛氳緭鍏ユ祦锛堣锛夊拰杈撳嚭娴侊紙鍐欙級銆?br>2锛氬洜涓哄鐞嗙殑鏁版嵁涓嶅悓锛屽垎涓哄瓧鑺傛祦鍜屽瓧绗︽祦銆?br>
瀛楄妭娴侊細
澶勭悊瀛楄妭鏁版嵁鐨勬祦瀵硅薄銆傝澶囦笂鐨勬暟鎹棤璁烘槸鍥剧墖鎴栬€卍vd锛屾枃瀛楋紝瀹冧滑閮戒互浜岃繘鍒跺瓨鍌ㄧ殑銆備簩杩涘埗鐨勬渶缁堥兘鏄互涓€涓?浣嶄负鏁版嵁鍗曞厓杩涜浣撶幇锛屾墍浠ヨ绠楁満涓殑鏈€灏忔暟鎹崟鍏冨氨鏄瓧鑺傘€傛剰鍛崇潃锛屽瓧鑺傛祦鍙互澶勭悊璁惧涓婄殑鎵€鏈夋暟鎹紝鎵€浠ュ瓧鑺傛祦涓€鏍峰彲浠ュ鐞嗗瓧绗︽暟鎹€?br>
閭d箞涓轰粈涔堣鏈夊瓧绗︽祦鍛紵鍥犱负瀛楃姣忎釜鍥藉閮戒笉涓€鏍凤紝鎵€浠ユ秹鍙婂埌浜嗗瓧绗︾紪鐮侀棶棰橈紝閭d箞GBK缂栫爜鐨勪腑鏂囩敤unicode缂栫爜瑙f瀽鏄湁闂鐨勶紝鎵€浠ラ渶瑕佽幏鍙栦腑鏂囧瓧鑺傛暟鎹殑鍚屾椂+ 鎸囧畾鐨勭紪鐮佽〃鎵嶅彲浠ヨВ鏋愭纭暟鎹€備负浜嗘柟渚夸簬鏂囧瓧鐨勮В鏋愶紝鎵€浠ュ皢瀛楄妭娴佸拰缂栫爜琛ㄥ皝瑁呮垚瀵硅薄锛岃繖涓璞″氨鏄瓧绗︽祦銆傚彧瑕佹搷浣滃瓧绗︽暟鎹紝浼樺厛鑰冭檻浣跨敤瀛楃娴佷綋绯汇€?br>
娉ㄦ剰锛氭祦鐨勬搷浣滃彧鏈変袱绉嶏細璇诲拰鍐欍€?br>
娴佺殑浣撶郴鍥犱负鍔熻兘涓嶅悓锛屼絾鏄湁鍏辨€у唴瀹癸紝涓嶆柇鎶藉彇锛屽舰鎴愮户鎵夸綋绯汇€傝浣撶郴涓€鍏辨湁鍥涗釜鍩虹被锛岃€屼笖閮芥槸鎶借薄绫汇€?br>
瀛楄妭娴侊細InputStream OutputStream
瀛楃娴侊細Reader Writer
鍦ㄨ繖鍥涗釜绯荤粺涓紝瀹冧滑鐨勫瓙绫伙紝閮芥湁涓€涓叡鎬х壒鐐癸細瀛愮被鍚嶅悗缂€閮芥槸鐖剁被鍚嶏紝鍓嶇紑鍚嶉兘鏄繖涓瓙绫荤殑鍔熻兘鍚嶇О銆?br>--------------------------------------------------------------------------------------------------------------------
public static void main(String[] args) throws IOException { //璇汇€佸啓閮戒細鍙戠敓IO寮傚父
/*
1锛氬垱寤轰竴涓瓧绗﹁緭鍑烘祦瀵硅薄锛岀敤浜庢搷浣滄枃浠躲€傝瀵硅薄涓€寤虹珛锛屽氨蹇呴』鏄庣‘鏁版嵁瀛樺偍浣嶇疆锛屾槸涓€涓枃浠躲€?br> 2锛氬璞′骇鐢熷悗锛屼細鍦ㄥ爢鍐呭瓨涓湁涓€涓疄浣擄紝鍚屾椂涔熻皟鐢ㄤ簡绯荤粺搴曞眰璧勬簮锛屽湪鎸囧畾鐨勪綅缃垱寤轰簡涓€涓瓨鍌ㄦ暟鎹殑鏂囦欢銆?br> 3锛氬鏋滄寚瀹氫綅缃紝鍑虹幇浜嗗悓鍚嶆枃浠讹紝鏂囦欢浼氳瑕嗙洊銆?br> */
FileWriter fw = new FileWriter("demo.txt"); // FileNotFoundException
/*
璋冪敤Writer绫讳腑鐨剋rite鏂规硶鍐欏叆瀛楃涓层€傚瓧绗︿覆骞舵湭鐩存帴鍐欏叆鍒扮洰鐨勫湴涓紝鑰屾槸鍐欏叆鍒颁簡娴佷腑锛?鍏跺疄鏄啓鍏ュ埌鍐呭瓨缂撳啿鍖轰腑)銆傛€庝箞鎶婃暟鎹紕鍒版枃浠朵腑锛?br> */
fw.write("abcde");
fw.flush(); // 鍒锋柊缂撳啿鍖猴紝灏嗙紦鍐插尯涓殑鏁版嵁鍒峰埌鐩殑鍦版枃浠朵腑銆?br> fw.close(); // 鍏抽棴娴侊紝鍏跺疄鍏抽棴鐨勫氨鏄痡ava璋冪敤鐨勭郴缁熷簳灞傝祫婧愩€傚湪鍏抽棴鍓嶏紝浼氬厛鍒锋柊璇ユ祦銆?br>}
close()鍜宖lush()鐨勫尯鍒細
flush()锛氬皢缂撳啿鍖虹殑鏁版嵁鍒峰埌鐩殑鍦颁腑鍚庯紝娴佸彲浠ヤ娇鐢ㄣ€?br>close()锛氬皢缂撳啿鍖虹殑鏁版嵁鍒峰埌鐩殑鍦颁腑鍚庯紝娴佸氨鍏抽棴浜嗭紝璇ユ柟娉曚富瑕佺敤浜庣粨鏉熻皟鐢ㄧ殑搴曞眰璧勬簮銆傝繖涓姩浣滀竴瀹氬仛銆?br>--------------------------------------------------------------------------------------------------------------------
io寮傚父鐨勫鐞嗘柟寮忥細
io涓€瀹氳鍐檉inally锛?br>
FileWriter鍐欏叆鏁版嵁鐨勭粏鑺傦細
1锛歸indow涓殑鎹㈣绗︼細
涓や釜绗﹀彿缁勬垚銆?linux锛歕n銆?br>2锛氱画鍐欐暟鎹紝鍙鍦ㄦ瀯閫犲嚱鏁颁腑浼犲叆鏂扮殑鍙傛暟true銆?br>3锛氱洰褰曞垎鍓茬锛歸indow \ /
public static void main(String[] args) {
FileWriter fw = null;
try {
fw = new FileWriter("demo.txt",true);
fw.write("abcde");
}
catch (IOException e ){
System.out.println(e.toString()+"....");
}
finally{
if(fw!=null)
try{
fw.close();
}
catch (IOException e){
System.out.println("close:"+e.toString());
}
}
}
--------------------------------------------------------------------------------------------------------------------
FileReader锛氫娇鐢≧eader浣撶郴锛岃鍙栦竴涓枃鏈枃浠朵腑鐨勬暟鎹€傝繑鍥?-1 锛屾爣蹇楄鍒扮粨灏俱€?br>import java.io.*;
class FileReaderDemo {
public static void main(String[] args) throws IOException {
/*
鍒涘缓鍙互璇诲彇鏂囨湰鏂囦欢鐨勬祦瀵硅薄锛孎ileReader璁╁垱寤哄ソ鐨勬祦瀵硅薄鍜屾寚瀹氱殑鏂囦欢鐩稿叧鑱斻€?br> */
FileReader fr = new FileReader("demo.txt");
int ch = 0;
while((ch = fr.read())!= -1) { //鏉′欢鏄病鏈夎鍒扮粨灏?br> System.out.println((char)ch); //璋冪敤璇诲彇娴佺殑read鏂规硶锛岃鍙栦竴涓瓧绗︺€?br> }
fr.close();
}
}
--------------------------------------------------------------------------------------------------------------------
璇诲彇鏁版嵁鐨勭浜岀鏂瑰紡锛氱浜岀鏂瑰紡杈冧负楂樻晥锛岃嚜瀹氫箟缂撳啿鍖恒€?br>import java.io.*;
class FileReaderDemo2 {
public static void main(String[] args) throws IOException {
FileReader fr = new FileReader("demo.txt"); //鍒涘缓璇诲彇娴佸璞″拰鎸囧畾鏂囦欢鍏宠仈銆?br> //鍥犱负瑕佷娇鐢╮ead(char[])鏂规硶锛屽皢璇诲彇鍒板瓧绗﹀瓨鍏ユ暟缁勩€傛墍浠ヨ鍒涘缓涓€涓瓧绗︽暟缁勶紝涓€鑸暟缁勭殑闀垮害閮芥槸1024鐨勬暣鏁板€嶃€?br> char[] buf = new char[1024];
int len = 0;
while(( len=fr.read(buf)) != -1) {
System.out.println(new String(buf,0,len));
}
fr.close();
}
}
--------------------------------------------------------------------------------------------------------------------
IO涓殑浣跨敤鍒颁簡涓€涓璁℃ā寮忥細瑁呴グ璁捐妯″紡銆?br>瑁呴グ璁捐妯″紡瑙e喅锛氬涓€缁勭被杩涜鍔熻兘鐨勫寮恒€?br>鍖呰锛氬啓涓€涓被(鍖呰绫?瀵硅鍖呰瀵硅薄杩涜鍖呰锛?br> * 1銆佸寘瑁呯被鍜岃鍖呰瀵硅薄瑕佸疄鐜板悓鏍风殑鎺ュ彛锛?br> * 2銆佸寘瑁呯被瑕佹寔鏈変竴涓鍖呰瀵硅薄锛?br> * 3銆佸寘瑁呯被鍦ㄥ疄鐜版帴鍙f椂锛屽ぇ閮ㄥ垎鏂规硶鏄潬璋冪敤琚寘瑁呭璞℃潵瀹炵幇鐨勶紝瀵逛簬闇€瑕佷慨鏀圭殑鏂规硶鎴戜滑鑷繁瀹炵幇锛?br>--------------------------------------------------------------------------------------------------------------------
瀛楃娴侊細
Reader锛氱敤浜庤鍙栧瓧绗︽祦鐨勬娊璞$被銆傚瓙绫诲繀椤诲疄鐜扮殑鏂规硶鍙湁 read(char[], int, int) 鍜?close()銆?br> |---BufferedReader锛氫粠瀛楃杈撳叆娴佷腑璇诲彇鏂囨湰锛岀紦鍐插悇涓瓧绗︼紝浠庤€屽疄鐜板瓧绗︺€佹暟缁勫拰琛岀殑楂樻晥璇诲彇銆?鍙互鎸囧畾缂撳啿鍖虹殑澶у皬锛屾垨鑰呭彲浣跨敤榛樿鐨勫ぇ灏忋€傚ぇ澶氭暟鎯呭喌涓嬶紝榛樿鍊煎氨瓒冲澶т簡銆?br> |---LineNumberReader锛氳窡韪鍙风殑缂撳啿瀛楃杈撳叆娴併€傛绫诲畾涔変簡鏂规硶 setLineNumber(int) 鍜?getLineNumber()锛屽畠浠彲鍒嗗埆鐢ㄤ簬璁剧疆鍜岃幏鍙栧綋鍓嶈鍙枫€?br> |---InputStreamReader锛氭槸瀛楄妭娴侀€氬悜瀛楃娴佺殑妗ユ锛氬畠浣跨敤鎸囧畾鐨?charset 璇诲彇瀛楄妭骞跺皢鍏惰В鐮佷负瀛楃銆傚畠浣跨敤鐨勫瓧绗﹂泦鍙互鐢卞悕绉版寚瀹氭垨鏄惧紡缁欏畾锛屾垨鑰呭彲浠ユ帴鍙楀钩鍙伴粯璁ょ殑瀛楃闆嗐€?br> |---FileReader锛氱敤鏉ヨ鍙栧瓧绗︽枃浠剁殑渚挎嵎绫汇€傛绫荤殑鏋勯€犳柟娉曞亣瀹氶粯璁ゅ瓧绗︾紪鐮佸拰榛樿瀛楄妭缂撳啿鍖哄ぇ灏忛兘鏄€傚綋鐨勩€傝鑷繁鎸囧畾杩欎簺鍊硷紝鍙互鍏堝湪 FileInputStream 涓婃瀯閫犱竴涓?InputStreamReader銆?br> |---CharArrayReader锛?br> |---StringReader锛?br>-------------------------------------------------
Writer锛氬啓鍏ュ瓧绗︽祦鐨勬娊璞$被銆傚瓙绫诲繀椤诲疄鐜扮殑鏂规硶浠呮湁 write(char[], int, int)銆乫lush() 鍜?close()銆?br> |---BufferedWriter锛氬皢鏂囨湰鍐欏叆瀛楃杈撳嚭娴侊紝缂撳啿鍚勪釜瀛楃锛屼粠鑰屾彁渚涘崟涓瓧绗︺€佹暟缁勫拰瀛楃涓茬殑楂樻晥鍐欏叆銆?br> |---OutputStreamWriter锛氭槸瀛楃娴侀€氬悜瀛楄妭娴佺殑妗ユ锛氬彲浣跨敤鎸囧畾鐨?charset 灏嗚鍐欏叆娴佷腑鐨勫瓧绗︾紪鐮佹垚瀛楄妭銆傚畠浣跨敤鐨勫瓧绗﹂泦鍙互鐢卞悕绉版寚瀹氭垨鏄惧紡缁欏畾锛屽惁鍒欏皢鎺ュ彈骞冲彴榛樿鐨勫瓧绗﹂泦銆?br> |---FileWriter锛氱敤鏉ュ啓鍏ュ瓧绗︽枃浠剁殑渚挎嵎绫汇€傛绫荤殑鏋勯€犳柟娉曞亣瀹氶粯璁ゅ瓧绗︾紪鐮佸拰榛樿瀛楄妭缂撳啿鍖哄ぇ灏忛兘鏄彲鎺ュ彈鐨勩€傝鑷繁鎸囧畾杩欎簺鍊硷紝鍙互鍏堝湪 FileOutputStream 涓婃瀯閫犱竴涓?OutputStreamWriter銆?br> |---PrintWriter锛?br> |---CharArrayWriter锛?br> |---StringWriter锛?br>---------------------------------
瀛楄妭娴侊細
InputStream锛氭槸琛ㄧず瀛楄妭杈撳叆娴佺殑鎵€鏈夌被鐨勮秴绫汇€?br> |--- FileInputStream锛氫粠鏂囦欢绯荤粺涓殑鏌愪釜鏂囦欢涓幏寰楄緭鍏ュ瓧鑺傘€傚摢浜涙枃浠跺彲鐢ㄥ彇鍐充簬涓绘満鐜銆侳ileInputStream 鐢ㄤ簬璇诲彇璇稿鍥惧儚鏁版嵁涔嬬被鐨勫師濮嬪瓧鑺傛祦銆傝璇诲彇瀛楃娴侊紝璇疯€冭檻浣跨敤 FileReader銆?br> |--- FilterInputStream锛氬寘鍚叾浠栦竴浜涜緭鍏ユ祦锛屽畠灏嗚繖浜涙祦鐢ㄤ綔鍏跺熀鏈暟鎹簮锛屽畠鍙互鐩存帴浼犺緭鏁版嵁鎴栨彁渚涗竴浜涢澶栫殑鍔熻兘銆?br> |--- BufferedInputStream锛氳绫诲疄鐜扮紦鍐茬殑杈撳叆娴併€?br> |--- Stream锛?br> |--- ObjectInputStream锛?br> |--- PipedInputStream锛?br>-----------------------------------------------
OutputStream锛氭鎶借薄绫绘槸琛ㄧず杈撳嚭瀛楄妭娴佺殑鎵€鏈夌被鐨勮秴绫汇€?br> |--- FileOutputStream锛氭枃浠惰緭鍑烘祦鏄敤浜庡皢鏁版嵁鍐欏叆 File 鎴?FileDescriptor 鐨勮緭鍑烘祦銆?br> |--- FilterOutputStream锛氭绫绘槸杩囨护杈撳嚭娴佺殑鎵€鏈夌被鐨勮秴绫汇€?br> |--- BufferedOutputStream锛氳绫诲疄鐜扮紦鍐茬殑杈撳嚭娴併€?br> |--- PrintStream锛?br> |--- DataOutputStream锛?br> |--- ObjectOutputStream锛?br> |--- PipedOutputStream锛?br>--------------------------------
缂撳啿鍖烘槸鎻愰珮鏁堢巼鐢ㄧ殑锛岀粰璋佹彁楂樺憿锛?br>BufferedWriter锛氭槸缁欏瓧绗﹁緭鍑烘祦鎻愰珮鏁堢巼鐢ㄧ殑锛岄偅灏辨剰鍛崇潃锛岀紦鍐插尯瀵硅薄寤虹珛鏃讹紝蹇呴』瑕佸厛鏈夋祦瀵硅薄銆傛槑纭鎻愰珮鍏蜂綋鐨勬祦瀵硅薄鐨勬晥鐜囥€?br> FileWriter fw = new FileWriter("bufdemo.txt");
BufferedWriter bufw = new BufferedWriter(fw);//璁╃紦鍐插尯鍜屾寚瀹氭祦鐩稿叧鑱斻€?br> for(int x=0; x<4; x++){
bufw.write(x+"abc");
bufw.newLine(); //鍐欏叆涓€涓崲琛岀锛岃繖涓崲琛岀鍙互渚濇嵁骞冲彴鐨勪笉鍚屽啓鍏ヤ笉鍚岀殑鎹㈣绗︺€?br> bufw.flush();//瀵圭紦鍐插尯杩涜鍒锋柊锛屽彲浠ヨ鏁版嵁鍒扮洰鐨勫湴涓€?br> }
bufw.close();//鍏抽棴缂撳啿鍖猴紝鍏跺疄灏辨槸鍦ㄥ叧闂叿浣撶殑娴併€?br>-----------------------------
BufferedReader锛?br> FileReader fr = new FileReader("bufdemo.txt");
BufferedReader bufr = new BufferedReader(fr);
String line = null;
while((line=bufr.readLine())!=null){ //readLine鏂规硶杩斿洖鐨勬椂鍊欐槸涓嶅甫鎹㈣绗︾殑銆?br> System.out.println(line);
}
bufr.close();
-----------------------------
//璁颁綇锛屽彧瑕佷竴璇诲彇閿洏褰曞叆锛屽氨鐢ㄨ繖鍙ヨ瘽銆?br>BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bufw = new BufferedWriter(new OutputStreamWriter(System.out));//杈撳嚭鍒版帶鍒跺彴
String line = null;
while((line=bufr.readLine())!=null){
if("over".equals(line))
break;
bufw.write(line.toUpperCase());//灏嗚緭鍏ョ殑瀛楃杞垚澶у啓瀛楃杈撳嚭
bufw.newLine();
bufw.flush();
}
bufw.close();
bufr.close();
------------------------------
娴佸璞★細
鍏跺疄寰堢畝鍗曪紝灏辨槸璇诲彇鍜屽啓鍏ャ€備絾鏄洜涓哄姛鑳界殑涓嶅悓锛屾祦鐨勪綋绯讳腑鎻愪緵N澶氱殑瀵硅薄銆傞偅涔堝紑濮嬫椂锛屽埌搴曡鐢ㄥ摢涓璞℃洿涓哄悎閫傚憿锛熻繖灏遍渶瑕佹槑纭祦鐨勬搷浣滆寰嬨€?br>
娴佺殑鎿嶄綔瑙勫緥锛?br>1锛屾槑纭簮鍜岀洰鐨勩€?br> 鏁版嵁婧愶細灏辨槸闇€瑕佽鍙栵紝鍙互浣跨敤涓や釜浣撶郴锛欼nputStream銆丷eader锛?br> 鏁版嵁姹囷細灏辨槸闇€瑕佸啓鍏ワ紝鍙互浣跨敤涓や釜浣撶郴锛歄utputStream銆乄riter锛?br>2锛屾搷浣滅殑鏁版嵁鏄惁鏄函鏂囨湰鏁版嵁锛?br> 濡傛灉鏄細鏁版嵁婧愶細Reader
鏁版嵁姹囷細Writer
濡傛灉涓嶆槸锛氭暟鎹簮锛欼nputStream
鏁版嵁姹囷細OutputStream
3锛岃櫧鐒剁‘瀹氫簡涓€涓綋绯伙紝浣嗘槸璇ヤ綋绯讳腑鏈夊お澶氱殑瀵硅薄锛屽埌搴曠敤鍝釜鍛紵
鏄庣‘鎿嶄綔鐨勬暟鎹澶囥€?br> 鏁版嵁婧愬搴旂殑璁惧锛氱‖鐩?File)锛屽唴瀛?鏁扮粍)锛岄敭鐩?System.in)
鏁版嵁姹囧搴旂殑璁惧锛氱‖鐩?File)锛屽唴瀛?鏁扮粍)锛屾帶鍒跺彴(System.out)銆?br>4锛岄渶瑕佸湪鍩烘湰鎿嶄綔涓婇檮鍔犲叾浠栧姛鑳藉悧锛熸瘮濡傜紦鍐层€?br> 濡傛灉闇€瑕佸氨杩涜瑁呴グ銆?br>
杞崲娴佺壒鏈夊姛鑳斤細杞崲娴佸彲浠ュ皢瀛楄妭杞垚瀛楃锛屽師鍥犲湪浜庯紝灏嗚幏鍙栧埌鐨勫瓧鑺傞€氳繃鏌ョ紪鐮佽〃鑾峰彇鍒版寚瀹氬搴斿瓧绗︺€?br>
杞崲娴佺殑鏈€寮哄姛鑳藉氨鏄熀浜?瀛楄妭娴?+ 缂栫爜琛?銆傛病鏈夎浆鎹紝娌℃湁瀛楃娴併€?br>
鍙戠幇杞崲娴佹湁涓€涓瓙绫诲氨鏄搷浣滄枃浠剁殑瀛楃娴佸璞★細
InputStreamReader
|--FileReader
OutputStreamWriter
|--FileWrier
鎯宠鎿嶄綔鏂囨湰鏂囦欢锛屽繀椤昏杩涜缂栫爜杞崲锛岃€岀紪鐮佽浆鎹㈠姩浣滆浆鎹㈡祦閮藉畬鎴愪簡銆傛墍浠ユ搷浣滄枃浠剁殑娴佸璞″彧瑕佺户鎵胯嚜杞崲娴佸氨鍙互璇诲彇涓€涓瓧绗︿簡銆?br>
浣嗘槸瀛愮被鏈変竴涓眬闄愭€э紝灏辨槸瀛愮被涓娇鐢ㄧ殑缂栫爜鏄浐瀹氱殑锛屾槸鏈満榛樿鐨勭紪鐮佽〃锛屽浜庣畝浣撲腑鏂囩増鐨勭郴缁熼粯璁ょ爜琛ㄦ槸GBK銆?br>FileReader fr = new FileReader("a.txt");
InputStreamReader isr = new InputStreamReader(new FileInputStream("a.txt"),"gbk");
浠ヤ笂涓ゅ彞浠g爜鍔熻兘涓€鑷达紝
濡傛灉浠呬粎浣跨敤骞冲彴榛樿鐮佽〃锛屽氨浣跨敤FileReader fr = new FileReader("a.txt"); //鍥犱负绠€鍖栥€?br>
濡傛灉闇€瑕佸埗瀹氱爜琛紝蹇呴』鐢ㄨ浆鎹㈡祦銆?br>杞崲娴?= 瀛楄妭娴?缂栫爜琛ㄣ€?br>杞崲娴佺殑瀛愮被File = 瀛楄妭娴?+ 榛樿缂栫爜琛ㄣ€?br>
鍑℃槸鎿嶄綔璁惧涓婄殑鏂囨湰鏁版嵁锛屾秹鍙婄紪鐮佽浆鎹紝蹇呴』浣跨敤杞崲娴併€?br>-----------------------------------------------------------------------------------------------
File绫伙細
灏嗘枃浠剁郴缁熶腑鐨勬枃浠跺拰鏂囦欢澶瑰皝瑁呮垚浜嗗璞°€傛彁渚涗簡鏇村鐨勫睘鎬у拰琛屼负鍙互瀵硅繖浜涙枃浠跺拰鏂囦欢澶硅繘琛屾搷浣溿€傝繖浜涙槸娴佸璞″姙涓嶅埌鐨勶紝鍥犱负娴佸彧鎿嶄綔鏁版嵁銆?br>
File绫诲父瑙佹柟娉曪細
1锛氬垱寤恒€?br> boolean createNewFile()锛氬湪鎸囧畾鐩綍涓嬪垱寤烘枃浠讹紝濡傛灉璇ユ枃浠跺凡瀛樺湪锛屽垯涓嶅垱寤恒€傝€屽鎿嶄綔鏂囦欢鐨勮緭鍑烘祦鑰岃█锛岃緭鍑烘祦瀵硅薄宸插缓绔嬶紝灏变細鍒涘缓鏂囦欢锛屽鏋滄枃浠跺凡瀛樺湪锛屼細瑕嗙洊銆傞櫎闈炵画鍐欍€?br> boolean mkdir()锛氬垱寤烘鎶借薄璺緞鍚嶆寚瀹氱殑鐩綍銆?br> boolean mkdirs()锛氬垱寤哄绾х洰褰曘€?br>2锛氬垹闄ゃ€?br> boolean delete()锛氬垹闄ゆ鎶借薄璺緞鍚嶈〃绀虹殑鏂囦欢鎴栫洰褰曘€?br> void deleteOnExit()锛氬湪铏氭嫙鏈洪€€鍑烘椂鍒犻櫎銆?br>娉ㄦ剰锛氬湪鍒犻櫎鏂囦欢澶规椂锛屽繀椤讳繚璇佽繖涓枃浠跺す涓病鏈変换浣曞唴瀹癸紝鎵嶅彲浠ュ皢璇ユ枃浠跺す鐢╠elete鍒犻櫎銆?br> window鐨勫垹闄ゅ姩浣滐紝鏄粠閲屽線澶栧垹銆傛敞鎰忥細java鍒犻櫎鏂囦欢涓嶈蛋鍥炴敹绔欍€傝鎱庣敤銆?br>3锛氳幏鍙?
long length()锛氳幏鍙栨枃浠跺ぇ灏忋€?br> String getName()锛氳繑鍥炵敱姝ゆ娊璞¤矾寰勫悕琛ㄧず鐨勬枃浠舵垨鐩綍鐨勫悕绉般€?br> String getPath()锛氬皢姝ゆ娊璞¤矾寰勫悕杞崲涓轰竴涓矾寰勫悕瀛楃涓层€?br> String getAbsolutePath()锛氳繑鍥炴鎶借薄璺緞鍚嶇殑缁濆璺緞鍚嶅瓧绗︿覆銆?br> String getParent()锛氳繑鍥炴鎶借薄璺緞鍚嶇埗鐩綍鐨勬娊璞¤矾寰勫悕锛屽鏋滄璺緞鍚嶆病鏈夋寚瀹氱埗鐩綍锛屽垯杩斿洖 null銆?br>long lastModified()锛氳繑鍥炴鎶借薄璺緞鍚嶈〃绀虹殑鏂囦欢鏈€鍚庝竴娆¤淇敼鐨勬椂闂淬€?br>File.pathSeparator锛氳繑鍥炲綋鍓嶇郴缁熼粯璁ょ殑璺緞鍒嗛殧绗︼紝windows榛樿涓?鈥滐紱鈥濄€?br>File.Separator锛氳繑鍥炲綋鍓嶇郴缁熼粯璁ょ殑鐩綍鍒嗛殧绗︼紝windows榛樿涓?鈥淺鈥濄€?br>4锛氬垽鏂細
boolean exists()锛氬垽鏂枃浠舵垨鑰呮枃浠跺す鏄惁瀛樺湪銆?br> boolean isDirectory()锛氭祴璇曟鎶借薄璺緞鍚嶈〃绀虹殑鏂囦欢鏄惁鏄竴涓洰褰曘€?br> boolean isFile()锛氭祴璇曟鎶借薄璺緞鍚嶈〃绀虹殑鏂囦欢鏄惁鏄竴涓爣鍑嗘枃浠躲€?br> boolean isHidden()锛氭祴璇曟鎶借薄璺緞鍚嶆寚瀹氱殑鏂囦欢鏄惁鏄竴涓殣钘忔枃浠躲€?br> boolean isAbsolute()锛氭祴璇曟鎶借薄璺緞鍚嶆槸鍚︿负缁濆璺緞鍚嶃€?br>5锛氶噸鍛藉悕銆?br> boolean renameTo(File dest)锛氬彲浠ュ疄鐜扮Щ鍔ㄧ殑鏁堟灉銆傚壀鍒?閲嶅懡鍚嶃€?br>
String[] list()锛氬垪鍑烘寚瀹氱洰褰曚笅鐨勫綋鍓嶇殑鏂囦欢鍜屾枃浠跺す鐨勫悕绉般€傚寘鍚殣钘忔枃浠躲€?br>濡傛灉璋冪敤list鏂规硶鐨凢ile 瀵硅薄涓皝瑁呯殑鏄竴涓枃浠讹紝閭d箞list鏂规硶杩斿洖鏁扮粍涓簄ull銆傚鏋滃皝瑁呯殑瀵硅薄涓嶅瓨鍦ㄤ篃浼氳繑鍥瀗ull銆傚彧鏈夊皝瑁呯殑瀵硅薄瀛樺湪骞朵笖鏄枃浠跺す鏃讹紝杩欎釜鏂规硶鎵嶆湁鏁堛€?br>------------------------------------------------------------------------------------------------
閫掑綊锛氬氨鏄嚱鏁拌嚜韬皟鐢ㄨ嚜韬€?/h1>
浠€涔堟椂鍊欑敤閫掑綊鍛紵
褰撲竴涓姛鑳借閲嶅浣跨敤锛岃€屾瘡涓€娆′娇鐢ㄨ鍔熻兘鏃剁殑鍙傛暟涓嶇‘瀹氾紝閮界敱涓婃鐨勫姛鑳藉厓绱犵粨鏋滄潵纭畾銆?br>绠€鍗曡锛氬姛鑳藉唴閮ㄥ張鐢ㄥ埌璇ュ姛鑳斤紝浣嗘槸浼犻€掔殑鍙傛暟鍊间笉纭畾銆?姣忔鍔熻兘鍙備笌杩愮畻鐨勬湭鐭ュ唴瀹逛笉纭畾)銆?br>
閫掑綊鐨勬敞鎰忎簨椤癸細
1锛氫竴瀹氳瀹氫箟閫掑綊鐨勬潯浠躲€?br>2锛氶€掑綊鐨勬鏁颁笉瑕佽繃澶氥€傚鏄撳嚭鐜?StackOverflowError 鏍堝唴瀛樻孩鍑洪敊璇€?br>鍏跺疄閫掑綊灏辨槸鍦ㄦ爤鍐呭瓨涓笉鏂殑鍔犺浇鍚屼竴涓嚱鏁般€?br>------------------------------------------------------------------------------------------------
Java.util.Properties锛?/h3>
涓€涓彲浠ュ皢閿€艰繘琛屾寔涔呭寲瀛樺偍鐨勫璞°€侻ap--Hashtable鐨勫瓙绫汇€?br>Map
|--Hashtable
|--Properties锛氱敤浜庡睘鎬ч厤缃枃浠讹紝閿拰鍊奸兘鏄瓧绗︿覆绫诲瀷銆?br>鐗圭偣锛?锛氬彲浠ユ寔涔呭寲瀛樺偍鏁版嵁銆?锛氶敭鍊奸兘鏄瓧绗︿覆銆?锛氫竴鑸敤浜庨厤缃枃浠躲€?br>
|-- load()锛氬皢娴佷腑鐨勬暟鎹姞杞借繘闆嗗悎銆?br>鍘熺悊锛氬叾瀹炲氨鏄皢璇诲彇娴佸拰鎸囧畾鏂囦欢鐩稿叧鑱旓紝骞惰鍙栦竴琛屾暟鎹紝鍥犱负鏁版嵁鏄鍒欑殑key=value锛屾墍浠ヨ幏鍙栦竴琛屽悗锛岄€氳繃 = 瀵硅琛屾暟鎹繘琛屽垏鍓诧紝宸﹁竟灏辨槸閿紝鍙宠竟灏辨槸鍊硷紝灏嗛敭銆佸€煎瓨鍌ㄥ埌properties闆嗗悎涓€?br>|-- store()锛氬啓鍏ュ悇涓」鍚庯紝鍒锋柊杈撳嚭娴併€?br>|-- list()锛氬皢闆嗗悎鐨勯敭鍊兼暟鎹垪鍑哄埌鎸囧畾鐨勭洰鐨勫湴銆?br>-------------------------------------------------------------------------------------------------
浠ヤ笅浠嬬粛IO鍖呬腑鎵╁睍鍔熻兘鐨勬祦瀵硅薄锛氬熀鏈兘鏄楗拌璁℃ā寮忋€?/h3>
Java.io.outputstream.PrintStream锛氭墦鍗版祦
1锛氭彁渚涗簡鏇村鐨勫姛鑳斤紝姣斿鎵撳嵃鏂规硶銆傚彲浠ョ洿鎺ユ墦鍗颁换鎰忕被鍨嬬殑鏁版嵁銆?br>2锛氬畠鏈変竴涓嚜鍔ㄥ埛鏂版満鍒讹紝鍒涘缓璇ュ璞★紝鎸囧畾鍙傛暟锛屽浜庢寚瀹氭柟娉曞彲浠ヨ嚜鍔ㄥ埛鏂般€?br>3锛氬畠浣跨敤鐨勬湰鏈洪粯璁ょ殑瀛楃缂栫爜.
4锛氳娴佺殑print鏂规硶涓嶆姏鍑篒OException銆?br>
璇ュ璞$殑鏋勯€犲嚱鏁般€?br>PrintStream(File file) 锛氬垱寤哄叿鏈夋寚瀹氭枃浠朵笖涓嶅甫鑷姩琛屽埛鏂扮殑鏂版墦鍗版祦銆?br>PrintStream(File file, String csn) 锛氬垱寤哄叿鏈夋寚瀹氭枃浠跺悕绉板拰瀛楃闆嗕笖涓嶅甫鑷姩琛屽埛鏂扮殑鏂版墦鍗版祦銆?br>PrintStream(OutputStream out) 锛氬垱寤烘柊鐨勬墦鍗版祦銆?br>PrintStream(OutputStream out, boolean autoFlush) 锛氬垱寤烘柊鐨勬墦鍗版祦銆?br>PrintStream(OutputStream out, boolean autoFlush, String encoding) 锛氬垱寤烘柊鐨勬墦鍗版祦銆?br>PrintStream(String fileName) 锛氬垱寤哄叿鏈夋寚瀹氭枃浠跺悕绉颁笖涓嶅甫鑷姩琛屽埛鏂扮殑鏂版墦鍗版祦銆?br>PrintStream(String fileName, String csn)
PrintStream鍙互鎿嶄綔鐩殑锛?锛欶ile瀵硅薄銆?锛氬瓧绗︿覆璺緞銆?锛氬瓧鑺傝緭鍑烘祦銆?br>鍓嶄袱涓兘JDK1.5鐗堟湰鎵嶅嚭鐜般€傝€屼笖鍦ㄦ搷浣滄枃鏈枃浠舵椂锛屽彲鎸囧畾瀛楃缂栫爜浜嗐€?br>
褰撶洰鐨勬槸涓€涓瓧鑺傝緭鍑烘祦鏃讹紝濡傛灉浣跨敤鐨刾rintln鏂规硶锛屽彲浠ュ湪printStream瀵硅薄涓婂姞鍏ヤ竴涓猼rue鍙傛暟銆傝繖鏍峰浜巔rintln鏂规硶鍙互杩涜鑷姩鐨勫埛鏂帮紝鑰屼笉鏄瓑寰呯紦鍐插尯婊′簡鍐嶅埛鏂般€傛渶缁坧rint鏂规硶閮藉皢鍏蜂綋鐨勬暟鎹浆鎴愬瓧绗︿覆锛岃€屼笖閮藉IO寮傚父杩涜浜嗗唴閮ㄥ鐞嗐€?br>
鏃㈢劧鎿嶄綔鐨勬暟鎹兘杞垚浜嗗瓧绗︿覆锛岄偅涔堜娇鐢≒rintWriter鏇村ソ涓€浜涖€傚洜涓篜rintWrite鏄瓧绗︽祦鐨勫瓙绫伙紝鍙互鐩存帴鎿嶄綔瀛楃鏁版嵁锛屽悓鏃朵篃鍙互鎸囧畾鍏蜂綋鐨勭紪鐮併€?br>--------------------------------------------------------
PrintWriter锛氬叿澶囦簡PrintStream鐨勭壒鐐瑰悓鏃讹紝杩樻湁鑷韩鐗圭偣锛?br>璇ュ璞$殑鐩殑鍦版湁鍥涗釜锛?锛欶ile瀵硅薄銆?锛氬瓧绗︿覆璺緞銆?锛氬瓧鑺傝緭鍑烘祦銆?锛氬瓧绗﹁緭鍑烘祦銆?br>
寮€鍙戞椂灏介噺浣跨敤PrintWriter銆?br>
鏂规硶涓洿鎺ユ搷浣滄枃浠剁殑绗簩鍙傛暟鏄紪鐮佽〃銆?br>鐩存帴鎿嶄綔杈撳嚭娴佺殑锛岀浜屽弬鏁版槸鑷姩鍒锋柊銆?br>
//璇诲彇閿洏褰曞叆灏嗘暟鎹浆鎴愬ぇ鍐欐樉绀哄湪鎺у埗鍙?
BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));//婧愶細閿洏杈撳叆
//鐩殑锛氭妸鏁版嵁鍐欏埌鏂囦欢涓紝杩樻兂鑷姩鍒锋柊銆?br>PrintWriter out = new PrintWriter(new FileWriter("out.txt"),true);//璁剧疆true鍚庤嚜鍔ㄥ埛鏂?br>String line = null;
while((line=bufr.readLine())!=null){
if("over".equals(line))
break;
out.println(line.toUpperCase());//杞ぇ鍐欒緭鍑?br>}
//娉ㄦ剰锛歋ystem.in锛孲ystem.out杩欎袱涓爣鍑嗙殑杈撳叆杈撳嚭娴侊紝鍦╦vm鍚姩鏃跺凡缁忓瓨鍦ㄤ簡銆傞殢鏃跺彲浠ヤ娇鐢ㄣ€傚綋jvm缁撴潫浜嗭紝杩欎袱涓祦灏辩粨鏉熶簡銆備絾鏄紝褰撲娇鐢ㄤ簡鏄剧ず鐨刢lose鏂规硶鍏抽棴鏃讹紝杩欎袱涓祦鍦ㄦ彁鍓嶇粨鏉熶簡銆?br>out.close();
bufr.close();
------------------------------------------------------------------------------------------------
SequenceInputStream锛氬簭鍒楁祦锛屼綔鐢ㄥ氨鏄皢澶氫釜璇诲彇娴佸悎骞舵垚涓€涓鍙栨祦銆傚疄鐜版暟鎹悎骞躲€?br>琛ㄧず鍏朵粬杈撳叆娴佺殑閫昏緫涓茶仈銆傚畠浠庤緭鍏ユ祦鐨勬湁搴忛泦鍚堝紑濮嬶紝骞朵粠绗竴涓緭鍏ユ祦寮€濮嬭鍙栵紝鐩村埌鍒拌揪鏂囦欢鏈熬锛屾帴鐫€浠庣浜屼釜杈撳叆娴佽鍙栵紝渚濇绫绘帹锛岀洿鍒板埌杈惧寘鍚殑鏈€鍚庝竴涓緭鍏ユ祦鐨勬枃浠舵湯灏句负姝€?br>杩欐牱鍋氾紝鍙互鏇存柟渚跨殑鎿嶄綔澶氫釜璇诲彇娴侊紝鍏跺疄杩欎釜搴忓垪娴佸唴閮ㄤ細鏈変竴涓湁搴忕殑闆嗗悎瀹瑰櫒锛岀敤浜庡瓨鍌ㄥ涓鍙栨祦瀵硅薄銆?br>璇ュ璞$殑鏋勯€犲嚱鏁板弬鏁版槸鏋氫妇锛屾兂瑕佽幏鍙栨灇涓撅紝闇€瑕佹湁Vector闆嗗悎锛屼絾涓嶉珮鏁堛€傞渶鐢ˋrrayList锛屼絾ArrayList涓病鏈夋灇涓撅紝鍙湁鑷繁鍘诲垱寤烘灇涓惧璞°€?br>浣嗘槸鏂规硶鎬庝箞瀹炵幇鍛紵鍥犱负鏋氫妇鎿嶄綔鐨勬槸鍏蜂綋闆嗗悎涓殑鍏冪礌锛屾墍浠ユ棤娉曞叿浣撳疄鐜帮紝浣嗘槸鏋氫妇鍜岃凯浠e櫒鏄姛鑳戒竴鏍风殑锛屾墍浠ワ紝鍙互鐢ㄨ凯浠f浛浠f灇涓俱€?br>
鍚堝苟鍘熺悊锛氬涓鍙栨祦瀵瑰簲涓€涓緭鍑烘祦銆?br>鍒囧壊鍘熺悊锛氫竴涓鍙栨祦瀵瑰簲澶氫釜杈撳嚭娴併€?br>
import java.io.*;
import java.util.*;
class SplitFileDemo{
private static final String CFG = ".properties";
private static final String SP = ".part";
public static void main(String[] args) throws IOException{
File file = new File("c:\0.bmp");
File dir = new File("c:\partfiles");
meger(dir);
}
//鏁版嵁鐨勫悎骞躲€?br> public static void meger(File dir)throws IOException{
if(!(dir.exists() && dir.isDirectory()))
throw new RuntimeException("鎸囧畾鐨勭洰褰曚笉瀛樺湪锛屾垨鑰呬笉鏄纭殑鐩綍");
File[] files = dir.listFiles(new SuffixFilter(CFG));
if(files.length==0)
throw new RuntimeException("鎵╁睍鍚?proerpties鐨勬枃浠朵笉瀛樺湪");
//鑾峰彇鍒伴厤缃枃浠?br> File config = files[0];
//鑾峰彇閰嶇疆鏂囦欢鐨勪俊鎭€?br> Properties prop = new Properties();
FileInputStream fis = new FileInputStream(config);
prop.load(fis);
String fileName = prop.getProperty("filename");
int partcount = Integer.parseInt(prop.getProperty("partcount"));
//--------------------------
File[] partFiles = dir.listFiles(new SuffixFilter(SP));
if(partFiles.length!=partcount)
throw new RuntimeException("缂哄皯纰庣墖鏂囦欢");
//---------------------
ArrayList<FileInputStream> al = new ArrayList<FileInputStream>();
for(int x=0; x<partcount; x++){
al.add(new FileInputStream(new File(dir,x+SP)));
}
Enumeration<FileInputStream> en = Collections.enumeration(al);
SequenceInputStream sis = new SequenceInputStream(en);
File file = new File(dir,fileName);
FileOutputStream fos = new FileOutputStream(file);
byte[] buf = new byte[1024];
int len = 0;
while((len=sis.read(buf))!=-1){
fos.write(buf,0,len);
}
fos.close();
sis.close();
}
//甯︽湁閰嶇疆淇℃伅鐨勬暟鎹垏鍓层€?br> public static void splitFile(File file)throws IOException{
//鐢ㄤ竴涓鍙栨祦鍜屾枃浠跺叧鑱斻€?br> FileInputStream fis = new FileInputStream(file);
//鍒涘缓鐩殑鍦般€傚洜涓烘湁澶氫釜銆傛墍浠ュ厛鍒涘缓寮曠敤銆?br> FileOutputStream fos = null;
//鎸囧畾纰庣墖鐨勪綅缃€?br> File dir = new File("c:\partfiles");
if(!dir.exists())
dir.mkdir();
//纰庣墖鏂囦欢澶у皬寮曠敤銆?br> File f = null;
byte[] buf = new byte[1024*1024];
//鍥犱负鍒囧壊瀹岀殑鏂囦欢閫氬父閮芥湁瑙勫緥鐨勩€備负浜嗙畝鍗曟爣璁拌寰嬩娇鐢ㄨ鏁板櫒銆?br> int count = 0;
int len = 0;
while((len=fis.read(buf))!=-1){
f = new File(dir,(count++)+".part");
fos = new FileOutputStream(f);
fos.write(buf,0,len);
fos.close();
}
//纰庣墖鏂囦欢鐢熸垚鍚庯紝杩橀渶瑕佸畾涔夐厤缃枃浠惰褰曠敓鎴愮殑纰庣墖鏂囦欢涓暟銆備互鍙婅鍒囧壊鏂囦欢鐨勫悕绉般€?br> //瀹氫箟绠€鍗曠殑閿€间俊鎭紝鍙槸鐢≒roperties銆?br> String filename = file.getName();
Properties prop = new Properties();
prop.setProperty("filename",filename);
prop.setProperty("partcount",count+"");
File config = new File(dir,count+".properties");
fos = new FileOutputStream(config);
prop.store(fos,"");
fos.close();
fis.close();
}
}
class SuffixFilter implements FileFilter{
private String suffix;
SuffixFilter(String suffix){
this.suffix = suffix;
}
public boolean accept(File file){
return file.getName().endsWith(suffix);
}
}
-----------------------------------------------------------------------------------------------
RandomAccessFile:
鐗圭偣锛?br>1锛氳瀵硅薄鍗冲彲璇诲彇锛屽張鍙啓鍏ャ€?br>2锛氳瀵硅薄涓殑瀹氫箟浜嗕竴涓ぇ鍨嬬殑byte鏁扮粍锛岄€氳繃瀹氫箟鎸囬拡鏉ユ搷浣滆繖涓暟缁勩€?br>3锛氬彲浠ラ€氳繃璇ュ璞$殑getFilePointer()鑾峰彇鎸囬拡鐨勪綅缃紝閫氳繃seek()鏂规硶璁剧疆鎸囬拡鐨勪綅缃€?br>4锛氳瀵硅薄鎿嶄綔鐨勬簮鍜岀洰鐨勫繀椤绘槸鏂囦欢銆?br>5锛氬叾瀹炶瀵硅薄鍐呴儴灏佽浜嗗瓧鑺傝鍙栨祦鍜屽瓧鑺傚啓鍏ユ祦銆?br>娉ㄦ剰锛氬疄鐜伴殢鏈鸿闂紝鏈€濂芥槸鏁版嵁鏈夎寰嬨€?br>
class RandomAccessFileDemo{
public static void main(String[] args) throws IOException{
write();
read();
randomWrite();
}
//闅忔満鍐欏叆鏁版嵁锛屽彲浠ュ疄鐜板凡鏈夋暟鎹殑淇敼銆?br> public static void randomWrite()throws IOException{
RandomAccessFile raf = new RandomAccessFile("random.txt","rw");
raf.seek(8*4);
System.out.println("pos :"+raf.getFilePointer());
raf.write("鐜嬫".getBytes());
raf.writeInt(102);
raf.close();
}
public static void read()throws IOException{
RandomAccessFile raf = new RandomAccessFile("random.txt","r");//鍙妯″紡銆?br> //鎸囧畾鎸囬拡鐨勪綅缃€?br> raf.seek(8*1);//瀹炵幇闅忔満璇诲彇鏂囦欢涓殑鏁版嵁銆傛敞鎰忥細鏁版嵁鏈€濂芥湁瑙勫緥銆?br> System.out.println("pos1 :"+raf.getFilePointer());
byte[] buf = new byte[4];
raf.read(buf);
String name = new String(buf);
int age = raf.readInt();
System.out.println(name+"::"+age);
System.out.println("pos2 :"+raf.getFilePointer());
raf.close();
}
public static void write()throws IOException{
//rw锛氬綋杩欎釜鏂囦欢涓嶅瓨鍦紝浼氬垱寤鸿鏂囦欢銆傚綋鏂囦欢宸插瓨鍦紝涓嶄細鍒涘缓銆傛墍浠ヤ笉浼氬儚杈撳嚭娴佷竴鏍疯鐩栥€?br> RandomAccessFile raf = new RandomAccessFile("random.txt","rw");//rw璇诲啓妯″紡
//寰€鏂囦欢涓啓鍏ヤ汉鐨勫熀鏈俊鎭紝濮撳悕锛屽勾榫勩€?br> raf.write("寮犱笁".getBytes());
raf.writeInt(97);
raf.close();
}
}
------------------------------------------------------------------------------------------------
绠¢亾娴侊細
绠¢亾璇诲彇娴佸拰绠¢亾鍐欏叆娴佸彲浠ュ儚绠¢亾涓€鏍峰鎺ヤ笂锛岀閬撹鍙栨祦灏卞彲浠ヨ鍙栫閬撳啓鍏ユ祦鍐欏叆鐨勬暟鎹€?br>娉ㄦ剰锛氶渶瑕佸姞鍏ュ绾跨▼鎶€鏈紝鍥犱负鍗曠嚎绋嬶紝鍏堟墽琛宺ead锛屼細鍙戠敓姝婚攣锛屽洜涓簉ead鏂规硶鏄樆濉炲紡鐨勶紝娌℃湁鏁版嵁鐨剅ead鏂规硶浼氳绾跨▼绛夊緟銆?br>public static void main(String[] args) throws IOException{
PipedInputStream pipin = new PipedInputStream();
PipedOutputStream pipout = new PipedOutputStream();
pipin.connect(pipout);
new Thread(new Input(pipin)).start();
new Thread(new Output(pipout)).start();
}
------------------------------------------------------------------------------------------------
瀵硅薄鐨勫簭鍒楀寲锛氱洰鐨勶細灏嗕竴涓叿浣撶殑瀵硅薄杩涜鎸佷箙鍖栵紝鍐欏叆鍒扮‖鐩樹笂銆?br>娉ㄦ剰锛氶潤鎬佹暟鎹笉鑳借搴忓垪鍖栵紝鍥犱负闈欐€佹暟鎹笉鍦ㄥ爢鍐呭瓨涓紝鏄瓨鍌ㄥ湪闈欐€佹柟娉曞尯涓€?br>
濡備綍灏嗛潪闈欐€佺殑鏁版嵁涓嶈繘琛屽簭鍒楀寲锛熺敤transient 鍏抽敭瀛椾慨楗版鍙橀噺鍗冲彲銆?br>
Serializable锛氱敤浜庡惎鍔ㄥ璞$殑搴忓垪鍖栧姛鑳斤紝鍙互寮哄埗璁╂寚瀹氱被鍏峰搴忓垪鍖栧姛鑳斤紝璇ユ帴鍙d腑娌℃湁鎴愬憳锛岃繖鏄竴涓爣璁版帴鍙c€傝繖涓爣璁版帴鍙g敤浜庣粰搴忓垪鍖栫被鎻愪緵UID銆傝繖涓猽id鏄緷鎹被涓殑鎴愬憳鐨勬暟瀛楃鍚嶈繘琛岃繍琛岃幏鍙栫殑銆傚鏋滀笉闇€瑕佽嚜鍔ㄨ幏鍙栦竴涓猽id锛屽彲浠ュ湪绫讳腑锛屾墜鍔ㄦ寚瀹氫竴涓悕绉颁负serialVersionUID id鍙枫€備緷鎹紪璇戝櫒鐨勪笉鍚岋紝鎴栬€呭淇℃伅鐨勯珮搴︽晱鎰熸€с€傛渶濂芥瘡涓€涓簭鍒楀寲鐨勭被閮借繘琛屾墜鍔ㄦ樉绀虹殑UID鐨勬寚瀹氥€?br>
import java.io.*;
class ObjectStreamDemo {
public static void main(String[] args) throws Exception{
writeObj();
readObj();
}
public static void readObj()throws Exception{
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("obj.txt"));
Object obj = ois.readObject();//璇诲彇涓€涓璞°€?br> System.out.println(obj.toString());
}
public static void writeObj()throws IOException{
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("obj.txt"));
oos.writeObject(new Person("lisi",25)); //鍐欏叆涓€涓璞°€?br> oos.close();
}
}
class Person implements Serializable{
private static final long serialVersionUID = 42L;
private transient String name;//鐢╰ransient淇グ鍚巒ame灏嗕笉浼氳繘琛屽簭鍒楀寲
public int age;
Person(String name,int age){
this.name = name;
this.age = age;
}
public String toString(){
return name+"::"+age;
}
}
-----------------------------------------------------------------------------------------------
DataOutputStream銆丏ataInputStream锛氫笓闂ㄧ敤浜庢搷浣滃熀鏈暟鎹被鍨嬫暟鎹殑瀵硅薄銆?/h3>
DataOutputStream dos = new DataOutputStream(new FileOutputStream("data.txt"));
dos.writeInt(256);
dos.close();
DataInputStream dis = new DataInputStream(new FileInputStream("data.txt"));
int num = dis.readInt();
System.out.println(num);
dis.close();
-----------------------------------------------------------------------------------------------
ByteArrayInputStream锛氭簮锛氬唴瀛?
ByteArrayOutputStream锛氱洰鐨勶細鍐呭瓨銆?
杩欎袱涓祦瀵硅薄涓嶆秹鍙婂簳灞傝祫婧愯皟鐢紝鎿嶄綔鐨勯兘鏄唴瀛樹腑鏁扮粍锛屾墍浠ヤ笉闇€瑕佸叧闂€?br>鐩存帴鎿嶄綔瀛楄妭鏁扮粍灏卞彲浠ヤ簡锛屼负浠€涔堣繕瑕佹妸鏁扮粍灏佽鍒版祦瀵硅薄涓憿锛熷洜涓烘暟缁勬湰韬病鏈夋柟娉曪紝鍙湁涓€涓猯ength灞炴€с€備负浜嗕究浜庢暟缁勭殑鎿嶄綔锛屽皢鏁扮粍杩涜灏佽锛屽澶栨彁渚涙柟娉曟搷浣滄暟缁勪腑鐨勫厓绱犮€?br>
瀵逛簬鏁扮粍鍏冪礌鎿嶄綔鏃犻潪涓ょ鎿嶄綔锛氳缃紙鍐欙級鍜岃幏鍙栵紙璇伙級锛岃€岃繖涓ゆ搷浣滄濂藉搴旀祦鐨勮鍐欐搷浣溿€傝繖涓や釜瀵硅薄灏辨槸浣跨敤浜嗘祦鐨勮鍐欐€濇兂鏉ユ搷浣滄暟缁勩€?br>//鍒涘缓婧愶細
ByteArrayInputStream bis = new ByteArrayInputStream("abcdef".getBytes());
//鍒涘缓鐩殑锛?br> ByteArrayOutputStream bos = new ByteArrayOutputStream();
int ch = 0;
while((ch=bis.read())!=-1){
bos.write(ch);
}
System.out.println(bos.toString());
-----------------------------------------------------------------------------------------------
缃戠粶缂栫▼锛?/h1>
绔彛锛?br>鐗╃悊绔彛锛?br>閫昏緫绔彛锛氱敤浜庢爣璇嗚繘绋嬬殑閫昏緫鍦板潃锛屼笉鍚岃繘绋嬬殑鏍囪瘑锛涙湁鏁堢鍙o細0~65535锛屽叾涓?~1024绯荤粺浣跨敤鎴栦繚鐣欑鍙c€?br>
java 涓璱p瀵硅薄锛欼netAddress.
import java.net.*;
class IPDemo{
public static void main(String[] args) throws UnknownHostException{
//閫氳繃鍚嶇О(ip瀛楃涓瞣r涓绘満鍚?鏉ヨ幏鍙栦竴涓猧p瀵硅薄銆?br> InetAddress ip = InetAddress.getByName("www.baidu.com");//java.net.UnknownHostException
System.out.println("addr:"+ip.getHostAddress());
System.out.println("name:"+ip.getHostName());
}
}
Socket锛氣槄鈽呪槄鈽咃紝濂楁帴瀛楋紝閫氫俊鐨勭鐐广€?br>灏辨槸涓虹綉缁滄湇鍔℃彁渚涚殑涓€绉嶆満鍒讹紝閫氫俊鐨勪袱绔兘鏈塖ocket锛岀綉缁滈€氫俊鍏跺疄灏辨槸Socket闂寸殑閫氫俊锛屾暟鎹湪涓や釜Socket闂撮€氳繃IO浼犺緭銆?br>
UDP浼犺緭锛?/h2>
1锛屽彧瑕佹槸缃戠粶浼犺緭锛屽繀椤绘湁socket 銆?br>2锛屾暟鎹竴瀹氳灏佽鍒版暟鎹寘涓紝鏁版嵁鍖呬腑鍖呮嫭鐩殑鍦板潃銆佺鍙c€佹暟鎹瓑淇℃伅銆?br>
鐩存帴鎿嶄綔udp涓嶅彲鑳斤紝瀵逛簬java璇█搴旇灏唘dp灏佽鎴愬璞★紝鏄撲簬鎴戜滑鐨勪娇鐢紝杩欎釜瀵硅薄灏辨槸DatagramSocket. 灏佽浜唘dp浼犺緭鍗忚鐨剆ocket瀵硅薄銆?br>
鍥犱负鏁版嵁鍖呬腑鍖呭惈鐨勪俊鎭緝澶氾紝涓轰簡鎿嶄綔杩欎簺淇℃伅鏂逛究锛屼篃涓€鏍蜂細灏嗗叾灏佽鎴愬璞°€傝繖涓暟鎹寘瀵硅薄灏辨槸锛欴atagramPacket.閫氳繃杩欎釜瀵硅薄涓殑鏂规硶锛屽氨鍙互鑾峰彇鍒版暟鎹寘涓殑鍚勭淇℃伅銆?br>
DatagramSocket鍏峰鍙戦€佸拰鎺ュ彈鍔熻兘锛屽湪杩涜udp浼犺緭鏃讹紝闇€瑕佹槑纭竴涓槸鍙戦€佺锛屼竴涓槸鎺ユ敹绔€?br>
udp鐨勫彂閫佺锛?/h3>
1锛屽缓绔媢dp鐨剆ocket鏈嶅姟锛屽垱寤哄璞℃椂濡傛灉娌℃湁鏄庣‘绔彛锛岀郴缁熶細鑷姩鍒嗛厤涓€涓湭琚娇鐢ㄧ殑绔彛銆?br>2锛屾槑纭鍙戦€佺殑鍏蜂綋鏁版嵁銆?br>3锛屽皢鏁版嵁灏佽鎴愪簡鏁版嵁鍖呫€?br>4锛岀敤socket鏈嶅姟鐨剆end鏂规硶灏嗘暟鎹寘鍙戦€佸嚭鍘汇€?br>5锛屽叧闂祫婧愩€?br>--------------------------------------------------------------
import java.net.*;
class UdpSend{
public static void main(String[] args)throws Exception {
// 1锛屽缓绔媢dp鐨剆ocket鏈嶅姟銆?br> DatagramSocket ds = new DatagramSocket(8888);//鎸囧畾鍙戦€佺鍙o紝涓嶆寚瀹氱郴缁熶細闅忔満鍒嗛厤銆?br>// 2锛屾槑纭鍙戦€佺殑鍏蜂綋鏁版嵁銆?br> String text = "udp浼犺緭婕旂ず 鍝ヤ滑鏉ヤ簡";
byte[] buf = text.getBytes();
// 3锛屽皢鏁版嵁灏佽鎴愪簡鏁版嵁鍖呫€?br> DatagramPacket dp = new DatagramPacket(buf,
buf.length,InetAddress.getByName("10.1.31.127"),10000);
// 4锛岀敤socket鏈嶅姟鐨剆end鏂规硶灏嗘暟鎹寘鍙戦€佸嚭鍘汇€?br> ds.send(dp);
// 5锛屽叧闂祫婧愩€?br> ds.close();
}
}
-------------------------------------------------------------
udp鐨勬帴鏀剁锛?/h2>
1锛屽垱寤簎dp鐨剆ocket鏈嶅姟锛屽繀椤昏鏄庣‘涓€涓鍙o紝浣滅敤鍦ㄤ簬锛屽彧鏈夊彂閫佸埌杩欎釜绔彛鐨勬暟鎹墠鏄繖涓帴鏀剁鍙互澶勭悊鐨勬暟鎹€?br>2锛屽畾涔夋暟鎹寘锛岀敤浜庡瓨鍌ㄦ帴鏀跺埌鏁版嵁銆?br>3锛岄€氳繃socket鏈嶅姟鐨勬帴鏀舵柟娉曞皢鏀跺埌鐨勬暟鎹瓨鍌ㄥ埌鏁版嵁鍖呬腑銆?br>4锛岄€氳繃鏁版嵁鍖呯殑鏂规硶鑾峰彇鏁版嵁鍖呬腑鐨勫叿浣撴暟鎹唴瀹癸紝姣斿ip銆佺鍙c€佹暟鎹瓑绛夈€?br>5锛屽叧闂祫婧愩€?br>-------------------------------------------------------------
class UdpRece {
public static void main(String[] args) throws Exception{
// 1锛屽垱寤簎dp鐨剆ocket鏈嶅姟銆?br> DatagramSocket ds = new DatagramSocket(10000);
// 2锛屽畾涔夋暟鎹寘锛岀敤浜庡瓨鍌ㄦ帴鏀跺埌鏁版嵁銆傚厛瀹氫箟瀛楄妭鏁扮粍锛屾暟鎹寘浼氭妸鏁版嵁瀛樺偍鍒板瓧鑺傛暟缁勪腑銆?br> byte[] buf = new byte[1024];
DatagramPacket dp = new DatagramPacket(buf,buf.length);
// 3锛岄€氳繃socket鏈嶅姟鐨勬帴鏀舵柟娉曞皢鏀跺埌鐨勬暟鎹瓨鍌ㄥ埌鏁版嵁鍖呬腑銆?br> ds.receive(dp);//璇ユ柟娉曟槸闃诲寮忔柟娉曘€?br>// 4锛岄€氳繃鏁版嵁鍖呯殑鏂规硶鑾峰彇鏁版嵁鍖呬腑鐨勫叿浣撴暟鎹唴瀹癸紝姣斿ip锛岀鍙o紝鏁版嵁绛夌瓑銆?br> String ip = dp.getAddress().getHostAddress();
int port = dp.getPort();
String text = new String(dp.getData(),0,dp.getLength());//灏嗗瓧鑺傛暟缁勪腑鐨勬湁鏁堥儴鍒嗚浆鎴愬瓧绗︿覆銆?br> System.out.println(ip+":"+port+"--"+text);
// 5锛屽叧闂祫婧愩€?br> ds.close();
}
}
-------------------------------------------------------------
TCP浼犺緭锛氫袱涓鐐圭殑寤虹珛杩炴帴鍚庝細鏈変竴涓紶杈撴暟鎹殑閫氶亾锛岃繖閫氶亾绉颁负娴侊紝鑰屼笖鏄缓绔嬪湪缃戠粶鍩虹涓婄殑娴侊紝绉颁箣涓簊ocket娴併€傝娴佷腑鏃㈡湁璇诲彇锛屼篃鏈夊啓鍏ャ€?br>
tcp鐨勪袱涓鐐癸細涓€涓槸瀹㈡埛绔紝涓€涓槸鏈嶅姟绔€?br>瀹㈡埛绔細瀵瑰簲鐨勫璞★紝Socket
鏈嶅姟绔細瀵瑰簲鐨勫璞★紝ServerSocket
TCP瀹㈡埛绔細
1锛屽缓绔媡cp鐨剆ocket鏈嶅姟锛屾渶濂芥槑纭叿浣撶殑鍦板潃鍜岀鍙c€傝繖涓璞″湪鍒涘缓鏃讹紝灏卞凡缁忓彲浠ュ鎸囧畾ip鍜岀鍙h繘琛岃繛鎺?涓夋鎻℃墜)銆?br>2锛屽鏋滆繛鎺ユ垚鍔燂紝灏辨剰鍛崇潃閫氶亾寤虹珛浜嗭紝socket娴佸氨宸茬粡浜х敓浜嗐€傚彧瑕佽幏鍙栧埌socket娴佷腑鐨勮鍙栨祦鍜屽啓鍏ユ祦鍗冲彲锛屽彧瑕侀€氳繃getInputStream鍜実etOutputStream灏卞彲浠ヨ幏鍙栦袱涓祦瀵硅薄銆?br>3锛屽叧闂祫婧愩€?br>--------------------------------------------------------------
import java.net.*;
import java.io.*;
//闇€姹傦細瀹㈡埛绔粰鏈嶅姟鍣ㄧ鍙戦€佷竴涓暟鎹€?br>class TcpClient{
public static void main(String[] args) throws Exception{
Socket s = new Socket("10.1.31.69",10002);
OutputStream out = s.getOutputStream();//鑾峰彇浜唖ocket娴佷腑鐨勮緭鍑烘祦瀵硅薄銆?br> out.write("tcp婕旂ず锛屽摜浠張鏉ヤ簡!".getBytes());
s.close();
}
}
--------------------------------------------------------------
TCP鏈嶅姟绔細
1锛屽垱寤烘湇鍔$socket鏈嶅姟锛屽苟鐩戝惉涓€涓鍙c€?br>2锛屾湇鍔$涓轰簡缁欏鎴风鎻愪緵鏈嶅姟锛岃幏鍙栧鎴风鐨勫唴瀹癸紝鍙互閫氳繃accept鏂规硶鑾峰彇杩炴帴杩囨潵鐨勫鎴风瀵硅薄銆?br>3锛屽彲浠ラ€氳繃鑾峰彇鍒扮殑socket瀵硅薄涓殑socket娴佸拰鍏蜂綋鐨勫鎴风杩涜閫氳銆?br>4锛屽鏋滈€氳缁撴潫锛屽叧闂祫婧愩€傛敞鎰忥細瑕佸厛鍏冲鎴风锛屽啀鍏虫湇鍔$銆?br>--------------------------------------------------------------
class TcpServer{
public static void main(String[] args) throws Exception{
ServerSocket ss = new ServerSocket(10002);//寤虹珛鏈嶅姟绔殑socket鏈嶅姟
Socket s = ss.accept();//鑾峰彇瀹㈡埛绔璞?br> String ip = s.getInetAddress().getHostAddress();
System.out.println(ip+".....connected");
// 鍙互閫氳繃鑾峰彇鍒扮殑socket瀵硅薄涓殑socket娴佸拰鍏蜂綋鐨勫鎴风杩涜閫氳銆?br> InputStream in = s.getInputStream();//璇诲彇瀹㈡埛绔殑鏁版嵁锛屼娇鐢ㄥ鎴风瀵硅薄鐨剆ocket璇诲彇娴?br> byte[] buf = new byte[1024];
int len = in.read(buf);
String text = new String(buf,0,len);
System.out.println(text);
// 濡傛灉閫氳缁撴潫锛屽叧闂祫婧愩€傛敞鎰忥細瑕佸厛鍏冲鎴风锛屽湪鍏虫湇鍔$銆?br> s.close();
ss.close();
}
}
-----------------------------------------------------------------------------------------------
鍙嶅皠鎶€鏈細
鍏跺疄灏辨槸鍔ㄦ€佸姞杞戒竴涓寚瀹氱殑绫伙紝骞惰幏鍙栬绫讳腑鐨勬墍鏈夌殑鍐呭銆傝€屼笖灏嗗瓧鑺傜爜鏂囦欢灏佽鎴愬璞★紝骞跺皢瀛楄妭鐮佹枃浠朵腑鐨勫唴瀹归兘灏佽鎴愬璞★紝杩欐牱渚夸簬鎿嶄綔杩欎簺鎴愬憳銆傜畝鍗曡锛氬弽灏勬妧鏈彲浠ュ涓€涓被杩涜瑙e墫銆?br>
鍙嶅皠鐨勫ソ澶勶細澶уぇ鐨勫寮轰簡绋嬪簭鐨勬墿灞曟€с€?br>
鍙嶅皠鐨勫熀鏈楠わ細
1銆佽幏寰桟lass瀵硅薄锛屽氨鏄幏鍙栧埌鎸囧畾鐨勫悕绉扮殑瀛楄妭鐮佹枃浠跺璞°€?br>2銆佸疄渚嬪寲瀵硅薄锛岃幏寰楃被鐨勫睘鎬с€佹柟娉曟垨鏋勯€犲嚱鏁般€?br>3銆佽闂睘鎬с€佽皟鐢ㄦ柟娉曘€佽皟鐢ㄦ瀯閫犲嚱鏁板垱寤哄璞°€?br>
鑾峰彇杩欎釜Class瀵硅薄锛屾湁涓夌鏂瑰紡锛?br>1锛氶€氳繃姣忎釜瀵硅薄閮藉叿澶囩殑鏂规硶getClass鏉ヨ幏鍙栥€傚紛绔細蹇呴』瑕佸垱寤鸿绫诲璞★紝鎵嶅彲浠ヨ皟鐢╣etClass鏂规硶銆?br>2锛氭瘡涓€涓暟鎹被鍨?鍩烘湰鏁版嵁绫诲瀷鍜屽紩鐢ㄦ暟鎹被鍨?閮芥湁涓€涓潤鎬佺殑灞炴€lass銆傚紛绔細蹇呴』瑕佸厛鏄庣‘璇ョ被銆?br> 鍓嶄袱绉嶆柟寮忎笉鍒╀簬绋嬪簭鐨勬墿灞曪紝鍥犱负閮介渶瑕佸湪绋嬪簭浣跨敤鍏蜂綋鐨勭被鏉ュ畬鎴愩€?br>3锛氫娇鐢ㄧ殑Class绫讳腑鐨勬柟娉曪紝闈欐€佺殑forName鏂规硶銆?br> 鎸囧畾浠€涔堢被鍚嶏紝灏辫幏鍙栦粈涔堢被瀛楄妭鐮佹枃浠跺璞★紝杩欑鏂瑰紡鐨勬墿灞曟€ф渶寮猴紝鍙灏嗙被鍚嶇殑瀛楃涓蹭紶鍏ュ嵆鍙€?br>// 1. 鏍规嵁缁欏畾鐨勭被鍚嶆潵鑾峰緱 鐢ㄤ簬绫诲姞杞?br>String classname = "cn.itcast.reflect.Person";// 鏉ヨ嚜閰嶇疆鏂囦欢
Class clazz = Class.forName(classname);// 姝ゅ璞′唬琛≒erson.class
// 2. 濡傛灉鎷垮埌浜嗗璞★紝涓嶇煡閬撴槸浠€涔堢被鍨?nbsp; 鐢ㄤ簬鑾峰緱瀵硅薄鐨勭被鍨?br>Object obj = new Person();
Class clazz1 = obj.getClass();// 鑾峰緱瀵硅薄鍏蜂綋鐨勭被鍨?br>// 3. 濡傛灉鏄槑纭湴鑾峰緱鏌愪釜绫荤殑Class瀵硅薄 涓昏鐢ㄤ簬浼犲弬
Class clazz2 = Person.class;
鍙嶅皠鐨勭敤娉曪細
1锛夈€侀渶瑕佽幏寰梛ava绫荤殑鍚勪釜缁勬垚閮ㄥ垎锛岄鍏堥渶瑕佽幏寰楃被鐨凜lass瀵硅薄锛岃幏寰桟lass瀵硅薄鐨勪笁绉嶆柟寮忥細
Class.forName(classname) 鐢ㄤ簬鍋氱被鍔犺浇
obj.getClass() 鐢ㄤ簬鑾峰緱瀵硅薄鐨勭被鍨?br> 绫诲悕.class 鐢ㄤ簬鑾峰緱鎸囧畾鐨勭被鍨嬶紝浼犲弬鐢?br>
2)銆佸弽灏勭被鐨勬垚鍛樻柟娉曪細
Class clazz = Person.class;
Method method = clazz.getMethod(methodName, new Class[]{paramClazz1, paramClazz2});
method.invoke();
3)銆佸弽灏勭被鐨勬瀯閫犲嚱鏁帮細
Constructor con = clazz.getConstructor(new Class[]{paramClazz1, paramClazz2,...})
con.newInstance(params...)
4)銆佸弽灏勭被鐨勫睘鎬э細
Field field = clazz.getField(fieldName);
field.setAccessible(true);
field.setObject(value);
鑾峰彇浜嗗瓧鑺傜爜鏂囦欢瀵硅薄鍚庯紝鏈€缁堥兘闇€瑕佸垱寤烘寚瀹氱被鐨勫璞★細
鍒涘缓瀵硅薄鐨勪袱绉嶆柟寮?鍏跺疄灏辨槸瀵硅薄鍦ㄨ繘琛屽疄渚嬪寲鏃剁殑鍒濆鍖栨柟寮?锛?br>1锛岃皟鐢ㄧ┖鍙傛暟鐨勬瀯閫犲嚱鏁帮細浣跨敤浜咰lass绫讳腑鐨刵ewInstance()鏂规硶銆?br>2锛岃皟鐢ㄥ甫鍙傛暟鐨勬瀯閫犲嚱鏁帮細鍏堣鑾峰彇鎸囧畾鍙傛暟鍒楄〃鐨勬瀯閫犲嚱鏁板璞★紝鐒跺悗閫氳繃璇ユ瀯閫犲嚱鏁扮殑瀵硅薄鐨刵ewInstance(瀹為檯鍙傛暟) 杩涜瀵硅薄鐨勫垵濮嬪寲銆?br>
缁间笂鎵€杩帮紝绗簩绉嶆柟寮忥紝蹇呴』瑕佸厛鏄庣‘鍏蜂綋鐨勬瀯閫犲嚱鏁扮殑鍙傛暟绫诲瀷锛屼笉渚夸簬鎵╁睍銆傛墍浠ヤ竴鑸儏鍐典笅锛岃鍙嶅皠鐨勭被锛屽唴閮ㄩ€氬父閮戒細鎻愪緵涓€涓叕鏈夌殑绌哄弬鏁扮殑鏋勯€犲嚱鏁般€?br>------------------------------------------------------
// 濡備綍鐢熸垚鑾峰彇鍒板瓧鑺傜爜鏂囦欢瀵硅薄鐨勫疄渚嬪璞°€?br> Class clazz = Class.forName("cn.itcast.bean.Person");//绫诲姞杞?br>// 鐩存帴鑾峰緱鎸囧畾鐨勭被鍨?br> clazz = Person.class;
// 鏍规嵁瀵硅薄鑾峰緱绫诲瀷
Object obj = new Person("zhangsan", 19);
clazz = obj.getClass();
Object obj = clazz.newInstance();//璇ュ疄渚嬪寲瀵硅薄鐨勬柟娉曡皟鐢ㄥ氨鏄寚瀹氱被涓殑绌哄弬鏁版瀯閫犲嚱鏁帮紝缁欏垱寤哄璞¤繘琛屽垵濮嬪寲銆傚綋鎸囧畾绫讳腑娌℃湁绌哄弬鏁版瀯閫犲嚱鏁版椂锛岃濡備綍鍒涘缓璇ョ被瀵硅薄鍛紵璇风湅method_2();
public static void method_2() throws Exception {
Class clazz = Class.forName("cn.itcast.bean.Person");
//鏃㈢劧绫讳腑娌℃湁绌哄弬鏁扮殑鏋勯€犲嚱鏁?閭d箞鍙湁鑾峰彇鎸囧畾鍙傛暟鐨勬瀯閫犲嚱鏁?鐢ㄨ鍑芥暟鏉ヨ繘琛屽疄渚嬪寲銆?br> //鑾峰彇涓€涓甫鍙傛暟鐨勬瀯閫犲櫒銆?br> Constructor constructor = clazz.getConstructor(String.class,int.class);
//鎯宠瀵瑰璞¤繘琛屽垵濮嬪寲锛屼娇鐢ㄦ瀯閫犲櫒鐨勬柟娉昻ewInstance();
Object obj = constructor.newInstance("zhagnsan",30);
//鑾峰彇鎵€鏈夋瀯閫犲櫒銆?br> Constructor[] constructors = clazz.getConstructors();//鍙寘鍚叕鍏辩殑
constructors = clazz.getDeclaredConstructors();//鍖呭惈绉佹湁鐨?br> for(Constructor con : constructors) {
System.out.println(con);
}
}
------------------------------------------------------
鍙嶅皠鎸囧畾绫讳腑鐨勬柟娉曪細
//鑾峰彇绫讳腑鎵€鏈夌殑鏂规硶銆?br> public static void method_1() throws Exception {
Class clazz = Class.forName("cn.itcast.bean.Person");
Method[] methods = clazz.getMethods();//鑾峰彇鐨勬槸璇ョ被涓殑鍏湁鏂规硶鍜岀埗绫讳腑鐨勫叕鏈夋柟娉曘€?br> methods = clazz.getDeclaredMethods();//鑾峰彇鏈被涓殑鏂规硶锛屽寘鍚鏈夋柟娉曘€?br> for(Method method : methods) {
System.out.println(method);
}
}
//鑾峰彇鎸囧畾鏂规硶锛?br> public static void method_2() throws Exception {
Class clazz = Class.forName("cn.itcast.bean.Person");
//鑾峰彇鎸囧畾鍚嶇О鐨勬柟娉曘€?br> Method method = clazz.getMethod("show", int.class,String.class);
//鎯宠杩愯鎸囧畾鏂规硶锛屽綋鐒舵槸鏂规硶瀵硅薄鏈€娓呮锛屼负浜嗚鏂规硶杩愯锛岃皟鐢ㄦ柟娉曞璞$殑invoke鏂规硶鍗冲彲锛屼絾鏄柟娉曡繍琛屽繀椤昏鏄庣‘鎵€灞炵殑瀵硅薄鍜屽叿浣撶殑瀹為檯鍙傛暟銆?br> Object obj = clazz.newInstance();
method.invoke(obj, 39,"hehehe");//鎵ц涓€涓柟娉?br> }
//鎯宠杩愯绉佹湁鏂规硶銆?br> public static void method_3() throws Exception {
Class clazz = Class.forName("cn.itcast.bean.Person");
//鎯宠鑾峰彇绉佹湁鏂规硶銆傚繀椤荤敤getDeclearMethod();
Method method = clazz.getDeclaredMethod("method", null);
// 绉佹湁鏂规硶涓嶈兘鐩存帴璁块棶锛屽洜涓烘潈闄愪笉澶熴€傞潪瑕佽闂紝鍙互閫氳繃鏆村姏鐨勬柟寮忋€?br> method.setAccessible(true);//涓€鑸緢灏戠敤锛屽洜涓虹鏈夊氨鏄殣钘忚捣鏉ワ紝鎵€浠ュ敖閲忎笉瑕佽闂€?br> }
//鍙嶅皠闈欐€佹柟娉曘€?br> public static void method_4() throws Exception {
Class clazz = Class.forName("cn.itcast.bean.Person");
Method method = clazz.getMethod("function",null);
method.invoke(null,null);
}
------------------------------------------------------------------------------------------------
銆€銆€銆€銆€銆€銆€涓嶈冻涔嬪璇疯皡瑙o紝鏈変换浣曞紓璁?璇?鍦ㄨ瘎璁哄尯鎸囧嚭锛屾偍瀹濊吹鐨勫缓璁氨鏄垜鐨勪笉瓒筹紒锛侊紒
class Outer{
int num = 4;
class Inner {
void show(){
System.out.println("inner show run "+num);
}
}
public void method(){
Inner in = new Inner();//鍒涘缓鍐呴儴绫荤殑瀵硅薄銆?br> in.show();//璋冪敤鍐呴儴绫荤殑鏂规硶銆?br> }
}
-------------------------------------------------------
褰撳唴閮ㄧ被瀹氫箟鍦ㄥ閮ㄧ被涓殑鎴愬憳浣嶇疆涓婏紝鍙互浣跨敤涓€浜涙垚鍛樹慨楗扮淇グ private銆乻tatic銆?br>1锛氶粯璁や慨楗扮銆?br>鐩存帴璁块棶鍐呴儴绫绘牸寮忥細澶栭儴绫诲悕.鍐呴儴绫诲悕 鍙橀噺鍚?= 澶栭儴绫诲璞?鍐呴儴绫诲璞?
Outer.Inner in = new Outer.new Inner();//杩欑褰㈠紡寰堝皯鐢ㄣ€?br> 浣嗘槸杩欑搴旂敤涓嶅瑙侊紝鍥犱负鍐呴儴绫讳箣鎵€浠ュ畾涔夊湪鍐呴儴灏辨槸涓轰簡灏佽銆傛兂瑕佽幏鍙栧唴閮ㄧ被瀵硅薄閫氬父閮介€氳繃澶栭儴绫荤殑鏂规硶鏉ヨ幏鍙栥€傝繖鏍峰彲浠ュ鍐呴儴绫诲璞¤繘琛屾帶鍒躲€?br>2锛氱鏈変慨楗扮銆?br> 閫氬父鍐呴儴绫昏灏佽锛岄兘浼氳绉佹湁鍖栵紝鍥犱负灏佽鎬т笉璁╁叾浠栫▼搴忕洿鎺ヨ闂€?br>3锛氶潤鎬佷慨楗扮銆?br> 濡傛灉鍐呴儴绫昏闈欐€佷慨楗帮紝鐩稿綋浜庡閮ㄧ被锛屼細鍑虹幇璁块棶灞€闄愭€э紝鍙兘璁块棶澶栭儴绫讳腑鐨勯潤鎬佹垚鍛樸€?br> 娉ㄦ剰锛涘鏋滃唴閮ㄧ被涓畾涔変簡闈欐€佹垚鍛橈紝閭d箞璇ュ唴閮ㄧ被蹇呴』鏄潤鎬佺殑銆?br>
鍐呴儴绫荤紪璇戝悗鐨勬枃浠跺悕涓猴細鈥滃閮ㄧ被鍚?鍐呴儴绫诲悕.java鈥濓紱
涓轰粈涔堝唴閮ㄧ被鍙互鐩存帴璁块棶澶栭儴绫讳腑鐨勬垚鍛樺憿锛?br>閭f槸鍥犱负鍐呴儴涓兘鎸佹湁涓€涓閮ㄧ被鐨勫紩鐢ㄣ€傝繖涓槸寮曠敤鏄?澶栭儴绫诲悕.this
鍐呴儴绫诲彲浠ュ畾涔夊湪澶栭儴绫讳腑鐨勬垚鍛樹綅缃笂锛屼篃鍙互瀹氫箟鍦ㄥ閮ㄧ被涓殑灞€閮ㄤ綅缃笂銆?br>褰撳唴閮ㄧ被琚畾涔夊湪灞€閮ㄤ綅缃笂锛屽彧鑳借闂眬閮ㄤ腑琚玣inal淇グ鐨勫眬閮ㄥ彉閲忋€?br>
鍖垮悕鍐呴儴绫伙細娌℃湁鍚嶅瓧鐨勫唴閮ㄧ被銆傚氨鏄唴閮ㄧ被鐨勭畝鍖栧舰寮忋€備竴鑸彧鐢ㄤ竴娆″氨鍙互鐢ㄨ繖绉嶅舰寮忋€傚尶鍚嶅唴閮ㄧ被鍏跺疄灏辨槸涓€涓尶鍚嶅瓙绫诲璞°€傛兂瑕佸畾涔夊尶鍚嶅唴閮ㄧ被锛氶渶瑕佸墠鎻愶紝鍐呴儴绫诲繀椤荤户鎵夸竴涓被鎴栬€呭疄鐜版帴鍙c€?br>
鍖垮悕鍐呴儴绫荤殑鏍煎紡锛歯ew 鐖剁被鍚?amp;鎺ュ彛鍚?){ 瀹氫箟瀛愮被鎴愬憳鎴栬€呰鐩栫埗绫绘柟娉?}.鏂规硶銆?br>
鍖垮悕鍐呴儴绫荤殑浣跨敤鍦烘櫙锛?br>褰撳嚱鏁扮殑鍙傛暟鏄帴鍙g被鍨嬪紩鐢ㄦ椂锛屽鏋滄帴鍙d腑鐨勬柟娉曚笉瓒呰繃3涓€傚彲浠ラ€氳繃鍖垮悕鍐呴儴绫绘潵瀹屾垚鍙傛暟鐨勪紶閫掋€?br>鍏跺疄灏辨槸鍦ㄥ垱寤哄尶鍚嶅唴閮ㄧ被鏃讹紝璇ョ被涓殑灏佽鐨勬柟娉曚笉瑕佽繃澶氾紝鏈€濂戒袱涓垨鑰呬袱涓互鍐呫€?br>--------------------------------------------------------
//闈㈣瘯
//1
new Object(){
void show(){
System.out.println("show run");
}
}.show();
//2
Object obj = new Object(){
void show(){
System.out.println("show run");
}
};
obj.show();
1鍜?鐨勫啓娉曟纭悧锛熸湁鍖哄埆鍚楋紵璇村嚭鍘熷洜銆?br> 鍐欐硶鏄纭紝1鍜?閮芥槸鍦ㄩ€氳繃鍖垮悕鍐呴儴绫诲缓绔嬩竴涓狾bject绫荤殑瀛愮被瀵硅薄銆?br> 鍖哄埆锛?br> 绗竴涓彲鏄紪璇戦€氳繃锛屽苟杩愯銆?br> 绗簩涓紪璇戝け璐ワ紝鍥犱负鍖垮悕鍐呴儴绫绘槸涓€涓瓙绫诲璞★紝褰撶敤Object鐨刼bj寮曠敤鎸囧悜鏃讹紝灏辫鎻愬崌涓轰簡
Object绫诲瀷锛岃€岀紪璇戞椂妫€鏌bject绫讳腑鏄惁鏈塻how鏂规硶锛屾墍浠ョ紪璇戝け璐ャ€?br>-------------------------------------------------------
class InnerClassDemo6 {
+锛坰tatic锛塩lass Inner{
void show(){}
}
public void method(){
this.new Inner().show();//鍙互
}
public static void main(String[] args) {//static涓嶅厑璁竧his
This.new Inner().show();//閿欒锛孖nner绫婚渶瑕佸畾涔夋垚static
}
}
------------------------------------------------------
interface Inter{
void show();
}
class Outer{//閫氳繃鍖垮悕鍐呴儴绫昏ˉ瓒砄uter绫讳腑鐨勪唬鐮併€?br> public static Inter method(){
return new Inter(){
public void show(){}
};
}
}
class InnerClassDemo7 {
public static void main(String[] args) {
Outer.method().show();
/*
Outer.method():鎰忔€濇槸锛歄uter涓湁涓€涓悕绉颁负method鐨勬柟娉曪紝鑰屼笖杩欎釜鏂规硶鏄潤鎬佺殑銆?br> Outer.method().show():褰揙uter绫昏皟鐢ㄩ潤鎬佺殑method鏂规硶杩愮畻缁撴潫鍚庣殑缁撴灉鍙堣皟鐢ㄤ簡show鏂规硶锛屾剰鍛崇潃锛歮ethod()鏂规硶杩愮畻瀹屼竴涓槸瀵硅薄锛岃€屼笖杩欎釜瀵硅薄鏄疘nter绫诲瀷鐨勩€?br> */
function (new Inter(){
public void show(){}
}); //鍖垮悕鍐呴儴绫讳綔涓烘柟娉曠殑鍙傛暟杩涜浼犻€掋€?br> }
public static void function(Inter in){
in.show();
}
}
------------------------------------------------------------------------------------------------
寮?甯革細鈽呪槄鈽呪槄
寮傚父锛氬氨鏄笉姝e父銆傜▼搴忓湪杩愯鏃跺嚭鐜扮殑涓嶆甯告儏鍐点€傚叾瀹炲氨鏄▼搴忎腑鍑虹幇鐨勯棶棰樸€傝繖涓棶棰樻寜鐓ч潰鍚戝璞℃€濇兂杩涜鎻忚堪锛屽苟灏佽鎴愪簡瀵硅薄銆傚洜涓洪棶棰樼殑浜х敓鏈変骇鐢熺殑鍘熷洜銆佹湁闂鐨勫悕绉般€佹湁闂鐨勬弿杩扮瓑澶氫釜灞炴€т俊鎭瓨鍦ㄣ€傚綋鍑虹幇澶氬睘鎬т俊鎭渶鏂逛究鐨勬柟寮忓氨鏄皢杩欎簺淇℃伅杩涜灏佽銆傚紓甯稿氨鏄痡ava鎸夌収闈㈠悜瀵硅薄鐨勬€濇兂灏嗛棶棰樿繘琛屽璞″皝瑁呫€傝繖鏍峰氨鏂逛究浜庢搷浣滈棶棰樹互鍙婂鐞嗛棶棰樸€?br>鍑虹幇鐨勯棶棰樻湁寰堝绉嶏紝姣斿瑙掓爣瓒婄晫锛岀┖鎸囬拡绛夐兘鏄€傚氨瀵硅繖浜涢棶棰樿繘琛屽垎绫汇€傝€屼笖杩欎簺闂閮芥湁鍏辨€у唴瀹规瘮濡傦細姣忎竴涓棶棰橀兘鏈夊悕绉帮紝鍚屾椂杩樻湁闂鎻忚堪鐨勪俊鎭紝闂鍑虹幇鐨勪綅缃紝鎵€浠ュ彲浠ヤ笉鏂殑鍚戜笂鎶藉彇銆傚舰鎴愪簡寮傚父浣撶郴銆?br>
--------java.lang.Throwable锛?br>Throwable锛氬彲鎶涘嚭鐨勩€?br> |--Error锛氶敊璇紝涓€鑸儏鍐典笅锛屼笉缂栧啓閽堝鎬х殑浠g爜杩涜澶勭悊锛岄€氬父鏄痡vm鍙戠敓鐨勶紝闇€瑕佸绋嬪簭杩涜淇銆?br> |--Exception锛氬紓甯革紝鍙互鏈夐拡瀵规€х殑澶勭悊鏂瑰紡
鏃犺鏄敊璇繕鏄紓甯革紝瀹冧滑閮芥湁鍏蜂綋鐨勫瓙绫讳綋鐜版瘡涓€涓棶棰橈紝瀹冧滑鐨勫瓙绫婚兘鏈変竴涓叡鎬э紝灏辨槸閮戒互鐖剁被鍚嶆墠浣滀负瀛愮被鐨勫悗缂€鍚嶃€?br>
杩欎釜浣撶郴涓殑鎵€鏈夌被鍜屽璞¢兘鍏峰涓€涓嫭鏈夌殑鐗圭偣锛涘氨鏄彲鎶涙€с€?br>鍙姏鎬х殑浣撶幇锛氬氨鏄繖涓綋绯讳腑鐨勭被鍜屽璞¢兘鍙互琚玹hrows鍜宼hrow涓や釜鍏抽敭瀛楁墍鎿嶄綔銆?br>------------------------------------------------------
class ExceptionDemo{
public static void main(String[] args) {
// byte[] buf = new byte[1024*1024*700];//java.lang.OutOfMemoryError鍐呭瓨婧㈠嚭閿欒
}
}
------------------------------------------------------
鍦ㄥ紑鍙戞椂锛屽鏋滃畾涔夊姛鑳芥椂锛屽彂鐜拌鍔熻兘浼氬嚭鐜颁竴浜涢棶棰橈紝搴旇灏嗛棶棰樺湪瀹氫箟鍔熻兘鏃舵爣绀哄嚭鏉ワ紝杩欐牱璋冪敤鑰呭氨鍙互鍦ㄤ娇鐢ㄨ繖涓姛鑳界殑鏃跺€欙紝棰勫厛缁欏嚭澶勭悊鏂瑰紡銆?br>
濡備綍鏍囩ず鍛紵閫氳繃throws鍏抽敭瀛楀畬鎴愶紝鏍煎紡锛歵hrows 寮傚父绫诲悕,寮傚父绫诲悕...
杩欐牱鏍囩ず鍚庯紝璋冪敤鑰咃紝鍦ㄤ娇鐢ㄨ鍔熻兘鏃讹紝灏卞繀椤昏澶勭悊锛屽惁鍒欑紪璇戝け璐ャ€?br>
澶勭悊鏂瑰紡鏈変袱绉嶏細1銆佹崟鎹夛紱2銆佹姏鍑恒€?br>瀵逛簬鎹曟崏锛歫ava鏈夐拡瀵规€х殑璇彞鍧楄繘琛屽鐞嗐€?br>try {
闇€瑕佽妫€娴嬬殑浠g爜锛?br>}
catch(寮傚父绫?鍙橀噺鍚?{
寮傚父澶勭悊浠g爜锛?br>}
fianlly{
涓€瀹氫細鎵ц鐨勪唬鐮侊紱
}
--------------------------------------------------------
catch (Exception e) { //e鐢ㄤ簬鎺ユ敹try妫€娴嬪埌鐨勫紓甯稿璞°€?br> System.out.println("message:"+e.getMessage());//鑾峰彇鐨勬槸寮傚父鐨勪俊鎭€?br> System.out.println("toString:"+e.toString());//鑾峰彇鐨勬槸寮傚父鐨勫悕瀛?寮傚父鐨勪俊鎭€?br> e.printStackTrace();//鎵撳嵃寮傚父鍦ㄥ爢鏍堜腑淇℃伅锛涘紓甯稿悕绉?寮傚父淇℃伅+寮傚父鐨勪綅缃€?br>}
---------------------------------------------------------
寮傚父澶勭悊鍘熷垯锛氬姛鑳芥姏鍑哄嚑涓紓甯革紝鍔熻兘璋冪敤濡傛灉杩涜try澶勭悊锛岄渶瑕佷笌涔嬪搴旂殑catch澶勭悊浠g爜鍧楋紝杩欐牱鐨勫鐞嗘湁閽堝鎬э紝鎶涘嚑涓氨澶勭悊鍑犱釜銆?br>
鐗规畩鎯呭喌锛歵ry瀵瑰簲澶氫釜catch鏃讹紝濡傛灉鏈夌埗绫荤殑catch璇彞鍧楋紝涓€瀹氳鏀惧湪涓嬮潰銆?br>
throw 鍜宼hrows鍏抽敭瀛楃殑鍖哄埆锛?br>throw鐢ㄤ簬鎶涘嚭寮傚父瀵硅薄锛屽悗闈㈣窡鐨勬槸寮傚父瀵硅薄锛泃hrow鐢ㄥ湪鍑芥暟鍐呫€?br>throws鐢ㄤ簬鎶涘嚭寮傚父绫伙紝鍚庨潰璺熺殑寮傚父绫诲悕锛屽彲浠ヨ窡澶氫釜锛岀敤閫楀彿闅斿紑銆倀hrows鐢ㄥ湪鍑芥暟涓娿€?br>
閫氬父鎯呭喌锛氬嚱鏁板唴瀹瑰鏋滄湁throw锛屾姏鍑哄紓甯稿璞★紝骞舵病鏈夎繘琛屽鐞嗭紝閭d箞鍑芥暟涓婁竴瀹氳澹版槑锛屽惁鍒欑紪璇戝け璐ャ€備絾鏄篃鏈夌壒娈婃儏鍐点€?br>
寮傚父鍒嗕袱绉嶏細
1锛氱紪璇戞椂琚鏌ョ殑寮傚父锛屽彧瑕佹槸Exception鍙婂叾瀛愮被閮芥槸缂栬瘧鏃惰妫€娴嬬殑寮傚父銆?br>2锛氳繍琛屾椂寮傚父锛屽叾涓璄xception鏈変竴涓壒娈婄殑瀛愮被RuntimeException锛屼互鍙奟untimeException鐨勫瓙绫绘槸杩愯寮傚父锛屼篃灏辫杩欎釜寮傚父鏄紪璇戞椂涓嶈妫€鏌ョ殑寮傚父銆?br>缂栬瘧鏃惰妫€鏌ョ殑寮傚父鍜岃繍琛屾椂寮傚父鐨勫尯鍒細
缂栬瘧琚鏌ョ殑寮傚父鍦ㄥ嚱鏁板唴琚姏鍑猴紝鍑芥暟蹇呴』瑕佸0鏄庯紝鍚︾紪璇戝け璐ャ€?br>澹版槑鐨勫師鍥狅細鏄渶瑕佽皟鐢ㄨ€呭璇ュ紓甯歌繘琛屽鐞嗐€?br>杩愯鏃跺紓甯稿鏋滃湪鍑芥暟鍐呰鎶涘嚭锛屽湪鍑芥暟涓婁笉闇€瑕佸0鏄庛€?br>涓嶅0鏄庣殑鍘熷洜锛氫笉闇€瑕佽皟鐢ㄨ€呭鐞嗭紝杩愯鏃跺紓甯稿彂鐢燂紝宸茬粡鏃犳硶鍐嶈绋嬪簭缁х画杩愯锛屾墍浠ワ紝涓嶈璋冪敤澶勭悊鐨勶紝鐩存帴璁╃▼搴忓仠姝紝鐢辫皟鐢ㄨ€呭浠g爜杩涜淇銆?br>
瀹氫箟寮傚父澶勭悊鏃讹紝浠€涔堟椂鍊欏畾涔塼ry锛屼粈涔堟椂鍊欏畾涔塼hrows鍛紵
鍔熻兘鍐呴儴濡傛灉鍑虹幇寮傚父锛屽鏋滃唴閮ㄥ彲浠ュ鐞嗭紝灏辩敤try锛?br>濡傛灉鍔熻兘鍐呴儴澶勭悊涓嶄簡锛屽氨蹇呴』澹版槑鍑烘潵锛岃璋冪敤鑰呭鐞嗐€?br>
鑷畾涔夊紓甯革細褰撳紑鍙戞椂锛岄」鐩腑鍑虹幇浜唈ava涓病鏈夊畾涔夎繃鐨勯棶棰樻椂锛岃繖鏃跺氨闇€瑕佹垜浠寜鐓ava寮傚父寤虹珛鎬濇兂锛屽皢椤圭洰鐨勪腑鐨勭壒鏈夐棶棰樹篃杩涜瀵硅薄鐨勫皝瑁呫€傝繖涓紓甯革紝绉颁负鑷畾涔夊紓甯搞€?br>
瀵逛簬闄ゆ硶杩愮畻锛?浣滀负闄ゆ暟鏄笉鍙互鐨勩€俲ava涓杩欑闂鐢ˋrithmeticException绫昏繘琛屾弿杩般€傚浜庤繖涓姛鑳斤紝鍦ㄦ垜浠」鐩腑锛岄櫎鏁伴櫎浜嗕笉鍙互涓?澶栵紝杩樹笉鍙互涓鸿礋鏁般€傚彲鏄礋鏁扮殑閮ㄥ垎java骞舵病鏈夐拡瀵规弿杩般€傛墍浠ユ垜浠氨闇€瑕佽嚜瀹氫箟杩欎釜寮傚父銆?br>
鑷畾涔夊紓甯哥殑姝ラ锛?br>1锛氬畾涔変竴涓瓙绫荤户鎵縀xception鎴朢untimeException锛岃璇ョ被鍏峰鍙姏鎬с€?br>2锛氶€氳繃throw 鎴栬€卼hrows杩涜鎿嶄綔銆?br>
寮傚父鐨勮浆鎹㈡€濇兂锛氬綋鍑虹幇鐨勫紓甯告槸璋冪敤鑰呭鐞嗕笉浜嗙殑锛屽氨闇€瑕佸皢姝ゅ紓甯歌浆鎹负涓€涓皟鐢ㄨ€呭彲浠ュ鐞嗙殑寮傚父鎶涘嚭銆?br>
try catch finally鐨勫嚑绉嶇粨鍚堟柟寮忥細
2,
try
catch
try
finally
catch
finally
杩欑鎯呭喌锛屽鏋滃嚭鐜板紓甯革紝骞朵笉澶勭悊锛屼絾鏄祫婧愪竴瀹氬叧闂紝鎵€浠ry finally闆嗗悎鍙负鍏抽棴璧勬簮銆?br>璁颁綇锛歠inally寰堟湁鐢紝涓昏鐢ㄦ埛鍏抽棴璧勬簮銆傛棤璁烘槸鍚﹀彂鐢熷紓甯革紝璧勬簮閮藉繀椤昏繘琛屽叧闂€?br>System.exit(0); //閫€鍑簀vm锛屽彧鏈夎繖绉嶆儏鍐礷inally涓嶆墽琛屻€?br>
褰撳紓甯稿嚭鐜板悗锛屽湪瀛愮埗绫昏繘琛岃鐩栨椂锛屾湁浜嗕竴浜涙柊鐨勭壒鐐癸細
1锛氬綋瀛愮被瑕嗙洊鐖剁被鐨勬柟娉曟椂锛屽鏋滅埗绫荤殑鏂规硶鎶涘嚭浜嗗紓甯革紝閭d箞瀛愮被鐨勬柟娉曡涔堜笉鎶涘嚭寮傚父瑕佷箞鎶涘嚭鐖剁被寮傚父鎴栬€呰寮傚父鐨勫瓙绫伙紝涓嶈兘鎶涘嚭鍏朵粬寮傚父銆?br>2锛氬鏋滅埗绫绘姏鍑轰簡澶氫釜寮傚父锛岄偅涔堝瓙绫诲湪瑕嗙洊鏃跺彧鑳芥姏鍑虹埗绫荤殑寮傚父鐨勫瓙闆嗐€?br>娉ㄦ剰锛?br>濡傛灉鐖剁被鎴栬€呮帴鍙d腑鐨勬柟娉曟病鏈夋姏鍑鸿繃寮傚父锛岄偅涔堝瓙绫绘槸涓嶅彲浠ユ姏鍑哄紓甯哥殑锛屽鏋滃瓙绫荤殑瑕嗙洊鐨勬柟娉曚腑鍑虹幇浜嗗紓甯革紝鍙兘try涓嶈兘throws銆?br>濡傛灉杩欎釜寮傚父瀛愮被鏃犳硶澶勭悊锛屽凡缁忓奖鍝嶄簡瀛愮被鏂规硶鐨勫叿浣撹繍绠楋紝杩欐椂鍙互鍦ㄥ瓙绫绘柟娉曚腑锛岄€氳繃throw鎶涘嚭RuntimeException寮傚父鎴栬€呭叾瀛愮被锛岃繖鏍凤紝瀛愮被鐨勬柟娉曚笂鏄笉闇€瑕乼hrows澹版槑鐨勩€?br>
甯歌寮傚父锛?br>1銆佽剼鏍囪秺鐣屽紓甯革紙IndexOutOfBoundsException锛夊寘鎷暟缁勩€佸瓧绗︿覆锛?br>绌烘寚閽堝紓甯革紙NullPointerException锛?br>2銆佺被鍨嬭浆鎹㈠紓甯革細ClassCastException
3銆佹病鏈夎繖涓厓绱犲紓甯革細NullPointerException
4銆佷笉鏀寔鎿嶄綔寮傚父锛?br>寮傚父瑕佸敖閲忛伩鍏嶏紝濡傛灉閬垮厤涓嶄簡锛岄渶瑕侀鍏堢粰鍑哄鐞嗘柟寮忋€傛瘮濡傚搴鑽紝姣斿鐏伀鍣ㄣ€?br>-----------------------------------------------------------------------------------------------
鍖咃細瀹氫箟鍖呯敤package鍏抽敭瀛椼€?/h3>
1锛氬绫绘枃浠惰繘琛屽垎绫荤鐞嗐€?br>2锛氱粰绫绘枃浠舵彁渚涘灞傚悕绉扮┖闂淬€?br>
濡傛灉鐢熸垚鐨勫寘涓嶅湪褰撳墠鐩綍涓嬶紝闇€瑕佹渶濂芥墽琛宑lasspath锛屽皢鍖呮墍鍦ㄧ埗鐩綍瀹氫箟鍒癱lasspath鍙橀噺涓嵆鍙€?br>涓€鑸湪瀹氫箟鍖呭悕鏃讹紝鍥犱负鍖呯殑鍑虹幇鏄负浜嗗尯鍒嗛噸鍚嶇殑绫汇€傛墍浠ュ寘鍚嶈灏介噺鍞竴銆傛€庝箞淇濊瘉鍞竴鎬у憿锛熷彲浠ヤ娇鐢╱rl鍩熷悕鏉ヨ繘琛屽寘鍚嶇О鐨勫畾涔夈€?br>package pack;//瀹氫箟浜嗕竴涓寘锛屽悕绉颁负pack銆?娉ㄦ剰锛氬寘鍚嶇殑鍐欐硶瑙勮寖锛氭墍鏈夊瓧姣嶉兘灏忓啓銆?br>//package cn.itcast.pack.demo;
绫荤殑鍏ㄥ悕绉版槸 鍖呭悕.绫诲悕
缂栬瘧鍛戒护锛歫avac 鈥揹 浣嶇疆锛?褰撳墠璺緞锛?java婧愭枃浠?锛堝氨鍙互鑷姩鐢熸垚鍖咃級
鍖呮槸涓€绉嶅皝瑁呭舰寮忥紝鐢ㄤ簬灏佽绫伙紝鎯宠琚寘浠ュ鐨勭▼搴忚闂紝璇ョ被蹇呴』public锛?br>绫讳腑鐨勬垚鍛橈紝濡傛灉琚寘浠ュ璁块棶锛屼篃蹇呴』public锛?br>
鍖呬笌鍖呬箣闂磋闂彲浠ヤ娇鐢ㄧ殑鏉冮檺鏈変袱绉嶏細
1锛歱ublic
2锛歱rotected锛氬彧鑳芥槸涓嶅悓鍖呬腑鐨勫瓙绫诲彲浠ヤ娇鐢ㄧ殑鏉冮檺銆?br>
鎬荤粨java涓殑鍥涚鏉冮檺锛?/h1>
鑼? 鍥?nbsp; public protected default private
鍚屼竴涓被涓?nbsp; ok ok ok ok
鍚?涓€ 鍖?涓?nbsp; ok ok ok
瀛? 绫? ok
涓?鍚屽寘 涓?nbsp; ok
-----------------------------------------------------------------------------------------------
Import - 瀵煎叆锛氱被鍚嶇О鍙橀暱锛屽啓璧锋潵寰堥夯鐑︺€備负浜嗙畝鍖栵紝浣跨敤浜嗕竴涓叧閿瓧锛歩mport锛屽彲浠ヤ娇鐢ㄨ繖涓叧閿瓧瀵煎叆鎸囧畾鍖呬腑鐨勭被銆傝浣忥細瀹為檯寮€鍙戞椂,鍒扮殑鍝釜绫诲氨瀵煎叆鍝釜绫伙紝涓嶅缓璁娇鐢?.
import packa.*;//杩欎釜浠呬粎鏄鍏ヤ簡packa褰撳墠鐩綍涓嬬殑鎵€鏈夌殑绫汇€備笉鍖呭惈瀛愬寘銆?br>import packa.abc.*;//瀵煎叆浜唒acka鍖呬腑鐨勫瓙鍖卆bc涓嬬殑褰撳墠鐨勬墍鏈夌被銆?br>
濡傛灉瀵煎叆鐨勪袱涓寘涓瓨鍦ㄧ潃鐩稿悓鍚嶇О鐨勭被銆傝繖鏃跺鏋滅敤鍒拌绫伙紝蹇呴』鍦ㄤ唬鐮佷腑鎸囧畾鍖呭悕銆?br>
甯歌鐨勮蒋浠跺寘:
java.lang : language java鐨勬牳蹇冨寘锛孫bject System String Throwable jdk1.2鐗堟湰鍚庯紝璇ュ寘涓殑绫昏嚜鍔ㄨ瀵煎叆銆?br>java.awt : 瀹氫箟鐨勯兘鏄敤浜巎ava鍥惧舰鐣岄潰寮€鍙戠殑瀵硅薄銆?br>javax.swing: 鎻愪緵鎵€鏈夌殑windows妗岄潰搴旂敤绋嬪簭鍖呮嫭鐨勬帶浠?姣斿锛欶rame , Dialog, Table, List 绛夌瓑,灏辨槸java鐨勫浘褰㈢晫闈㈠簱銆?br>java.net : 鐢ㄤ簬java缃戠粶缂栫▼鏂归潰鐨勫璞¢兘鍦ㄨ鍖呬腑銆?br>java.io : input output 鐢ㄤ簬鎿嶄綔璁惧涓婃暟鎹殑瀵硅薄閮藉湪璇ュ寘涓€傛瘮濡傦細璇诲彇纭洏鏁版嵁锛屽線纭洏鍐欏叆鏁版嵁銆?br>java.util : java鐨勫伐鍏峰寘锛屾椂闂村璞★紝闆嗗悎妗嗘灦銆?br>java.applet: application+let 瀹㈡埛绔痡ava灏忕▼搴忋€俿erver+let --> servlet 鏈嶅姟绔痡ava灏忕▼搴忋€?br>
jar 锛歫ava鐨勫帇缂╁寘锛屼富瑕佺敤浜庡瓨鍌ㄧ被鏂囦欢锛屾垨鑰呴厤缃枃浠剁瓑銆?br>鍛戒护鏍煎紡锛歫ar 鈥揷f 鍖呭悕.jar 鍖呯洰褰?br> 瑙e帇缂╋細jar 鈥搙vf 鍖呭悕.jar
灏唈ar鍖呯洰褰曞垪琛ㄩ噸瀹氬悜鍒颁竴涓枃浠朵腑锛歫ar 鈥搕f 鍖呭悕.jar >c:1.txt
-----------------------------------------------------------------------------------------------
澶氱嚎绋嬶細鈽呪槄鈽呪槄
杩涚▼锛氭鍦ㄨ繘琛屼腑鐨勭▼搴忋€傚叾瀹炶繘绋嬪氨鏄竴涓簲鐢ㄧ▼搴忚繍琛屾椂鐨勫唴瀛樺垎閰嶇┖闂淬€?br>绾跨▼锛氬叾瀹炲氨鏄繘绋嬩腑涓€涓▼搴忔墽琛屾帶鍒跺崟鍏冿紝涓€鏉℃墽琛岃矾寰勩€傝繘绋嬭礋璐g殑鏄簲鐢ㄧ▼搴忕殑绌洪棿鐨勬爣绀恒€傜嚎绋嬭礋璐g殑鏄簲鐢ㄧ▼搴忕殑鎵ц椤哄簭銆?br>
涓€涓繘绋嬭嚦灏戞湁涓€涓嚎绋嬪湪杩愯锛屽綋涓€涓繘绋嬩腑鍑虹幇澶氫釜绾跨▼鏃讹紝灏辩О杩欎釜搴旂敤绋嬪簭鏄绾跨▼搴旂敤绋嬪簭锛屾瘡涓嚎绋嬪湪鏍堝尯涓兘鏈夎嚜宸辩殑鎵ц绌洪棿锛岃嚜宸辩殑鏂规硶鍖恒€佽嚜宸辩殑鍙橀噺銆?br>jvm鍦ㄥ惎鍔ㄧ殑鏃讹紝棣栧厛鏈変竴涓富绾跨▼锛岃礋璐g▼搴忕殑鎵ц锛岃皟鐢ㄧ殑鏄痬ain鍑芥暟銆備富绾跨▼鎵ц鐨勪唬鐮侀兘鍦╩ain鏂规硶涓€?br>褰撲骇鐢熷瀮鍦炬椂锛屾敹鍨冨溇鐨勫姩浣滐紝鏄笉闇€瑕佷富绾跨▼鏉ュ畬鎴愶紝鍥犱负杩欐牱锛屼細鍑虹幇涓荤嚎绋嬩腑鐨勪唬鐮佹墽琛屼細鍋滄锛屼細鍘昏繍琛屽瀮鍦惧洖鏀跺櫒浠g爜锛屾晥鐜囪緝浣庯紝鎵€浠ョ敱鍗曠嫭涓€涓嚎绋嬫潵璐熻矗鍨冨溇鍥炴敹銆?br>
闅忔満鎬х殑鍘熺悊锛氬洜涓篶pu鐨勫揩閫熷垏鎹㈤€犳垚锛屽摢涓嚎绋嬭幏鍙栧埌浜哻pu鐨勬墽琛屾潈锛屽摢涓嚎绋嬪氨鎵ц銆?br>
杩斿洖褰撳墠绾跨▼鐨勫悕绉帮細Thread.currentThread().getName()
绾跨▼鐨勫悕绉版槸鐢憋細Thread-缂栧彿瀹氫箟鐨勩€傜紪鍙蜂粠0寮€濮嬨€?br>绾跨▼瑕佽繍琛岀殑浠g爜閮界粺涓€瀛樻斁鍦ㄤ簡run鏂规硶涓€?br>
绾跨▼瑕佽繍琛屽繀椤昏閫氳繃绫讳腑鎸囧畾鐨勬柟娉曞紑鍚€俿tart鏂规硶銆傦紙鍚姩鍚庯紝灏卞浜嗕竴鏉℃墽琛岃矾寰勶級
start鏂规硶锛?锛夈€佸惎鍔ㄤ簡绾跨▼锛?锛夈€佽jvm璋冪敤浜唕un鏂规硶銆?br>
鍒涘缓绾跨▼鐨勭涓€绉嶆柟寮忥細
缁ф壙Thread 锛岀敱瀛愮被澶嶅啓run鏂规硶銆?br>姝ラ锛?br>1锛屽畾涔夌被缁ф壙Thread绫伙紱
2锛岀洰鐨勬槸澶嶅啓run鏂规硶锛屽皢瑕佽绾跨▼杩愯鐨勪唬鐮侀兘瀛樺偍鍒皉un鏂规硶涓紱
3锛岄€氳繃鍒涘缓Thread绫荤殑瀛愮被瀵硅薄锛屽垱寤虹嚎绋嬪璞★紱
4锛岃皟鐢ㄧ嚎绋嬬殑start鏂规硶锛屽紑鍚嚎绋嬶紝骞舵墽琛宺un鏂规硶銆?br>
绾跨▼鐘舵€侊細
琚垱寤猴細start()
杩愯锛氬叿澶囨墽琛岃祫鏍硷紝鍚屾椂鍏峰鎵ц鏉冿紱
鍐荤粨锛歴leep(time),wait()鈥攏otify()鍞ら啋锛涚嚎绋嬮噴鏀句簡鎵ц鏉冿紝鍚屾椂閲婃斁鎵ц璧勬牸锛?br>涓存椂闃诲鐘舵€侊細绾跨▼鍏峰cpu鐨勬墽琛岃祫鏍硷紝娌℃湁cpu鐨勬墽琛屾潈锛?br>娑堜骸锛歴top()
鍒涘缓绾跨▼鐨勭浜岀鏂瑰紡锛?/h3>
瀹炵幇涓€涓帴鍙unnable銆?br>姝ラ锛?br>1锛屽畾涔夌被瀹炵幇Runnable鎺ュ彛銆?br>2锛岃鐩栨帴鍙d腑鐨剅un鏂规硶锛堢敤浜庡皝瑁呯嚎绋嬭杩愯鐨勪唬鐮侊級銆?br>3锛岄€氳繃Thread绫诲垱寤虹嚎绋嬪璞★紱
4锛屽皢瀹炵幇浜哛unnable鎺ュ彛鐨勫瓙绫诲璞′綔涓哄疄闄呭弬鏁颁紶閫掔粰Thread绫讳腑鐨勬瀯閫犲嚱鏁般€?br>涓轰粈涔堣浼犻€掑憿锛熷洜涓鸿璁╃嚎绋嬪璞℃槑纭杩愯鐨剅un鏂规硶鎵€灞炵殑瀵硅薄銆?br>5锛岃皟鐢═hread瀵硅薄鐨剆tart鏂规硶銆傚紑鍚嚎绋嬶紝骞惰繍琛孯unnable鎺ュ彛瀛愮被涓殑run鏂规硶銆?br>Ticket t = new Ticket();
/*
鐩存帴鍒涘缓Ticket瀵硅薄锛屽苟涓嶆槸鍒涘缓绾跨▼瀵硅薄銆?br> 鍥犱负鍒涘缓瀵硅薄鍙兘閫氳繃new Thread绫伙紝鎴栬€卬ew Thread绫荤殑瀛愮被鎵嶅彲浠ャ€?br> 鎵€浠ユ渶缁堟兂瑕佸垱寤虹嚎绋嬨€傛棦鐒舵病鏈変簡Thread绫荤殑瀛愮被锛屽氨鍙兘鐢═hread绫汇€?br> */
Thread t1 = new Thread(t); //鍒涘缓绾跨▼銆?br> /*
鍙灏唗浣滀负Thread绫荤殑鏋勯€犲嚱鏁扮殑瀹為檯鍙傛暟浼犲叆鍗冲彲瀹屾垚绾跨▼瀵硅薄鍜宼涔嬮棿鐨勫叧鑱?br> 涓轰粈涔堣灏唗浼犵粰Thread绫荤殑鏋勯€犲嚱鏁板憿锛熷叾瀹炲氨鏄负浜嗘槑纭嚎绋嬭杩愯鐨勪唬鐮乺un鏂规硶銆?br> */
t1.start();
涓轰粈涔堣鏈塕unnable鎺ュ彛鐨勫嚭鐜帮紵
1锛氶€氳繃缁ф壙Thread绫荤殑鏂瑰紡锛屽彲浠ュ畬鎴愬绾跨▼鐨勫缓绔嬨€備絾鏄繖绉嶆柟寮忔湁涓€涓眬闄愭€э紝濡傛灉涓€涓被宸茬粡鏈変簡鑷繁鐨勭埗绫伙紝灏变笉鍙互缁ф壙Thread绫伙紝鍥犱负java鍗曠户鎵跨殑灞€闄愭€с€?br>鍙槸璇ョ被涓殑杩樻湁閮ㄥ垎浠g爜闇€瑕佽澶氫釜绾跨▼鍚屾椂鎵ц銆傝繖鏃舵€庝箞鍔炲憿锛?br>鍙湁瀵硅绫昏繘琛岄澶栫殑鍔熻兘鎵╁睍锛宩ava灏辨彁渚涗簡涓€涓帴鍙unnable銆傝繖涓帴鍙d腑瀹氫箟浜唕un鏂规硶锛屽叾瀹瀝un鏂规硶鐨勫畾涔夊氨鏄负浜嗗瓨鍌ㄥ绾跨▼瑕佽繍琛岀殑浠g爜銆?br>鎵€浠ワ紝閫氬父鍒涘缓绾跨▼閮界敤绗簩绉嶆柟寮忋€?br>鍥犱负瀹炵幇Runnable鎺ュ彛鍙互閬垮厤鍗曠户鎵跨殑灞€闄愭€с€?br>
2锛氬叾瀹炴槸灏嗕笉鍚岀被涓渶瑕佽澶氱嚎绋嬫墽琛岀殑浠g爜杩涜鎶藉彇銆傚皢澶氱嚎绋嬭杩愯鐨勪唬鐮佺殑浣嶇疆鍗曠嫭瀹氫箟鍒版帴鍙d腑銆備负鍏朵粬绫昏繘琛屽姛鑳芥墿灞曟彁渚涗簡鍓嶆彁銆?br>鎵€浠hread绫诲湪鎻忚堪绾跨▼鏃讹紝鍐呴儴瀹氫箟鐨剅un鏂规硶锛屼篃鏉ヨ嚜浜嶳unnable鎺ュ彛銆?br>
瀹炵幇Runnable鎺ュ彛鍙互閬垮厤鍗曠户鎵跨殑灞€闄愭€с€傝€屼笖锛岀户鎵縏hread锛屾槸鍙互瀵筎hread绫讳腑鐨勬柟娉曪紝杩涜瀛愮被澶嶅啓鐨勩€備絾鏄笉闇€瑕佸仛杩欎釜澶嶅啓鍔ㄤ綔鐨勮瘽锛屽彧涓哄畾涔夌嚎绋嬩唬鐮佸瓨鏀句綅缃紝瀹炵幇Runnable鎺ュ彛鏇存柟渚夸竴浜涖€傛墍浠unnable鎺ュ彛灏嗙嚎绋嬭鎵ц鐨勪换鍔″皝瑁呮垚浜嗗璞°€?br>-------------------------------------------------------
//闈㈣瘯
new Thread(new Runnable(){ //鍖垮悕
public void run(){
System.out.println("runnable run");
}
})
{
public void run(){
System.out.println("subthread run");
}
}.start(); //缁撴灉锛歴ubthread run
---------------------------------------------------------
Try {
Thread.sleep(10);
}catch(InterruptedException e){}// 褰撳埢鎰忚绾跨▼绋嶅井鍋滀竴涓嬶紝妯℃嫙cpu 鍒囨崲鎯呭喌銆?br>
澶氱嚎绋嬪畨鍏ㄩ棶棰樼殑鍘熷洜锛?br>閫氳繃鍥捐В锛氬彂鐜颁竴涓嚎绋嬪湪鎵ц澶氭潯璇彞鏃讹紝骞惰繍绠楀悓涓€涓暟鎹椂锛屽湪鎵ц杩囩▼涓紝鍏朵粬绾跨▼鍙備笌杩涙潵锛屽苟鎿嶄綔浜嗚繖涓暟鎹€傚鑷村埌浜嗛敊璇暟鎹殑浜х敓銆?br>
娑夊強鍒颁袱涓洜绱狅細
1锛屽涓嚎绋嬪湪鎿嶄綔鍏变韩鏁版嵁銆?br>2锛屾湁澶氭潯璇彞瀵瑰叡浜暟鎹繘琛岃繍绠椼€?br>鍘熷洜锛氳繖澶氭潯璇彞锛屽湪鏌愪竴涓椂鍒昏涓€涓嚎绋嬫墽琛屾椂锛岃繕娌℃湁鎵ц瀹岋紝灏辫鍏朵粬绾跨▼鎵ц浜嗐€?br>
瑙e喅瀹夊叏闂鐨勫師鐞嗭細
鍙灏嗘搷浣滃叡浜暟鎹殑璇彞鍦ㄦ煇涓€鏃舵璁╀竴涓嚎绋嬫墽琛屽畬锛屽湪鎵ц杩囩▼涓紝鍏朵粬绾跨▼涓嶈兘杩涙潵鎵ц灏卞彲浠ヨВ鍐宠繖涓棶棰樸€?br>
濡備綍杩涜澶氬彞鎿嶄綔鍏变韩鏁版嵁浠g爜鐨勫皝瑁呭憿锛?br>java涓彁渚涗簡涓€涓В鍐虫柟寮忥細灏辨槸鍚屾浠g爜鍧椼€?br>鏍煎紡锛?br>synchronized(瀵硅薄) { // 浠绘剰瀵硅薄閮藉彲浠ャ€傝繖涓璞″氨鏄攣銆?br> 闇€瑕佽鍚屾鐨勪唬鐮侊紱
}
---------------------------------------------------------------
鍚屾锛氣槄鈽呪槄鈽呪槄
濂藉锛氳В鍐充簡绾跨▼瀹夊叏闂銆?br>寮婄锛氱浉瀵归檷浣庢€ц兘锛屽洜涓哄垽鏂攣闇€瑕佹秷鑰楄祫婧愶紝浜х敓浜嗘閿併€?br>
瀹氫箟鍚屾鏄湁鍓嶆彁鐨勶細
1锛屽繀椤昏鏈変袱涓垨鑰呬袱涓互涓婄殑绾跨▼锛屾墠闇€瑕佸悓姝ャ€?br>2锛屽涓嚎绋嬪繀椤讳繚璇佷娇鐢ㄧ殑鏄悓涓€涓攣銆?br>
鍚屾鐨勭浜岀琛ㄧ幇褰㈠紡锛?br>鍚屾鍑芥暟锛氬叾瀹炲氨鏄皢鍚屾鍏抽敭瀛楀畾涔夊湪鍑芥暟涓婏紝璁╁嚱鏁板叿澶囦簡鍚屾鎬с€?br>
鍚屾鍑芥暟鏄敤鐨勫摢涓攣鍛紵
閫氳繃楠岃瘉锛屽嚱鏁伴兘鏈夎嚜宸辨墍灞炵殑瀵硅薄this锛屾墍浠ュ悓姝ュ嚱鏁版墍浣跨敤鐨勯攣灏辨槸this閿併€?br>
褰撳悓姝ュ嚱鏁拌static淇グ鏃讹紝杩欐椂鐨勫悓姝ョ敤鐨勬槸鍝釜閿佸憿锛?br>闈欐€佸嚱鏁板湪鍔犺浇鏃舵墍灞炰簬绫伙紝杩欐椂鏈夊彲鑳借繕娌℃湁璇ョ被浜х敓鐨勫璞★紝浣嗘槸璇ョ被鐨勫瓧鑺傜爜鏂囦欢鍔犺浇杩涘唴瀛樺氨宸茬粡琚皝瑁呮垚浜嗗璞★紝杩欎釜瀵硅薄灏辨槸璇ョ被鐨勫瓧鑺傜爜鏂囦欢瀵硅薄銆?br>鎵€浠ラ潤鎬佸姞杞芥椂锛屽彧鏈変竴涓璞″瓨鍦紝閭d箞闈欐€佸悓姝ュ嚱鏁板氨浣跨敤鐨勮繖涓璞°€?br>杩欎釜瀵硅薄灏辨槸 绫诲悕.class
鍚屾浠g爜鍧楀拰鍚屾鍑芥暟鐨勫尯鍒紵
鍚屾浠g爜鍧椾娇鐢ㄧ殑閿佸彲浠ユ槸浠绘剰瀵硅薄銆?br>鍚屾鍑芥暟浣跨敤鐨勯攣鏄痶his锛岄潤鎬佸悓姝ュ嚱鏁扮殑閿佹槸璇ョ被鐨勫瓧鑺傜爜鏂囦欢瀵硅薄銆?br>
鍦ㄤ竴涓被涓彧鏈変竴涓悓姝ワ紝鍙互浣跨敤鍚屾鍑芥暟銆傚鏋滄湁澶氬悓姝ワ紝蹇呴』浣跨敤鍚屾浠g爜鍧楋紝鏉ョ‘瀹氫笉鍚岀殑閿併€傛墍浠ュ悓姝ヤ唬鐮佸潡鐩稿鐏垫椿涓€浜涖€?br>-------------------------------------------------------
鈽呰€冪偣闂锛?br>璇峰啓涓€涓欢杩熷姞杞界殑鍗曚緥妯″紡锛熷啓鎳掓眽寮忥紱褰撳嚭鐜板绾跨▼璁块棶鏃舵€庝箞瑙e喅锛熷姞鍚屾锛岃В鍐冲畨鍏ㄩ棶棰橈紱鏁堢巼楂樺悧锛熶笉楂橈紱鎬庢牱瑙e喅锛熼€氳繃鍙岄噸鍒ゆ柇鐨勫舰寮忚В鍐炽€?br>//鎳掓眽寮忥細寤惰繜鍔犺浇鏂瑰紡銆?br>褰撳绾跨▼璁块棶鎳掓眽寮忔椂锛屽洜涓烘噿姹夊紡鐨勬柟娉曞唴瀵瑰叡鎬ф暟鎹繘琛屽鏉¤鍙ョ殑鎿嶄綔銆傛墍浠ュ鏄撳嚭鐜扮嚎绋嬪畨鍏ㄩ棶棰樸€備负浜嗚В鍐筹紝鍔犲叆鍚屾鏈哄埗锛岃В鍐冲畨鍏ㄩ棶棰樸€備絾鏄嵈甯︽潵浜嗘晥鐜囬檷浣庛€?br>涓轰簡鏁堢巼闂锛岄€氳繃鍙岄噸鍒ゆ柇鐨勫舰寮忚В鍐炽€?br>class Single{
private static Single s = null;
private Single(){}
public static Single getInstance(){ //閿佹槸璋侊紵瀛楄妭鐮佹枃浠跺璞★紱
if(s == null){
synchronized(Single.class){
if(s == null)
s = new Single();
}
}
return s;
}
}
---------------------------------------------------------
鍚屾姝婚攣锛氶€氬父鍙灏嗗悓姝ヨ繘琛屽祵濂楋紝灏卞彲浠ョ湅鍒扮幇璞°€傚悓姝ュ嚱鏁颁腑鏈夊悓姝ヤ唬鐮佸潡锛屽悓姝ヤ唬鐮佸潡涓繕鏈夊悓姝ュ嚱鏁般€?br>
绾跨▼闂撮€氫俊锛?/h2>
鍚屼竴涓被涓?nbsp; ok ok ok ok
鍚?涓€ 鍖?涓?nbsp; ok ok ok
瀛? 绫? ok
涓?鍚屽寘 涓?nbsp; ok
-----------------------------------------------------------------------------------------------
Import - 瀵煎叆锛氱被鍚嶇О鍙橀暱锛屽啓璧锋潵寰堥夯鐑︺€備负浜嗙畝鍖栵紝浣跨敤浜嗕竴涓叧閿瓧锛歩mport锛屽彲浠ヤ娇鐢ㄨ繖涓叧閿瓧瀵煎叆鎸囧畾鍖呬腑鐨勭被銆傝浣忥細瀹為檯寮€鍙戞椂,鍒扮殑鍝釜绫诲氨瀵煎叆鍝釜绫伙紝涓嶅缓璁娇鐢?.
import packa.*;//杩欎釜浠呬粎鏄鍏ヤ簡packa褰撳墠鐩綍涓嬬殑鎵€鏈夌殑绫汇€備笉鍖呭惈瀛愬寘銆?br>import packa.abc.*;//瀵煎叆浜唒acka鍖呬腑鐨勫瓙鍖卆bc涓嬬殑褰撳墠鐨勬墍鏈夌被銆?br>
濡傛灉瀵煎叆鐨勪袱涓寘涓瓨鍦ㄧ潃鐩稿悓鍚嶇О鐨勭被銆傝繖鏃跺鏋滅敤鍒拌绫伙紝蹇呴』鍦ㄤ唬鐮佷腑鎸囧畾鍖呭悕銆?br>
甯歌鐨勮蒋浠跺寘:
java.lang : language java鐨勬牳蹇冨寘锛孫bject System String Throwable jdk1.2鐗堟湰鍚庯紝璇ュ寘涓殑绫昏嚜鍔ㄨ瀵煎叆銆?br>java.awt : 瀹氫箟鐨勯兘鏄敤浜巎ava鍥惧舰鐣岄潰寮€鍙戠殑瀵硅薄銆?br>javax.swing: 鎻愪緵鎵€鏈夌殑windows妗岄潰搴旂敤绋嬪簭鍖呮嫭鐨勬帶浠?姣斿锛欶rame , Dialog, Table, List 绛夌瓑,灏辨槸java鐨勫浘褰㈢晫闈㈠簱銆?br>java.net : 鐢ㄤ簬java缃戠粶缂栫▼鏂归潰鐨勫璞¢兘鍦ㄨ鍖呬腑銆?br>java.io : input output 鐢ㄤ簬鎿嶄綔璁惧涓婃暟鎹殑瀵硅薄閮藉湪璇ュ寘涓€傛瘮濡傦細璇诲彇纭洏鏁版嵁锛屽線纭洏鍐欏叆鏁版嵁銆?br>java.util : java鐨勫伐鍏峰寘锛屾椂闂村璞★紝闆嗗悎妗嗘灦銆?br>java.applet: application+let 瀹㈡埛绔痡ava灏忕▼搴忋€俿erver+let --> servlet 鏈嶅姟绔痡ava灏忕▼搴忋€?br>jar 锛歫ava鐨勫帇缂╁寘锛屼富瑕佺敤浜庡瓨鍌ㄧ被鏂囦欢锛屾垨鑰呴厤缃枃浠剁瓑銆?br>鍛戒护鏍煎紡锛歫ar 鈥揷f 鍖呭悕.jar 鍖呯洰褰?br> 瑙e帇缂╋細jar 鈥搙vf 鍖呭悕.jar
灏唈ar鍖呯洰褰曞垪琛ㄩ噸瀹氬悜鍒颁竴涓枃浠朵腑锛歫ar 鈥搕f 鍖呭悕.jar >c:1.txt
-----------------------------------------------------------------------------------------------
澶氱嚎绋嬶細鈽呪槄鈽呪槄
杩涚▼锛氭鍦ㄨ繘琛屼腑鐨勭▼搴忋€傚叾瀹炶繘绋嬪氨鏄竴涓簲鐢ㄧ▼搴忚繍琛屾椂鐨勫唴瀛樺垎閰嶇┖闂淬€?br>绾跨▼锛氬叾瀹炲氨鏄繘绋嬩腑涓€涓▼搴忔墽琛屾帶鍒跺崟鍏冿紝涓€鏉℃墽琛岃矾寰勩€傝繘绋嬭礋璐g殑鏄簲鐢ㄧ▼搴忕殑绌洪棿鐨勬爣绀恒€傜嚎绋嬭礋璐g殑鏄簲鐢ㄧ▼搴忕殑鎵ц椤哄簭銆?br>涓€涓繘绋嬭嚦灏戞湁涓€涓嚎绋嬪湪杩愯锛屽綋涓€涓繘绋嬩腑鍑虹幇澶氫釜绾跨▼鏃讹紝灏辩О杩欎釜搴旂敤绋嬪簭鏄绾跨▼搴旂敤绋嬪簭锛屾瘡涓嚎绋嬪湪鏍堝尯涓兘鏈夎嚜宸辩殑鎵ц绌洪棿锛岃嚜宸辩殑鏂规硶鍖恒€佽嚜宸辩殑鍙橀噺銆?br>jvm鍦ㄥ惎鍔ㄧ殑鏃讹紝棣栧厛鏈変竴涓富绾跨▼锛岃礋璐g▼搴忕殑鎵ц锛岃皟鐢ㄧ殑鏄痬ain鍑芥暟銆備富绾跨▼鎵ц鐨勪唬鐮侀兘鍦╩ain鏂规硶涓€?br>褰撲骇鐢熷瀮鍦炬椂锛屾敹鍨冨溇鐨勫姩浣滐紝鏄笉闇€瑕佷富绾跨▼鏉ュ畬鎴愶紝鍥犱负杩欐牱锛屼細鍑虹幇涓荤嚎绋嬩腑鐨勪唬鐮佹墽琛屼細鍋滄锛屼細鍘昏繍琛屽瀮鍦惧洖鏀跺櫒浠g爜锛屾晥鐜囪緝浣庯紝鎵€浠ョ敱鍗曠嫭涓€涓嚎绋嬫潵璐熻矗鍨冨溇鍥炴敹銆?br>
闅忔満鎬х殑鍘熺悊锛氬洜涓篶pu鐨勫揩閫熷垏鎹㈤€犳垚锛屽摢涓嚎绋嬭幏鍙栧埌浜哻pu鐨勬墽琛屾潈锛屽摢涓嚎绋嬪氨鎵ц銆?br>
杩斿洖褰撳墠绾跨▼鐨勫悕绉帮細Thread.currentThread().getName()
绾跨▼鐨勫悕绉版槸鐢憋細Thread-缂栧彿瀹氫箟鐨勩€傜紪鍙蜂粠0寮€濮嬨€?br>绾跨▼瑕佽繍琛岀殑浠g爜閮界粺涓€瀛樻斁鍦ㄤ簡run鏂规硶涓€?br>
绾跨▼瑕佽繍琛屽繀椤昏閫氳繃绫讳腑鎸囧畾鐨勬柟娉曞紑鍚€俿tart鏂规硶銆傦紙鍚姩鍚庯紝灏卞浜嗕竴鏉℃墽琛岃矾寰勶級
start鏂规硶锛?锛夈€佸惎鍔ㄤ簡绾跨▼锛?锛夈€佽jvm璋冪敤浜唕un鏂规硶銆?br>
鍒涘缓绾跨▼鐨勭涓€绉嶆柟寮忥細
缁ф壙Thread 锛岀敱瀛愮被澶嶅啓run鏂规硶銆?br>姝ラ锛?br>1锛屽畾涔夌被缁ф壙Thread绫伙紱2锛岀洰鐨勬槸澶嶅啓run鏂规硶锛屽皢瑕佽绾跨▼杩愯鐨勪唬鐮侀兘瀛樺偍鍒皉un鏂规硶涓紱
3锛岄€氳繃鍒涘缓Thread绫荤殑瀛愮被瀵硅薄锛屽垱寤虹嚎绋嬪璞★紱
4锛岃皟鐢ㄧ嚎绋嬬殑start鏂规硶锛屽紑鍚嚎绋嬶紝骞舵墽琛宺un鏂规硶銆?br>
绾跨▼鐘舵€侊細
琚垱寤猴細start()
杩愯锛氬叿澶囨墽琛岃祫鏍硷紝鍚屾椂鍏峰鎵ц鏉冿紱
鍐荤粨锛歴leep(time),wait()鈥攏otify()鍞ら啋锛涚嚎绋嬮噴鏀句簡鎵ц鏉冿紝鍚屾椂閲婃斁鎵ц璧勬牸锛?br>涓存椂闃诲鐘舵€侊細绾跨▼鍏峰cpu鐨勬墽琛岃祫鏍硷紝娌℃湁cpu鐨勬墽琛屾潈锛?br>娑堜骸锛歴top()
鍒涘缓绾跨▼鐨勭浜岀鏂瑰紡锛?/h3>
瀹炵幇涓€涓帴鍙unnable銆?br>姝ラ锛?br>1锛屽畾涔夌被瀹炵幇Runnable鎺ュ彛銆?br>2锛岃鐩栨帴鍙d腑鐨剅un鏂规硶锛堢敤浜庡皝瑁呯嚎绋嬭杩愯鐨勪唬鐮侊級銆?br>3锛岄€氳繃Thread绫诲垱寤虹嚎绋嬪璞★紱
4锛屽皢瀹炵幇浜哛unnable鎺ュ彛鐨勫瓙绫诲璞′綔涓哄疄闄呭弬鏁颁紶閫掔粰Thread绫讳腑鐨勬瀯閫犲嚱鏁般€?br>涓轰粈涔堣浼犻€掑憿锛熷洜涓鸿璁╃嚎绋嬪璞℃槑纭杩愯鐨剅un鏂规硶鎵€灞炵殑瀵硅薄銆?br>5锛岃皟鐢═hread瀵硅薄鐨剆tart鏂规硶銆傚紑鍚嚎绋嬶紝骞惰繍琛孯unnable鎺ュ彛瀛愮被涓殑run鏂规硶銆?br>Ticket t = new Ticket();
/*
鐩存帴鍒涘缓Ticket瀵硅薄锛屽苟涓嶆槸鍒涘缓绾跨▼瀵硅薄銆?br> 鍥犱负鍒涘缓瀵硅薄鍙兘閫氳繃new Thread绫伙紝鎴栬€卬ew Thread绫荤殑瀛愮被鎵嶅彲浠ャ€?br> 鎵€浠ユ渶缁堟兂瑕佸垱寤虹嚎绋嬨€傛棦鐒舵病鏈変簡Thread绫荤殑瀛愮被锛屽氨鍙兘鐢═hread绫汇€?br> */
Thread t1 = new Thread(t); //鍒涘缓绾跨▼銆?br> /*
鍙灏唗浣滀负Thread绫荤殑鏋勯€犲嚱鏁扮殑瀹為檯鍙傛暟浼犲叆鍗冲彲瀹屾垚绾跨▼瀵硅薄鍜宼涔嬮棿鐨勫叧鑱?br> 涓轰粈涔堣灏唗浼犵粰Thread绫荤殑鏋勯€犲嚱鏁板憿锛熷叾瀹炲氨鏄负浜嗘槑纭嚎绋嬭杩愯鐨勪唬鐮乺un鏂规硶銆?br> */
t1.start();
涓轰粈涔堣鏈塕unnable鎺ュ彛鐨勫嚭鐜帮紵
1锛氶€氳繃缁ф壙Thread绫荤殑鏂瑰紡锛屽彲浠ュ畬鎴愬绾跨▼鐨勫缓绔嬨€備絾鏄繖绉嶆柟寮忔湁涓€涓眬闄愭€э紝濡傛灉涓€涓被宸茬粡鏈変簡鑷繁鐨勭埗绫伙紝灏变笉鍙互缁ф壙Thread绫伙紝鍥犱负java鍗曠户鎵跨殑灞€闄愭€с€?br>鍙槸璇ョ被涓殑杩樻湁閮ㄥ垎浠g爜闇€瑕佽澶氫釜绾跨▼鍚屾椂鎵ц銆傝繖鏃舵€庝箞鍔炲憿锛?br>鍙湁瀵硅绫昏繘琛岄澶栫殑鍔熻兘鎵╁睍锛宩ava灏辨彁渚涗簡涓€涓帴鍙unnable銆傝繖涓帴鍙d腑瀹氫箟浜唕un鏂规硶锛屽叾瀹瀝un鏂规硶鐨勫畾涔夊氨鏄负浜嗗瓨鍌ㄥ绾跨▼瑕佽繍琛岀殑浠g爜銆?br>鎵€浠ワ紝閫氬父鍒涘缓绾跨▼閮界敤绗簩绉嶆柟寮忋€?br>鍥犱负瀹炵幇Runnable鎺ュ彛鍙互閬垮厤鍗曠户鎵跨殑灞€闄愭€с€?br>
2锛氬叾瀹炴槸灏嗕笉鍚岀被涓渶瑕佽澶氱嚎绋嬫墽琛岀殑浠g爜杩涜鎶藉彇銆傚皢澶氱嚎绋嬭杩愯鐨勪唬鐮佺殑浣嶇疆鍗曠嫭瀹氫箟鍒版帴鍙d腑銆備负鍏朵粬绫昏繘琛屽姛鑳芥墿灞曟彁渚涗簡鍓嶆彁銆?br>鎵€浠hread绫诲湪鎻忚堪绾跨▼鏃讹紝鍐呴儴瀹氫箟鐨剅un鏂规硶锛屼篃鏉ヨ嚜浜嶳unnable鎺ュ彛銆?br>
瀹炵幇Runnable鎺ュ彛鍙互閬垮厤鍗曠户鎵跨殑灞€闄愭€с€傝€屼笖锛岀户鎵縏hread锛屾槸鍙互瀵筎hread绫讳腑鐨勬柟娉曪紝杩涜瀛愮被澶嶅啓鐨勩€備絾鏄笉闇€瑕佸仛杩欎釜澶嶅啓鍔ㄤ綔鐨勮瘽锛屽彧涓哄畾涔夌嚎绋嬩唬鐮佸瓨鏀句綅缃紝瀹炵幇Runnable鎺ュ彛鏇存柟渚夸竴浜涖€傛墍浠unnable鎺ュ彛灏嗙嚎绋嬭鎵ц鐨勪换鍔″皝瑁呮垚浜嗗璞°€?br>-------------------------------------------------------
//闈㈣瘯
new Thread(new Runnable(){ //鍖垮悕
public void run(){
System.out.println("runnable run");
}
})
{
public void run(){
System.out.println("subthread run");
}
}.start(); //缁撴灉锛歴ubthread run
---------------------------------------------------------
Try {
Thread.sleep(10);
}catch(InterruptedException e){}// 褰撳埢鎰忚绾跨▼绋嶅井鍋滀竴涓嬶紝妯℃嫙cpu 鍒囨崲鎯呭喌銆?br>
澶氱嚎绋嬪畨鍏ㄩ棶棰樼殑鍘熷洜锛?br>閫氳繃鍥捐В锛氬彂鐜颁竴涓嚎绋嬪湪鎵ц澶氭潯璇彞鏃讹紝骞惰繍绠楀悓涓€涓暟鎹椂锛屽湪鎵ц杩囩▼涓紝鍏朵粬绾跨▼鍙備笌杩涙潵锛屽苟鎿嶄綔浜嗚繖涓暟鎹€傚鑷村埌浜嗛敊璇暟鎹殑浜х敓銆?br>
娑夊強鍒颁袱涓洜绱狅細
1锛屽涓嚎绋嬪湪鎿嶄綔鍏变韩鏁版嵁銆?br>2锛屾湁澶氭潯璇彞瀵瑰叡浜暟鎹繘琛岃繍绠椼€?br>鍘熷洜锛氳繖澶氭潯璇彞锛屽湪鏌愪竴涓椂鍒昏涓€涓嚎绋嬫墽琛屾椂锛岃繕娌℃湁鎵ц瀹岋紝灏辫鍏朵粬绾跨▼鎵ц浜嗐€?br>
瑙e喅瀹夊叏闂鐨勫師鐞嗭細
鍙灏嗘搷浣滃叡浜暟鎹殑璇彞鍦ㄦ煇涓€鏃舵璁╀竴涓嚎绋嬫墽琛屽畬锛屽湪鎵ц杩囩▼涓紝鍏朵粬绾跨▼涓嶈兘杩涙潵鎵ц灏卞彲浠ヨВ鍐宠繖涓棶棰樸€?br>
濡備綍杩涜澶氬彞鎿嶄綔鍏变韩鏁版嵁浠g爜鐨勫皝瑁呭憿锛?br>java涓彁渚涗簡涓€涓В鍐虫柟寮忥細灏辨槸鍚屾浠g爜鍧椼€?br>鏍煎紡锛?br>synchronized(瀵硅薄) { // 浠绘剰瀵硅薄閮藉彲浠ャ€傝繖涓璞″氨鏄攣銆?br> 闇€瑕佽鍚屾鐨勪唬鐮侊紱
}
---------------------------------------------------------------
鍚屾锛氣槄鈽呪槄鈽呪槄
濂藉锛氳В鍐充簡绾跨▼瀹夊叏闂銆?br>寮婄锛氱浉瀵归檷浣庢€ц兘锛屽洜涓哄垽鏂攣闇€瑕佹秷鑰楄祫婧愶紝浜х敓浜嗘閿併€?br>
瀹氫箟鍚屾鏄湁鍓嶆彁鐨勶細
1锛屽繀椤昏鏈変袱涓垨鑰呬袱涓互涓婄殑绾跨▼锛屾墠闇€瑕佸悓姝ャ€?br>2锛屽涓嚎绋嬪繀椤讳繚璇佷娇鐢ㄧ殑鏄悓涓€涓攣銆?br>
鍚屾鐨勭浜岀琛ㄧ幇褰㈠紡锛?br>鍚屾鍑芥暟锛氬叾瀹炲氨鏄皢鍚屾鍏抽敭瀛楀畾涔夊湪鍑芥暟涓婏紝璁╁嚱鏁板叿澶囦簡鍚屾鎬с€?br>
鍚屾鍑芥暟鏄敤鐨勫摢涓攣鍛紵
閫氳繃楠岃瘉锛屽嚱鏁伴兘鏈夎嚜宸辨墍灞炵殑瀵硅薄this锛屾墍浠ュ悓姝ュ嚱鏁版墍浣跨敤鐨勯攣灏辨槸this閿併€?br>
褰撳悓姝ュ嚱鏁拌static淇グ鏃讹紝杩欐椂鐨勫悓姝ョ敤鐨勬槸鍝釜閿佸憿锛?br>闈欐€佸嚱鏁板湪鍔犺浇鏃舵墍灞炰簬绫伙紝杩欐椂鏈夊彲鑳借繕娌℃湁璇ョ被浜х敓鐨勫璞★紝浣嗘槸璇ョ被鐨勫瓧鑺傜爜鏂囦欢鍔犺浇杩涘唴瀛樺氨宸茬粡琚皝瑁呮垚浜嗗璞★紝杩欎釜瀵硅薄灏辨槸璇ョ被鐨勫瓧鑺傜爜鏂囦欢瀵硅薄銆?br>鎵€浠ラ潤鎬佸姞杞芥椂锛屽彧鏈変竴涓璞″瓨鍦紝閭d箞闈欐€佸悓姝ュ嚱鏁板氨浣跨敤鐨勮繖涓璞°€?br>杩欎釜瀵硅薄灏辨槸 绫诲悕.class
鍚屾浠g爜鍧楀拰鍚屾鍑芥暟鐨勫尯鍒紵
鍚屾浠g爜鍧椾娇鐢ㄧ殑閿佸彲浠ユ槸浠绘剰瀵硅薄銆?br>鍚屾鍑芥暟浣跨敤鐨勯攣鏄痶his锛岄潤鎬佸悓姝ュ嚱鏁扮殑閿佹槸璇ョ被鐨勫瓧鑺傜爜鏂囦欢瀵硅薄銆?br>
鍦ㄤ竴涓被涓彧鏈変竴涓悓姝ワ紝鍙互浣跨敤鍚屾鍑芥暟銆傚鏋滄湁澶氬悓姝ワ紝蹇呴』浣跨敤鍚屾浠g爜鍧楋紝鏉ョ‘瀹氫笉鍚岀殑閿併€傛墍浠ュ悓姝ヤ唬鐮佸潡鐩稿鐏垫椿涓€浜涖€?br>-------------------------------------------------------
鈽呰€冪偣闂锛?br>璇峰啓涓€涓欢杩熷姞杞界殑鍗曚緥妯″紡锛熷啓鎳掓眽寮忥紱褰撳嚭鐜板绾跨▼璁块棶鏃舵€庝箞瑙e喅锛熷姞鍚屾锛岃В鍐冲畨鍏ㄩ棶棰橈紱鏁堢巼楂樺悧锛熶笉楂橈紱鎬庢牱瑙e喅锛熼€氳繃鍙岄噸鍒ゆ柇鐨勫舰寮忚В鍐炽€?br>//鎳掓眽寮忥細寤惰繜鍔犺浇鏂瑰紡銆?br>褰撳绾跨▼璁块棶鎳掓眽寮忔椂锛屽洜涓烘噿姹夊紡鐨勬柟娉曞唴瀵瑰叡鎬ф暟鎹繘琛屽鏉¤鍙ョ殑鎿嶄綔銆傛墍浠ュ鏄撳嚭鐜扮嚎绋嬪畨鍏ㄩ棶棰樸€備负浜嗚В鍐筹紝鍔犲叆鍚屾鏈哄埗锛岃В鍐冲畨鍏ㄩ棶棰樸€備絾鏄嵈甯︽潵浜嗘晥鐜囬檷浣庛€?br>涓轰簡鏁堢巼闂锛岄€氳繃鍙岄噸鍒ゆ柇鐨勫舰寮忚В鍐炽€?br>class Single{
private static Single s = null;
private Single(){}
public static Single getInstance(){ //閿佹槸璋侊紵瀛楄妭鐮佹枃浠跺璞★紱
if(s == null){
synchronized(Single.class){
if(s == null)
s = new Single();
}
}
return s;
}
}
---------------------------------------------------------
鍚屾姝婚攣锛氶€氬父鍙灏嗗悓姝ヨ繘琛屽祵濂楋紝灏卞彲浠ョ湅鍒扮幇璞°€傚悓姝ュ嚱鏁颁腑鏈夊悓姝ヤ唬鐮佸潡锛屽悓姝ヤ唬鐮佸潡涓繕鏈夊悓姝ュ嚱鏁般€?br>
绾跨▼闂撮€氫俊锛?/h2>
绛夊緟鍞ら啋鏈哄埗锛氭秹鍙婄殑鏂规硶锛?br>wait:灏嗗悓姝ヤ腑鐨勭嚎绋嬪浜庡喕缁撶姸鎬併€傞噴鏀句簡鎵ц鏉冿紝閲婃斁浜嗚祫鏍笺€傚悓鏃跺皢绾跨▼瀵硅薄瀛樺偍鍒扮嚎绋嬫睜涓€?br>notify锛氬敜閱掔嚎绋嬫睜涓煇涓€涓瓑寰呯嚎绋嬨€?br>notifyAll:鍞ら啋鐨勬槸绾跨▼姹犱腑鐨勬墍鏈夌嚎绋嬨€?br>
娉ㄦ剰锛?br>1锛氳繖浜涙柟娉曢兘闇€瑕佸畾涔夊湪鍚屾涓€?br>2锛氬洜涓鸿繖浜涙柟娉曞繀椤昏鏍囩ず鎵€灞炵殑閿併€?br> 浣犺鐭ラ亾 A閿佷笂鐨勭嚎绋嬭wait浜?閭h繖涓嚎绋嬪氨鐩稿綋浜庡浜嶢閿佺殑绾跨▼姹犱腑锛屽彧鑳紸閿佺殑notify鍞ら啋銆?br>3锛氳繖涓変釜鏂规硶閮藉畾涔夊湪Object绫讳腑銆備负浠€涔堟搷浣滅嚎绋嬬殑鏂规硶瀹氫箟鍦∣bject绫讳腑锛?br> 鍥犱负杩欎笁涓柟娉曢兘闇€瑕佸畾涔夊悓姝ュ唴锛屽苟鏍囩ず鎵€灞炵殑鍚屾閿侊紝鏃㈢劧琚攣璋冪敤锛岃€岄攣鍙堝彲浠ユ槸浠绘剰瀵硅薄锛岄偅涔堣兘琚换鎰忓璞¤皟鐢ㄧ殑鏂规硶涓€瀹氬畾涔夊湪Object绫讳腑銆?br>
wait鍜宻leep鍖哄埆锛?鍒嗘瀽杩欎袱涓柟娉曪細浠庢墽琛屾潈鍜岄攣涓婃潵鍒嗘瀽锛?br>wait锛氬彲浠ユ寚瀹氭椂闂翠篃鍙互涓嶆寚瀹氭椂闂淬€備笉鎸囧畾鏃堕棿锛屽彧鑳界敱瀵瑰簲鐨刵otify鎴栬€卬otifyAll鏉ュ敜閱掋€?br>sleep锛氬繀椤绘寚瀹氭椂闂达紝鏃堕棿鍒拌嚜鍔ㄤ粠鍐荤粨鐘舵€佽浆鎴愯繍琛岀姸鎬?涓存椂闃诲鐘舵€?銆?br>wait锛氱嚎绋嬩細閲婃斁鎵ц鏉冿紝鑰屼笖绾跨▼浼氶噴鏀鹃攣銆?br>Sleep锛氱嚎绋嬩細閲婃斁鎵ц鏉冿紝浣嗘槸涓嶉噴鏀鹃攣銆?br>
绾跨▼鐨勫仠姝細閫氳繃stop鏂规硶灏卞彲浠ュ仠姝㈢嚎绋嬨€備絾鏄繖涓柟寮忚繃鏃朵簡銆?br>鍋滄绾跨▼锛氬師鐞嗗氨鏄細璁╃嚎绋嬭繍琛岀殑浠g爜缁撴潫锛屼篃灏辨槸缁撴潫run鏂规硶銆?br>鎬庝箞缁撴潫run鏂规硶锛熶竴鑸瑀un鏂规硶閲岃偗瀹氬畾涔夊惊鐜€傛墍浠ュ彧瑕佺粨鏉熷惊鐜嵆鍙€?br>绗竴绉嶆柟寮忥細瀹氫箟寰幆鐨勭粨鏉熸爣璁般€?br>绗簩绉嶆柟寮忥細濡傛灉绾跨▼澶勪簬浜嗗喕缁撶姸鎬侊紝鏄笉鍙兘璇诲埌鏍囪鐨勶紝杩欐椂灏遍渶瑕侀€氳繃Thread绫讳腑鐨刬nterrupt鏂规硶锛屽皢鍏跺喕缁撶姸鎬佸己鍒舵竻闄ゃ€傝绾跨▼鎭㈠鍏峰鎵ц璧勬牸鐨勭姸鎬侊紝璁╃嚎绋嬪彲浠ヨ鍒版爣璁帮紝骞剁粨鏉熴€?br>
---------< java.lang.Thread >----------
interrupt()锛氫腑鏂嚎绋嬨€?br>setPriority(int newPriority)锛氭洿鏀圭嚎绋嬬殑浼樺厛绾с€?br>getPriority()锛氳繑鍥炵嚎绋嬬殑浼樺厛绾с€?br>toString()锛氳繑鍥炶绾跨▼鐨勫瓧绗︿覆琛ㄧず褰㈠紡锛屽寘鎷嚎绋嬪悕绉般€佷紭鍏堢骇鍜岀嚎绋嬬粍銆?br>Thread.yield()锛氭殏鍋滃綋鍓嶆鍦ㄦ墽琛岀殑绾跨▼瀵硅薄锛屽苟鎵ц鍏朵粬绾跨▼銆?br>setDaemon(true)锛氬皢璇ョ嚎绋嬫爣璁颁负瀹堟姢绾跨▼鎴栫敤鎴风嚎绋嬨€傚皢璇ョ嚎绋嬫爣璁颁负瀹堟姢绾跨▼鎴栫敤鎴风嚎绋嬨€傚綋姝e湪杩愯鐨勭嚎绋嬮兘鏄畧鎶ょ嚎绋嬫椂锛孞ava 铏氭嫙鏈洪€€鍑恒€傝鏂规硶蹇呴』鍦ㄥ惎鍔ㄧ嚎绋嬪墠璋冪敤銆?br>join锛氫复鏃跺姞鍏ヤ竴涓嚎绋嬬殑鏃跺€欏彲浠ヤ娇鐢╦oin鏂规硶銆?br>褰揂绾跨▼鎵ц鍒颁簡B绾跨▼鐨刯oin鏂瑰紡銆侫绾跨▼澶勪簬鍐荤粨鐘舵€侊紝閲婃斁浜嗘墽琛屾潈锛孊寮€濮嬫墽琛屻€侫浠€涔堟椂鍊欐墽琛屽憿锛熷彧鏈夊綋B绾跨▼杩愯缁撴潫鍚庯紝A鎵嶄粠鍐荤粨鐘舵€佹仮澶嶈繍琛岀姸鎬佹墽琛屻€?br>-----------------------------------------------------------Lock鎺ュ彛锛氬绾跨▼鍦↗DK1.5鐗堟湰鍗囩骇鏃讹紝鎺ㄥ嚭涓€涓帴鍙ock鎺ュ彛銆?br>瑙e喅绾跨▼瀹夊叏闂浣跨敤鍚屾鐨勫舰寮忥紝(鍚屾浠g爜鍧楋紝瑕佷箞鍚屾鍑芥暟)鍏跺疄鏈€缁堜娇鐢ㄧ殑閮芥槸閿佹満鍒躲€?br>
鍒颁簡鍚庢湡鐗堟湰锛岀洿鎺ュ皢閿佸皝瑁呮垚浜嗗璞°€傜嚎绋嬭繘鍏ュ悓姝ュ氨鏄叿澶囦簡閿侊紝鎵ц瀹岋紝绂诲紑鍚屾锛屽氨鏄噴鏀句簡閿併€?br>鍦ㄥ悗鏈熷閿佺殑鍒嗘瀽杩囩▼涓紝鍙戠幇锛岃幏鍙栭攣锛屾垨鑰呴噴鏀鹃攣鐨勫姩浣滃簲璇ユ槸閿佽繖涓簨鐗╂洿娓呮銆傛墍浠ュ皢杩欎簺鍔ㄤ綔瀹氫箟鍦ㄤ簡閿佸綋涓紝骞舵妸閿佸畾涔夋垚瀵硅薄銆?br>
鎵€浠ュ悓姝ユ槸闅愮ず鐨勯攣鎿嶄綔锛岃€孡ock瀵硅薄鏄樉绀虹殑閿佹搷浣滐紝瀹冪殑鍑虹幇灏辨浛浠d簡鍚屾銆?br>
鍦ㄤ箣鍓嶇殑鐗堟湰涓娇鐢∣bject绫讳腑wait銆乶otify銆乶otifyAll鐨勬柟寮忔潵瀹屾垚鐨勩€傞偅鏄洜涓哄悓姝ヤ腑鐨勯攣鏄换鎰忓璞★紝鎵€浠ユ搷浣滈攣鐨勭瓑寰呭敜閱掔殑鏂规硶閮藉畾涔夊湪Object绫讳腑銆?br>
鑰岀幇鍦ㄩ攣鏄寚瀹氬璞ock銆傛墍浠ユ煡鎵剧瓑寰呭敜閱掓満鍒舵柟寮忛渶瑕侀€氳繃Lock鎺ュ彛鏉ュ畬鎴愩€傝€孡ock鎺ュ彛涓苟娌℃湁鐩存帴鎿嶄綔绛夊緟鍞ら啋鐨勬柟娉曪紝鑰屾槸灏嗚繖浜涙柟寮忓張鍗曠嫭灏佽鍒颁簡涓€涓璞′腑銆傝繖涓璞″氨鏄疌ondition锛屽皢Object涓殑涓変釜鏂规硶杩涜鍗曠嫭鐨勫皝瑁呫€傚苟鎻愪緵浜嗗姛鑳戒竴鑷寸殑鏂规硶 await()銆乻ignal()銆乻ignalAll()浣撶幇鏂扮増鏈璞$殑濂藉銆?br>< java.util.concurrent.locks > Condition鎺ュ彛锛歛wait()銆乻ignal()銆乻ignalAll()锛?br>--------------------------------------------------------
class BoundedBuffer {
final Lock lock = new ReentrantLock();
final Condition notFull = lock.newCondition();
final Condition notEmpty = lock.newCondition();
final Object[] items = new Object[100];
int putptr, takeptr, count;
public void put(Object x) throws InterruptedException {
lock.lock();
try {
while (count == items.length)
notFull.await();
items[putptr] = x;
if (++putptr == items.length) putptr = 0;
++count;
notEmpty.signal();
}
finally {
lock.unlock();
}
}
public Object take() throws InterruptedException {
lock.lock();
try {
while (count == 0)
notEmpty.await();
Object x = items[takeptr];
if (++takeptr == items.length) takeptr = 0;
--count;
notFull.signal();
return x;
}
finally {
lock.unlock();
}
}
}
------------------------------------------------------------------------------------------------
API锛?/h1>
锛圓pplication Programming Interface,搴旂敤绋嬪簭缂栫▼鎺ュ彛锛夋槸涓€浜涢鍏堝畾涔夌殑鍑芥暟锛岀洰鐨勬槸鎻愪緵搴旂敤绋嬪簭涓庡紑鍙戜汉鍛樺熀浜庢煇杞欢鎴栫‖浠剁殑浠ヨ闂竴缁勪緥绋嬬殑鑳藉姏锛岃€屽張鏃犻渶璁块棶婧愮爜锛屾垨鐞嗚В鍐呴儴宸ヤ綔鏈哄埗鐨勭粏鑺傘€?br>
--< java.lang >-- String瀛楃涓诧細鈽呪槄鈽呪槅
java涓敤String绫昏繘琛屾弿杩般€傚瀛楃涓茶繘琛屼簡瀵硅薄鐨勫皝瑁呫€傝繖鏍风殑濂藉鏄彲浠ュ瀛楃涓茶繖绉嶅父瑙佹暟鎹繘琛屾柟渚跨殑鎿嶄綔銆傚璞″皝瑁呭悗锛屽彲浠ュ畾涔塏澶氬睘鎬у拰琛屼负銆?br>
濡備綍瀹氫箟瀛楃涓插璞″憿锛烻tring s = "abc";鍙鏄弻寮曞彿寮曡捣鐨勬暟鎹兘鏄瓧绗︿覆瀵硅薄銆?br>
鐗圭偣锛氬瓧绗︿覆涓€鏃﹁鍒濆鍖栵紝灏变笉鍙互琚敼鍙橈紝瀛樻斁鍦ㄦ柟娉曞尯涓殑甯搁噺姹犱腑銆?br>------------------------------------------------------
String s1 = "abc"; // s1鎸囧悜鐨勫唴瀛樹腑鍙湁涓€涓璞bc銆?br>String s2 = new String("abc"); // s2鎸囧悜鐨勫唴瀹逛腑鏈変袱涓璞bc銆乶ew 銆?br>
System.out.println(s1==s2);//false
System.out.println(s1.equals(s2));//true 锛屽瓧绗︿覆涓璭quals姣旇緝鐨勬槸瀛楃涓插唴瀹规槸鍚︾浉鍚屻€?br>-------------------------------------------------------
瀛楃涓茬殑鏂规硶锛?br>
1锛氭瀯閫犳柟娉曪細
灏嗗瓧鑺傛暟缁勬垨鑰呭瓧绗︽暟缁勮浆鎴愬瓧绗︿覆銆?br>String s1 = new String();//鍒涘缓浜嗕竴涓┖鍐呭鐨勫瓧绗︿覆銆?br>String s2 = null;//s2娌℃湁浠讳綍瀵硅薄鎸囧悜锛屾槸涓€涓猲ull甯搁噺鍊笺€?br>String s3 = "";//s3鎸囧悜涓€涓叿浣撶殑瀛楃涓插璞★紝鍙笉杩囪繖涓瓧绗︿覆涓病鏈夊唴瀹广€?br>//涓€鑸湪瀹氫箟瀛楃涓叉椂锛屼笉鐢╪ew銆?br>String s4 = new String("abc");
String s5 = "abc"; 涓€鑸敤姝ゅ啓娉?br>new String(char[]);//灏嗗瓧绗︽暟缁勮浆鎴愬瓧绗︿覆銆?br>new String(char[],offset,count);//灏嗗瓧绗︽暟缁勪腑鐨勪竴閮ㄥ垎杞垚瀛楃涓层€?br>
2锛氫竴鑸柟娉曪細
鎸夌収闈㈠悜瀵硅薄鐨勬€濇兂锛?br>2.1 鑾峰彇锛?br> 2.1.1锛氳幏鍙栧瓧绗︿覆鐨勯暱搴︺€俵ength();
2.1.2锛氭寚瀹氫綅缃殑瀛楃銆俢har charAt(int index);
2.1.3锛氳幏鍙栨寚瀹氬瓧绗︾殑浣嶇疆銆傚鏋滀笉瀛樺湪杩斿洖-1锛屾墍浠ュ彲浠ラ€氳繃杩斿洖鍊?1鏉ュ垽鏂煇涓€涓瓧绗︿笉瀛樺湪鐨勬儏鍐点€?br> int indexOf(int ch);//杩斿洖绗竴娆℃壘鍒扮殑瀛楃瑙掓爣
int indexOf(int ch,int fromIndex); //杩斿洖浠庢寚瀹氫綅缃紑濮嬬涓€娆℃壘鍒扮殑瑙掓爣
int indexOf(String str); //杩斿洖绗竴娆℃壘鍒扮殑瀛楃涓茶鏍?br> int indexOf(String str,int fromIndex);
int lastIndexOf(int ch);
int lastIndexOf(int ch,int fromIndex);
int lastIndexOf(String str);
int lastIndexOf(String str,int fromIndex);
2.1.4锛氳幏鍙栧瓙涓层€?br> String substring(int start);//浠巗tart浣嶅紑濮嬶紝鍒發ength()-1涓烘.
String substring(int start,int end);//浠巗tart寮€濮嬪埌end涓烘銆?/鍖呭惈start浣嶏紝涓嶅寘鍚玡nd浣嶃€?br> substring(0,str.length());//鑾峰彇鏁翠覆
2.2 鍒ゆ柇锛?br> 2.2.1锛氬瓧绗︿覆涓寘鍚寚瀹氱殑瀛楃涓插悧锛?br> boolean contains(String substring);
2.2.2锛氬瓧绗︿覆鏄惁浠ユ寚瀹氬瓧绗︿覆寮€澶村晩锛?br> boolean startsWith(string);
2.2.3锛氬瓧绗︿覆鏄惁浠ユ寚瀹氬瓧绗︿覆缁撳熬鍟婏紵
boolean endsWith(string);
2.2.4锛氬垽鏂瓧绗︿覆鏄惁鐩稿悓
boolean equals(string);//瑕嗙洊浜哋bject涓殑鏂规硶锛屽垽鏂瓧绗︿覆鍐呭鏄惁鐩稿悓銆?br> 2.2.5锛氬垽鏂瓧绗︿覆鍐呭鏄惁鐩稿悓锛屽拷鐣ュぇ灏忓啓銆?br> boolean equalsIgnoreCase(string) ;
2.3 杞崲锛?br> 2.3.1锛氶€氳繃鏋勯€犲嚱鏁板彲浠ュ皢瀛楃鏁扮粍鎴栬€呭瓧鑺傛暟缁勮浆鎴愬瓧绗︿覆銆?br> 2.3.2锛氬彲浠ラ€氳繃瀛楃涓蹭腑鐨勯潤鎬佹柟娉曪紝灏嗗瓧绗︽暟缁勮浆鎴愬瓧绗︿覆銆?br> static String copyValueOf(char[] );
static String copyValueOf(char[],int offset,int count);
static String valueOf(char[]);
static String valueOf(char[],int offset,int count);
2.3.3锛氬皢鍩烘湰鏁版嵁绫诲瀷鎴栬€呭璞¤浆鎴愬瓧绗︿覆銆?br> static String valueOf(char);
static String valueOf(boolean);
static String valueOf(double);
static String valueOf(float);
static String valueOf(int);
static String valueOf(long);
static String valueOf(Object);
2.3.4锛氬皢瀛楃涓茶浆鎴愬ぇ灏忓啓銆?br> String toLowerCase();
String toUpperCase();
2.3.5锛氬皢瀛楃涓茶浆鎴愭暟缁勩€?br> char[] toCharArray();//杞垚瀛楃鏁扮粍銆?br> byte[] getBytes();//鍙互鍔犲叆缂栫爜琛ㄣ€傝浆鎴愬瓧鑺傛暟缁勩€?br> 2.3.6锛氬皢瀛楃涓茶浆鎴愬瓧绗︿覆鏁扮粍銆傚垏鍓叉柟娉曘€?br> String[] split(鍒嗗壊鐨勮鍒?瀛楃涓?;
2.3.7锛氬皢瀛楃涓茶繘琛屽唴瀹规浛鎹€傛敞鎰忥細淇敼鍚庡彉鎴愭柊瀛楃涓诧紝骞朵笉鏄皢鍘熷瓧绗︿覆鐩存帴淇敼銆?br> String replace(oldChar,newChar);
String replace(oldstring,newstring);
2.3.8锛?String concat(string); //瀵瑰瓧绗︿覆杩涜杩藉姞銆?br> String trim();//鍘婚櫎瀛楃涓蹭袱绔殑绌烘牸
int compareTo();//濡傛灉鍙傛暟瀛楃涓茬瓑浜庢瀛楃涓诧紝鍒欒繑鍥炲€?0锛涘鏋滄瀛楃涓叉寜瀛楀吀椤哄簭灏忎簬瀛楃涓插弬鏁帮紝鍒欒繑鍥炰竴涓皬浜?0 鐨勫€硷紱濡傛灉姝ゅ瓧绗︿覆鎸夊瓧鍏搁『搴忓ぇ浜庡瓧绗︿覆鍙傛暟锛屽垯杩斿洖涓€涓ぇ浜?0 鐨勫€笺€?br>------------------------------------------------------------------------------------------------
--< java.lang >-- StringBuffer瀛楃涓茬紦鍐插尯锛氣槄鈽呪槄鈽?/h3>
鏋勯€犱竴涓叾涓笉甯﹀瓧绗︾殑瀛楃涓茬紦鍐插尯锛屽垵濮嬪閲忎负 16 涓瓧绗︺€?br>鐗圭偣锛?br>1锛氬彲浠ュ瀛楃涓插唴瀹硅繘琛屼慨鏀广€?br>2锛氭槸涓€涓鍣ㄣ€?br>3锛氭槸鍙彉闀垮害鐨勩€?br>4锛氱紦鍐插尯涓彲浠ュ瓨鍌ㄤ换鎰忕被鍨嬬殑鏁版嵁銆?br>5锛氭渶缁堥渶瑕佸彉鎴愬瓧绗︿覆銆?br>
瀹瑰櫒閫氬父鍏峰涓€浜涘浐瀹氱殑鏂规硶锛?br>1锛屾坊鍔犮€?br> StringBuffer append(data):鍦ㄧ紦鍐插尯涓拷鍔犳暟鎹€傝拷鍔犲埌灏鹃儴銆?br> StringBuffer insert(index,data):鍦ㄦ寚瀹氫綅缃彃鍏ユ暟鎹€?br>2锛屽垹闄ゃ€?br> StringBuffer delete(start,end);鍒犻櫎浠巗tart鑷砮nd-1鑼冨洿鐨勫厓绱?br> StringBuffer deleteCharAt(index);鍒犻櫎鎸囧畾浣嶇疆鐨勫厓绱?br>//sb.delete(0,sb.length());//娓呯┖缂撳啿鍖恒€?br>3锛屼慨鏀广€?br> StringBuffer replace(start,end,string);灏唖tart鑷砮nd-1鏇挎崲鎴恠tring
void setCharAt(index,char);鏇挎崲鎸囧畾浣嶇疆鐨勫瓧绗?br> void setLength(len);灏嗗師瀛楃涓茬疆涓烘寚瀹氶暱搴︾殑瀛楃涓?br>4锛屾煡鎵俱€傦紙鏌ヤ笉鍒拌繑鍥?1锛?br> int indexOf(string); 杩斿洖鎸囧畾瀛愬瓧绗︿覆鍦ㄦ瀛楃涓蹭腑绗竴娆″嚭鐜板鐨勭储寮曘€?br> int indexOf(string,int fromIndex);浠庢寚瀹氫綅缃紑濮嬫煡鎵惧瓧绗︿覆
int lastIndexOf(string); 杩斿洖鎸囧畾瀛愬瓧绗︿覆鍦ㄦ瀛楃涓蹭腑鏈€鍙宠竟鍑虹幇澶勭殑绱㈠紩銆?br> int lastIndexOf(string,int fromIndex); 浠庢寚瀹氱殑绱㈠紩寮€濮嬪弽鍚戞悳绱?br>5锛岃幏鍙栧瓙涓层€?br> string substring(start); 杩斿洖start鍒扮粨灏剧殑瀛愪覆
string substring(start,end); 杩斿洖start鑷砮nd-1鐨勫瓙涓?br>6锛屽弽杞€?br> StringBuffer reverse();瀛楃涓插弽杞?br>------------------------------------------------------------------------------------------------
--< java.lang >-- StringBuilder瀛楃涓茬紦鍐插尯锛氣槄鈽呪槄鈽?/h3>
JDK1.5鍑虹幇StringBuiler锛涙瀯閫犱竴涓叾涓笉甯﹀瓧绗︾殑瀛楃涓茬敓鎴愬櫒锛屽垵濮嬪閲忎负 16 涓瓧绗︺€傝绫昏璁捐鐢ㄤ綔 StringBuffer 鐨勪竴涓畝鏄撴浛鎹紝鐢ㄥ湪瀛楃涓茬紦鍐插尯琚崟涓嚎绋嬩娇鐢ㄧ殑鏃跺€欙紙杩欑鎯呭喌寰堟櫘閬嶏級銆?br>鏂规硶鍜孲tringBuffer涓€鏍凤紱
StringBuffer 鍜?StringBuilder 鐨勫尯鍒細
StringBuffer绾跨▼瀹夊叏銆?br>StringBuilder绾跨▼涓嶅畨鍏ㄣ€?br>
鍗曠嚎绋嬫搷浣滐紝浣跨敤StringBuilder 鏁堢巼楂樸€?br>澶氱嚎绋嬫搷浣滐紝浣跨敤StringBuffer 瀹夊叏銆?br>---------------------------------------------------------
StringBuilder sb = new StringBuilder("abcdefg");
sb.append("ak"); //abcdefgak
sb.insert(1,"et");//aetbcdefg
sb.deleteCharAt(2);//abdefg
sb.delete(2,4);//abefg
sb.setLength(4);//abcd
sb.setCharAt(0,鈥榢鈥?;//kbcdefg
sb.replace(0,2,"hhhh");//hhhhcdefg
//鎯宠浣跨敤缂撳啿鍖猴紝鍏堣寤虹珛瀵硅薄銆?br> StringBuffer sb = new StringBuffer();
sb.append(12).append("haha");//鏂规硶璋冪敤閾俱€?br> String s = "abc"+4+鈥榪鈥?
s = new StringBuffer().append("abc").append(4).append(鈥榪鈥?.toString();
---------------------------------------------------------
class Test{
public static void main(String[] args) {
String s1 = "java";
String s2 = "hello";
method_1(s1,s2);
System.out.println(s1+"...."+s2); //java....hello
StringBuilder s11 = new StringBuilder("java");
StringBuilder s22 = new StringBuilder("hello");
method_2(s11,s22);
System.out.println(s11+"-----"+s22); //javahello-----hello
}
public static void method_1(String s1,String s2){
s1.replace(鈥榓鈥?鈥榢鈥?;
s1 = s2;
}
public static void method_2(StringBuilder s1,StringBuilder s2){
s1.append(s2);
s1 = s2;
}
}
---------------------------------------------------------
鍩烘湰鏁版嵁绫诲瀷瀵硅薄鍖呰绫伙細
鏄寜鐓ч潰鍚戝璞℃€濇兂灏嗗熀鏈暟鎹被鍨嬪皝瑁呮垚浜嗗璞°€?br>濂藉锛?br>1锛氬彲浠ラ€氳繃瀵硅薄涓殑灞炴€у拰琛屼负鎿嶄綔鍩烘湰鏁版嵁銆?br>2锛氬彲浠ュ疄鐜板熀鏈暟鎹被鍨嬪拰瀛楃涓蹭箣闂寸殑杞崲銆?br>
鍏抽敭瀛?nbsp; 瀵瑰簲鐨勭被鍚?br>byte Byte
short Short parseShort(numstring);
int Integer 闈欐€佹柟娉曪細parseInt(numstring)
long Long
float Float
double Double
char Character
Boolean Boolean
鍩烘湰鏁版嵁绫诲瀷瀵硅薄鍖呰绫伙細閮芥湁 XXX parseXXX 鏂规硶
鍙湁涓€涓被鍨嬫病鏈塸arse鏂规硶锛欳haracter 锛?br>--------------------------------------------------------
Integer瀵硅薄锛?鈽呪槄鈽呪槅
鏁板瓧鏍煎紡鐨勫瓧绗︿覆杞垚鍩烘湰鏁版嵁绫诲瀷鐨勬柟娉曪細
1锛氬皢璇ュ瓧绗︿覆灏佽鎴愪簡Integer瀵硅薄锛屽苟璋冪敤瀵硅薄鐨勬柟娉昳ntValue();
2锛氫娇鐢↖nteger.parseInt(numstring):涓嶇敤寤虹珛瀵硅薄锛岀洿鎺ョ被鍚嶈皟鐢紱
灏嗗熀鏈被鍨嬭浆鎴愬瓧绗︿覆锛?br>1锛欼nteger涓殑闈欐€佹柟娉?String toString(int);
2锛歩nt+"";
灏嗕竴涓崄杩涘埗鏁存暟杞垚鍏朵粬杩涘埗锛?/h2>
杞垚浜岃繘鍒讹細toBinaryString
杞垚鍏繘鍒讹細toOctalString
杞垚鍗佸叚杩涘埗锛歵oHexString
toString(int num,int radix);
灏嗗叾浠栬繘鍒惰浆鎹㈠崄杩涘埗锛?/h2>
parseInt(string,radix); //灏嗙粰瀹氱殑鏁拌浆鎴愭寚瀹氱殑鍩烘暟杩涘埗锛?br>
鍦╦dk1.5鐗堟湰鍚庯紝瀵瑰熀鏈暟鎹被鍨嬪璞″寘瑁呯被杩涜鍗囩骇銆傚湪鍗囩骇涓紝浣跨敤鍩烘湰鏁版嵁绫诲瀷瀵硅薄鍖呰绫诲彲浠ュ儚浣跨敤鍩烘湰鏁版嵁绫诲瀷涓€鏍凤紝杩涜杩愮畻銆?br> Integer i = new Integer(4); //1.5鐗堟湰涔嬪墠鐨勫啓娉曪紱
Integer i = 4; //鑷姩瑁呯锛?.5鐗堟湰鍚庣殑鍐欐硶锛?br> i = i + 5;
//i瀵硅薄鏄笉鑳界洿鎺ュ拰5鐩稿姞鐨勶紝鍏跺疄搴曞眰鍏堝皢i杞垚int绫诲瀷锛屽湪鍜?鐩稿姞銆傝€岃浆鎴恑nt绫诲瀷鐨勬搷浣滄槸闅愬紡鐨勩€傝嚜鍔ㄦ媶绠憋細鎷嗙鐨勫師鐞嗗氨鏄痠.intValue();i+5杩愮畻瀹屾槸涓€涓猧nt鏁存暟銆傚浣曡祴鍊肩粰寮曠敤绫诲瀷i鍛紵鍏跺疄鏈夊缁撴灉杩涜瑁呯銆?br>Integer c = 127;
Integer d = 127;
System.out.println(c = = d); //true
//鍦ㄨ绠辨椂锛屽鏋滄暟鍊煎湪byte鑼冨洿涔嬪唴锛岄偅涔堟暟鍊肩浉鍚岋紝涓嶄細浜х敓鏂扮殑瀵硅薄锛屼篃灏辨槸璇村涓暟鍊肩浉鍚岀殑寮曠敤鎸囧悜鐨勬槸鍚屼竴涓璞°€?br>------------------------------------------------------------------------------------------------
API--- java.lang.System:
灞炴€у拰琛屼负閮芥槸闈欐€佺殑銆?br>long currentTimeMillis(); // 杩斿洖褰撳墠鏃堕棿姣鍊?br>exit(); // 閫€鍑鸿櫄鎷熸満
Properties getProperties() ; // 鑾峰彇褰撳墠绯荤粺鐨勫睘鎬т俊鎭?br>
Properties prop = System.getProperties(); //鑾峰彇绯荤粺鐨勫睘鎬т俊鎭紝骞跺皢杩欎簺淇℃伅瀛樺偍鍒癙roperties闆嗗悎涓€?
System.setProperty("myname","鍑摜"); //缁欑郴缁熷睘鎬т俊鎭泦娣诲姞鍏蜂綋鐨勫睘鎬т俊鎭?br>//涓存椂璁剧疆鏂瑰紡锛氳繍琛宩vm鏃讹紝鍙互閫氳繃jvm鐨勫弬鏁拌繘琛岀郴缁熷睘鎬х殑涓存椂璁剧疆锛屽彲浠ュ湪java鍛戒护鐨勫悗闈㈠姞鍏?鈥揇<name>=<value> 鐢ㄦ硶锛歫ava 鈥揇myname=灏忔槑 绫诲悕銆?br>
String name = System.getProperty("os.name");//鑾峰彇鎸囧畾灞炴€х殑淇℃伅
//鎯宠鐭ラ亾璇ョ郴缁熸槸鍚︽槸璇ヨ蒋浠舵墍鏀寔鐨勭郴缁熶腑鐨勪竴涓€?br>Set<String> hs = new HashSet<String>();
hs.add("Windows XP");
hs.add("Windows 7");
if(hs.contains(name))
System.out.println("鍙互鏀寔");
else
System.out.println("涓嶆敮鎸?);
--------------------------------------------------------------------------------------------------------------------
API--- java.util.Math:
鐢ㄤ簬鏁板杩愮畻鐨勫伐鍏风被锛屽睘鎬у拰琛屼负閮芥槸闈欐€佺殑銆傝绫绘槸final涓嶅厑璁哥户鎵裤€?br>
static double ceil(double a) ; //杩斿洖澶т簬鎸囧畾鏁板€肩殑鏈€灏忔暣鏁?br>static double floor(double a) ; //杩斿洖灏忎簬鎸囧畾鏁板€肩殑鏈€澶ф暣鏁?br>static long round(double a) ; //鍥涜垗浜斿叆鎴愭暣鏁?br>static double pow(double a, double b) ; //a鐨刡娆″箓
static double random(); //杩斿洖0~1鐨勪吉闅忔満鏁?br>
public static void main(String[] args) {
Random r = new Random();
for(int x=0; x<10; x++) {
//double d = Math.floor(Math.random()*10+1);
//int d = (int)(Math.random()*10+1);
int d = r.nextInt(10)+1;
System.out.println(d);
}
}
--------------------------------------------------------------------------------------------------------------------
API--- java.lang.Runtime:
绫讳腑娌℃湁鏋勯€犳柟娉曪紝涓嶈兘鍒涘缓瀵硅薄銆?br>浣嗘槸鏈夐潪闈欐€佹柟娉曘€傝鏄庤绫讳腑搴旇瀹氫箟濂戒簡瀵硅薄锛屽苟鍙互閫氳繃涓€涓猻tatic鏂规硶鑾峰彇杩欎釜瀵硅薄銆傜敤杩欎釜瀵硅薄鏉ヨ皟鐢ㄩ潪闈欐€佹柟娉曘€傝繖涓柟娉曞氨鏄?static Runtime getRuntime();
杩欎釜Runtime鍏跺疄浣跨敤鍗曚緥璁捐妯″紡杩涜璁捐銆?br>
class RuntimeDemo {
public static void main(String[] args) throws Exception {
Runtime r = Runtime.getRuntime();
Process p = r.exec("notepad.exe SystemDemo.java"); //杩愯鎸囧畾鐨勭▼搴?br> Thread.sleep(4000);
p.destroy(); //鏉€鎺夎繘绋?br> }
}
--------------------------------------------------------------------------------------------------------------------
API--- java.util.Date锛?/h2>
鏃ユ湡绫伙紝鏈堜唤浠?-11锛?br> /*
鏃ユ湡瀵硅薄鍜屾绉掑€间箣闂寸殑杞崲銆?br> 1锛屾棩鏈熷璞¤浆鎴愭绉掑€笺€侱ate绫讳腑鐨刧etTime鏂规硶銆?br> 2锛屽浣曞皢鑾峰彇鍒扮殑姣鍊艰浆鎴愬叿浣撶殑鏃ユ湡鍛紵
Date绫讳腑鐨剆etTime鏂规硶銆備篃鍙互閫氳繃鏋勯€犲嚱鏁般€?
*/
//鏃ユ湡瀵硅薄杞垚姣鍊?br> Date d = new Date();
long time1 = d.getTime();
long time2 = System.currentTimeMillis(); / /姣鍊笺€?br>
//姣鍊艰浆鎴愬叿浣撶殑鏃ユ湡
long time = 1322709921312l;
Date d = new Date();
d.setTime(time);
/*
灏嗘棩鏈熷瓧绗︿覆杞崲鎴愭棩鏈熷璞★細浣跨敤鐨勫氨鏄疍ateFormat鏂规硶涓殑 Date parse(String source) 锛?br> */
public static void method() throws Exception {
String str_time = "2011/10/25";
DateFormat df = new SimpleDateFormat("yyyy/MM/dd"); //SimpleDateFormat浣滀负鍙互鎸囧畾鐢ㄦ埛鑷畾涔夌殑鏍煎紡鏉ュ畬鎴愭牸寮忓寲銆?br> Date d = df.parse(str_time);
}
/*
濡傛灉涓嶉渶瑕佷娇鐢ㄧ壒瀹氱殑鏍煎紡鍖栭鏍硷紝瀹屽叏鍙互浣跨敤DateFormat绫讳腑鐨勯潤鎬佸伐鍘傛柟娉曡幏鍙栧叿浣撶殑宸茬粡灏佽濂介鏍肩殑瀵硅薄銆俫etDateInstance();getDateTimeInstance();
*/
Date d = new Date();
DateFormat df = DateFormat.getDateInstance(DateFormat.LONG);
df = DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.LONG);
String str_time = df.format(d);
//灏嗘棩鏈熷璞¤浆鎹㈡垚瀛楃涓茬殑鏂瑰紡锛欴ateFormat绫讳腑鐨刦ormat鏂规硶銆?br> //鍒涘缓鏃ユ湡鏍煎紡瀵硅薄銆?
DateFormat df = new SimpleDateFormat(); //璇ュ璞$殑寤虹珛鍐呴儴浼氬皝瑁呬竴涓粯璁ょ殑鏃ユ湡鏍煎紡銆?1-12-1 涓嬪崍1:48
//濡傛灉鎯宠鑷畾涔夋棩鏈熸牸寮忕殑璇濄€傚彲浣跨敤SimpleDateFormat鐨勬瀯閫犲嚱鏁般€傚皢鍏蜂綋鐨勬牸寮忎綔涓哄弬鏁颁紶鍏ュ埌鏋勯€犲嚱鏁颁腑銆傚浣曡〃绀烘棩鏈熶腑骞寸殑閮ㄥ垎鍛紵鍙互蹇呴』瑕佸弬涓庢牸寮忓璞℃枃妗c€?
df = new SimpleDateFormat("yyyy骞碝M鏈坉d鏃?HH:mm:ss");
//璋冪敤DateFormat涓殑format鏂规硶銆傚宸叉湁鐨勬棩鏈熷璞¤繘琛屾牸寮忓寲銆?br> String str_time = df.format(d);
--------------------------------------------------------------------------------------------------------------------
API--- java.util. Calendar锛氭棩鍘嗙被
public static void method(){
Calendar c = Calendar.getInstance();
System.out.println(c.get(Calendar.YEAR)+"骞?+(c.get(Calendar.MONTH)+1)+"鏈?
+getNum(c.get(Calendar.DAY_OF_MONTH))+"鏃?
+"鏄熸湡"+getWeek(c.get(Calendar.DAY_OF_WEEK)));
}
public static String getNum(int num){
return num>9 ? num+"" : "0"+num;
}
public static String getWeek(int index){
/*
鏌ヨ〃娉曪細寤虹珛鏁版嵁鐨勫搴斿叧绯?
鏈€濂斤細鏁版嵁涓暟鏄‘瀹氱殑锛岃€屼笖鏈夊搴斿叧绯汇€傚鏋滃搴斿叧绯荤殑涓€鏂癸紝鏄暟瀛楋紝鑰屼笖鍙互浣滀负瑙掓爣锛岄偅涔堝彲浠ラ€氳繃鏁扮粍鏉ヤ綔涓鸿〃銆?br> */
String[] weeks = {"","鏃?,"涓€","浜?,"涓?,"鍥?,"浜?,"鍏?};
return weeks[index];
}
------------------------------------------------------------------------------------------------------------------------------------------------
姝e垯琛ㄨ揪寮忥細鈽呪槄鈽呪槅锛?/h2>
瀹瑰櫒閫氬父鍏峰涓€浜涘浐瀹氱殑鏂规硶锛?br>1锛屾坊鍔犮€?br> StringBuffer append(data):鍦ㄧ紦鍐插尯涓拷鍔犳暟鎹€傝拷鍔犲埌灏鹃儴銆?br> StringBuffer insert(index,data):鍦ㄦ寚瀹氫綅缃彃鍏ユ暟鎹€?br>2锛屽垹闄ゃ€?br> StringBuffer delete(start,end);鍒犻櫎浠巗tart鑷砮nd-1鑼冨洿鐨勫厓绱?br> StringBuffer deleteCharAt(index);鍒犻櫎鎸囧畾浣嶇疆鐨勫厓绱?br>//sb.delete(0,sb.length());//娓呯┖缂撳啿鍖恒€?br>3锛屼慨鏀广€?br> StringBuffer replace(start,end,string);灏唖tart鑷砮nd-1鏇挎崲鎴恠tring
void setCharAt(index,char);鏇挎崲鎸囧畾浣嶇疆鐨勫瓧绗?br> void setLength(len);灏嗗師瀛楃涓茬疆涓烘寚瀹氶暱搴︾殑瀛楃涓?br>4锛屾煡鎵俱€傦紙鏌ヤ笉鍒拌繑鍥?1锛?br> int indexOf(string); 杩斿洖鎸囧畾瀛愬瓧绗︿覆鍦ㄦ瀛楃涓蹭腑绗竴娆″嚭鐜板鐨勭储寮曘€?br> int indexOf(string,int fromIndex);浠庢寚瀹氫綅缃紑濮嬫煡鎵惧瓧绗︿覆
int lastIndexOf(string); 杩斿洖鎸囧畾瀛愬瓧绗︿覆鍦ㄦ瀛楃涓蹭腑鏈€鍙宠竟鍑虹幇澶勭殑绱㈠紩銆?br> int lastIndexOf(string,int fromIndex); 浠庢寚瀹氱殑绱㈠紩寮€濮嬪弽鍚戞悳绱?br>5锛岃幏鍙栧瓙涓层€?br> string substring(start); 杩斿洖start鍒扮粨灏剧殑瀛愪覆
string substring(start,end); 杩斿洖start鑷砮nd-1鐨勫瓙涓?br>6锛屽弽杞€?br> StringBuffer reverse();瀛楃涓插弽杞?br>------------------------------------------------------------------------------------------------
--< java.lang >-- StringBuilder瀛楃涓茬紦鍐插尯锛氣槄鈽呪槄鈽?/h3>
JDK1.5鍑虹幇StringBuiler锛涙瀯閫犱竴涓叾涓笉甯﹀瓧绗︾殑瀛楃涓茬敓鎴愬櫒锛屽垵濮嬪閲忎负 16 涓瓧绗︺€傝绫昏璁捐鐢ㄤ綔 StringBuffer 鐨勪竴涓畝鏄撴浛鎹紝鐢ㄥ湪瀛楃涓茬紦鍐插尯琚崟涓嚎绋嬩娇鐢ㄧ殑鏃跺€欙紙杩欑鎯呭喌寰堟櫘閬嶏級銆?br>鏂规硶鍜孲tringBuffer涓€鏍凤紱
StringBuffer 鍜?StringBuilder 鐨勫尯鍒細
StringBuffer绾跨▼瀹夊叏銆?br>StringBuilder绾跨▼涓嶅畨鍏ㄣ€?br>
鍗曠嚎绋嬫搷浣滐紝浣跨敤StringBuilder 鏁堢巼楂樸€?br>澶氱嚎绋嬫搷浣滐紝浣跨敤StringBuffer 瀹夊叏銆?br>---------------------------------------------------------
StringBuilder sb = new StringBuilder("abcdefg");
sb.append("ak"); //abcdefgak
sb.insert(1,"et");//aetbcdefg
sb.deleteCharAt(2);//abdefg
sb.delete(2,4);//abefg
sb.setLength(4);//abcd
sb.setCharAt(0,鈥榢鈥?;//kbcdefg
sb.replace(0,2,"hhhh");//hhhhcdefg
//鎯宠浣跨敤缂撳啿鍖猴紝鍏堣寤虹珛瀵硅薄銆?br> StringBuffer sb = new StringBuffer();
sb.append(12).append("haha");//鏂规硶璋冪敤閾俱€?br> String s = "abc"+4+鈥榪鈥?
s = new StringBuffer().append("abc").append(4).append(鈥榪鈥?.toString();
---------------------------------------------------------
class Test{
public static void main(String[] args) {
String s1 = "java";
String s2 = "hello";
method_1(s1,s2);
System.out.println(s1+"...."+s2); //java....hello
StringBuilder s11 = new StringBuilder("java");
StringBuilder s22 = new StringBuilder("hello");
method_2(s11,s22);
System.out.println(s11+"-----"+s22); //javahello-----hello
}
public static void method_1(String s1,String s2){
s1.replace(鈥榓鈥?鈥榢鈥?;
s1 = s2;
}
public static void method_2(StringBuilder s1,StringBuilder s2){
s1.append(s2);
s1 = s2;
}
}
---------------------------------------------------------
鍩烘湰鏁版嵁绫诲瀷瀵硅薄鍖呰绫伙細
鏄寜鐓ч潰鍚戝璞℃€濇兂灏嗗熀鏈暟鎹被鍨嬪皝瑁呮垚浜嗗璞°€?br>濂藉锛?br>1锛氬彲浠ラ€氳繃瀵硅薄涓殑灞炴€у拰琛屼负鎿嶄綔鍩烘湰鏁版嵁銆?br>2锛氬彲浠ュ疄鐜板熀鏈暟鎹被鍨嬪拰瀛楃涓蹭箣闂寸殑杞崲銆?br>
鍏抽敭瀛?nbsp; 瀵瑰簲鐨勭被鍚?br>byte Byte
short Short parseShort(numstring);
int Integer 闈欐€佹柟娉曪細parseInt(numstring)
long Long
float Float
double Double
char Character
Boolean Boolean
鍩烘湰鏁版嵁绫诲瀷瀵硅薄鍖呰绫伙細閮芥湁 XXX parseXXX 鏂规硶
鍙湁涓€涓被鍨嬫病鏈塸arse鏂规硶锛欳haracter 锛?br>--------------------------------------------------------
Integer瀵硅薄锛?鈽呪槄鈽呪槅
鏁板瓧鏍煎紡鐨勫瓧绗︿覆杞垚鍩烘湰鏁版嵁绫诲瀷鐨勬柟娉曪細
1锛氬皢璇ュ瓧绗︿覆灏佽鎴愪簡Integer瀵硅薄锛屽苟璋冪敤瀵硅薄鐨勬柟娉昳ntValue();
2锛氫娇鐢↖nteger.parseInt(numstring):涓嶇敤寤虹珛瀵硅薄锛岀洿鎺ョ被鍚嶈皟鐢紱
灏嗗熀鏈被鍨嬭浆鎴愬瓧绗︿覆锛?br>1锛欼nteger涓殑闈欐€佹柟娉?String toString(int);
2锛歩nt+"";
灏嗕竴涓崄杩涘埗鏁存暟杞垚鍏朵粬杩涘埗锛?/h2>
杞垚浜岃繘鍒讹細toBinaryString
杞垚鍏繘鍒讹細toOctalString
杞垚鍗佸叚杩涘埗锛歵oHexString
toString(int num,int radix);
灏嗗叾浠栬繘鍒惰浆鎹㈠崄杩涘埗锛?/h2>
parseInt(string,radix); //灏嗙粰瀹氱殑鏁拌浆鎴愭寚瀹氱殑鍩烘暟杩涘埗锛?br>
鍦╦dk1.5鐗堟湰鍚庯紝瀵瑰熀鏈暟鎹被鍨嬪璞″寘瑁呯被杩涜鍗囩骇銆傚湪鍗囩骇涓紝浣跨敤鍩烘湰鏁版嵁绫诲瀷瀵硅薄鍖呰绫诲彲浠ュ儚浣跨敤鍩烘湰鏁版嵁绫诲瀷涓€鏍凤紝杩涜杩愮畻銆?br> Integer i = new Integer(4); //1.5鐗堟湰涔嬪墠鐨勫啓娉曪紱
Integer i = 4; //鑷姩瑁呯锛?.5鐗堟湰鍚庣殑鍐欐硶锛?br> i = i + 5;
//i瀵硅薄鏄笉鑳界洿鎺ュ拰5鐩稿姞鐨勶紝鍏跺疄搴曞眰鍏堝皢i杞垚int绫诲瀷锛屽湪鍜?鐩稿姞銆傝€岃浆鎴恑nt绫诲瀷鐨勬搷浣滄槸闅愬紡鐨勩€傝嚜鍔ㄦ媶绠憋細鎷嗙鐨勫師鐞嗗氨鏄痠.intValue();i+5杩愮畻瀹屾槸涓€涓猧nt鏁存暟銆傚浣曡祴鍊肩粰寮曠敤绫诲瀷i鍛紵鍏跺疄鏈夊缁撴灉杩涜瑁呯銆?br>Integer c = 127;
Integer d = 127;
System.out.println(c = = d); //true
//鍦ㄨ绠辨椂锛屽鏋滄暟鍊煎湪byte鑼冨洿涔嬪唴锛岄偅涔堟暟鍊肩浉鍚岋紝涓嶄細浜х敓鏂扮殑瀵硅薄锛屼篃灏辨槸璇村涓暟鍊肩浉鍚岀殑寮曠敤鎸囧悜鐨勬槸鍚屼竴涓璞°€?br>------------------------------------------------------------------------------------------------
API--- java.lang.System:
灞炴€у拰琛屼负閮芥槸闈欐€佺殑銆?br>long currentTimeMillis(); // 杩斿洖褰撳墠鏃堕棿姣鍊?br>exit(); // 閫€鍑鸿櫄鎷熸満
Properties getProperties() ; // 鑾峰彇褰撳墠绯荤粺鐨勫睘鎬т俊鎭?br>
Properties prop = System.getProperties(); //鑾峰彇绯荤粺鐨勫睘鎬т俊鎭紝骞跺皢杩欎簺淇℃伅瀛樺偍鍒癙roperties闆嗗悎涓€?
System.setProperty("myname","鍑摜"); //缁欑郴缁熷睘鎬т俊鎭泦娣诲姞鍏蜂綋鐨勫睘鎬т俊鎭?br>//涓存椂璁剧疆鏂瑰紡锛氳繍琛宩vm鏃讹紝鍙互閫氳繃jvm鐨勫弬鏁拌繘琛岀郴缁熷睘鎬х殑涓存椂璁剧疆锛屽彲浠ュ湪java鍛戒护鐨勫悗闈㈠姞鍏?鈥揇<name>=<value> 鐢ㄦ硶锛歫ava 鈥揇myname=灏忔槑 绫诲悕銆?br>
String name = System.getProperty("os.name");//鑾峰彇鎸囧畾灞炴€х殑淇℃伅
//鎯宠鐭ラ亾璇ョ郴缁熸槸鍚︽槸璇ヨ蒋浠舵墍鏀寔鐨勭郴缁熶腑鐨勪竴涓€?br>Set<String> hs = new HashSet<String>();
hs.add("Windows XP");
hs.add("Windows 7");
if(hs.contains(name))
System.out.println("鍙互鏀寔");
else
System.out.println("涓嶆敮鎸?);
--------------------------------------------------------------------------------------------------------------------
API--- java.util.Math:
鐢ㄤ簬鏁板杩愮畻鐨勫伐鍏风被锛屽睘鎬у拰琛屼负閮芥槸闈欐€佺殑銆傝绫绘槸final涓嶅厑璁哥户鎵裤€?br>
static double ceil(double a) ; //杩斿洖澶т簬鎸囧畾鏁板€肩殑鏈€灏忔暣鏁?br>static double floor(double a) ; //杩斿洖灏忎簬鎸囧畾鏁板€肩殑鏈€澶ф暣鏁?br>static long round(double a) ; //鍥涜垗浜斿叆鎴愭暣鏁?br>static double pow(double a, double b) ; //a鐨刡娆″箓
static double random(); //杩斿洖0~1鐨勪吉闅忔満鏁?br>
public static void main(String[] args) {
Random r = new Random();
for(int x=0; x<10; x++) {
//double d = Math.floor(Math.random()*10+1);
//int d = (int)(Math.random()*10+1);
int d = r.nextInt(10)+1;
System.out.println(d);
}
}
--------------------------------------------------------------------------------------------------------------------
API--- java.lang.Runtime:
绫讳腑娌℃湁鏋勯€犳柟娉曪紝涓嶈兘鍒涘缓瀵硅薄銆?br>浣嗘槸鏈夐潪闈欐€佹柟娉曘€傝鏄庤绫讳腑搴旇瀹氫箟濂戒簡瀵硅薄锛屽苟鍙互閫氳繃涓€涓猻tatic鏂规硶鑾峰彇杩欎釜瀵硅薄銆傜敤杩欎釜瀵硅薄鏉ヨ皟鐢ㄩ潪闈欐€佹柟娉曘€傝繖涓柟娉曞氨鏄?static Runtime getRuntime();
杩欎釜Runtime鍏跺疄浣跨敤鍗曚緥璁捐妯″紡杩涜璁捐銆?br>
class RuntimeDemo {
public static void main(String[] args) throws Exception {
Runtime r = Runtime.getRuntime();
Process p = r.exec("notepad.exe SystemDemo.java"); //杩愯鎸囧畾鐨勭▼搴?br> Thread.sleep(4000);
p.destroy(); //鏉€鎺夎繘绋?br> }
}
--------------------------------------------------------------------------------------------------------------------
API--- java.util.Date锛?/h2>
鏃ユ湡绫伙紝鏈堜唤浠?-11锛?br> /*
鏃ユ湡瀵硅薄鍜屾绉掑€间箣闂寸殑杞崲銆?br> 1锛屾棩鏈熷璞¤浆鎴愭绉掑€笺€侱ate绫讳腑鐨刧etTime鏂规硶銆?br> 2锛屽浣曞皢鑾峰彇鍒扮殑姣鍊艰浆鎴愬叿浣撶殑鏃ユ湡鍛紵
Date绫讳腑鐨剆etTime鏂规硶銆備篃鍙互閫氳繃鏋勯€犲嚱鏁般€?
*/
//鏃ユ湡瀵硅薄杞垚姣鍊?br> Date d = new Date();
long time1 = d.getTime();
long time2 = System.currentTimeMillis(); / /姣鍊笺€?br>
//姣鍊艰浆鎴愬叿浣撶殑鏃ユ湡
long time = 1322709921312l;
Date d = new Date();
d.setTime(time);
/*
灏嗘棩鏈熷瓧绗︿覆杞崲鎴愭棩鏈熷璞★細浣跨敤鐨勫氨鏄疍ateFormat鏂规硶涓殑 Date parse(String source) 锛?br> */
public static void method() throws Exception {
String str_time = "2011/10/25";
DateFormat df = new SimpleDateFormat("yyyy/MM/dd"); //SimpleDateFormat浣滀负鍙互鎸囧畾鐢ㄦ埛鑷畾涔夌殑鏍煎紡鏉ュ畬鎴愭牸寮忓寲銆?br> Date d = df.parse(str_time);
}
/*
濡傛灉涓嶉渶瑕佷娇鐢ㄧ壒瀹氱殑鏍煎紡鍖栭鏍硷紝瀹屽叏鍙互浣跨敤DateFormat绫讳腑鐨勯潤鎬佸伐鍘傛柟娉曡幏鍙栧叿浣撶殑宸茬粡灏佽濂介鏍肩殑瀵硅薄銆俫etDateInstance();getDateTimeInstance();
*/
Date d = new Date();
DateFormat df = DateFormat.getDateInstance(DateFormat.LONG);
df = DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.LONG);
String str_time = df.format(d);
//灏嗘棩鏈熷璞¤浆鎹㈡垚瀛楃涓茬殑鏂瑰紡锛欴ateFormat绫讳腑鐨刦ormat鏂规硶銆?br> //鍒涘缓鏃ユ湡鏍煎紡瀵硅薄銆?
DateFormat df = new SimpleDateFormat(); //璇ュ璞$殑寤虹珛鍐呴儴浼氬皝瑁呬竴涓粯璁ょ殑鏃ユ湡鏍煎紡銆?1-12-1 涓嬪崍1:48
//濡傛灉鎯宠鑷畾涔夋棩鏈熸牸寮忕殑璇濄€傚彲浣跨敤SimpleDateFormat鐨勬瀯閫犲嚱鏁般€傚皢鍏蜂綋鐨勬牸寮忎綔涓哄弬鏁颁紶鍏ュ埌鏋勯€犲嚱鏁颁腑銆傚浣曡〃绀烘棩鏈熶腑骞寸殑閮ㄥ垎鍛紵鍙互蹇呴』瑕佸弬涓庢牸寮忓璞℃枃妗c€?
df = new SimpleDateFormat("yyyy骞碝M鏈坉d鏃?HH:mm:ss");
//璋冪敤DateFormat涓殑format鏂规硶銆傚宸叉湁鐨勬棩鏈熷璞¤繘琛屾牸寮忓寲銆?br> String str_time = df.format(d);
--------------------------------------------------------------------------------------------------------------------
API--- java.util. Calendar锛氭棩鍘嗙被
public static void method(){
Calendar c = Calendar.getInstance();
System.out.println(c.get(Calendar.YEAR)+"骞?+(c.get(Calendar.MONTH)+1)+"鏈?
+getNum(c.get(Calendar.DAY_OF_MONTH))+"鏃?
+"鏄熸湡"+getWeek(c.get(Calendar.DAY_OF_WEEK)));
}
public static String getNum(int num){
return num>9 ? num+"" : "0"+num;
}
public static String getWeek(int index){
/*
鏌ヨ〃娉曪細寤虹珛鏁版嵁鐨勫搴斿叧绯?
鏈€濂斤細鏁版嵁涓暟鏄‘瀹氱殑锛岃€屼笖鏈夊搴斿叧绯汇€傚鏋滃搴斿叧绯荤殑涓€鏂癸紝鏄暟瀛楋紝鑰屼笖鍙互浣滀负瑙掓爣锛岄偅涔堝彲浠ラ€氳繃鏁扮粍鏉ヤ綔涓鸿〃銆?br> */
String[] weeks = {"","鏃?,"涓€","浜?,"涓?,"鍥?,"浜?,"鍏?};
return weeks[index];
}
------------------------------------------------------------------------------------------------------------------------------------------------
姝e垯琛ㄨ揪寮忥細鈽呪槄鈽呪槅锛?/h2>
杞垚鍏繘鍒讹細toOctalString
杞垚鍗佸叚杩涘埗锛歵oHexString
toString(int num,int radix);
灏嗗叾浠栬繘鍒惰浆鎹㈠崄杩涘埗锛?/h2>
parseInt(string,radix); //灏嗙粰瀹氱殑鏁拌浆鎴愭寚瀹氱殑鍩烘暟杩涘埗锛?br>
鍦╦dk1.5鐗堟湰鍚庯紝瀵瑰熀鏈暟鎹被鍨嬪璞″寘瑁呯被杩涜鍗囩骇銆傚湪鍗囩骇涓紝浣跨敤鍩烘湰鏁版嵁绫诲瀷瀵硅薄鍖呰绫诲彲浠ュ儚浣跨敤鍩烘湰鏁版嵁绫诲瀷涓€鏍凤紝杩涜杩愮畻銆?br> Integer i = new Integer(4); //1.5鐗堟湰涔嬪墠鐨勫啓娉曪紱
Integer i = 4; //鑷姩瑁呯锛?.5鐗堟湰鍚庣殑鍐欐硶锛?br> i = i + 5;
//i瀵硅薄鏄笉鑳界洿鎺ュ拰5鐩稿姞鐨勶紝鍏跺疄搴曞眰鍏堝皢i杞垚int绫诲瀷锛屽湪鍜?鐩稿姞銆傝€岃浆鎴恑nt绫诲瀷鐨勬搷浣滄槸闅愬紡鐨勩€傝嚜鍔ㄦ媶绠憋細鎷嗙鐨勫師鐞嗗氨鏄痠.intValue();i+5杩愮畻瀹屾槸涓€涓猧nt鏁存暟銆傚浣曡祴鍊肩粰寮曠敤绫诲瀷i鍛紵鍏跺疄鏈夊缁撴灉杩涜瑁呯銆?br>Integer c = 127;
Integer d = 127;
System.out.println(c = = d); //true
//鍦ㄨ绠辨椂锛屽鏋滄暟鍊煎湪byte鑼冨洿涔嬪唴锛岄偅涔堟暟鍊肩浉鍚岋紝涓嶄細浜х敓鏂扮殑瀵硅薄锛屼篃灏辨槸璇村涓暟鍊肩浉鍚岀殑寮曠敤鎸囧悜鐨勬槸鍚屼竴涓璞°€?br>------------------------------------------------------------------------------------------------
API--- java.lang.System:
灞炴€у拰琛屼负閮芥槸闈欐€佺殑銆?br>long currentTimeMillis(); // 杩斿洖褰撳墠鏃堕棿姣鍊?br>exit(); // 閫€鍑鸿櫄鎷熸満
Properties getProperties() ; // 鑾峰彇褰撳墠绯荤粺鐨勫睘鎬т俊鎭?br>
Properties prop = System.getProperties(); //鑾峰彇绯荤粺鐨勫睘鎬т俊鎭紝骞跺皢杩欎簺淇℃伅瀛樺偍鍒癙roperties闆嗗悎涓€?
System.setProperty("myname","鍑摜"); //缁欑郴缁熷睘鎬т俊鎭泦娣诲姞鍏蜂綋鐨勫睘鎬т俊鎭?br>//涓存椂璁剧疆鏂瑰紡锛氳繍琛宩vm鏃讹紝鍙互閫氳繃jvm鐨勫弬鏁拌繘琛岀郴缁熷睘鎬х殑涓存椂璁剧疆锛屽彲浠ュ湪java鍛戒护鐨勫悗闈㈠姞鍏?鈥揇<name>=<value> 鐢ㄦ硶锛歫ava 鈥揇myname=灏忔槑 绫诲悕銆?br>
String name = System.getProperty("os.name");//鑾峰彇鎸囧畾灞炴€х殑淇℃伅
//鎯宠鐭ラ亾璇ョ郴缁熸槸鍚︽槸璇ヨ蒋浠舵墍鏀寔鐨勭郴缁熶腑鐨勪竴涓€?br>Set<String> hs = new HashSet<String>();
hs.add("Windows XP");
hs.add("Windows 7");
if(hs.contains(name))
System.out.println("鍙互鏀寔");
else
System.out.println("涓嶆敮鎸?);
--------------------------------------------------------------------------------------------------------------------
API--- java.util.Math:
鐢ㄤ簬鏁板杩愮畻鐨勫伐鍏风被锛屽睘鎬у拰琛屼负閮芥槸闈欐€佺殑銆傝绫绘槸final涓嶅厑璁哥户鎵裤€?br>
static double ceil(double a) ; //杩斿洖澶т簬鎸囧畾鏁板€肩殑鏈€灏忔暣鏁?br>static double floor(double a) ; //杩斿洖灏忎簬鎸囧畾鏁板€肩殑鏈€澶ф暣鏁?br>static long round(double a) ; //鍥涜垗浜斿叆鎴愭暣鏁?br>static double pow(double a, double b) ; //a鐨刡娆″箓
static double random(); //杩斿洖0~1鐨勪吉闅忔満鏁?br>
public static void main(String[] args) {
Random r = new Random();
for(int x=0; x<10; x++) {
//double d = Math.floor(Math.random()*10+1);
//int d = (int)(Math.random()*10+1);
int d = r.nextInt(10)+1;
System.out.println(d);
}
}
--------------------------------------------------------------------------------------------------------------------
API--- java.lang.Runtime:
绫讳腑娌℃湁鏋勯€犳柟娉曪紝涓嶈兘鍒涘缓瀵硅薄銆?br>浣嗘槸鏈夐潪闈欐€佹柟娉曘€傝鏄庤绫讳腑搴旇瀹氫箟濂戒簡瀵硅薄锛屽苟鍙互閫氳繃涓€涓猻tatic鏂规硶鑾峰彇杩欎釜瀵硅薄銆傜敤杩欎釜瀵硅薄鏉ヨ皟鐢ㄩ潪闈欐€佹柟娉曘€傝繖涓柟娉曞氨鏄?static Runtime getRuntime();
杩欎釜Runtime鍏跺疄浣跨敤鍗曚緥璁捐妯″紡杩涜璁捐銆?br>
class RuntimeDemo {
public static void main(String[] args) throws Exception {
Runtime r = Runtime.getRuntime();
Process p = r.exec("notepad.exe SystemDemo.java"); //杩愯鎸囧畾鐨勭▼搴?br> Thread.sleep(4000);
p.destroy(); //鏉€鎺夎繘绋?br> }
}
--------------------------------------------------------------------------------------------------------------------
API--- java.util.Date锛?/h2>
鏃ユ湡绫伙紝鏈堜唤浠?-11锛?br> /*
鏃ユ湡瀵硅薄鍜屾绉掑€间箣闂寸殑杞崲銆?br> 1锛屾棩鏈熷璞¤浆鎴愭绉掑€笺€侱ate绫讳腑鐨刧etTime鏂规硶銆?br> 2锛屽浣曞皢鑾峰彇鍒扮殑姣鍊艰浆鎴愬叿浣撶殑鏃ユ湡鍛紵
Date绫讳腑鐨剆etTime鏂规硶銆備篃鍙互閫氳繃鏋勯€犲嚱鏁般€?
*/
//鏃ユ湡瀵硅薄杞垚姣鍊?br> Date d = new Date();
long time1 = d.getTime();
long time2 = System.currentTimeMillis(); / /姣鍊笺€?br>
//姣鍊艰浆鎴愬叿浣撶殑鏃ユ湡
long time = 1322709921312l;
Date d = new Date();
d.setTime(time);
/*
灏嗘棩鏈熷瓧绗︿覆杞崲鎴愭棩鏈熷璞★細浣跨敤鐨勫氨鏄疍ateFormat鏂规硶涓殑 Date parse(String source) 锛?br> */
public static void method() throws Exception {
String str_time = "2011/10/25";
DateFormat df = new SimpleDateFormat("yyyy/MM/dd"); //SimpleDateFormat浣滀负鍙互鎸囧畾鐢ㄦ埛鑷畾涔夌殑鏍煎紡鏉ュ畬鎴愭牸寮忓寲銆?br> Date d = df.parse(str_time);
}
/*
濡傛灉涓嶉渶瑕佷娇鐢ㄧ壒瀹氱殑鏍煎紡鍖栭鏍硷紝瀹屽叏鍙互浣跨敤DateFormat绫讳腑鐨勯潤鎬佸伐鍘傛柟娉曡幏鍙栧叿浣撶殑宸茬粡灏佽濂介鏍肩殑瀵硅薄銆俫etDateInstance();getDateTimeInstance();
*/
Date d = new Date();
DateFormat df = DateFormat.getDateInstance(DateFormat.LONG);
df = DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.LONG);
String str_time = df.format(d);
//灏嗘棩鏈熷璞¤浆鎹㈡垚瀛楃涓茬殑鏂瑰紡锛欴ateFormat绫讳腑鐨刦ormat鏂规硶銆?br> //鍒涘缓鏃ユ湡鏍煎紡瀵硅薄銆?
DateFormat df = new SimpleDateFormat(); //璇ュ璞$殑寤虹珛鍐呴儴浼氬皝瑁呬竴涓粯璁ょ殑鏃ユ湡鏍煎紡銆?1-12-1 涓嬪崍1:48
//濡傛灉鎯宠鑷畾涔夋棩鏈熸牸寮忕殑璇濄€傚彲浣跨敤SimpleDateFormat鐨勬瀯閫犲嚱鏁般€傚皢鍏蜂綋鐨勬牸寮忎綔涓哄弬鏁颁紶鍏ュ埌鏋勯€犲嚱鏁颁腑銆傚浣曡〃绀烘棩鏈熶腑骞寸殑閮ㄥ垎鍛紵鍙互蹇呴』瑕佸弬涓庢牸寮忓璞℃枃妗c€?
df = new SimpleDateFormat("yyyy骞碝M鏈坉d鏃?HH:mm:ss");
//璋冪敤DateFormat涓殑format鏂规硶銆傚宸叉湁鐨勬棩鏈熷璞¤繘琛屾牸寮忓寲銆?br> String str_time = df.format(d);
--------------------------------------------------------------------------------------------------------------------
API--- java.util. Calendar锛氭棩鍘嗙被
public static void method(){
Calendar c = Calendar.getInstance();
System.out.println(c.get(Calendar.YEAR)+"骞?+(c.get(Calendar.MONTH)+1)+"鏈?
+getNum(c.get(Calendar.DAY_OF_MONTH))+"鏃?
+"鏄熸湡"+getWeek(c.get(Calendar.DAY_OF_WEEK)));
}
public static String getNum(int num){
return num>9 ? num+"" : "0"+num;
}
public static String getWeek(int index){
/*
鏌ヨ〃娉曪細寤虹珛鏁版嵁鐨勫搴斿叧绯?
鏈€濂斤細鏁版嵁涓暟鏄‘瀹氱殑锛岃€屼笖鏈夊搴斿叧绯汇€傚鏋滃搴斿叧绯荤殑涓€鏂癸紝鏄暟瀛楋紝鑰屼笖鍙互浣滀负瑙掓爣锛岄偅涔堝彲浠ラ€氳繃鏁扮粍鏉ヤ綔涓鸿〃銆?br> */
String[] weeks = {"","鏃?,"涓€","浜?,"涓?,"鍥?,"浜?,"鍏?};
return weeks[index];
}
------------------------------------------------------------------------------------------------------------------------------------------------
姝e垯琛ㄨ揪寮忥細鈽呪槄鈽呪槅锛?/h2>
鏃ユ湡瀵硅薄鍜屾绉掑€间箣闂寸殑杞崲銆?br> 1锛屾棩鏈熷璞¤浆鎴愭绉掑€笺€侱ate绫讳腑鐨刧etTime鏂规硶銆?br> 2锛屽浣曞皢鑾峰彇鍒扮殑姣鍊艰浆鎴愬叿浣撶殑鏃ユ湡鍛紵
Date绫讳腑鐨剆etTime鏂规硶銆備篃鍙互閫氳繃鏋勯€犲嚱鏁般€?
*/
//鏃ユ湡瀵硅薄杞垚姣鍊?br> Date d = new Date();
long time1 = d.getTime();
long time2 = System.currentTimeMillis(); / /姣鍊笺€?br>
//姣鍊艰浆鎴愬叿浣撶殑鏃ユ湡
long time = 1322709921312l;
Date d = new Date();
d.setTime(time);
/*
灏嗘棩鏈熷瓧绗︿覆杞崲鎴愭棩鏈熷璞★細浣跨敤鐨勫氨鏄疍ateFormat鏂规硶涓殑 Date parse(String source) 锛?br> */
public static void method() throws Exception {
String str_time = "2011/10/25";
DateFormat df = new SimpleDateFormat("yyyy/MM/dd"); //SimpleDateFormat浣滀负鍙互鎸囧畾鐢ㄦ埛鑷畾涔夌殑鏍煎紡鏉ュ畬鎴愭牸寮忓寲銆?br> Date d = df.parse(str_time);
}
/*
濡傛灉涓嶉渶瑕佷娇鐢ㄧ壒瀹氱殑鏍煎紡鍖栭鏍硷紝瀹屽叏鍙互浣跨敤DateFormat绫讳腑鐨勯潤鎬佸伐鍘傛柟娉曡幏鍙栧叿浣撶殑宸茬粡灏佽濂介鏍肩殑瀵硅薄銆俫etDateInstance();getDateTimeInstance();
*/
Date d = new Date();
DateFormat df = DateFormat.getDateInstance(DateFormat.LONG);
df = DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.LONG);
String str_time = df.format(d);
//灏嗘棩鏈熷璞¤浆鎹㈡垚瀛楃涓茬殑鏂瑰紡锛欴ateFormat绫讳腑鐨刦ormat鏂规硶銆?br> //鍒涘缓鏃ユ湡鏍煎紡瀵硅薄銆?
DateFormat df = new SimpleDateFormat(); //璇ュ璞$殑寤虹珛鍐呴儴浼氬皝瑁呬竴涓粯璁ょ殑鏃ユ湡鏍煎紡銆?1-12-1 涓嬪崍1:48
//濡傛灉鎯宠鑷畾涔夋棩鏈熸牸寮忕殑璇濄€傚彲浣跨敤SimpleDateFormat鐨勬瀯閫犲嚱鏁般€傚皢鍏蜂綋鐨勬牸寮忎綔涓哄弬鏁颁紶鍏ュ埌鏋勯€犲嚱鏁颁腑銆傚浣曡〃绀烘棩鏈熶腑骞寸殑閮ㄥ垎鍛紵鍙互蹇呴』瑕佸弬涓庢牸寮忓璞℃枃妗c€?
df = new SimpleDateFormat("yyyy骞碝M鏈坉d鏃?HH:mm:ss");
//璋冪敤DateFormat涓殑format鏂规硶銆傚宸叉湁鐨勬棩鏈熷璞¤繘琛屾牸寮忓寲銆?br> String str_time = df.format(d);
--------------------------------------------------------------------------------------------------------------------
API--- java.util. Calendar锛氭棩鍘嗙被
public static void method(){Calendar c = Calendar.getInstance();
System.out.println(c.get(Calendar.YEAR)+"骞?+(c.get(Calendar.MONTH)+1)+"鏈?
+getNum(c.get(Calendar.DAY_OF_MONTH))+"鏃?
+"鏄熸湡"+getWeek(c.get(Calendar.DAY_OF_WEEK)));
}
public static String getNum(int num){
return num>9 ? num+"" : "0"+num;
}
public static String getWeek(int index){
/*
鏌ヨ〃娉曪細寤虹珛鏁版嵁鐨勫搴斿叧绯?
鏈€濂斤細鏁版嵁涓暟鏄‘瀹氱殑锛岃€屼笖鏈夊搴斿叧绯汇€傚鏋滃搴斿叧绯荤殑涓€鏂癸紝鏄暟瀛楋紝鑰屼笖鍙互浣滀负瑙掓爣锛岄偅涔堝彲浠ラ€氳繃鏁扮粍鏉ヤ綔涓鸿〃銆?br> */
String[] weeks = {"","鏃?,"涓€","浜?,"涓?,"鍥?,"浜?,"鍏?};
return weeks[index];
}
------------------------------------------------------------------------------------------------------------------------------------------------
姝e垯琛ㄨ揪寮忥細鈽呪槄鈽呪槅锛?/h2>
缁勶細鐢ㄥ皬鎷彿鏍囩ず锛屾瘡瀹氫箟涓€涓皬鎷彿锛屽氨鏄竴涓粍锛岃€屼笖鏈夎嚜鍔ㄧ紪鍙凤紝浠?寮€濮嬨€?br> 鍙浣跨敤缁勶紝瀵瑰簲鐨勬暟瀛楀氨鏄娇鐢ㄨ缁勭殑鍐呭銆傚埆蹇樹簡锛屾暟缁勮鍔燶銆?br> (aaa(wwww(ccc))(eee))鎶€宸э紝浠庡乏鎷彿寮€濮嬫暟鍗冲彲銆傛湁鍑犱釜宸︽嫭鍙峰氨鏄嚑缁勩€?br>
甯歌鎿嶄綔锛?br>1锛屽尮閰嶏細鍏跺疄鐢ㄧ殑灏辨槸String绫讳腑鐨刴atches鏂规硶銆?br>String reg = "[1-9][0-9]{4,14}";
boolean b = qq.matches(reg);//灏嗘鍒欏拰瀛楃涓插叧鑱斿瀛楃涓茶繘琛屽尮閰嶃€?br>2锛屽垏鍓诧細鍏跺疄鐢ㄧ殑灏辨槸String绫讳腑鐨剆plit鏂规硶銆?br>3锛屾浛鎹細鍏跺疄鐢ㄧ殑灏辨槸String绫讳腑鐨剅eplaceAll();
4锛岃幏鍙栵細
1锛夛紝鍏堣灏嗘鍒欒〃杈惧紡缂栬瘧鎴愭鍒欏璞°€備娇鐢ㄧ殑鏄疨attern涓潤鎬佹柟娉?compile(regex);
2锛夛紝閫氳繃Pattern瀵硅薄鑾峰彇Matcher瀵硅薄銆?br> Pattern鐢ㄤ簬鎻忚堪姝e垯琛ㄨ揪寮忥紝鍙互瀵规鍒欒〃杈惧紡杩涜瑙f瀽銆?br> 鑰屽皢瑙勫垯鎿嶄綔瀛楃涓诧紝闇€瑕佷粠鏂板皝瑁呭埌鍖归厤鍣ㄥ璞atcher涓€?br> 鐒跺悗浣跨敤Matcher瀵硅薄鐨勬柟娉曟潵鎿嶄綔瀛楃涓层€?br> 濡備綍鑾峰彇鍖归厤鍣ㄥ璞″憿锛?br> 閫氳繃Pattern瀵硅薄涓殑matcher鏂规硶銆傝鏂规硶鍙互姝e垯瑙勫垯鍜屽瓧绗︿覆鎯冲叧鑱斻€傚苟杩斿洖鍖归厤鍣ㄥ璞°€?br> 3锛夛紝浣跨敤Matcher瀵硅薄涓殑鏂规硶鍗冲彲瀵瑰瓧绗︿覆杩涜鍚勭姝e垯鎿嶄綔銆?br>----------------------------------------------------------------------------------------------
闆嗗悎妗嗘灦锛氣槄鈽呪槄鈽呪槄锛?/h1>
鐢ㄤ簬瀛樺偍鏁版嵁鐨勫鍣ㄣ€?br>鐗圭偣锛?br>1锛氬璞″皝瑁呮暟鎹紝瀵硅薄澶氫簡涔熼渶瑕佸瓨鍌ㄣ€傞泦鍚堢敤浜庡瓨鍌ㄥ璞°€?br>2锛氬璞$殑涓暟纭畾鍙互浣跨敤鏁扮粍锛屼絾鏄笉纭畾鎬庝箞鍔烇紵鍙互鐢ㄩ泦鍚堛€傚洜涓洪泦鍚堟槸鍙彉闀垮害鐨勩€?br>
闆嗗悎鍜屾暟缁勭殑鍖哄埆锛?br>1锛氭暟缁勬槸鍥哄畾闀垮害鐨勶紱闆嗗悎鍙彉闀垮害鐨勩€?br>2锛氭暟缁勫彲浠ュ瓨鍌ㄥ熀鏈暟鎹被鍨嬶紝涔熷彲浠ュ瓨鍌ㄥ紩鐢ㄦ暟鎹被鍨嬶紱闆嗗悎鍙兘瀛樺偍寮曠敤鏁版嵁绫诲瀷銆?br>3锛氭暟缁勫瓨鍌ㄧ殑鍏冪礌蹇呴』鏄悓涓€涓暟鎹被鍨嬶紱闆嗗悎瀛樺偍鐨勫璞″彲浠ユ槸涓嶅悓鏁版嵁绫诲瀷銆?br>
鏁版嵁缁撴瀯锛氬氨鏄鍣ㄤ腑瀛樺偍鏁版嵁鐨勬柟寮忋€?br>
瀵逛簬闆嗗悎瀹瑰櫒锛屾湁寰堝绉嶃€傚洜涓烘瘡涓€涓鍣ㄧ殑鑷韩鐗圭偣涓嶅悓锛屽叾瀹炲師鐞嗗湪浜庢瘡涓鍣ㄧ殑鍐呴儴鏁版嵁缁撴瀯涓嶅悓銆?br>闆嗗悎瀹瑰櫒鍦ㄤ笉鏂悜涓婃娊鍙栬繃绋嬩腑銆傚嚭鐜颁簡闆嗗悎浣撶郴銆?br>鍦ㄤ娇鐢ㄤ竴涓綋绯绘椂锛屽師鍒欙細鍙傞槄椤跺眰鍐呭銆傚缓绔嬪簳灞傚璞°€?br>
------------------------------------------------------------
--< java.util >-- Collection鎺ュ彛锛?br>Collection锛?br> |--List锛氭湁搴?鍏冪礌瀛樺叆闆嗗悎鐨勯『搴忓拰鍙栧嚭鐨勯『搴忎竴鑷?锛屽厓绱犻兘鏈夌储寮曘€傚厓绱犲彲浠ラ噸澶嶃€?br> |--Set锛氭棤搴?瀛樺叆鍜屽彇鍑洪『搴忔湁鍙兘涓嶄竴鑷?锛屼笉鍙互瀛樺偍閲嶅鍏冪礌銆傚繀椤讳繚璇佸厓绱犲敮涓€鎬с€?br>
1锛屾坊鍔狅細
add(object)锛氭坊鍔犱竴涓厓绱?br> addAll(Collection) 锛氭坊鍔犱竴涓泦鍚堜腑鐨勬墍鏈夊厓绱犮€?br>2锛屽垹闄わ細
clear()锛氬皢闆嗗悎涓殑鍏冪礌鍏ㄥ垹闄わ紝娓呯┖闆嗗悎銆?br> remove(obj) 锛氬垹闄ら泦鍚堜腑鎸囧畾鐨勫璞°€傛敞鎰忥細鍒犻櫎鎴愬姛锛岄泦鍚堢殑闀垮害浼氭敼鍙樸€?br> removeAll(collection) 锛氬垹闄ら儴鍒嗗厓绱犮€傞儴鍒嗗厓绱犲拰浼犲叆Collection涓€鑷淬€?br>3锛屽垽鏂細
boolean contains(obj) 锛氶泦鍚堜腑鏄惁鍖呭惈鎸囧畾鍏冪礌 銆?br> boolean containsAll(Collection) 锛氶泦鍚堜腑鏄惁鍖呭惈鎸囧畾鐨勫涓厓绱犮€?br> boolean isEmpty()锛氶泦鍚堜腑鏄惁鏈夊厓绱犮€?br>4锛岃幏鍙栵細
int size()锛氶泦鍚堜腑鏈夊嚑涓厓绱犮€?br>5锛屽彇浜ら泦锛?br> boolean retainAll(Collection) 锛氬褰撳墠闆嗗悎涓繚鐣欏拰鎸囧畾闆嗗悎涓殑鐩稿悓鐨勫厓绱犮€傚鏋滀袱涓泦鍚堝厓绱犵浉鍚岋紝杩斿洖flase锛涘鏋渞etainAll淇敼浜嗗綋鍓嶉泦鍚堬紝杩斿洖true銆?br>6锛岃幏鍙栭泦鍚堜腑鎵€鏈夊厓绱狅細
Iterator iterator()锛氳凯浠e櫒
7锛屽皢闆嗗悎鍙樻垚鏁扮粍锛?br> toArray();
------------------------------------------------------------
--< java.util >-- Iterator鎺ュ彛锛?br>杩唬鍣細鏄竴涓帴鍙c€備綔鐢細鐢ㄤ簬鍙栭泦鍚堜腑鐨勫厓绱犮€?br> boolean hasNext() 濡傛灉浠嶆湁鍏冪礌鍙互杩唬锛屽垯杩斿洖 true銆?br> E next() 杩斿洖杩唬鐨勪笅涓€涓厓绱犮€?br> void remove() 浠庤凯浠e櫒鎸囧悜鐨?collection 涓Щ闄よ凯浠e櫒杩斿洖鐨勬渶鍚庝竴涓厓绱狅紙鍙€夋搷浣滐級銆?br>
姣忎竴涓泦鍚堥兘鏈夎嚜宸辩殑鏁版嵁缁撴瀯锛岄兘鏈夌壒瀹氱殑鍙栧嚭鑷繁鍐呴儴鍏冪礌鐨勬柟寮忋€備负浜嗕究浜庢搷浣滄墍鏈夌殑瀹瑰櫒锛屽彇鍑哄厓绱犮€傚皢瀹瑰櫒鍐呴儴鐨勫彇鍑烘柟寮忔寜鐓т竴涓粺涓€鐨勮鍒欏悜澶栨彁渚涳紝杩欎釜瑙勫垯灏辨槸Iterator鎺ュ彛銆?br>涔熷氨璇达紝鍙閫氳繃璇ユ帴鍙e氨鍙互鍙栧嚭Collection闆嗗悎涓殑鍏冪礌锛岃嚦浜庢瘡涓€涓叿浣撶殑瀹瑰櫒渚濇嵁鑷繁鐨勬暟鎹粨鏋勶紝濡備綍瀹炵幇鐨勫叿浣撳彇鍑虹粏鑺傦紝杩欎釜涓嶇敤鍏冲績锛岃繖鏍峰氨闄嶄綆浜嗗彇鍑哄厓绱犲拰鍏蜂綋闆嗗悎鐨勮€﹀悎鎬с€?br>
Iterator it = coll.iterator();//鑾峰彇瀹瑰櫒涓殑杩唬鍣ㄥ璞★紝鑷充簬杩欎釜瀵硅薄鏄槸浠€涔堜笉閲嶈銆傝繖瀵硅薄鑲畾绗﹀悎涓€涓鍒橧terator鎺ュ彛銆?br>-----------------------------------------------------------------------------
public static void main(String[] args) {
Collection coll = new ArrayList();
coll.add("abc0");
coll.add("abc1");
coll.add("abc2");
//--------------鏂瑰紡1----------------------
Iterator it = coll.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
//---------------鏂瑰紡2鐢ㄦ绉?---------------------
for(Iterator it = coll.iterator();it.hasNext(); ){
System.out.println(it.next());
}
}
-----------------------------------------------------------------------------
--< java.util >-- List鎺ュ彛锛?/h2>
List鏈韩鏄疌ollection鎺ュ彛鐨勫瓙鎺ュ彛锛屽叿澶囦簡Collection鐨勬墍鏈夋柟娉曘€傜幇鍦ㄥ涔燣ist浣撶郴鐗规湁鐨勫叡鎬ф柟娉曪紝鏌ラ槄鏂规硶鍙戠幇List鐨勭壒鏈夋柟娉曢兘鏈夌储寮曪紝杩欐槸璇ラ泦鍚堟渶澶х殑鐗圭偣銆?br>
List锛氭湁搴?鍏冪礌瀛樺叆闆嗗悎鐨勯『搴忓拰鍙栧嚭鐨勯『搴忎竴鑷?锛屽厓绱犻兘鏈夌储寮曘€傚厓绱犲彲浠ラ噸澶嶃€?br> |--ArrayList锛氬簳灞傜殑鏁版嵁缁撴瀯鏄暟缁?绾跨▼涓嶅悓姝ワ紝ArrayList鏇夸唬浜哣ector锛屾煡璇㈠厓绱犵殑閫熷害闈炲父蹇€?br> |--LinkedList锛氬簳灞傜殑鏁版嵁缁撴瀯鏄摼琛紝绾跨▼涓嶅悓姝ワ紝澧炲垹鍏冪礌鐨勯€熷害闈炲父蹇€?br> |--Vector锛氬簳灞傜殑鏁版嵁缁撴瀯灏辨槸鏁扮粍锛岀嚎绋嬪悓姝ョ殑锛孷ector鏃犺鏌ヨ鍜屽鍒犻兘宸ㄦ參銆?br>
1锛屾坊鍔狅細
add(index,element) 锛氬湪鎸囧畾鐨勭储寮曚綅鎻掑叆鍏冪礌銆?br> addAll(index,collection) 锛氬湪鎸囧畾鐨勭储寮曚綅鎻掑叆涓€鍫嗗厓绱犮€?br>2锛屽垹闄わ細
remove(index) 锛氬垹闄ゆ寚瀹氱储寮曚綅鐨勫厓绱犮€?杩斿洖琚垹鐨勫厓绱犮€?br>3锛岃幏鍙栵細
Object get(index) 锛氶€氳繃绱㈠紩鑾峰彇鎸囧畾鍏冪礌銆?br> int indexOf(obj) 锛氳幏鍙栨寚瀹氬厓绱犵涓€娆″嚭鐜扮殑绱㈠紩浣嶏紝濡傛灉璇ュ厓绱犱笉瀛樺湪杩斿洖-1锛?br> 鎵€浠ワ紝閫氳繃-1锛屽彲浠ュ垽鏂竴涓厓绱犳槸鍚﹀瓨鍦ㄣ€?br> int lastIndexOf(Object o) 锛氬弽鍚戠储寮曟寚瀹氬厓绱犵殑浣嶇疆銆?br> List subList(start,end) 锛氳幏鍙栧瓙鍒楄〃銆?br>4锛屼慨鏀癸細
Object set(index,element) 锛氬鎸囧畾绱㈠紩浣嶈繘琛屽厓绱犵殑淇敼銆?br>5锛岃幏鍙栨墍鏈夊厓绱狅細
ListIterator listIterator()锛歭ist闆嗗悎鐗规湁鐨勮凯浠e櫒銆?br>
List闆嗗悎鏀寔瀵瑰厓绱犵殑澧炪€佸垹銆佹敼銆佹煡銆?br>
List闆嗗悎鍥犱负瑙掓爣鏈変簡鑷繁鐨勮幏鍙栧厓绱犵殑鏂瑰紡锛?閬嶅巻銆?br>for(int x=0; x<list.size(); x++){
sop("get:"+list.get(x));
}
鍦ㄨ繘琛宭ist鍒楄〃鍏冪礌杩唬鐨勬椂鍊欙紝濡傛灉鎯宠鍦ㄨ凯浠h繃绋嬩腑锛屾兂瑕佸鍏冪礌杩涜鎿嶄綔鐨勬椂鍊欙紝姣斿婊¤冻鏉′欢娣诲姞鏂板厓绱犮€備細鍙戠敓.ConcurrentModificationException骞跺彂淇敼寮傚父銆?br>瀵艰嚧鐨勫師鍥犳槸锛?br>闆嗗悎寮曠敤鍜岃凯浠e櫒寮曠敤鍦ㄥ悓鏃舵搷浣滃厓绱狅紝閫氳繃闆嗗悎鑾峰彇鍒板搴旂殑杩唬鍣ㄥ悗锛屽湪杩唬涓紝杩涜闆嗗悎寮曠敤鐨勫厓绱犳坊鍔狅紝杩唬鍣ㄥ苟涓嶇煡閬擄紝鎵€浠ヤ細鍑虹幇寮傚父鎯呭喌銆?br>濡備綍瑙e喅鍛紵
鏃㈢劧鏄湪杩唬涓鍏冪礌杩涜鎿嶄綔,鎵捐凯浠e櫒鐨勬柟娉曟渶涓哄悎閫?鍙槸Iterator涓彧鏈塰asNext,next,remove鏂规硶.閫氳繃鏌ラ槄鐨勫畠鐨勫瓙鎺ュ彛,ListIterator,鍙戠幇璇ュ垪琛ㄨ凯浠e櫒鎺ュ彛鍏峰浜嗗鍏冪礌鐨勫銆佸垹銆佹敼銆佹煡鐨勫姩浣溿€?br>
ListIterator鏄疞ist闆嗗悎鐗规湁鐨勮凯浠e櫒銆?br>ListIterator it = list.listIterator;//鍙栦唬Iterator it = list.iterator;
鏂规硶鎽樿
void add(E e) 灏嗘寚瀹氱殑鍏冪礌鎻掑叆鍒楄〃锛堝彲閫夋搷浣滐級銆?br> boolean hasNext() 浠ユ鍚戦亶鍘嗗垪琛ㄦ椂锛屽鏋滃垪琛ㄨ凯浠e櫒鏈夊涓厓绱狅紝鍒欒繑鍥?true锛堟崲鍙ヨ瘽璇达紝濡傛灉 next 杩斿洖涓€涓厓绱犺€屼笉鏄姏鍑哄紓甯革紝鍒欒繑鍥?true锛夈€?br> boolean hasPrevious() 濡傛灉浠ラ€嗗悜閬嶅巻鍒楄〃锛屽垪琛ㄨ凯浠e櫒鏈夊涓厓绱狅紝鍒欒繑鍥?true銆?br> E next() 杩斿洖鍒楄〃涓殑涓嬩竴涓厓绱犮€?br> int nextIndex() 杩斿洖瀵?next 鐨勫悗缁皟鐢ㄦ墍杩斿洖鍏冪礌鐨勭储寮曘€?br> E previous() 杩斿洖鍒楄〃涓殑鍓嶄竴涓厓绱犮€?br> int previousIndex() 杩斿洖瀵?previous 鐨勫悗缁皟鐢ㄦ墍杩斿洖鍏冪礌鐨勭储寮曘€?br> void remove() 浠庡垪琛ㄤ腑绉婚櫎鐢?next 鎴?previous 杩斿洖鐨勬渶鍚庝竴涓厓绱狅紙鍙€夋搷浣滐級銆?br> void set(E e) 鐢ㄦ寚瀹氬厓绱犳浛鎹?next 鎴?previous 杩斿洖鐨勬渶鍚庝竴涓厓绱狅紙鍙€夋搷浣滐級銆?br>
鍙彉闀垮害鏁扮粍鐨勫師鐞嗭細
褰撳厓绱犺秴鍑烘暟缁勯暱搴︼紝浼氫骇鐢熶竴涓柊鏁扮粍锛屽皢鍘熸暟缁勭殑鏁版嵁澶嶅埗鍒版柊鏁扮粍涓紝鍐嶅皢鏂扮殑鍏冪礌娣诲姞鍒版柊鏁扮粍涓€?br>ArrayList锛?br>鏄寜鐓у師鏁扮粍鐨?0%寤堕暱銆傛瀯閫犱竴涓垵濮嬪閲忎负 10 鐨勭┖鍒楄〃銆?br>Vector锛?br>鏄寜鐓у師鏁扮粍鐨?00%寤堕暱銆?br>
娉ㄦ剰锛氬浜巐ist闆嗗悎锛屽簳灞傚垽鏂厓绱犳槸鍚︾浉鍚岋紝鍏跺疄鐢ㄧ殑鏄厓绱犺嚜韬殑equals鏂规硶瀹屾垚鐨勩€傛墍浠ュ缓璁厓绱犻兘瑕佸鍐檈quals鏂规硶锛屽缓绔嬪厓绱犲璞¤嚜宸辩殑姣旇緝鐩稿悓鐨勬潯浠朵緷鎹€?br>
LinkedList锛氱殑鐗规湁鏂规硶銆?br>addFirst();
addLast();
鍦╦dk1.6浠ュ悗銆?br>offerFirst();
offerLast();
getFirst():鑾峰彇閾捐〃涓殑绗竴涓厓绱犮€傚鏋滈摼琛ㄤ负绌猴紝鎶涘嚭NoSuchElementException;
getLast();
鍦╦dk1.6浠ュ悗銆?br>peekFirst();鑾峰彇閾捐〃涓殑绗竴涓厓绱犮€傚鏋滈摼琛ㄤ负绌猴紝杩斿洖null銆?br>peekLast();
removeFirst()锛氳幏鍙栭摼琛ㄤ腑鐨勭涓€涓厓绱狅紝浣嗘槸浼氬垹闄ら摼琛ㄤ腑鐨勭涓€涓厓绱犮€傚鏋滈摼琛ㄤ负绌猴紝鎶涘嚭NoSuchElementException
removeLast();
鍦╦dk1.6浠ュ悗銆?br>pollFirst();鑾峰彇閾捐〃涓殑绗竴涓厓绱狅紝浣嗘槸浼氬垹闄ら摼琛ㄤ腑鐨勭涓€涓厓绱犮€傚鏋滈摼琛ㄤ负绌猴紝杩斿洖null銆?br>pollLast();
------------------------------------------------------------
--< java.util >-- Set鎺ュ彛锛?/h2>
Set鎺ュ彛涓殑鏂规硶鍜孋ollection涓柟娉曚竴鑷寸殑銆係et鎺ュ彛鍙栧嚭鏂瑰紡鍙湁涓€绉嶏紝杩唬鍣ㄣ€?br> |--HashSet锛氬簳灞傛暟鎹粨鏋勬槸鍝堝笇琛紝绾跨▼鏄笉鍚屾鐨勩€傛棤搴忥紝楂樻晥锛?br> HashSet闆嗗悎淇濊瘉鍏冪礌鍞竴鎬э細閫氳繃鍏冪礌鐨刪ashCode鏂规硶锛屽拰equals鏂规硶瀹屾垚鐨勩€?br> 褰撳厓绱犵殑hashCode鍊肩浉鍚屾椂锛屾墠缁х画鍒ゆ柇鍏冪礌鐨別quals鏄惁涓簍rue銆?br> 濡傛灉涓簍rue锛岄偅涔堣涓虹浉鍚屽厓绱狅紝涓嶅瓨銆傚鏋滀负false锛岄偅涔堝瓨鍌ㄣ€?br> 濡傛灉hashCode鍊间笉鍚岋紝閭d箞涓嶅垽鏂璭quals锛屼粠鑰屾彁楂樺璞℃瘮杈冪殑閫熷害銆?br> |--LinkedHashSet锛氭湁搴忥紝hashset鐨勫瓙绫汇€?br> |--TreeSet锛氬Set闆嗗悎涓殑鍏冪礌鐨勮繘琛屾寚瀹氶『搴忕殑鎺掑簭銆備笉鍚屾銆俆reeSet搴曞眰鐨勬暟鎹粨鏋勫氨鏄簩鍙夋爲銆?br>
鍝堝笇琛ㄧ殑鍘熺悊锛?br>1锛屽瀵硅薄鍏冪礌涓殑鍏抽敭瀛?瀵硅薄涓殑鐗规湁鏁版嵁)锛岃繘琛屽搱甯岀畻娉曠殑杩愮畻锛屽苟寰楀嚭涓€涓叿浣撶殑绠楁硶鍊硷紝杩欎釜鍊?绉颁负鍝堝笇鍊笺€?br>2锛屽搱甯屽€煎氨鏄繖涓厓绱犵殑浣嶇疆銆?br>3锛屽鏋滃搱甯屽€煎嚭鐜板啿绐侊紝鍐嶆鍒ゆ柇杩欎釜鍏抽敭瀛楀搴旂殑瀵硅薄鏄惁鐩稿悓銆傚鏋滃璞$浉鍚岋紝灏变笉瀛樺偍锛屽洜涓哄厓绱犻噸澶嶃€傚鏋滃璞′笉鍚岋紝灏卞瓨鍌紝鍦ㄥ師鏉ュ璞$殑鍝堝笇鍊煎熀纭€ +1椤哄欢銆?br>4锛屽瓨鍌ㄥ搱甯屽€肩殑缁撴瀯锛屾垜浠О涓哄搱甯岃〃銆?br>5锛屾棦鐒跺搱甯岃〃鏄牴鎹搱甯屽€煎瓨鍌ㄧ殑锛屼负浜嗘彁楂樻晥鐜囷紝鏈€濂戒繚璇佸璞$殑鍏抽敭瀛楁槸鍞竴鐨勩€?br> 杩欐牱鍙互灏介噺灏戠殑鍒ゆ柇鍏抽敭瀛楀搴旂殑瀵硅薄鏄惁鐩稿悓锛屾彁楂樹簡鍝堝笇琛ㄧ殑鎿嶄綔鏁堢巼銆?br>
瀵逛簬ArrayList闆嗗悎锛屽垽鏂厓绱犳槸鍚﹀瓨鍦紝鎴栬€呭垹鍏冪礌搴曞眰渚濇嵁閮芥槸equals鏂规硶銆?br>瀵逛簬HashSet闆嗗悎锛屽垽鏂厓绱犳槸鍚﹀瓨鍦紝鎴栬€呭垹闄ゅ厓绱狅紝搴曞眰渚濇嵁鐨勬槸hashCode鏂规硶鍜宔quals鏂规硶銆?br>
TreeSet:
鐢ㄤ簬瀵筍et闆嗗悎杩涜鍏冪礌鐨勬寚瀹氶『搴忔帓搴忥紝鎺掑簭闇€瑕佷緷鎹厓绱犺嚜韬叿澶囩殑姣旇緝鎬с€?br> 濡傛灉鍏冪礌涓嶅叿澶囨瘮杈冩€э紝鍦ㄨ繍琛屾椂浼氬彂鐢烠lassCastException寮傚父銆?br> 鎵€浠ラ渶瑕佸厓绱犲疄鐜癈omparable鎺ュ彛锛屽己鍒惰鍏冪礌鍏峰姣旇緝鎬э紝澶嶅啓compareTo鏂规硶銆?br> 渚濇嵁compareTo鏂规硶鐨勮繑鍥炲€硷紝纭畾鍏冪礌鍦═reeSet鏁版嵁缁撴瀯涓殑浣嶇疆銆?br> TreeSet鏂规硶淇濊瘉鍏冪礌鍞竴鎬х殑鏂瑰紡锛氬氨鏄弬鑰冩瘮杈冩柟娉曠殑缁撴灉鏄惁涓?锛屽鏋渞eturn 0锛岃涓轰袱涓璞¢噸澶嶏紝涓嶅瓨銆?br>
娉ㄦ剰锛氬湪杩涜姣旇緝鏃讹紝濡傛灉鍒ゆ柇鍏冪礌涓嶅敮涓€锛屾瘮濡傦紝鍚屽鍚嶏紝鍚屽勾榫勶紝鎵嶈涓哄悓涓€涓汉銆?br> 鍦ㄥ垽鏂椂锛岄渶瑕佸垎涓昏鏉′欢鍜屾瑕佹潯浠讹紝褰撲富瑕佹潯浠剁浉鍚屾椂锛屽啀鍒ゆ柇娆¤鏉′欢锛屾寜鐓ф瑕佹潯浠舵帓搴忋€?br>
TreeSet闆嗗悎鎺掑簭鏈変袱绉嶆柟寮忥紝Comparable鍜孋omparator鍖哄埆锛?br>1锛氳鍏冪礌鑷韩鍏峰姣旇緝鎬э紝闇€瑕佸厓绱犲璞″疄鐜癈omparable鎺ュ彛锛岃鐩朿ompareTo鏂规硶銆?br>2锛氳闆嗗悎鑷韩鍏峰姣旇緝鎬э紝闇€瑕佸畾涔変竴涓疄鐜颁簡Comparator鎺ュ彛鐨勬瘮杈冨櫒锛屽苟瑕嗙洊compare鏂规硶锛屽苟灏嗚绫诲璞′綔涓哄疄闄呭弬鏁颁紶閫掔粰TreeSet闆嗗悎鐨勬瀯閫犲嚱鏁般€?br>绗簩绉嶆柟寮忚緝涓虹伒娲汇€?br>
------------------------------------------------------------
Map闆嗗悎锛?/h2>
|--Hashtable锛氬簳灞傛槸鍝堝笇琛ㄦ暟鎹粨鏋勶紝鏄嚎绋嬪悓姝ョ殑銆備笉鍙互瀛樺偍null閿紝null鍊笺€?br>|--HashMap锛氬簳灞傛槸鍝堝笇琛ㄦ暟鎹粨鏋勶紝鏄嚎绋嬩笉鍚屾鐨勩€傚彲浠ュ瓨鍌╪ull閿紝null鍊笺€傛浛浠d簡Hashtable.
|--TreeMap锛氬簳灞傛槸浜屽弶鏍戠粨鏋勶紝鍙互瀵筸ap闆嗗悎涓殑閿繘琛屾寚瀹氶『搴忕殑鎺掑簭銆?br>
Map闆嗗悎瀛樺偍鍜孋ollection鏈夌潃寰堝ぇ涓嶅悓锛?br>Collection涓€娆″瓨涓€涓厓绱狅紱Map涓€娆″瓨涓€瀵瑰厓绱犮€?br>Collection鏄崟鍒楅泦鍚堬紱Map鏄弻鍒楅泦鍚堛€?br>Map涓殑瀛樺偍鐨勪竴瀵瑰厓绱狅細涓€涓槸閿紝涓€涓槸鍊硷紝閿笌鍊间箣闂存湁瀵瑰簲(鏄犲皠)鍏崇郴銆?br>鐗圭偣锛氳淇濊瘉map闆嗗悎涓敭鐨勫敮涓€鎬с€?br>
1锛屾坊鍔犮€?br> put(key,value)锛氬綋瀛樺偍鐨勯敭鐩稿悓鏃讹紝鏂扮殑鍊间細鏇挎崲鑰佺殑鍊硷紝骞跺皢鑰佸€艰繑鍥炪€傚鏋滈敭娌℃湁閲嶅锛岃繑鍥瀗ull銆?br> void putAll(Map);
2锛屽垹闄ゃ€?br> void clear()锛氭竻绌?br> value remove(key) 锛氬垹闄ゆ寚瀹氶敭銆?br>3锛屽垽鏂€?br> boolean isEmpty()锛?br> boolean containsKey(key)锛氭槸鍚﹀寘鍚玨ey
boolean containsValue(value) 锛氭槸鍚﹀寘鍚玽alue
4锛屽彇鍑恒€?br> int size()锛氳繑鍥為暱搴?br> value get(key) 锛氶€氳繃鎸囧畾閿幏鍙栧搴旂殑鍊笺€傚鏋滆繑鍥瀗ull锛屽彲浠ュ垽鏂閿笉瀛樺湪銆傚綋鐒舵湁鐗规畩鎯呭喌锛屽氨鏄湪hashmap闆嗗悎涓紝鏄彲浠ュ瓨鍌╪ull閿畁ull鍊肩殑銆?br> Collection values()锛氳幏鍙杕ap闆嗗悎涓殑鎵€鏈夌殑鍊笺€?br>5锛屾兂瑕佽幏鍙杕ap涓殑鎵€鏈夊厓绱狅細
鍘熺悊锛歮ap涓槸娌℃湁杩唬鍣ㄧ殑锛宑ollection鍏峰杩唬鍣紝鍙灏唌ap闆嗗悎杞垚Set闆嗗悎锛屽彲浠ヤ娇鐢ㄨ凯浠e櫒浜嗐€備箣鎵€浠ヨ浆鎴恠et锛屾槸鍥犱负map闆嗗悎鍏峰鐫€閿殑鍞竴鎬э紝鍏跺疄set闆嗗悎灏辨潵鑷簬map锛宻et闆嗗悎搴曞眰鍏跺疄鐢ㄧ殑灏辨槸map鐨勬柟娉曘€?br>鎶妋ap闆嗗悎杞垚set鐨勬柟娉曪細
Set keySet();
Set entrySet();//鍙栫殑鏄敭鍜屽€肩殑鏄犲皠鍏崇郴銆?br>Entry灏辨槸Map鎺ュ彛涓殑鍐呴儴鎺ュ彛锛?br>涓轰粈涔堣瀹氫箟鍦╩ap鍐呴儴鍛紵entry鏄闂敭鍊煎叧绯荤殑鍏ュ彛锛屾槸map鐨勫叆鍙o紝璁块棶鐨勬槸map涓殑閿€煎銆?br>---------------------------------------------------------
鍙栧嚭map闆嗗悎涓墍鏈夊厓绱犵殑鏂瑰紡涓€锛歬eySet()鏂规硶銆?br>鍙互灏唌ap闆嗗悎涓殑閿兘鍙栧嚭瀛樻斁鍒皊et闆嗗悎涓€傚set闆嗗悎杩涜杩唬銆傝凯浠e畬鎴愶紝鍐嶉€氳繃get鏂规硶瀵硅幏鍙栧埌鐨勯敭杩涜鍊肩殑鑾峰彇銆?br> Set keySet = map.keySet();
Iterator it = keySet.iterator();
while(it.hasNext()) {
Object key = it.next();
Object value = map.get(key);
System.out.println(key+":"+value);
}
--------------------------------------------------------
鍙栧嚭map闆嗗悎涓墍鏈夊厓绱犵殑鏂瑰紡浜岋細entrySet()鏂规硶銆?br>Set entrySet = map.entrySet();
Iterator it = entrySet.iterator();
while(it.hasNext()) {
Map.Entry me = (Map.Entry)it.next();
System.out.println(me.getKey()+"::::"+me.getValue());
}
--------------------------------------------------------
浣跨敤闆嗗悎鐨勬妧宸э細
鐪嬪埌Array灏辨槸鏁扮粍缁撴瀯锛屾湁瑙掓爣锛屾煡璇㈤€熷害寰堝揩銆?br>鐪嬪埌link灏辨槸閾捐〃缁撴瀯锛氬鍒犻€熷害蹇紝鑰屼笖鏈夌壒鏈夋柟娉曘€俛ddFirst锛?addLast锛?removeFirst()锛?removeLast()锛?getFirst()锛沢etLast()锛?br>鐪嬪埌hash灏辨槸鍝堝笇琛紝灏辫鎯宠鍝堝笇鍊硷紝灏辫鎯冲埌鍞竴鎬э紝灏辫鎯冲埌瀛樺叆鍒拌缁撴瀯鐨勪腑鐨勫厓绱犲繀椤昏鐩杊ashCode锛宔quals鏂规硶銆?br>鐪嬪埌tree灏辨槸浜屽弶鏍戯紝灏辫鎯冲埌鎺掑簭锛屽氨鎯宠鐢ㄥ埌姣旇緝銆?br>姣旇緝鐨勪袱绉嶆柟寮忥細
涓€涓槸Comparable锛氳鐩朿ompareTo鏂规硶锛?br>涓€涓槸Comparator锛氳鐩朿ompare鏂规硶銆?br>LinkedHashSet锛孡inkedHashMap:杩欎袱涓泦鍚堝彲浠ヤ繚璇佸搱甯岃〃鏈夊瓨鍏ラ『搴忓拰鍙栧嚭椤哄簭涓€鑷达紝淇濊瘉鍝堝笇琛ㄦ湁搴忋€?br>
闆嗗悎浠€涔堟椂鍊欑敤锛?br>褰撳瓨鍌ㄧ殑鏄竴涓厓绱犳椂锛屽氨鐢–ollection銆傚綋瀛樺偍瀵硅薄涔嬮棿瀛樺湪鐫€鏄犲皠鍏崇郴鏃讹紝灏变娇鐢∕ap闆嗗悎銆?br>
淇濊瘉鍞竴锛屽氨鐢⊿et銆備笉淇濊瘉鍞竴锛屽氨鐢↙ist銆?br>------------------------------------------------------------------------------------------------
Collections锛氬畠鐨勫嚭鐜扮粰闆嗗悎鎿嶄綔鎻愪緵浜嗘洿澶氱殑鍔熻兘銆傝繖涓被涓嶉渶瑕佸垱寤哄璞★紝鍐呴儴鎻愪緵鐨勯兘鏄潤鎬佹柟娉曘€?br>闈欐€佹柟娉曪細
Collections.sort(list);//list闆嗗悎杩涜鍏冪礌鐨勮嚜鐒堕『搴忔帓搴忋€?br>Collections.sort(list,new ComparatorByLen());//鎸夋寚瀹氱殑姣旇緝鍣ㄦ柟娉曟帓搴忋€?br>class ComparatorByLen implements Comparator<String>{
public int compare(String s1,String s2){
int temp = s1.length()-s2.length();
return temp==0?s1.compareTo(s2):temp;
}
}
Collections.max(list); //杩斿洖list涓瓧鍏搁『搴忔渶澶х殑鍏冪礌銆?br>int index = Collections.binarySearch(list,"zz");//浜屽垎鏌ユ壘锛岃繑鍥炶鏍囥€?br>Collections.reverseOrder();//閫嗗悜鍙嶈浆鎺掑簭銆?br>Collections.shuffle(list);//闅忔満瀵筶ist涓殑鍏冪礌杩涜浣嶇疆鐨勭疆鎹€?br>
灏嗛潪鍚屾闆嗗悎杞垚鍚屾闆嗗悎鐨勬柟娉曪細Collections涓殑 XXX synchronizedXXX(XXX);
List synchronizedList(list);
Map synchronizedMap(map);
鍘熺悊锛氬畾涔変竴涓被锛屽皢闆嗗悎鎵€鏈夌殑鏂规硶鍔犲悓涓€鎶婇攣鍚庤繑鍥炪€?br>
Collection 鍜?Collections鐨勫尯鍒細
Collections鏄釜java.util涓嬬殑绫伙紝鏄拡瀵归泦鍚堢被鐨勪竴涓伐鍏风被,鎻愪緵涓€绯诲垪闈欐€佹柟娉?瀹炵幇瀵归泦鍚堢殑鏌ユ壘銆佹帓搴忋€佹浛鎹€佺嚎绋嬪畨鍏ㄥ寲锛堝皢闈炲悓姝ョ殑闆嗗悎杞崲鎴愬悓姝ョ殑锛夌瓑鎿嶄綔銆?br>Collection鏄釜java.util涓嬬殑鎺ュ彛锛屽畠鏄悇绉嶉泦鍚堢粨鏋勭殑鐖舵帴鍙o紝缁ф壙浜庡畠鐨勬帴鍙d富瑕佹湁Set鍜孡ist,鎻愪緵浜嗗叧浜庨泦鍚堢殑涓€浜涙搷浣?濡傛彃鍏ャ€佸垹闄ゃ€佸垽鏂竴涓厓绱犳槸鍚﹀叾鎴愬憳銆侀亶鍘嗙瓑銆?br>-------------------------------------------------------
Arrays锛?br>鐢ㄤ簬鎿嶄綔鏁扮粍瀵硅薄鐨勫伐鍏风被锛岄噷闈㈤兘鏄潤鎬佹柟娉曘€?br>asList鏂规硶锛氬皢鏁扮粍杞崲鎴恖ist闆嗗悎銆?br>String[] arr = {"abc","kk","qq"};
List<String> list = Arrays.asList(arr);//灏哸rr鏁扮粍杞垚list闆嗗悎銆?br>灏嗘暟缁勮浆鎹㈡垚闆嗗悎锛屾湁浠€涔堝ソ澶勫憿锛熺敤aslist鏂规硶锛屽皢鏁扮粍鍙樻垚闆嗗悎锛?br>鍙互閫氳繃list闆嗗悎涓殑鏂规硶鏉ユ搷浣滄暟缁勪腑鐨勫厓绱狅細isEmpty()銆乧ontains銆乮ndexOf銆乻et锛?br>娉ㄦ剰锛堝眬闄愭€э級锛氭暟缁勬槸鍥哄畾闀垮害锛屼笉鍙互浣跨敤闆嗗悎瀵硅薄澧炲姞鎴栬€呭垹闄ょ瓑锛屼細鏀瑰彉鏁扮粍闀垮害鐨勫姛鑳芥柟娉曘€傛瘮濡俛dd銆乺emove銆乧lear銆傦紙浼氭姤涓嶆敮鎸佹搷浣滃紓甯窾nsupportedOperationException锛夛紱
濡傛灉鏁扮粍涓瓨鍌ㄧ殑寮曠敤鏁版嵁绫诲瀷锛岀洿鎺ヤ綔涓洪泦鍚堢殑鍏冪礌鍙互鐩存帴鐢ㄩ泦鍚堟柟娉曟搷浣溿€?br>濡傛灉鏁扮粍涓瓨鍌ㄧ殑鏄熀鏈暟鎹被鍨嬶紝asList浼氬皢鏁扮粍瀹炰綋浣滀负闆嗗悎鍏冪礌瀛樺湪銆?br>
闆嗗悎鍙樻暟缁勶細鐢ㄧ殑鏄疌ollection鎺ュ彛涓殑鏂规硶锛歵oArray();
濡傛灉缁檛oArray浼犻€掔殑鎸囧畾绫诲瀷鐨勬暟鎹暱搴﹀皬浜庝簡闆嗗悎鐨剆ize锛岄偅涔坱oArray鏂规硶锛屼細鑷畾鍐嶅垱寤轰竴涓绫诲瀷鐨勬暟鎹紝闀垮害涓洪泦鍚堢殑size銆?br> 濡傛灉浼犻€掔殑鎸囧畾鐨勭被鍨嬬殑鏁扮粍鐨勯暱搴﹀ぇ浜庝簡闆嗗悎鐨剆ize锛岄偅涔坱oArray鏂规硶锛屽氨涓嶄細鍒涘缓鏂版暟缁勶紝鐩存帴浣跨敤璇ユ暟缁勫嵆鍙紝骞跺皢闆嗗悎涓殑鍏冪礌瀛樺偍鍒版暟缁勪腑锛屽叾浠栦负瀛樺偍鍏冪礌鐨勪綅缃粯璁ゅ€糿ull銆?br> 鎵€浠ワ紝鍦ㄤ紶閫掓寚瀹氱被鍨嬫暟缁勬椂锛屾渶濂界殑鏂瑰紡灏辨槸鎸囧畾鐨勯暱搴﹀拰size鐩哥瓑鐨勬暟缁勩€?br>灏嗛泦鍚堝彉鎴愭暟缁勫悗鏈変粈涔堝ソ澶勶紵闄愬畾浜嗗闆嗗悎涓殑鍏冪礌杩涜澧炲垹鎿嶄綔锛屽彧瑕佽幏鍙栬繖浜涘厓绱犲嵆鍙€?br>------------------------------------------------------------------------------------------------
Jdk5.0鏂扮壒鎬э細
Collection鍦╦dk1.5浠ュ悗锛屾湁浜嗕竴涓埗鎺ュ彛Iterable锛岃繖涓帴鍙g殑鍑虹幇鐨勫皢iterator鏂规硶杩涜鎶藉彇锛屾彁楂樹簡鎵╁睍鎬с€?br>--------------------------------------------------
澧炲己for寰幆锛歠oreach璇彞锛宖oreach绠€鍖栦簡杩唬鍣ㄣ€?br>鏍煎紡锛?/ 澧炲己for寰幆鎷彿閲屽啓涓や釜鍙傛暟锛岀涓€涓槸澹版槑涓€涓彉閲忥紝绗簩涓氨鏄渶瑕佽凯浠g殑瀹瑰櫒
for( 鍏冪礌绫诲瀷 鍙橀噺鍚?: Collection闆嗗悎 & 鏁扮粍 ) {
鈥?br>}
楂樼骇for寰幆鍜屼紶缁焒or寰幆鐨勫尯鍒細
楂樼骇for寰幆鍦ㄤ娇鐢ㄦ椂锛屽繀椤昏鏄庣‘琚亶鍘嗙殑鐩爣銆傝繖涓洰鏍囷紝鍙互鏄疌ollection闆嗗悎鎴栬€呮暟缁勶紝濡傛灉閬嶅巻Collection闆嗗悎锛屽湪閬嶅巻杩囩▼涓繕闇€瑕佸鍏冪礌杩涜鎿嶄綔锛屾瘮濡傚垹闄わ紝闇€瑕佷娇鐢ㄨ凯浠e櫒銆?br>濡傛灉閬嶅巻鏁扮粍锛岃繕闇€瑕佸鏁扮粍鍏冪礌杩涜鎿嶄綔锛屽缓璁敤浼犵粺for寰幆鍥犱负鍙互瀹氫箟瑙掓爣閫氳繃瑙掓爣鎿嶄綔鍏冪礌銆傚鏋滃彧涓洪亶鍘嗚幏鍙栵紝鍙互绠€鍖栨垚楂樼骇for寰幆锛屽畠鐨勫嚭鐜颁负浜嗙畝鍖栦功鍐欍€?br>
楂樼骇for寰幆鍙互閬嶅巻map闆嗗悎鍚楋紵涓嶅彲浠ャ€備絾鏄彲浠ュ皢map杞垚set鍚庡啀浣跨敤foreach璇彞銆?br>
1)銆佷綔鐢細瀵瑰瓨鍌ㄥ璞$殑瀹瑰櫒杩涜杩唬锛?鏁扮粍 collection map
2)銆佸寮篺or寰幆杩唬鏁扮粍锛?br>String [] arr = {"a", "b", "c"};//鏁扮粍鐨勯潤鎬佸畾涔夋柟寮忥紝鍙瘯鐢ㄤ簬鏁扮粍棣栨瀹氫箟鐨勬椂鍊?br>for(String s : arr) {
System.out.println(s);
}
3)銆佸崟鍒楅泦鍚?Collection锛?br>List list = new ArrayList();
list.add("aaa");
// 澧炲己for寰幆, 娌℃湁浣跨敤娉涘瀷鐨勯泦鍚堣兘涓嶈兘浣跨敤澧炲己for寰幆杩唬锛熻兘
for(Object obj : list) {
String s = (String) obj;
System.out.println(s);
}
4)銆佸弻鍒楅泦鍚?Map锛?br>Map map = new HashMap();
map.put("a", "aaa");
// 浼犵粺鏂瑰紡锛氬繀椤绘帉鎻¤繖绉嶆柟寮?br>Set entrys = map.entrySet(); // 1.鑾峰緱鎵€鏈夌殑閿€煎Entry瀵硅薄
iter = entrys.iterator(); // 2.杩唬鍑烘墍鏈夌殑entry
while(iter.hasNext()) {
Map.Entry entry = (Entry) iter.next();
String key = (String) entry.getKey(); // 鍒嗗埆鑾峰緱key鍜寁alue
String value = (String) entry.getValue();
System.out.println(key + "=" + value);
}
// 澧炲己for寰幆杩唬锛氬師鍒欎笂map闆嗗悎鏄棤娉曚娇鐢ㄥ寮篺or寰幆鏉ヨ凯浠g殑锛屽洜涓哄寮篺or寰幆鍙兘閽堝瀹炵幇浜咺terable鎺ュ彛鐨勯泦鍚堣繘琛岃凯浠o紱Iterable鏄痡dk5涓柊瀹氫箟鐨勬帴鍙o紝灏变竴涓柟娉昳terator鏂规硶锛屽彧鏈夊疄鐜颁簡Iterable鎺ュ彛鐨勭被锛屾墠鑳戒繚璇佷竴瀹氭湁iterator鏂规硶锛宩ava鏈夎繖鏍风殑闄愬畾鏄洜涓哄寮篺or寰幆鍐呴儴杩樻槸鐢ㄨ凯浠e櫒瀹炵幇鐨勶紝鑰屽疄闄呬笂锛屾垜浠彲浠ラ€氳繃鏌愮鏂瑰紡鏉ヤ娇鐢ㄥ寮篺or寰幆銆?br>for(Object obj : map.entrySet()) {
Map.Entry entry = (Entry) obj; // obj 渚濇琛ㄧずEntry
System.out.println(entry.getKey() + "=" + entry.getValue());
}
5锛夈€侀泦鍚堣凯浠f敞鎰忛棶棰橈細鍦ㄨ凯浠i泦鍚堢殑杩囩▼涓紝涓嶈兘瀵归泦鍚堣繘琛屽鍒犳搷浣滐紙浼氭姤骞跺彂璁块棶寮傚父锛夛紱鍙互鐢ㄨ凯浠e櫒鐨勬柟娉曡繘琛屾搷浣滐紙瀛愮被listIterator锛氭湁澧炲垹鐨勬柟娉曪級銆?br>6)銆佸寮篺or寰幆娉ㄦ剰闂锛氬湪浣跨敤澧炲己for寰幆鏃讹紝涓嶈兘瀵瑰厓绱犺繘琛岃祴鍊硷紱
int[] arr = {1,2,3};
for(int num : arr) {
num = 0; //涓嶈兘鏀瑰彉鏁扮粍鐨勫€?br>}
System.out.println(arr[1]); //2
--------------------------------------------------
鍙彉鍙傛暟锛?..锛夛細鐢ㄥ埌鍑芥暟鐨勫弬鏁颁笂锛屽綋瑕佹搷浣滅殑鍚屼竴涓被鍨嬪厓绱犱釜鏁颁笉纭畾鐨勬椂鍊欙紝鍙槸鐢ㄨ繖涓柟寮忥紝杩欎釜鍙傛暟鍙互鎺ュ彈浠绘剰涓暟鐨勫悓涓€绫诲瀷鐨勬暟鎹€?br>
鍜屼互鍓嶆帴鏀舵暟缁勪笉涓€鏍风殑鏄細
浠ュ墠瀹氫箟鏁扮粍绫诲瀷锛岄渶瑕佸厛鍒涘缓涓€涓暟缁勫璞★紝鍐嶅皢杩欎釜鏁扮粍瀵硅薄浣滀负鍙傛暟浼犻€掔粰鍑芥暟銆傜幇鍦紝鐩存帴灏嗘暟缁勪腑鐨勫厓绱犱綔涓哄弬鏁颁紶閫掑嵆鍙€傚簳灞傚叾瀹炴槸灏嗚繖浜涘厓绱犺繘琛屾暟缁勭殑灏佽锛岃€岃繖涓皝瑁呭姩浣滐紝鏄湪搴曞眰瀹屾垚鐨勶紝琚殣钘忎簡銆傛墍浠ョ畝鍖栦簡鐢ㄦ埛鐨勪功鍐欙紝灏戜簡璋冪敤鑰呭畾涔夋暟缁勭殑鍔ㄤ綔銆?br>濡傛灉鍦ㄥ弬鏁板垪琛ㄤ腑浣跨敤浜嗗彲鍙樺弬鏁帮紝鍙彉鍙傛暟蹇呴』瀹氫箟鍦ㄥ弬鏁板垪琛ㄧ粨灏?涔熷氨鏄繀椤绘槸鏈€鍚庝竴涓弬鏁帮紝鍚﹀垯缂栬瘧浼氬け璐ャ€?銆?br>濡傛灉瑕佽幏鍙栧涓猧nt鏁扮殑鍜屽憿锛熷彲浠ヤ娇鐢ㄥ皢澶氫釜int鏁板皝瑁呭埌鏁扮粍涓紝鐩存帴瀵规暟缁勬眰鍜屽嵆鍙€?br>---------------------------------------------------
闈欐€佸鍏ワ細瀵煎叆浜嗙被涓殑鎵€鏈夐潤鎬佹垚鍛橈紝绠€鍖栭潤鎬佹垚鍛樼殑涔﹀啓銆?br>import static java.util.Collections.*; //瀵煎叆浜咰ollections绫讳腑鐨勬墍鏈夐潤鎬佹垚鍛?br>---------------------------------------------------
鏋氫妇锛氬叧閿瓧 enum
闂锛氬璞$殑鏌愪釜灞炴€х殑鍊间笉鑳芥槸浠绘剰鐨勶紝蹇呴』涓哄浐瀹氱殑涓€缁勫彇鍊煎叾涓殑鏌愪竴涓紱
瑙e喅鍔炴硶锛?br>1锛夈€佸湪setGrade鏂规硶涓仛鍒ゆ柇锛屼笉绗﹀悎鏍煎紡瑕佹眰灏辨姏鍑哄紓甯革紱
2锛夈€佺洿鎺ラ檺瀹氱敤鎴风殑閫夋嫨锛岄€氳繃鑷畾涔夌被妯℃嫙鏋氫妇鐨勬柟寮忔潵闄愬畾鐢ㄦ埛鐨勮緭鍏ワ紝鍐欎竴涓狦rade绫伙紝绉佹湁鏋勯€犲嚱鏁帮紝瀵瑰鎻愪緵5涓潤鎬佺殑甯搁噺琛ㄧず绫荤殑瀹炰緥锛?br>3)銆乯dk5涓柊瀹氫箟浜嗘灇涓剧被鍨嬶紝涓撻棬鐢ㄤ簬瑙e喅姝ょ被闂锛?br>4)銆佹灇涓惧氨鏄竴涓壒娈婄殑java绫伙紝鍙互瀹氫箟灞炴€с€佹柟娉曘€佹瀯閫犲嚱鏁般€佸疄鐜版帴鍙c€佺户鎵跨被锛?br>------------------------------------------------------------------------------
鑷姩鎷嗚绠憋細java涓暟鎹被鍨嬪垎涓轰袱绉?锛?鍩烘湰鏁版嵁绫诲瀷 寮曠敤鏁版嵁绫诲瀷(瀵硅薄)
鍦?java绋嬪簭涓墍鏈夌殑鏁版嵁閮介渶瑕佸綋鍋氬璞℃潵澶勭悊锛岄拡瀵?绉嶅熀鏈暟鎹被鍨嬫彁渚涗簡鍖呰绫伙紝濡備笅锛?br>int --> Integer
byte --> Byte
short --> Short
long --> Long
char --> Character
double --> Double
float --> Float
boolean --> Boolean
jdk5浠ュ墠鍩烘湰鏁版嵁绫诲瀷鍜屽寘瑁呯被涔嬮棿闇€瑕佷簰杞細
鍩烘湰---寮曠敤 Integer x = new Integer(x);
寮曠敤---鍩烘湰 int num = x.intValue();
1)銆両nteger x = 1; x = x + 1; 缁忓巻浜嗕粈涔堣繃绋嬶紵瑁呯 脿 鎷嗙 脿 瑁呯锛?br>2)銆佷负浜嗕紭鍖栵紝铏氭嫙鏈轰负鍖呰绫绘彁渚涗簡缂撳啿姹狅紝Integer姹犵殑澶у皬 -128~127 涓€涓瓧鑺傜殑澶у皬锛?br>3)銆丼tring姹狅細Java涓轰簡浼樺寲瀛楃涓叉搷浣?鎻愪緵浜嗕竴涓紦鍐叉睜锛?br>----------------------------------------------------------
娉涘瀷锛歫dk1.5鐗堟湰浠ュ悗鍑虹幇鐨勪竴涓畨鍏ㄦ満鍒躲€傝〃鐜版牸寮忥細< >
濂藉锛?br>1锛氬皢杩愯鏃舵湡鐨勯棶棰楥lassCastException闂杞崲鎴愪簡缂栬瘧澶辫触锛屼綋鐜板湪缂栬瘧鏃舵湡锛岀▼搴忓憳灏卞彲浠ヨВ鍐抽棶棰樸€?br>2锛氶伩鍏嶄簡寮哄埗杞崲鐨勯夯鐑︺€?br>
鍙甯︽湁<>鐨勭被鎴栬€呮帴鍙o紝閮藉睘浜庡甫鏈夌被鍨嬪弬鏁扮殑绫绘垨鑰呮帴鍙o紝鍦ㄤ娇鐢ㄨ繖浜涚被鎴栬€呮帴鍙f椂锛屽繀椤荤粰<>涓紶閫掍竴涓叿浣撶殑寮曠敤鏁版嵁绫诲瀷銆?br>
娉涘瀷鎶€鏈細鍏跺疄搴旂敤鍦ㄧ紪璇戞椂鏈燂紝鏄粰缂栬瘧鍣ㄤ娇鐢ㄧ殑鎶€鏈紝鍒颁簡杩愯鏃舵湡锛屾硾鍨嬪氨涓嶅瓨鍦ㄤ簡銆?br>涓轰粈涔? 鍥犱负娉涘瀷鐨勬摝闄わ細涔熷氨鏄锛岀紪杈戝櫒妫€鏌ヤ簡娉涘瀷鐨勭被鍨嬫纭悗锛屽湪鐢熸垚鐨勭被鏂囦欢涓槸娌℃湁娉涘瀷鐨勩€?br>鍦ㄨ繍琛屾椂锛屽浣曠煡閬撹幏鍙栫殑鍏冪礌绫诲瀷鑰屼笉鐢ㄥ己杞憿锛?br>娉涘瀷鐨勮ˉ鍋匡細鍥犱负瀛樺偍鐨勬椂鍊欙紝绫诲瀷宸茬粡纭畾浜嗘槸鍚屼竴涓被鍨嬬殑鍏冪礌锛屾墍浠ュ湪杩愯鏃讹紝鍙鑾峰彇鍒拌鍏冪礌鐨勭被鍨嬶紝鍦ㄥ唴閮ㄨ繘琛屼竴娆¤浆鎹㈠嵆鍙紝鎵€浠ヤ娇鐢ㄨ€呬笉鐢ㄥ啀鍋氳浆鎹㈠姩浣滀簡銆?br>
浠€涔堟椂鍊欑敤娉涘瀷绫诲憿锛?br>褰撶被涓殑鎿嶄綔鐨勫紩鐢ㄦ暟鎹被鍨嬩笉纭畾鐨勬椂鍊欙紝浠ュ墠鐢ㄧ殑Object鏉ヨ繘琛屾墿灞曠殑锛岀幇鍦ㄥ彲浠ョ敤娉涘瀷鏉ヨ〃绀恒€傝繖鏍峰彲浠ラ伩鍏嶅己杞殑楹荤儲锛岃€屼笖灏嗚繍琛岄棶棰樿浆绉诲埌鐨勭紪璇戞椂鏈熴€?br>----------------------------------------------------------
娉涘瀷鍦ㄧ▼搴忓畾涔変笂鐨勪綋鐜帮細
//娉涘瀷绫伙細灏嗘硾鍨嬪畾涔夊湪绫讳笂銆?br>class Tool<Q> {
private Q obj;
public void setObject(Q obj) {
this.obj = obj;
}
public Q getObject() {
return obj;
}
}
//褰撴柟娉曟搷浣滅殑寮曠敤鏁版嵁绫诲瀷涓嶇‘瀹氱殑鏃跺€欙紝鍙互灏嗘硾鍨嬪畾涔夊湪鏂规硶涓娿€?br> public <W> void method(W w) {
System.out.println("method:"+w);
}
//闈欐€佹柟娉曚笂鐨勬硾鍨嬶細闈欐€佹柟娉曟棤娉曡闂被涓婂畾涔夌殑娉涘瀷銆傚鏋滈潤鎬佹柟娉曟搷浣滅殑寮曠敤鏁版嵁绫诲瀷涓嶇‘瀹氱殑鏃跺€欙紝蹇呴』瑕佸皢娉涘瀷瀹氫箟鍦ㄦ柟娉曚笂銆?br> public static <Q> void function(Q t) {
System.out.println("function:"+t);
}
//娉涘瀷鎺ュ彛.
interface Inter<T> {
void show(T t);
}
class InterImpl<R> implements Inter<R> {
public void show(R r) {
System.out.println("show:"+r);
}
}
------------------------------------------------------------
娉涘瀷涓殑閫氶厤绗︼細鍙互瑙e喅褰撳叿浣撶被鍨嬩笉纭畾鐨勬椂鍊欙紝杩欎釜閫氶厤绗﹀氨鏄?? 锛涘綋鎿嶄綔绫诲瀷鏃讹紝涓嶉渶瑕佷娇鐢ㄧ被鍨嬬殑鍏蜂綋鍔熻兘鏃讹紝鍙娇鐢∣bject绫讳腑鐨勫姛鑳姐€傞偅涔堝彲浠ョ敤 ? 閫氶厤绗︽潵琛ㄦ湭鐭ョ被鍨嬨€?br>
娉涘瀷闄愬畾锛?br> 涓婇檺锛氾紵extends E锛氬彲浠ユ帴鏀禘绫诲瀷鎴栬€匛鐨勫瓙绫诲瀷瀵硅薄銆?br> 涓嬮檺锛氾紵super E锛氬彲浠ユ帴鏀禘绫诲瀷鎴栬€匛鐨勭埗绫诲瀷瀵硅薄銆?br>
涓婇檺浠€涔堟椂鍊欑敤锛氬線闆嗗悎涓坊鍔犲厓绱犳椂锛屾棦鍙互娣诲姞E绫诲瀷瀵硅薄锛屽張鍙互娣诲姞E鐨勫瓙绫诲瀷瀵硅薄銆備负浠€涔堬紵鍥犱负鍙栫殑鏃跺€欙紝E绫诲瀷鏃㈠彲浠ユ帴鏀禘绫诲璞★紝鍙堝彲浠ユ帴鏀禘鐨勫瓙绫诲瀷瀵硅薄銆?br>
涓嬮檺浠€涔堟椂鍊欑敤锛氬綋浠庨泦鍚堜腑鑾峰彇鍏冪礌杩涜鎿嶄綔鐨勬椂鍊欙紝鍙互鐢ㄥ綋鍓嶅厓绱犵殑绫诲瀷鎺ユ敹锛屼篃鍙互鐢ㄥ綋鍓嶅厓绱犵殑鐖剁被鍨嬫帴鏀躲€?br>
娉涘瀷鐨勭粏鑺傦細
1锛夈€佹硾鍨嬪埌搴曚唬琛ㄤ粈涔堢被鍨嬪彇鍐充簬璋冪敤鑰呬紶鍏ョ殑绫诲瀷锛屽鏋滄病浼狅紝榛樿鏄疧bject绫诲瀷锛?br>2锛夈€佷娇鐢ㄥ甫娉涘瀷鐨勭被鍒涘缓瀵硅薄鏃讹紝绛夊紡涓よ竟鎸囧畾鐨勬硾鍨嬪繀椤讳竴鑷达紱
鍘熷洜锛氱紪璇戝櫒妫€鏌ュ璞¤皟鐢ㄦ柟娉曟椂鍙湅鍙橀噺锛岀劧鑰岀▼搴忚繍琛屾湡闂磋皟鐢ㄦ柟娉曟椂灏辫鑰冭檻瀵硅薄鍏蜂綋绫诲瀷浜嗭紱
3锛夈€佺瓑寮忎袱杈瑰彲浠ュ湪浠绘剰涓€杈逛娇鐢ㄦ硾鍨嬶紝鍦ㄥ彟涓€杈逛笉浣跨敤(鑰冭檻鍚戝悗鍏煎)锛?br>ArrayList<String> al = new ArrayList<Object>(); //閿?br>//瑕佷繚璇佸乏鍙充袱杈圭殑娉涘瀷鍏蜂綋绫诲瀷涓€鑷村氨鍙互浜嗭紝杩欐牱涓嶅鏄撳嚭閿欍€?br>ArrayList<? extends Object> al = new ArrayList<String>();
al.add("aa"); //閿?br>//鍥犱负闆嗗悎鍏蜂綋瀵硅薄涓棦鍙瓨鍌⊿tring锛屼篃鍙互瀛樺偍Object鐨勫叾浠栧瓙绫伙紝鎵€浠ユ坊鍔犲叿浣撶殑绫诲瀷瀵硅薄涓嶅悎閫傦紝绫诲瀷妫€鏌ヤ細鍑虹幇瀹夊叏闂銆?锛焑xtends Object 浠h〃Object鐨勫瓙绫诲瀷涓嶇‘瀹氾紝鎬庝箞鑳芥坊鍔犲叿浣撶被鍨嬬殑瀵硅薄鍛紵
public static void method(ArrayList<? extends Object> al) {
al.add("abc"); //閿?br> //鍙兘瀵筧l闆嗗悎涓殑鍏冪礌璋冪敤Object绫讳腑鐨勬柟娉曪紝鍏蜂綋瀛愮被鍨嬬殑鏂规硶閮戒笉鑳界敤锛屽洜涓哄瓙绫诲瀷涓嶇‘瀹氥€?br>}
------------------------------------------------------------------------------------------------------------------------------------------------
IO娴侊細鈽呪槄鈽呪槄鈽咃紝
鐢ㄤ簬澶勭悊璁惧涓婃暟鎹€?br>娴侊細鍙互鐞嗚В鏁版嵁鐨勬祦鍔紝灏辨槸涓€涓暟鎹祦銆侷O娴佹渶缁堣浠ュ璞℃潵浣撶幇锛屽璞¢兘瀛樺湪IO鍖呬腑銆?br>娴佷篃杩涜鍒嗙被锛?br>1锛氳緭鍏ユ祦锛堣锛夊拰杈撳嚭娴侊紙鍐欙級銆?br>2锛氬洜涓哄鐞嗙殑鏁版嵁涓嶅悓锛屽垎涓哄瓧鑺傛祦鍜屽瓧绗︽祦銆?br>
瀛楄妭娴侊細
澶勭悊瀛楄妭鏁版嵁鐨勬祦瀵硅薄銆傝澶囦笂鐨勬暟鎹棤璁烘槸鍥剧墖鎴栬€卍vd锛屾枃瀛楋紝瀹冧滑閮戒互浜岃繘鍒跺瓨鍌ㄧ殑銆備簩杩涘埗鐨勬渶缁堥兘鏄互涓€涓?浣嶄负鏁版嵁鍗曞厓杩涜浣撶幇锛屾墍浠ヨ绠楁満涓殑鏈€灏忔暟鎹崟鍏冨氨鏄瓧鑺傘€傛剰鍛崇潃锛屽瓧鑺傛祦鍙互澶勭悊璁惧涓婄殑鎵€鏈夋暟鎹紝鎵€浠ュ瓧鑺傛祦涓€鏍峰彲浠ュ鐞嗗瓧绗︽暟鎹€?br>
閭d箞涓轰粈涔堣鏈夊瓧绗︽祦鍛紵鍥犱负瀛楃姣忎釜鍥藉閮戒笉涓€鏍凤紝鎵€浠ユ秹鍙婂埌浜嗗瓧绗︾紪鐮侀棶棰橈紝閭d箞GBK缂栫爜鐨勪腑鏂囩敤unicode缂栫爜瑙f瀽鏄湁闂鐨勶紝鎵€浠ラ渶瑕佽幏鍙栦腑鏂囧瓧鑺傛暟鎹殑鍚屾椂+ 鎸囧畾鐨勭紪鐮佽〃鎵嶅彲浠ヨВ鏋愭纭暟鎹€備负浜嗘柟渚夸簬鏂囧瓧鐨勮В鏋愶紝鎵€浠ュ皢瀛楄妭娴佸拰缂栫爜琛ㄥ皝瑁呮垚瀵硅薄锛岃繖涓璞″氨鏄瓧绗︽祦銆傚彧瑕佹搷浣滃瓧绗︽暟鎹紝浼樺厛鑰冭檻浣跨敤瀛楃娴佷綋绯汇€?br>
娉ㄦ剰锛氭祦鐨勬搷浣滃彧鏈変袱绉嶏細璇诲拰鍐欍€?br>
娴佺殑浣撶郴鍥犱负鍔熻兘涓嶅悓锛屼絾鏄湁鍏辨€у唴瀹癸紝涓嶆柇鎶藉彇锛屽舰鎴愮户鎵夸綋绯汇€傝浣撶郴涓€鍏辨湁鍥涗釜鍩虹被锛岃€屼笖閮芥槸鎶借薄绫汇€?br>
瀛楄妭娴侊細InputStream OutputStream
瀛楃娴侊細Reader Writer
鍦ㄨ繖鍥涗釜绯荤粺涓紝瀹冧滑鐨勫瓙绫伙紝閮芥湁涓€涓叡鎬х壒鐐癸細瀛愮被鍚嶅悗缂€閮芥槸鐖剁被鍚嶏紝鍓嶇紑鍚嶉兘鏄繖涓瓙绫荤殑鍔熻兘鍚嶇О銆?br>--------------------------------------------------------------------------------------------------------------------
public static void main(String[] args) throws IOException { //璇汇€佸啓閮戒細鍙戠敓IO寮傚父
/*
1锛氬垱寤轰竴涓瓧绗﹁緭鍑烘祦瀵硅薄锛岀敤浜庢搷浣滄枃浠躲€傝瀵硅薄涓€寤虹珛锛屽氨蹇呴』鏄庣‘鏁版嵁瀛樺偍浣嶇疆锛屾槸涓€涓枃浠躲€?br> 2锛氬璞′骇鐢熷悗锛屼細鍦ㄥ爢鍐呭瓨涓湁涓€涓疄浣擄紝鍚屾椂涔熻皟鐢ㄤ簡绯荤粺搴曞眰璧勬簮锛屽湪鎸囧畾鐨勪綅缃垱寤轰簡涓€涓瓨鍌ㄦ暟鎹殑鏂囦欢銆?br> 3锛氬鏋滄寚瀹氫綅缃紝鍑虹幇浜嗗悓鍚嶆枃浠讹紝鏂囦欢浼氳瑕嗙洊銆?br> */
FileWriter fw = new FileWriter("demo.txt"); // FileNotFoundException
/*
璋冪敤Writer绫讳腑鐨剋rite鏂规硶鍐欏叆瀛楃涓层€傚瓧绗︿覆骞舵湭鐩存帴鍐欏叆鍒扮洰鐨勫湴涓紝鑰屾槸鍐欏叆鍒颁簡娴佷腑锛?鍏跺疄鏄啓鍏ュ埌鍐呭瓨缂撳啿鍖轰腑)銆傛€庝箞鎶婃暟鎹紕鍒版枃浠朵腑锛?br> */
fw.write("abcde");
fw.flush(); // 鍒锋柊缂撳啿鍖猴紝灏嗙紦鍐插尯涓殑鏁版嵁鍒峰埌鐩殑鍦版枃浠朵腑銆?br> fw.close(); // 鍏抽棴娴侊紝鍏跺疄鍏抽棴鐨勫氨鏄痡ava璋冪敤鐨勭郴缁熷簳灞傝祫婧愩€傚湪鍏抽棴鍓嶏紝浼氬厛鍒锋柊璇ユ祦銆?br>}
close()鍜宖lush()鐨勫尯鍒細
flush()锛氬皢缂撳啿鍖虹殑鏁版嵁鍒峰埌鐩殑鍦颁腑鍚庯紝娴佸彲浠ヤ娇鐢ㄣ€?br>close()锛氬皢缂撳啿鍖虹殑鏁版嵁鍒峰埌鐩殑鍦颁腑鍚庯紝娴佸氨鍏抽棴浜嗭紝璇ユ柟娉曚富瑕佺敤浜庣粨鏉熻皟鐢ㄧ殑搴曞眰璧勬簮銆傝繖涓姩浣滀竴瀹氬仛銆?br>--------------------------------------------------------------------------------------------------------------------
io寮傚父鐨勫鐞嗘柟寮忥細
io涓€瀹氳鍐檉inally锛?br>
FileWriter鍐欏叆鏁版嵁鐨勭粏鑺傦細
1锛歸indow涓殑鎹㈣绗︼細
涓や釜绗﹀彿缁勬垚銆?linux锛歕n銆?br>2锛氱画鍐欐暟鎹紝鍙鍦ㄦ瀯閫犲嚱鏁颁腑浼犲叆鏂扮殑鍙傛暟true銆?br>3锛氱洰褰曞垎鍓茬锛歸indow \ /
public static void main(String[] args) {
FileWriter fw = null;
try {
fw = new FileWriter("demo.txt",true);
fw.write("abcde");
}
catch (IOException e ){
System.out.println(e.toString()+"....");
}
finally{
if(fw!=null)
try{
fw.close();
}
catch (IOException e){
System.out.println("close:"+e.toString());
}
}
}
--------------------------------------------------------------------------------------------------------------------
FileReader锛氫娇鐢≧eader浣撶郴锛岃鍙栦竴涓枃鏈枃浠朵腑鐨勬暟鎹€傝繑鍥?-1 锛屾爣蹇楄鍒扮粨灏俱€?br>import java.io.*;
class FileReaderDemo {
public static void main(String[] args) throws IOException {
/*
鍒涘缓鍙互璇诲彇鏂囨湰鏂囦欢鐨勬祦瀵硅薄锛孎ileReader璁╁垱寤哄ソ鐨勬祦瀵硅薄鍜屾寚瀹氱殑鏂囦欢鐩稿叧鑱斻€?br> */
FileReader fr = new FileReader("demo.txt");
int ch = 0;
while((ch = fr.read())!= -1) { //鏉′欢鏄病鏈夎鍒扮粨灏?br> System.out.println((char)ch); //璋冪敤璇诲彇娴佺殑read鏂规硶锛岃鍙栦竴涓瓧绗︺€?br> }
fr.close();
}
}
--------------------------------------------------------------------------------------------------------------------
璇诲彇鏁版嵁鐨勭浜岀鏂瑰紡锛氱浜岀鏂瑰紡杈冧负楂樻晥锛岃嚜瀹氫箟缂撳啿鍖恒€?br>import java.io.*;
class FileReaderDemo2 {
public static void main(String[] args) throws IOException {
FileReader fr = new FileReader("demo.txt"); //鍒涘缓璇诲彇娴佸璞″拰鎸囧畾鏂囦欢鍏宠仈銆?br> //鍥犱负瑕佷娇鐢╮ead(char[])鏂规硶锛屽皢璇诲彇鍒板瓧绗﹀瓨鍏ユ暟缁勩€傛墍浠ヨ鍒涘缓涓€涓瓧绗︽暟缁勶紝涓€鑸暟缁勭殑闀垮害閮芥槸1024鐨勬暣鏁板€嶃€?br> char[] buf = new char[1024];
int len = 0;
while(( len=fr.read(buf)) != -1) {
System.out.println(new String(buf,0,len));
}
fr.close();
}
}
--------------------------------------------------------------------------------------------------------------------
IO涓殑浣跨敤鍒颁簡涓€涓璁℃ā寮忥細瑁呴グ璁捐妯″紡銆?br>瑁呴グ璁捐妯″紡瑙e喅锛氬涓€缁勭被杩涜鍔熻兘鐨勫寮恒€?br>鍖呰锛氬啓涓€涓被(鍖呰绫?瀵硅鍖呰瀵硅薄杩涜鍖呰锛?br> * 1銆佸寘瑁呯被鍜岃鍖呰瀵硅薄瑕佸疄鐜板悓鏍风殑鎺ュ彛锛?br> * 2銆佸寘瑁呯被瑕佹寔鏈変竴涓鍖呰瀵硅薄锛?br> * 3銆佸寘瑁呯被鍦ㄥ疄鐜版帴鍙f椂锛屽ぇ閮ㄥ垎鏂规硶鏄潬璋冪敤琚寘瑁呭璞℃潵瀹炵幇鐨勶紝瀵逛簬闇€瑕佷慨鏀圭殑鏂规硶鎴戜滑鑷繁瀹炵幇锛?br>--------------------------------------------------------------------------------------------------------------------
瀛楃娴侊細
Reader锛氱敤浜庤鍙栧瓧绗︽祦鐨勬娊璞$被銆傚瓙绫诲繀椤诲疄鐜扮殑鏂规硶鍙湁 read(char[], int, int) 鍜?close()銆?br> |---BufferedReader锛氫粠瀛楃杈撳叆娴佷腑璇诲彇鏂囨湰锛岀紦鍐插悇涓瓧绗︼紝浠庤€屽疄鐜板瓧绗︺€佹暟缁勫拰琛岀殑楂樻晥璇诲彇銆?鍙互鎸囧畾缂撳啿鍖虹殑澶у皬锛屾垨鑰呭彲浣跨敤榛樿鐨勫ぇ灏忋€傚ぇ澶氭暟鎯呭喌涓嬶紝榛樿鍊煎氨瓒冲澶т簡銆?br> |---LineNumberReader锛氳窡韪鍙风殑缂撳啿瀛楃杈撳叆娴併€傛绫诲畾涔変簡鏂规硶 setLineNumber(int) 鍜?getLineNumber()锛屽畠浠彲鍒嗗埆鐢ㄤ簬璁剧疆鍜岃幏鍙栧綋鍓嶈鍙枫€?br> |---InputStreamReader锛氭槸瀛楄妭娴侀€氬悜瀛楃娴佺殑妗ユ锛氬畠浣跨敤鎸囧畾鐨?charset 璇诲彇瀛楄妭骞跺皢鍏惰В鐮佷负瀛楃銆傚畠浣跨敤鐨勫瓧绗﹂泦鍙互鐢卞悕绉版寚瀹氭垨鏄惧紡缁欏畾锛屾垨鑰呭彲浠ユ帴鍙楀钩鍙伴粯璁ょ殑瀛楃闆嗐€?br> |---FileReader锛氱敤鏉ヨ鍙栧瓧绗︽枃浠剁殑渚挎嵎绫汇€傛绫荤殑鏋勯€犳柟娉曞亣瀹氶粯璁ゅ瓧绗︾紪鐮佸拰榛樿瀛楄妭缂撳啿鍖哄ぇ灏忛兘鏄€傚綋鐨勩€傝鑷繁鎸囧畾杩欎簺鍊硷紝鍙互鍏堝湪 FileInputStream 涓婃瀯閫犱竴涓?InputStreamReader銆?br> |---CharArrayReader锛?br> |---StringReader锛?br>-------------------------------------------------
Writer锛氬啓鍏ュ瓧绗︽祦鐨勬娊璞$被銆傚瓙绫诲繀椤诲疄鐜扮殑鏂规硶浠呮湁 write(char[], int, int)銆乫lush() 鍜?close()銆?br> |---BufferedWriter锛氬皢鏂囨湰鍐欏叆瀛楃杈撳嚭娴侊紝缂撳啿鍚勪釜瀛楃锛屼粠鑰屾彁渚涘崟涓瓧绗︺€佹暟缁勫拰瀛楃涓茬殑楂樻晥鍐欏叆銆?br> |---OutputStreamWriter锛氭槸瀛楃娴侀€氬悜瀛楄妭娴佺殑妗ユ锛氬彲浣跨敤鎸囧畾鐨?charset 灏嗚鍐欏叆娴佷腑鐨勫瓧绗︾紪鐮佹垚瀛楄妭銆傚畠浣跨敤鐨勫瓧绗﹂泦鍙互鐢卞悕绉版寚瀹氭垨鏄惧紡缁欏畾锛屽惁鍒欏皢鎺ュ彈骞冲彴榛樿鐨勫瓧绗﹂泦銆?br> |---FileWriter锛氱敤鏉ュ啓鍏ュ瓧绗︽枃浠剁殑渚挎嵎绫汇€傛绫荤殑鏋勯€犳柟娉曞亣瀹氶粯璁ゅ瓧绗︾紪鐮佸拰榛樿瀛楄妭缂撳啿鍖哄ぇ灏忛兘鏄彲鎺ュ彈鐨勩€傝鑷繁鎸囧畾杩欎簺鍊硷紝鍙互鍏堝湪 FileOutputStream 涓婃瀯閫犱竴涓?OutputStreamWriter銆?br> |---PrintWriter锛?br> |---CharArrayWriter锛?br> |---StringWriter锛?br>---------------------------------
瀛楄妭娴侊細
InputStream锛氭槸琛ㄧず瀛楄妭杈撳叆娴佺殑鎵€鏈夌被鐨勮秴绫汇€?br> |--- FileInputStream锛氫粠鏂囦欢绯荤粺涓殑鏌愪釜鏂囦欢涓幏寰楄緭鍏ュ瓧鑺傘€傚摢浜涙枃浠跺彲鐢ㄥ彇鍐充簬涓绘満鐜銆侳ileInputStream 鐢ㄤ簬璇诲彇璇稿鍥惧儚鏁版嵁涔嬬被鐨勫師濮嬪瓧鑺傛祦銆傝璇诲彇瀛楃娴侊紝璇疯€冭檻浣跨敤 FileReader銆?br> |--- FilterInputStream锛氬寘鍚叾浠栦竴浜涜緭鍏ユ祦锛屽畠灏嗚繖浜涙祦鐢ㄤ綔鍏跺熀鏈暟鎹簮锛屽畠鍙互鐩存帴浼犺緭鏁版嵁鎴栨彁渚涗竴浜涢澶栫殑鍔熻兘銆?br> |--- BufferedInputStream锛氳绫诲疄鐜扮紦鍐茬殑杈撳叆娴併€?br> |--- Stream锛?br> |--- ObjectInputStream锛?br> |--- PipedInputStream锛?br>-----------------------------------------------
OutputStream锛氭鎶借薄绫绘槸琛ㄧず杈撳嚭瀛楄妭娴佺殑鎵€鏈夌被鐨勮秴绫汇€?br> |--- FileOutputStream锛氭枃浠惰緭鍑烘祦鏄敤浜庡皢鏁版嵁鍐欏叆 File 鎴?FileDescriptor 鐨勮緭鍑烘祦銆?br> |--- FilterOutputStream锛氭绫绘槸杩囨护杈撳嚭娴佺殑鎵€鏈夌被鐨勮秴绫汇€?br> |--- BufferedOutputStream锛氳绫诲疄鐜扮紦鍐茬殑杈撳嚭娴併€?br> |--- PrintStream锛?br> |--- DataOutputStream锛?br> |--- ObjectOutputStream锛?br> |--- PipedOutputStream锛?br>--------------------------------
缂撳啿鍖烘槸鎻愰珮鏁堢巼鐢ㄧ殑锛岀粰璋佹彁楂樺憿锛?br>BufferedWriter锛氭槸缁欏瓧绗﹁緭鍑烘祦鎻愰珮鏁堢巼鐢ㄧ殑锛岄偅灏辨剰鍛崇潃锛岀紦鍐插尯瀵硅薄寤虹珛鏃讹紝蹇呴』瑕佸厛鏈夋祦瀵硅薄銆傛槑纭鎻愰珮鍏蜂綋鐨勬祦瀵硅薄鐨勬晥鐜囥€?br> FileWriter fw = new FileWriter("bufdemo.txt");
BufferedWriter bufw = new BufferedWriter(fw);//璁╃紦鍐插尯鍜屾寚瀹氭祦鐩稿叧鑱斻€?br> for(int x=0; x<4; x++){
bufw.write(x+"abc");
bufw.newLine(); //鍐欏叆涓€涓崲琛岀锛岃繖涓崲琛岀鍙互渚濇嵁骞冲彴鐨勪笉鍚屽啓鍏ヤ笉鍚岀殑鎹㈣绗︺€?br> bufw.flush();//瀵圭紦鍐插尯杩涜鍒锋柊锛屽彲浠ヨ鏁版嵁鍒扮洰鐨勫湴涓€?br> }
bufw.close();//鍏抽棴缂撳啿鍖猴紝鍏跺疄灏辨槸鍦ㄥ叧闂叿浣撶殑娴併€?br>-----------------------------
BufferedReader锛?br> FileReader fr = new FileReader("bufdemo.txt");
BufferedReader bufr = new BufferedReader(fr);
String line = null;
while((line=bufr.readLine())!=null){ //readLine鏂规硶杩斿洖鐨勬椂鍊欐槸涓嶅甫鎹㈣绗︾殑銆?br> System.out.println(line);
}
bufr.close();
-----------------------------
//璁颁綇锛屽彧瑕佷竴璇诲彇閿洏褰曞叆锛屽氨鐢ㄨ繖鍙ヨ瘽銆?br>BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bufw = new BufferedWriter(new OutputStreamWriter(System.out));//杈撳嚭鍒版帶鍒跺彴
String line = null;
while((line=bufr.readLine())!=null){
if("over".equals(line))
break;
bufw.write(line.toUpperCase());//灏嗚緭鍏ョ殑瀛楃杞垚澶у啓瀛楃杈撳嚭
bufw.newLine();
bufw.flush();
}
bufw.close();
bufr.close();
------------------------------
娴佸璞★細
鍏跺疄寰堢畝鍗曪紝灏辨槸璇诲彇鍜屽啓鍏ャ€備絾鏄洜涓哄姛鑳界殑涓嶅悓锛屾祦鐨勪綋绯讳腑鎻愪緵N澶氱殑瀵硅薄銆傞偅涔堝紑濮嬫椂锛屽埌搴曡鐢ㄥ摢涓璞℃洿涓哄悎閫傚憿锛熻繖灏遍渶瑕佹槑纭祦鐨勬搷浣滆寰嬨€?br>
娴佺殑鎿嶄綔瑙勫緥锛?br>1锛屾槑纭簮鍜岀洰鐨勩€?br> 鏁版嵁婧愶細灏辨槸闇€瑕佽鍙栵紝鍙互浣跨敤涓や釜浣撶郴锛欼nputStream銆丷eader锛?br> 鏁版嵁姹囷細灏辨槸闇€瑕佸啓鍏ワ紝鍙互浣跨敤涓や釜浣撶郴锛歄utputStream銆乄riter锛?br>2锛屾搷浣滅殑鏁版嵁鏄惁鏄函鏂囨湰鏁版嵁锛?br> 濡傛灉鏄細鏁版嵁婧愶細Reader
鏁版嵁姹囷細Writer
濡傛灉涓嶆槸锛氭暟鎹簮锛欼nputStream
鏁版嵁姹囷細OutputStream
3锛岃櫧鐒剁‘瀹氫簡涓€涓綋绯伙紝浣嗘槸璇ヤ綋绯讳腑鏈夊お澶氱殑瀵硅薄锛屽埌搴曠敤鍝釜鍛紵
鏄庣‘鎿嶄綔鐨勬暟鎹澶囥€?br> 鏁版嵁婧愬搴旂殑璁惧锛氱‖鐩?File)锛屽唴瀛?鏁扮粍)锛岄敭鐩?System.in)
鏁版嵁姹囧搴旂殑璁惧锛氱‖鐩?File)锛屽唴瀛?鏁扮粍)锛屾帶鍒跺彴(System.out)銆?br>4锛岄渶瑕佸湪鍩烘湰鎿嶄綔涓婇檮鍔犲叾浠栧姛鑳藉悧锛熸瘮濡傜紦鍐层€?br> 濡傛灉闇€瑕佸氨杩涜瑁呴グ銆?br>
杞崲娴佺壒鏈夊姛鑳斤細杞崲娴佸彲浠ュ皢瀛楄妭杞垚瀛楃锛屽師鍥犲湪浜庯紝灏嗚幏鍙栧埌鐨勫瓧鑺傞€氳繃鏌ョ紪鐮佽〃鑾峰彇鍒版寚瀹氬搴斿瓧绗︺€?br>
杞崲娴佺殑鏈€寮哄姛鑳藉氨鏄熀浜?瀛楄妭娴?+ 缂栫爜琛?銆傛病鏈夎浆鎹紝娌℃湁瀛楃娴併€?br>
鍙戠幇杞崲娴佹湁涓€涓瓙绫诲氨鏄搷浣滄枃浠剁殑瀛楃娴佸璞★細
InputStreamReader
|--FileReader
OutputStreamWriter
|--FileWrier
鎯宠鎿嶄綔鏂囨湰鏂囦欢锛屽繀椤昏杩涜缂栫爜杞崲锛岃€岀紪鐮佽浆鎹㈠姩浣滆浆鎹㈡祦閮藉畬鎴愪簡銆傛墍浠ユ搷浣滄枃浠剁殑娴佸璞″彧瑕佺户鎵胯嚜杞崲娴佸氨鍙互璇诲彇涓€涓瓧绗︿簡銆?br>
浣嗘槸瀛愮被鏈変竴涓眬闄愭€э紝灏辨槸瀛愮被涓娇鐢ㄧ殑缂栫爜鏄浐瀹氱殑锛屾槸鏈満榛樿鐨勭紪鐮佽〃锛屽浜庣畝浣撲腑鏂囩増鐨勭郴缁熼粯璁ょ爜琛ㄦ槸GBK銆?br>FileReader fr = new FileReader("a.txt");
InputStreamReader isr = new InputStreamReader(new FileInputStream("a.txt"),"gbk");
浠ヤ笂涓ゅ彞浠g爜鍔熻兘涓€鑷达紝
濡傛灉浠呬粎浣跨敤骞冲彴榛樿鐮佽〃锛屽氨浣跨敤FileReader fr = new FileReader("a.txt"); //鍥犱负绠€鍖栥€?br>
濡傛灉闇€瑕佸埗瀹氱爜琛紝蹇呴』鐢ㄨ浆鎹㈡祦銆?br>杞崲娴?= 瀛楄妭娴?缂栫爜琛ㄣ€?br>杞崲娴佺殑瀛愮被File = 瀛楄妭娴?+ 榛樿缂栫爜琛ㄣ€?br>
鍑℃槸鎿嶄綔璁惧涓婄殑鏂囨湰鏁版嵁锛屾秹鍙婄紪鐮佽浆鎹紝蹇呴』浣跨敤杞崲娴併€?br>-----------------------------------------------------------------------------------------------
File绫伙細
灏嗘枃浠剁郴缁熶腑鐨勬枃浠跺拰鏂囦欢澶瑰皝瑁呮垚浜嗗璞°€傛彁渚涗簡鏇村鐨勫睘鎬у拰琛屼负鍙互瀵硅繖浜涙枃浠跺拰鏂囦欢澶硅繘琛屾搷浣溿€傝繖浜涙槸娴佸璞″姙涓嶅埌鐨勶紝鍥犱负娴佸彧鎿嶄綔鏁版嵁銆?br>
File绫诲父瑙佹柟娉曪細
1锛氬垱寤恒€?br> boolean createNewFile()锛氬湪鎸囧畾鐩綍涓嬪垱寤烘枃浠讹紝濡傛灉璇ユ枃浠跺凡瀛樺湪锛屽垯涓嶅垱寤恒€傝€屽鎿嶄綔鏂囦欢鐨勮緭鍑烘祦鑰岃█锛岃緭鍑烘祦瀵硅薄宸插缓绔嬶紝灏变細鍒涘缓鏂囦欢锛屽鏋滄枃浠跺凡瀛樺湪锛屼細瑕嗙洊銆傞櫎闈炵画鍐欍€?br> boolean mkdir()锛氬垱寤烘鎶借薄璺緞鍚嶆寚瀹氱殑鐩綍銆?br> boolean mkdirs()锛氬垱寤哄绾х洰褰曘€?br>2锛氬垹闄ゃ€?br> boolean delete()锛氬垹闄ゆ鎶借薄璺緞鍚嶈〃绀虹殑鏂囦欢鎴栫洰褰曘€?br> void deleteOnExit()锛氬湪铏氭嫙鏈洪€€鍑烘椂鍒犻櫎銆?br>娉ㄦ剰锛氬湪鍒犻櫎鏂囦欢澶规椂锛屽繀椤讳繚璇佽繖涓枃浠跺す涓病鏈変换浣曞唴瀹癸紝鎵嶅彲浠ュ皢璇ユ枃浠跺す鐢╠elete鍒犻櫎銆?br> window鐨勫垹闄ゅ姩浣滐紝鏄粠閲屽線澶栧垹銆傛敞鎰忥細java鍒犻櫎鏂囦欢涓嶈蛋鍥炴敹绔欍€傝鎱庣敤銆?br>3锛氳幏鍙?
long length()锛氳幏鍙栨枃浠跺ぇ灏忋€?br> String getName()锛氳繑鍥炵敱姝ゆ娊璞¤矾寰勫悕琛ㄧず鐨勬枃浠舵垨鐩綍鐨勫悕绉般€?br> String getPath()锛氬皢姝ゆ娊璞¤矾寰勫悕杞崲涓轰竴涓矾寰勫悕瀛楃涓层€?br> String getAbsolutePath()锛氳繑鍥炴鎶借薄璺緞鍚嶇殑缁濆璺緞鍚嶅瓧绗︿覆銆?br> String getParent()锛氳繑鍥炴鎶借薄璺緞鍚嶇埗鐩綍鐨勬娊璞¤矾寰勫悕锛屽鏋滄璺緞鍚嶆病鏈夋寚瀹氱埗鐩綍锛屽垯杩斿洖 null銆?br>long lastModified()锛氳繑鍥炴鎶借薄璺緞鍚嶈〃绀虹殑鏂囦欢鏈€鍚庝竴娆¤淇敼鐨勬椂闂淬€?br>File.pathSeparator锛氳繑鍥炲綋鍓嶇郴缁熼粯璁ょ殑璺緞鍒嗛殧绗︼紝windows榛樿涓?鈥滐紱鈥濄€?br>File.Separator锛氳繑鍥炲綋鍓嶇郴缁熼粯璁ょ殑鐩綍鍒嗛殧绗︼紝windows榛樿涓?鈥淺鈥濄€?br>4锛氬垽鏂細
boolean exists()锛氬垽鏂枃浠舵垨鑰呮枃浠跺す鏄惁瀛樺湪銆?br> boolean isDirectory()锛氭祴璇曟鎶借薄璺緞鍚嶈〃绀虹殑鏂囦欢鏄惁鏄竴涓洰褰曘€?br> boolean isFile()锛氭祴璇曟鎶借薄璺緞鍚嶈〃绀虹殑鏂囦欢鏄惁鏄竴涓爣鍑嗘枃浠躲€?br> boolean isHidden()锛氭祴璇曟鎶借薄璺緞鍚嶆寚瀹氱殑鏂囦欢鏄惁鏄竴涓殣钘忔枃浠躲€?br> boolean isAbsolute()锛氭祴璇曟鎶借薄璺緞鍚嶆槸鍚︿负缁濆璺緞鍚嶃€?br>5锛氶噸鍛藉悕銆?br> boolean renameTo(File dest)锛氬彲浠ュ疄鐜扮Щ鍔ㄧ殑鏁堟灉銆傚壀鍒?閲嶅懡鍚嶃€?br>
String[] list()锛氬垪鍑烘寚瀹氱洰褰曚笅鐨勫綋鍓嶇殑鏂囦欢鍜屾枃浠跺す鐨勫悕绉般€傚寘鍚殣钘忔枃浠躲€?br>濡傛灉璋冪敤list鏂规硶鐨凢ile 瀵硅薄涓皝瑁呯殑鏄竴涓枃浠讹紝閭d箞list鏂规硶杩斿洖鏁扮粍涓簄ull銆傚鏋滃皝瑁呯殑瀵硅薄涓嶅瓨鍦ㄤ篃浼氳繑鍥瀗ull銆傚彧鏈夊皝瑁呯殑瀵硅薄瀛樺湪骞朵笖鏄枃浠跺す鏃讹紝杩欎釜鏂规硶鎵嶆湁鏁堛€?br>------------------------------------------------------------------------------------------------
閫掑綊锛氬氨鏄嚱鏁拌嚜韬皟鐢ㄨ嚜韬€?/h1>
浠€涔堟椂鍊欑敤閫掑綊鍛紵
褰撲竴涓姛鑳借閲嶅浣跨敤锛岃€屾瘡涓€娆′娇鐢ㄨ鍔熻兘鏃剁殑鍙傛暟涓嶇‘瀹氾紝閮界敱涓婃鐨勫姛鑳藉厓绱犵粨鏋滄潵纭畾銆?br>绠€鍗曡锛氬姛鑳藉唴閮ㄥ張鐢ㄥ埌璇ュ姛鑳斤紝浣嗘槸浼犻€掔殑鍙傛暟鍊间笉纭畾銆?姣忔鍔熻兘鍙備笌杩愮畻鐨勬湭鐭ュ唴瀹逛笉纭畾)銆?br>
閫掑綊鐨勬敞鎰忎簨椤癸細
1锛氫竴瀹氳瀹氫箟閫掑綊鐨勬潯浠躲€?br>2锛氶€掑綊鐨勬鏁颁笉瑕佽繃澶氥€傚鏄撳嚭鐜?StackOverflowError 鏍堝唴瀛樻孩鍑洪敊璇€?br>鍏跺疄閫掑綊灏辨槸鍦ㄦ爤鍐呭瓨涓笉鏂殑鍔犺浇鍚屼竴涓嚱鏁般€?br>------------------------------------------------------------------------------------------------
Java.util.Properties锛?/h3>
List锛氭湁搴?鍏冪礌瀛樺叆闆嗗悎鐨勯『搴忓拰鍙栧嚭鐨勯『搴忎竴鑷?锛屽厓绱犻兘鏈夌储寮曘€傚厓绱犲彲浠ラ噸澶嶃€?br> |--ArrayList锛氬簳灞傜殑鏁版嵁缁撴瀯鏄暟缁?绾跨▼涓嶅悓姝ワ紝ArrayList鏇夸唬浜哣ector锛屾煡璇㈠厓绱犵殑閫熷害闈炲父蹇€?br> |--LinkedList锛氬簳灞傜殑鏁版嵁缁撴瀯鏄摼琛紝绾跨▼涓嶅悓姝ワ紝澧炲垹鍏冪礌鐨勯€熷害闈炲父蹇€?br> |--Vector锛氬簳灞傜殑鏁版嵁缁撴瀯灏辨槸鏁扮粍锛岀嚎绋嬪悓姝ョ殑锛孷ector鏃犺鏌ヨ鍜屽鍒犻兘宸ㄦ參銆?br>
1锛屾坊鍔狅細
add(index,element) 锛氬湪鎸囧畾鐨勭储寮曚綅鎻掑叆鍏冪礌銆?br> addAll(index,collection) 锛氬湪鎸囧畾鐨勭储寮曚綅鎻掑叆涓€鍫嗗厓绱犮€?br>2锛屽垹闄わ細
remove(index) 锛氬垹闄ゆ寚瀹氱储寮曚綅鐨勫厓绱犮€?杩斿洖琚垹鐨勫厓绱犮€?br>3锛岃幏鍙栵細
Object get(index) 锛氶€氳繃绱㈠紩鑾峰彇鎸囧畾鍏冪礌銆?br> int indexOf(obj) 锛氳幏鍙栨寚瀹氬厓绱犵涓€娆″嚭鐜扮殑绱㈠紩浣嶏紝濡傛灉璇ュ厓绱犱笉瀛樺湪杩斿洖-1锛?br> 鎵€浠ワ紝閫氳繃-1锛屽彲浠ュ垽鏂竴涓厓绱犳槸鍚﹀瓨鍦ㄣ€?br> int lastIndexOf(Object o) 锛氬弽鍚戠储寮曟寚瀹氬厓绱犵殑浣嶇疆銆?br> List subList(start,end) 锛氳幏鍙栧瓙鍒楄〃銆?br>4锛屼慨鏀癸細
Object set(index,element) 锛氬鎸囧畾绱㈠紩浣嶈繘琛屽厓绱犵殑淇敼銆?br>5锛岃幏鍙栨墍鏈夊厓绱狅細
ListIterator listIterator()锛歭ist闆嗗悎鐗规湁鐨勮凯浠e櫒銆?br>
List闆嗗悎鏀寔瀵瑰厓绱犵殑澧炪€佸垹銆佹敼銆佹煡銆?br>
List闆嗗悎鍥犱负瑙掓爣鏈変簡鑷繁鐨勮幏鍙栧厓绱犵殑鏂瑰紡锛?閬嶅巻銆?br>for(int x=0; x<list.size(); x++){
sop("get:"+list.get(x));
}
鍦ㄨ繘琛宭ist鍒楄〃鍏冪礌杩唬鐨勬椂鍊欙紝濡傛灉鎯宠鍦ㄨ凯浠h繃绋嬩腑锛屾兂瑕佸鍏冪礌杩涜鎿嶄綔鐨勬椂鍊欙紝姣斿婊¤冻鏉′欢娣诲姞鏂板厓绱犮€備細鍙戠敓.ConcurrentModificationException骞跺彂淇敼寮傚父銆?br>瀵艰嚧鐨勫師鍥犳槸锛?br>闆嗗悎寮曠敤鍜岃凯浠e櫒寮曠敤鍦ㄥ悓鏃舵搷浣滃厓绱狅紝閫氳繃闆嗗悎鑾峰彇鍒板搴旂殑杩唬鍣ㄥ悗锛屽湪杩唬涓紝杩涜闆嗗悎寮曠敤鐨勫厓绱犳坊鍔狅紝杩唬鍣ㄥ苟涓嶇煡閬擄紝鎵€浠ヤ細鍑虹幇寮傚父鎯呭喌銆?br>濡備綍瑙e喅鍛紵
鏃㈢劧鏄湪杩唬涓鍏冪礌杩涜鎿嶄綔,鎵捐凯浠e櫒鐨勬柟娉曟渶涓哄悎閫?鍙槸Iterator涓彧鏈塰asNext,next,remove鏂规硶.閫氳繃鏌ラ槄鐨勫畠鐨勫瓙鎺ュ彛,ListIterator,鍙戠幇璇ュ垪琛ㄨ凯浠e櫒鎺ュ彛鍏峰浜嗗鍏冪礌鐨勫銆佸垹銆佹敼銆佹煡鐨勫姩浣溿€?br>
ListIterator鏄疞ist闆嗗悎鐗规湁鐨勮凯浠e櫒銆?br>ListIterator it = list.listIterator;//鍙栦唬Iterator it = list.iterator;
鏂规硶鎽樿
void add(E e) 灏嗘寚瀹氱殑鍏冪礌鎻掑叆鍒楄〃锛堝彲閫夋搷浣滐級銆?br> boolean hasNext() 浠ユ鍚戦亶鍘嗗垪琛ㄦ椂锛屽鏋滃垪琛ㄨ凯浠e櫒鏈夊涓厓绱狅紝鍒欒繑鍥?true锛堟崲鍙ヨ瘽璇达紝濡傛灉 next 杩斿洖涓€涓厓绱犺€屼笉鏄姏鍑哄紓甯革紝鍒欒繑鍥?true锛夈€?br> boolean hasPrevious() 濡傛灉浠ラ€嗗悜閬嶅巻鍒楄〃锛屽垪琛ㄨ凯浠e櫒鏈夊涓厓绱狅紝鍒欒繑鍥?true銆?br> E next() 杩斿洖鍒楄〃涓殑涓嬩竴涓厓绱犮€?br> int nextIndex() 杩斿洖瀵?next 鐨勫悗缁皟鐢ㄦ墍杩斿洖鍏冪礌鐨勭储寮曘€?br> E previous() 杩斿洖鍒楄〃涓殑鍓嶄竴涓厓绱犮€?br> int previousIndex() 杩斿洖瀵?previous 鐨勫悗缁皟鐢ㄦ墍杩斿洖鍏冪礌鐨勭储寮曘€?br> void remove() 浠庡垪琛ㄤ腑绉婚櫎鐢?next 鎴?previous 杩斿洖鐨勬渶鍚庝竴涓厓绱狅紙鍙€夋搷浣滐級銆?br> void set(E e) 鐢ㄦ寚瀹氬厓绱犳浛鎹?next 鎴?previous 杩斿洖鐨勬渶鍚庝竴涓厓绱狅紙鍙€夋搷浣滐級銆?br>
鍙彉闀垮害鏁扮粍鐨勫師鐞嗭細
褰撳厓绱犺秴鍑烘暟缁勯暱搴︼紝浼氫骇鐢熶竴涓柊鏁扮粍锛屽皢鍘熸暟缁勭殑鏁版嵁澶嶅埗鍒版柊鏁扮粍涓紝鍐嶅皢鏂扮殑鍏冪礌娣诲姞鍒版柊鏁扮粍涓€?br>ArrayList锛?br>鏄寜鐓у師鏁扮粍鐨?0%寤堕暱銆傛瀯閫犱竴涓垵濮嬪閲忎负 10 鐨勭┖鍒楄〃銆?br>Vector锛?br>鏄寜鐓у師鏁扮粍鐨?00%寤堕暱銆?br>
娉ㄦ剰锛氬浜巐ist闆嗗悎锛屽簳灞傚垽鏂厓绱犳槸鍚︾浉鍚岋紝鍏跺疄鐢ㄧ殑鏄厓绱犺嚜韬殑equals鏂规硶瀹屾垚鐨勩€傛墍浠ュ缓璁厓绱犻兘瑕佸鍐檈quals鏂规硶锛屽缓绔嬪厓绱犲璞¤嚜宸辩殑姣旇緝鐩稿悓鐨勬潯浠朵緷鎹€?br>
LinkedList锛氱殑鐗规湁鏂规硶銆?br>addFirst();
addLast();
鍦╦dk1.6浠ュ悗銆?br>offerFirst();
offerLast();
getFirst():鑾峰彇閾捐〃涓殑绗竴涓厓绱犮€傚鏋滈摼琛ㄤ负绌猴紝鎶涘嚭NoSuchElementException;
getLast();
鍦╦dk1.6浠ュ悗銆?br>peekFirst();鑾峰彇閾捐〃涓殑绗竴涓厓绱犮€傚鏋滈摼琛ㄤ负绌猴紝杩斿洖null銆?br>peekLast();
removeFirst()锛氳幏鍙栭摼琛ㄤ腑鐨勭涓€涓厓绱狅紝浣嗘槸浼氬垹闄ら摼琛ㄤ腑鐨勭涓€涓厓绱犮€傚鏋滈摼琛ㄤ负绌猴紝鎶涘嚭NoSuchElementException
removeLast();
鍦╦dk1.6浠ュ悗銆?br>pollFirst();鑾峰彇閾捐〃涓殑绗竴涓厓绱狅紝浣嗘槸浼氬垹闄ら摼琛ㄤ腑鐨勭涓€涓厓绱犮€傚鏋滈摼琛ㄤ负绌猴紝杩斿洖null銆?br>pollLast();
------------------------------------------------------------
--< java.util >-- Set鎺ュ彛锛?/h2>
Set鎺ュ彛涓殑鏂规硶鍜孋ollection涓柟娉曚竴鑷寸殑銆係et鎺ュ彛鍙栧嚭鏂瑰紡鍙湁涓€绉嶏紝杩唬鍣ㄣ€?br> |--HashSet锛氬簳灞傛暟鎹粨鏋勬槸鍝堝笇琛紝绾跨▼鏄笉鍚屾鐨勩€傛棤搴忥紝楂樻晥锛?br> HashSet闆嗗悎淇濊瘉鍏冪礌鍞竴鎬э細閫氳繃鍏冪礌鐨刪ashCode鏂规硶锛屽拰equals鏂规硶瀹屾垚鐨勩€?br> 褰撳厓绱犵殑hashCode鍊肩浉鍚屾椂锛屾墠缁х画鍒ゆ柇鍏冪礌鐨別quals鏄惁涓簍rue銆?br> 濡傛灉涓簍rue锛岄偅涔堣涓虹浉鍚屽厓绱狅紝涓嶅瓨銆傚鏋滀负false锛岄偅涔堝瓨鍌ㄣ€?br> 濡傛灉hashCode鍊间笉鍚岋紝閭d箞涓嶅垽鏂璭quals锛屼粠鑰屾彁楂樺璞℃瘮杈冪殑閫熷害銆?br> |--LinkedHashSet锛氭湁搴忥紝hashset鐨勫瓙绫汇€?br> |--TreeSet锛氬Set闆嗗悎涓殑鍏冪礌鐨勮繘琛屾寚瀹氶『搴忕殑鎺掑簭銆備笉鍚屾銆俆reeSet搴曞眰鐨勬暟鎹粨鏋勫氨鏄簩鍙夋爲銆?br>
鍝堝笇琛ㄧ殑鍘熺悊锛?br>1锛屽瀵硅薄鍏冪礌涓殑鍏抽敭瀛?瀵硅薄涓殑鐗规湁鏁版嵁)锛岃繘琛屽搱甯岀畻娉曠殑杩愮畻锛屽苟寰楀嚭涓€涓叿浣撶殑绠楁硶鍊硷紝杩欎釜鍊?绉颁负鍝堝笇鍊笺€?br>2锛屽搱甯屽€煎氨鏄繖涓厓绱犵殑浣嶇疆銆?br>3锛屽鏋滃搱甯屽€煎嚭鐜板啿绐侊紝鍐嶆鍒ゆ柇杩欎釜鍏抽敭瀛楀搴旂殑瀵硅薄鏄惁鐩稿悓銆傚鏋滃璞$浉鍚岋紝灏变笉瀛樺偍锛屽洜涓哄厓绱犻噸澶嶃€傚鏋滃璞′笉鍚岋紝灏卞瓨鍌紝鍦ㄥ師鏉ュ璞$殑鍝堝笇鍊煎熀纭€ +1椤哄欢銆?br>4锛屽瓨鍌ㄥ搱甯屽€肩殑缁撴瀯锛屾垜浠О涓哄搱甯岃〃銆?br>5锛屾棦鐒跺搱甯岃〃鏄牴鎹搱甯屽€煎瓨鍌ㄧ殑锛屼负浜嗘彁楂樻晥鐜囷紝鏈€濂戒繚璇佸璞$殑鍏抽敭瀛楁槸鍞竴鐨勩€?br> 杩欐牱鍙互灏介噺灏戠殑鍒ゆ柇鍏抽敭瀛楀搴旂殑瀵硅薄鏄惁鐩稿悓锛屾彁楂樹簡鍝堝笇琛ㄧ殑鎿嶄綔鏁堢巼銆?br>
瀵逛簬ArrayList闆嗗悎锛屽垽鏂厓绱犳槸鍚﹀瓨鍦紝鎴栬€呭垹鍏冪礌搴曞眰渚濇嵁閮芥槸equals鏂规硶銆?br>瀵逛簬HashSet闆嗗悎锛屽垽鏂厓绱犳槸鍚﹀瓨鍦紝鎴栬€呭垹闄ゅ厓绱狅紝搴曞眰渚濇嵁鐨勬槸hashCode鏂规硶鍜宔quals鏂规硶銆?br>
TreeSet:
鐢ㄤ簬瀵筍et闆嗗悎杩涜鍏冪礌鐨勬寚瀹氶『搴忔帓搴忥紝鎺掑簭闇€瑕佷緷鎹厓绱犺嚜韬叿澶囩殑姣旇緝鎬с€?br> 濡傛灉鍏冪礌涓嶅叿澶囨瘮杈冩€э紝鍦ㄨ繍琛屾椂浼氬彂鐢烠lassCastException寮傚父銆?br> 鎵€浠ラ渶瑕佸厓绱犲疄鐜癈omparable鎺ュ彛锛屽己鍒惰鍏冪礌鍏峰姣旇緝鎬э紝澶嶅啓compareTo鏂规硶銆?br> 渚濇嵁compareTo鏂规硶鐨勮繑鍥炲€硷紝纭畾鍏冪礌鍦═reeSet鏁版嵁缁撴瀯涓殑浣嶇疆銆?br> TreeSet鏂规硶淇濊瘉鍏冪礌鍞竴鎬х殑鏂瑰紡锛氬氨鏄弬鑰冩瘮杈冩柟娉曠殑缁撴灉鏄惁涓?锛屽鏋渞eturn 0锛岃涓轰袱涓璞¢噸澶嶏紝涓嶅瓨銆?br>
娉ㄦ剰锛氬湪杩涜姣旇緝鏃讹紝濡傛灉鍒ゆ柇鍏冪礌涓嶅敮涓€锛屾瘮濡傦紝鍚屽鍚嶏紝鍚屽勾榫勶紝鎵嶈涓哄悓涓€涓汉銆?br> 鍦ㄥ垽鏂椂锛岄渶瑕佸垎涓昏鏉′欢鍜屾瑕佹潯浠讹紝褰撲富瑕佹潯浠剁浉鍚屾椂锛屽啀鍒ゆ柇娆¤鏉′欢锛屾寜鐓ф瑕佹潯浠舵帓搴忋€?br>
TreeSet闆嗗悎鎺掑簭鏈変袱绉嶆柟寮忥紝Comparable鍜孋omparator鍖哄埆锛?br>1锛氳鍏冪礌鑷韩鍏峰姣旇緝鎬э紝闇€瑕佸厓绱犲璞″疄鐜癈omparable鎺ュ彛锛岃鐩朿ompareTo鏂规硶銆?br>2锛氳闆嗗悎鑷韩鍏峰姣旇緝鎬э紝闇€瑕佸畾涔変竴涓疄鐜颁簡Comparator鎺ュ彛鐨勬瘮杈冨櫒锛屽苟瑕嗙洊compare鏂规硶锛屽苟灏嗚绫诲璞′綔涓哄疄闄呭弬鏁颁紶閫掔粰TreeSet闆嗗悎鐨勬瀯閫犲嚱鏁般€?br>绗簩绉嶆柟寮忚緝涓虹伒娲汇€?br>
------------------------------------------------------------
Map闆嗗悎锛?/h2>
|--Hashtable锛氬簳灞傛槸鍝堝笇琛ㄦ暟鎹粨鏋勶紝鏄嚎绋嬪悓姝ョ殑銆備笉鍙互瀛樺偍null閿紝null鍊笺€?br>|--HashMap锛氬簳灞傛槸鍝堝笇琛ㄦ暟鎹粨鏋勶紝鏄嚎绋嬩笉鍚屾鐨勩€傚彲浠ュ瓨鍌╪ull閿紝null鍊笺€傛浛浠d簡Hashtable.
|--TreeMap锛氬簳灞傛槸浜屽弶鏍戠粨鏋勶紝鍙互瀵筸ap闆嗗悎涓殑閿繘琛屾寚瀹氶『搴忕殑鎺掑簭銆?br>
Map闆嗗悎瀛樺偍鍜孋ollection鏈夌潃寰堝ぇ涓嶅悓锛?br>Collection涓€娆″瓨涓€涓厓绱狅紱Map涓€娆″瓨涓€瀵瑰厓绱犮€?br>Collection鏄崟鍒楅泦鍚堬紱Map鏄弻鍒楅泦鍚堛€?br>Map涓殑瀛樺偍鐨勪竴瀵瑰厓绱狅細涓€涓槸閿紝涓€涓槸鍊硷紝閿笌鍊间箣闂存湁瀵瑰簲(鏄犲皠)鍏崇郴銆?br>鐗圭偣锛氳淇濊瘉map闆嗗悎涓敭鐨勫敮涓€鎬с€?br>
1锛屾坊鍔犮€?br> put(key,value)锛氬綋瀛樺偍鐨勯敭鐩稿悓鏃讹紝鏂扮殑鍊间細鏇挎崲鑰佺殑鍊硷紝骞跺皢鑰佸€艰繑鍥炪€傚鏋滈敭娌℃湁閲嶅锛岃繑鍥瀗ull銆?br> void putAll(Map);
2锛屽垹闄ゃ€?br> void clear()锛氭竻绌?br> value remove(key) 锛氬垹闄ゆ寚瀹氶敭銆?br>3锛屽垽鏂€?br> boolean isEmpty()锛?br> boolean containsKey(key)锛氭槸鍚﹀寘鍚玨ey
boolean containsValue(value) 锛氭槸鍚﹀寘鍚玽alue
4锛屽彇鍑恒€?br> int size()锛氳繑鍥為暱搴?br> value get(key) 锛氶€氳繃鎸囧畾閿幏鍙栧搴旂殑鍊笺€傚鏋滆繑鍥瀗ull锛屽彲浠ュ垽鏂閿笉瀛樺湪銆傚綋鐒舵湁鐗规畩鎯呭喌锛屽氨鏄湪hashmap闆嗗悎涓紝鏄彲浠ュ瓨鍌╪ull閿畁ull鍊肩殑銆?br> Collection values()锛氳幏鍙杕ap闆嗗悎涓殑鎵€鏈夌殑鍊笺€?br>5锛屾兂瑕佽幏鍙杕ap涓殑鎵€鏈夊厓绱狅細
鍘熺悊锛歮ap涓槸娌℃湁杩唬鍣ㄧ殑锛宑ollection鍏峰杩唬鍣紝鍙灏唌ap闆嗗悎杞垚Set闆嗗悎锛屽彲浠ヤ娇鐢ㄨ凯浠e櫒浜嗐€備箣鎵€浠ヨ浆鎴恠et锛屾槸鍥犱负map闆嗗悎鍏峰鐫€閿殑鍞竴鎬э紝鍏跺疄set闆嗗悎灏辨潵鑷簬map锛宻et闆嗗悎搴曞眰鍏跺疄鐢ㄧ殑灏辨槸map鐨勬柟娉曘€?br>鎶妋ap闆嗗悎杞垚set鐨勬柟娉曪細
Set keySet();
Set entrySet();//鍙栫殑鏄敭鍜屽€肩殑鏄犲皠鍏崇郴銆?br>Entry灏辨槸Map鎺ュ彛涓殑鍐呴儴鎺ュ彛锛?br>涓轰粈涔堣瀹氫箟鍦╩ap鍐呴儴鍛紵entry鏄闂敭鍊煎叧绯荤殑鍏ュ彛锛屾槸map鐨勫叆鍙o紝璁块棶鐨勬槸map涓殑閿€煎銆?br>---------------------------------------------------------
鍙栧嚭map闆嗗悎涓墍鏈夊厓绱犵殑鏂瑰紡涓€锛歬eySet()鏂规硶銆?br>鍙互灏唌ap闆嗗悎涓殑閿兘鍙栧嚭瀛樻斁鍒皊et闆嗗悎涓€傚set闆嗗悎杩涜杩唬銆傝凯浠e畬鎴愶紝鍐嶉€氳繃get鏂规硶瀵硅幏鍙栧埌鐨勯敭杩涜鍊肩殑鑾峰彇銆?br> Set keySet = map.keySet();
Iterator it = keySet.iterator();
while(it.hasNext()) {
Object key = it.next();
Object value = map.get(key);
System.out.println(key+":"+value);
}
--------------------------------------------------------
鍙栧嚭map闆嗗悎涓墍鏈夊厓绱犵殑鏂瑰紡浜岋細entrySet()鏂规硶銆?br>Set entrySet = map.entrySet();
Iterator it = entrySet.iterator();
while(it.hasNext()) {
Map.Entry me = (Map.Entry)it.next();
System.out.println(me.getKey()+"::::"+me.getValue());
}
--------------------------------------------------------
浣跨敤闆嗗悎鐨勬妧宸э細
鐪嬪埌Array灏辨槸鏁扮粍缁撴瀯锛屾湁瑙掓爣锛屾煡璇㈤€熷害寰堝揩銆?br>鐪嬪埌link灏辨槸閾捐〃缁撴瀯锛氬鍒犻€熷害蹇紝鑰屼笖鏈夌壒鏈夋柟娉曘€俛ddFirst锛?addLast锛?removeFirst()锛?removeLast()锛?getFirst()锛沢etLast()锛?br>鐪嬪埌hash灏辨槸鍝堝笇琛紝灏辫鎯宠鍝堝笇鍊硷紝灏辫鎯冲埌鍞竴鎬э紝灏辫鎯冲埌瀛樺叆鍒拌缁撴瀯鐨勪腑鐨勫厓绱犲繀椤昏鐩杊ashCode锛宔quals鏂规硶銆?br>鐪嬪埌tree灏辨槸浜屽弶鏍戯紝灏辫鎯冲埌鎺掑簭锛屽氨鎯宠鐢ㄥ埌姣旇緝銆?br>姣旇緝鐨勪袱绉嶆柟寮忥細
涓€涓槸Comparable锛氳鐩朿ompareTo鏂规硶锛?br>涓€涓槸Comparator锛氳鐩朿ompare鏂规硶銆?br>LinkedHashSet锛孡inkedHashMap:杩欎袱涓泦鍚堝彲浠ヤ繚璇佸搱甯岃〃鏈夊瓨鍏ラ『搴忓拰鍙栧嚭椤哄簭涓€鑷达紝淇濊瘉鍝堝笇琛ㄦ湁搴忋€?br>
闆嗗悎浠€涔堟椂鍊欑敤锛?br>褰撳瓨鍌ㄧ殑鏄竴涓厓绱犳椂锛屽氨鐢–ollection銆傚綋瀛樺偍瀵硅薄涔嬮棿瀛樺湪鐫€鏄犲皠鍏崇郴鏃讹紝灏变娇鐢∕ap闆嗗悎銆?br>
淇濊瘉鍞竴锛屽氨鐢⊿et銆備笉淇濊瘉鍞竴锛屽氨鐢↙ist銆?br>------------------------------------------------------------------------------------------------
Collections锛氬畠鐨勫嚭鐜扮粰闆嗗悎鎿嶄綔鎻愪緵浜嗘洿澶氱殑鍔熻兘銆傝繖涓被涓嶉渶瑕佸垱寤哄璞★紝鍐呴儴鎻愪緵鐨勯兘鏄潤鎬佹柟娉曘€?br>闈欐€佹柟娉曪細
Collections.sort(list);//list闆嗗悎杩涜鍏冪礌鐨勮嚜鐒堕『搴忔帓搴忋€?br>Collections.sort(list,new ComparatorByLen());//鎸夋寚瀹氱殑姣旇緝鍣ㄦ柟娉曟帓搴忋€?br>class ComparatorByLen implements Comparator<String>{
public int compare(String s1,String s2){
int temp = s1.length()-s2.length();
return temp==0?s1.compareTo(s2):temp;
}
}
Collections.max(list); //杩斿洖list涓瓧鍏搁『搴忔渶澶х殑鍏冪礌銆?br>int index = Collections.binarySearch(list,"zz");//浜屽垎鏌ユ壘锛岃繑鍥炶鏍囥€?br>Collections.reverseOrder();//閫嗗悜鍙嶈浆鎺掑簭銆?br>Collections.shuffle(list);//闅忔満瀵筶ist涓殑鍏冪礌杩涜浣嶇疆鐨勭疆鎹€?br>
灏嗛潪鍚屾闆嗗悎杞垚鍚屾闆嗗悎鐨勬柟娉曪細Collections涓殑 XXX synchronizedXXX(XXX);
List synchronizedList(list);
Map synchronizedMap(map);
鍘熺悊锛氬畾涔変竴涓被锛屽皢闆嗗悎鎵€鏈夌殑鏂规硶鍔犲悓涓€鎶婇攣鍚庤繑鍥炪€?br>
Collection 鍜?Collections鐨勫尯鍒細
Collections鏄釜java.util涓嬬殑绫伙紝鏄拡瀵归泦鍚堢被鐨勪竴涓伐鍏风被,鎻愪緵涓€绯诲垪闈欐€佹柟娉?瀹炵幇瀵归泦鍚堢殑鏌ユ壘銆佹帓搴忋€佹浛鎹€佺嚎绋嬪畨鍏ㄥ寲锛堝皢闈炲悓姝ョ殑闆嗗悎杞崲鎴愬悓姝ョ殑锛夌瓑鎿嶄綔銆?br>Collection鏄釜java.util涓嬬殑鎺ュ彛锛屽畠鏄悇绉嶉泦鍚堢粨鏋勭殑鐖舵帴鍙o紝缁ф壙浜庡畠鐨勬帴鍙d富瑕佹湁Set鍜孡ist,鎻愪緵浜嗗叧浜庨泦鍚堢殑涓€浜涙搷浣?濡傛彃鍏ャ€佸垹闄ゃ€佸垽鏂竴涓厓绱犳槸鍚﹀叾鎴愬憳銆侀亶鍘嗙瓑銆?br>-------------------------------------------------------
Arrays锛?br>鐢ㄤ簬鎿嶄綔鏁扮粍瀵硅薄鐨勫伐鍏风被锛岄噷闈㈤兘鏄潤鎬佹柟娉曘€?br>asList鏂规硶锛氬皢鏁扮粍杞崲鎴恖ist闆嗗悎銆?br>String[] arr = {"abc","kk","qq"};
List<String> list = Arrays.asList(arr);//灏哸rr鏁扮粍杞垚list闆嗗悎銆?br>灏嗘暟缁勮浆鎹㈡垚闆嗗悎锛屾湁浠€涔堝ソ澶勫憿锛熺敤aslist鏂规硶锛屽皢鏁扮粍鍙樻垚闆嗗悎锛?br>鍙互閫氳繃list闆嗗悎涓殑鏂规硶鏉ユ搷浣滄暟缁勪腑鐨勫厓绱狅細isEmpty()銆乧ontains銆乮ndexOf銆乻et锛?br>娉ㄦ剰锛堝眬闄愭€э級锛氭暟缁勬槸鍥哄畾闀垮害锛屼笉鍙互浣跨敤闆嗗悎瀵硅薄澧炲姞鎴栬€呭垹闄ょ瓑锛屼細鏀瑰彉鏁扮粍闀垮害鐨勫姛鑳芥柟娉曘€傛瘮濡俛dd銆乺emove銆乧lear銆傦紙浼氭姤涓嶆敮鎸佹搷浣滃紓甯窾nsupportedOperationException锛夛紱
濡傛灉鏁扮粍涓瓨鍌ㄧ殑寮曠敤鏁版嵁绫诲瀷锛岀洿鎺ヤ綔涓洪泦鍚堢殑鍏冪礌鍙互鐩存帴鐢ㄩ泦鍚堟柟娉曟搷浣溿€?br>濡傛灉鏁扮粍涓瓨鍌ㄧ殑鏄熀鏈暟鎹被鍨嬶紝asList浼氬皢鏁扮粍瀹炰綋浣滀负闆嗗悎鍏冪礌瀛樺湪銆?br>
闆嗗悎鍙樻暟缁勶細鐢ㄧ殑鏄疌ollection鎺ュ彛涓殑鏂规硶锛歵oArray();
濡傛灉缁檛oArray浼犻€掔殑鎸囧畾绫诲瀷鐨勬暟鎹暱搴﹀皬浜庝簡闆嗗悎鐨剆ize锛岄偅涔坱oArray鏂规硶锛屼細鑷畾鍐嶅垱寤轰竴涓绫诲瀷鐨勬暟鎹紝闀垮害涓洪泦鍚堢殑size銆?br> 濡傛灉浼犻€掔殑鎸囧畾鐨勭被鍨嬬殑鏁扮粍鐨勯暱搴﹀ぇ浜庝簡闆嗗悎鐨剆ize锛岄偅涔坱oArray鏂规硶锛屽氨涓嶄細鍒涘缓鏂版暟缁勶紝鐩存帴浣跨敤璇ユ暟缁勫嵆鍙紝骞跺皢闆嗗悎涓殑鍏冪礌瀛樺偍鍒版暟缁勪腑锛屽叾浠栦负瀛樺偍鍏冪礌鐨勪綅缃粯璁ゅ€糿ull銆?br> 鎵€浠ワ紝鍦ㄤ紶閫掓寚瀹氱被鍨嬫暟缁勬椂锛屾渶濂界殑鏂瑰紡灏辨槸鎸囧畾鐨勯暱搴﹀拰size鐩哥瓑鐨勬暟缁勩€?br>灏嗛泦鍚堝彉鎴愭暟缁勫悗鏈変粈涔堝ソ澶勶紵闄愬畾浜嗗闆嗗悎涓殑鍏冪礌杩涜澧炲垹鎿嶄綔锛屽彧瑕佽幏鍙栬繖浜涘厓绱犲嵆鍙€?br>------------------------------------------------------------------------------------------------
Jdk5.0鏂扮壒鎬э細
Collection鍦╦dk1.5浠ュ悗锛屾湁浜嗕竴涓埗鎺ュ彛Iterable锛岃繖涓帴鍙g殑鍑虹幇鐨勫皢iterator鏂规硶杩涜鎶藉彇锛屾彁楂樹簡鎵╁睍鎬с€?br>--------------------------------------------------
澧炲己for寰幆锛歠oreach璇彞锛宖oreach绠€鍖栦簡杩唬鍣ㄣ€?br>鏍煎紡锛?/ 澧炲己for寰幆鎷彿閲屽啓涓や釜鍙傛暟锛岀涓€涓槸澹版槑涓€涓彉閲忥紝绗簩涓氨鏄渶瑕佽凯浠g殑瀹瑰櫒
for( 鍏冪礌绫诲瀷 鍙橀噺鍚?: Collection闆嗗悎 & 鏁扮粍 ) {
鈥?br>}
楂樼骇for寰幆鍜屼紶缁焒or寰幆鐨勫尯鍒細
楂樼骇for寰幆鍦ㄤ娇鐢ㄦ椂锛屽繀椤昏鏄庣‘琚亶鍘嗙殑鐩爣銆傝繖涓洰鏍囷紝鍙互鏄疌ollection闆嗗悎鎴栬€呮暟缁勶紝濡傛灉閬嶅巻Collection闆嗗悎锛屽湪閬嶅巻杩囩▼涓繕闇€瑕佸鍏冪礌杩涜鎿嶄綔锛屾瘮濡傚垹闄わ紝闇€瑕佷娇鐢ㄨ凯浠e櫒銆?br>濡傛灉閬嶅巻鏁扮粍锛岃繕闇€瑕佸鏁扮粍鍏冪礌杩涜鎿嶄綔锛屽缓璁敤浼犵粺for寰幆鍥犱负鍙互瀹氫箟瑙掓爣閫氳繃瑙掓爣鎿嶄綔鍏冪礌銆傚鏋滃彧涓洪亶鍘嗚幏鍙栵紝鍙互绠€鍖栨垚楂樼骇for寰幆锛屽畠鐨勫嚭鐜颁负浜嗙畝鍖栦功鍐欍€?br>
楂樼骇for寰幆鍙互閬嶅巻map闆嗗悎鍚楋紵涓嶅彲浠ャ€備絾鏄彲浠ュ皢map杞垚set鍚庡啀浣跨敤foreach璇彞銆?br>
1)銆佷綔鐢細瀵瑰瓨鍌ㄥ璞$殑瀹瑰櫒杩涜杩唬锛?鏁扮粍 collection map
2)銆佸寮篺or寰幆杩唬鏁扮粍锛?br>String [] arr = {"a", "b", "c"};//鏁扮粍鐨勯潤鎬佸畾涔夋柟寮忥紝鍙瘯鐢ㄤ簬鏁扮粍棣栨瀹氫箟鐨勬椂鍊?br>for(String s : arr) {
System.out.println(s);
}
3)銆佸崟鍒楅泦鍚?Collection锛?br>List list = new ArrayList();
list.add("aaa");
// 澧炲己for寰幆, 娌℃湁浣跨敤娉涘瀷鐨勯泦鍚堣兘涓嶈兘浣跨敤澧炲己for寰幆杩唬锛熻兘
for(Object obj : list) {
String s = (String) obj;
System.out.println(s);
}
4)銆佸弻鍒楅泦鍚?Map锛?br>Map map = new HashMap();
map.put("a", "aaa");
// 浼犵粺鏂瑰紡锛氬繀椤绘帉鎻¤繖绉嶆柟寮?br>Set entrys = map.entrySet(); // 1.鑾峰緱鎵€鏈夌殑閿€煎Entry瀵硅薄
iter = entrys.iterator(); // 2.杩唬鍑烘墍鏈夌殑entry
while(iter.hasNext()) {
Map.Entry entry = (Entry) iter.next();
String key = (String) entry.getKey(); // 鍒嗗埆鑾峰緱key鍜寁alue
String value = (String) entry.getValue();
System.out.println(key + "=" + value);
}
// 澧炲己for寰幆杩唬锛氬師鍒欎笂map闆嗗悎鏄棤娉曚娇鐢ㄥ寮篺or寰幆鏉ヨ凯浠g殑锛屽洜涓哄寮篺or寰幆鍙兘閽堝瀹炵幇浜咺terable鎺ュ彛鐨勯泦鍚堣繘琛岃凯浠o紱Iterable鏄痡dk5涓柊瀹氫箟鐨勬帴鍙o紝灏变竴涓柟娉昳terator鏂规硶锛屽彧鏈夊疄鐜颁簡Iterable鎺ュ彛鐨勭被锛屾墠鑳戒繚璇佷竴瀹氭湁iterator鏂规硶锛宩ava鏈夎繖鏍风殑闄愬畾鏄洜涓哄寮篺or寰幆鍐呴儴杩樻槸鐢ㄨ凯浠e櫒瀹炵幇鐨勶紝鑰屽疄闄呬笂锛屾垜浠彲浠ラ€氳繃鏌愮鏂瑰紡鏉ヤ娇鐢ㄥ寮篺or寰幆銆?br>for(Object obj : map.entrySet()) {
Map.Entry entry = (Entry) obj; // obj 渚濇琛ㄧずEntry
System.out.println(entry.getKey() + "=" + entry.getValue());
}
5锛夈€侀泦鍚堣凯浠f敞鎰忛棶棰橈細鍦ㄨ凯浠i泦鍚堢殑杩囩▼涓紝涓嶈兘瀵归泦鍚堣繘琛屽鍒犳搷浣滐紙浼氭姤骞跺彂璁块棶寮傚父锛夛紱鍙互鐢ㄨ凯浠e櫒鐨勬柟娉曡繘琛屾搷浣滐紙瀛愮被listIterator锛氭湁澧炲垹鐨勬柟娉曪級銆?br>6)銆佸寮篺or寰幆娉ㄦ剰闂锛氬湪浣跨敤澧炲己for寰幆鏃讹紝涓嶈兘瀵瑰厓绱犺繘琛岃祴鍊硷紱
int[] arr = {1,2,3};
for(int num : arr) {
num = 0; //涓嶈兘鏀瑰彉鏁扮粍鐨勫€?br>}
System.out.println(arr[1]); //2
--------------------------------------------------
鍙彉鍙傛暟锛?..锛夛細鐢ㄥ埌鍑芥暟鐨勫弬鏁颁笂锛屽綋瑕佹搷浣滅殑鍚屼竴涓被鍨嬪厓绱犱釜鏁颁笉纭畾鐨勬椂鍊欙紝鍙槸鐢ㄨ繖涓柟寮忥紝杩欎釜鍙傛暟鍙互鎺ュ彈浠绘剰涓暟鐨勫悓涓€绫诲瀷鐨勬暟鎹€?br>
鍜屼互鍓嶆帴鏀舵暟缁勪笉涓€鏍风殑鏄細
浠ュ墠瀹氫箟鏁扮粍绫诲瀷锛岄渶瑕佸厛鍒涘缓涓€涓暟缁勫璞★紝鍐嶅皢杩欎釜鏁扮粍瀵硅薄浣滀负鍙傛暟浼犻€掔粰鍑芥暟銆傜幇鍦紝鐩存帴灏嗘暟缁勪腑鐨勫厓绱犱綔涓哄弬鏁颁紶閫掑嵆鍙€傚簳灞傚叾瀹炴槸灏嗚繖浜涘厓绱犺繘琛屾暟缁勭殑灏佽锛岃€岃繖涓皝瑁呭姩浣滐紝鏄湪搴曞眰瀹屾垚鐨勶紝琚殣钘忎簡銆傛墍浠ョ畝鍖栦簡鐢ㄦ埛鐨勪功鍐欙紝灏戜簡璋冪敤鑰呭畾涔夋暟缁勭殑鍔ㄤ綔銆?br>濡傛灉鍦ㄥ弬鏁板垪琛ㄤ腑浣跨敤浜嗗彲鍙樺弬鏁帮紝鍙彉鍙傛暟蹇呴』瀹氫箟鍦ㄥ弬鏁板垪琛ㄧ粨灏?涔熷氨鏄繀椤绘槸鏈€鍚庝竴涓弬鏁帮紝鍚﹀垯缂栬瘧浼氬け璐ャ€?銆?br>濡傛灉瑕佽幏鍙栧涓猧nt鏁扮殑鍜屽憿锛熷彲浠ヤ娇鐢ㄥ皢澶氫釜int鏁板皝瑁呭埌鏁扮粍涓紝鐩存帴瀵规暟缁勬眰鍜屽嵆鍙€?br>---------------------------------------------------
闈欐€佸鍏ワ細瀵煎叆浜嗙被涓殑鎵€鏈夐潤鎬佹垚鍛橈紝绠€鍖栭潤鎬佹垚鍛樼殑涔﹀啓銆?br>import static java.util.Collections.*; //瀵煎叆浜咰ollections绫讳腑鐨勬墍鏈夐潤鎬佹垚鍛?br>---------------------------------------------------
鏋氫妇锛氬叧閿瓧 enum
闂锛氬璞$殑鏌愪釜灞炴€х殑鍊间笉鑳芥槸浠绘剰鐨勶紝蹇呴』涓哄浐瀹氱殑涓€缁勫彇鍊煎叾涓殑鏌愪竴涓紱
瑙e喅鍔炴硶锛?br>1锛夈€佸湪setGrade鏂规硶涓仛鍒ゆ柇锛屼笉绗﹀悎鏍煎紡瑕佹眰灏辨姏鍑哄紓甯革紱
2锛夈€佺洿鎺ラ檺瀹氱敤鎴风殑閫夋嫨锛岄€氳繃鑷畾涔夌被妯℃嫙鏋氫妇鐨勬柟寮忔潵闄愬畾鐢ㄦ埛鐨勮緭鍏ワ紝鍐欎竴涓狦rade绫伙紝绉佹湁鏋勯€犲嚱鏁帮紝瀵瑰鎻愪緵5涓潤鎬佺殑甯搁噺琛ㄧず绫荤殑瀹炰緥锛?br>3)銆乯dk5涓柊瀹氫箟浜嗘灇涓剧被鍨嬶紝涓撻棬鐢ㄤ簬瑙e喅姝ょ被闂锛?br>4)銆佹灇涓惧氨鏄竴涓壒娈婄殑java绫伙紝鍙互瀹氫箟灞炴€с€佹柟娉曘€佹瀯閫犲嚱鏁般€佸疄鐜版帴鍙c€佺户鎵跨被锛?br>------------------------------------------------------------------------------
鑷姩鎷嗚绠憋細java涓暟鎹被鍨嬪垎涓轰袱绉?锛?鍩烘湰鏁版嵁绫诲瀷 寮曠敤鏁版嵁绫诲瀷(瀵硅薄)
鍦?java绋嬪簭涓墍鏈夌殑鏁版嵁閮介渶瑕佸綋鍋氬璞℃潵澶勭悊锛岄拡瀵?绉嶅熀鏈暟鎹被鍨嬫彁渚涗簡鍖呰绫伙紝濡備笅锛?br>int --> Integer
byte --> Byte
short --> Short
long --> Long
char --> Character
double --> Double
float --> Float
boolean --> Boolean
jdk5浠ュ墠鍩烘湰鏁版嵁绫诲瀷鍜屽寘瑁呯被涔嬮棿闇€瑕佷簰杞細
鍩烘湰---寮曠敤 Integer x = new Integer(x);
寮曠敤---鍩烘湰 int num = x.intValue();
1)銆両nteger x = 1; x = x + 1; 缁忓巻浜嗕粈涔堣繃绋嬶紵瑁呯 脿 鎷嗙 脿 瑁呯锛?br>2)銆佷负浜嗕紭鍖栵紝铏氭嫙鏈轰负鍖呰绫绘彁渚涗簡缂撳啿姹狅紝Integer姹犵殑澶у皬 -128~127 涓€涓瓧鑺傜殑澶у皬锛?br>3)銆丼tring姹狅細Java涓轰簡浼樺寲瀛楃涓叉搷浣?鎻愪緵浜嗕竴涓紦鍐叉睜锛?br>----------------------------------------------------------
娉涘瀷锛歫dk1.5鐗堟湰浠ュ悗鍑虹幇鐨勪竴涓畨鍏ㄦ満鍒躲€傝〃鐜版牸寮忥細< >
濂藉锛?br>1锛氬皢杩愯鏃舵湡鐨勯棶棰楥lassCastException闂杞崲鎴愪簡缂栬瘧澶辫触锛屼綋鐜板湪缂栬瘧鏃舵湡锛岀▼搴忓憳灏卞彲浠ヨВ鍐抽棶棰樸€?br>2锛氶伩鍏嶄簡寮哄埗杞崲鐨勯夯鐑︺€?br>
鍙甯︽湁<>鐨勭被鎴栬€呮帴鍙o紝閮藉睘浜庡甫鏈夌被鍨嬪弬鏁扮殑绫绘垨鑰呮帴鍙o紝鍦ㄤ娇鐢ㄨ繖浜涚被鎴栬€呮帴鍙f椂锛屽繀椤荤粰<>涓紶閫掍竴涓叿浣撶殑寮曠敤鏁版嵁绫诲瀷銆?br>
娉涘瀷鎶€鏈細鍏跺疄搴旂敤鍦ㄧ紪璇戞椂鏈燂紝鏄粰缂栬瘧鍣ㄤ娇鐢ㄧ殑鎶€鏈紝鍒颁簡杩愯鏃舵湡锛屾硾鍨嬪氨涓嶅瓨鍦ㄤ簡銆?br>涓轰粈涔? 鍥犱负娉涘瀷鐨勬摝闄わ細涔熷氨鏄锛岀紪杈戝櫒妫€鏌ヤ簡娉涘瀷鐨勭被鍨嬫纭悗锛屽湪鐢熸垚鐨勭被鏂囦欢涓槸娌℃湁娉涘瀷鐨勩€?br>鍦ㄨ繍琛屾椂锛屽浣曠煡閬撹幏鍙栫殑鍏冪礌绫诲瀷鑰屼笉鐢ㄥ己杞憿锛?br>娉涘瀷鐨勮ˉ鍋匡細鍥犱负瀛樺偍鐨勬椂鍊欙紝绫诲瀷宸茬粡纭畾浜嗘槸鍚屼竴涓被鍨嬬殑鍏冪礌锛屾墍浠ュ湪杩愯鏃讹紝鍙鑾峰彇鍒拌鍏冪礌鐨勭被鍨嬶紝鍦ㄥ唴閮ㄨ繘琛屼竴娆¤浆鎹㈠嵆鍙紝鎵€浠ヤ娇鐢ㄨ€呬笉鐢ㄥ啀鍋氳浆鎹㈠姩浣滀簡銆?br>
浠€涔堟椂鍊欑敤娉涘瀷绫诲憿锛?br>褰撶被涓殑鎿嶄綔鐨勫紩鐢ㄦ暟鎹被鍨嬩笉纭畾鐨勬椂鍊欙紝浠ュ墠鐢ㄧ殑Object鏉ヨ繘琛屾墿灞曠殑锛岀幇鍦ㄥ彲浠ョ敤娉涘瀷鏉ヨ〃绀恒€傝繖鏍峰彲浠ラ伩鍏嶅己杞殑楹荤儲锛岃€屼笖灏嗚繍琛岄棶棰樿浆绉诲埌鐨勭紪璇戞椂鏈熴€?br>----------------------------------------------------------
娉涘瀷鍦ㄧ▼搴忓畾涔変笂鐨勪綋鐜帮細
//娉涘瀷绫伙細灏嗘硾鍨嬪畾涔夊湪绫讳笂銆?br>class Tool<Q> {
private Q obj;
public void setObject(Q obj) {
this.obj = obj;
}
public Q getObject() {
return obj;
}
}
//褰撴柟娉曟搷浣滅殑寮曠敤鏁版嵁绫诲瀷涓嶇‘瀹氱殑鏃跺€欙紝鍙互灏嗘硾鍨嬪畾涔夊湪鏂规硶涓娿€?br> public <W> void method(W w) {
System.out.println("method:"+w);
}
//闈欐€佹柟娉曚笂鐨勬硾鍨嬶細闈欐€佹柟娉曟棤娉曡闂被涓婂畾涔夌殑娉涘瀷銆傚鏋滈潤鎬佹柟娉曟搷浣滅殑寮曠敤鏁版嵁绫诲瀷涓嶇‘瀹氱殑鏃跺€欙紝蹇呴』瑕佸皢娉涘瀷瀹氫箟鍦ㄦ柟娉曚笂銆?br> public static <Q> void function(Q t) {
System.out.println("function:"+t);
}
//娉涘瀷鎺ュ彛.
interface Inter<T> {
void show(T t);
}
class InterImpl<R> implements Inter<R> {
public void show(R r) {
System.out.println("show:"+r);
}
}
------------------------------------------------------------
娉涘瀷涓殑閫氶厤绗︼細鍙互瑙e喅褰撳叿浣撶被鍨嬩笉纭畾鐨勬椂鍊欙紝杩欎釜閫氶厤绗﹀氨鏄?? 锛涘綋鎿嶄綔绫诲瀷鏃讹紝涓嶉渶瑕佷娇鐢ㄧ被鍨嬬殑鍏蜂綋鍔熻兘鏃讹紝鍙娇鐢∣bject绫讳腑鐨勫姛鑳姐€傞偅涔堝彲浠ョ敤 ? 閫氶厤绗︽潵琛ㄦ湭鐭ョ被鍨嬨€?br>
娉涘瀷闄愬畾锛?br> 涓婇檺锛氾紵extends E锛氬彲浠ユ帴鏀禘绫诲瀷鎴栬€匛鐨勫瓙绫诲瀷瀵硅薄銆?br> 涓嬮檺锛氾紵super E锛氬彲浠ユ帴鏀禘绫诲瀷鎴栬€匛鐨勭埗绫诲瀷瀵硅薄銆?br>
涓婇檺浠€涔堟椂鍊欑敤锛氬線闆嗗悎涓坊鍔犲厓绱犳椂锛屾棦鍙互娣诲姞E绫诲瀷瀵硅薄锛屽張鍙互娣诲姞E鐨勫瓙绫诲瀷瀵硅薄銆備负浠€涔堬紵鍥犱负鍙栫殑鏃跺€欙紝E绫诲瀷鏃㈠彲浠ユ帴鏀禘绫诲璞★紝鍙堝彲浠ユ帴鏀禘鐨勫瓙绫诲瀷瀵硅薄銆?br>
涓嬮檺浠€涔堟椂鍊欑敤锛氬綋浠庨泦鍚堜腑鑾峰彇鍏冪礌杩涜鎿嶄綔鐨勬椂鍊欙紝鍙互鐢ㄥ綋鍓嶅厓绱犵殑绫诲瀷鎺ユ敹锛屼篃鍙互鐢ㄥ綋鍓嶅厓绱犵殑鐖剁被鍨嬫帴鏀躲€?br>
娉涘瀷鐨勭粏鑺傦細
1锛夈€佹硾鍨嬪埌搴曚唬琛ㄤ粈涔堢被鍨嬪彇鍐充簬璋冪敤鑰呬紶鍏ョ殑绫诲瀷锛屽鏋滄病浼狅紝榛樿鏄疧bject绫诲瀷锛?br>2锛夈€佷娇鐢ㄥ甫娉涘瀷鐨勭被鍒涘缓瀵硅薄鏃讹紝绛夊紡涓よ竟鎸囧畾鐨勬硾鍨嬪繀椤讳竴鑷达紱
鍘熷洜锛氱紪璇戝櫒妫€鏌ュ璞¤皟鐢ㄦ柟娉曟椂鍙湅鍙橀噺锛岀劧鑰岀▼搴忚繍琛屾湡闂磋皟鐢ㄦ柟娉曟椂灏辫鑰冭檻瀵硅薄鍏蜂綋绫诲瀷浜嗭紱
3锛夈€佺瓑寮忎袱杈瑰彲浠ュ湪浠绘剰涓€杈逛娇鐢ㄦ硾鍨嬶紝鍦ㄥ彟涓€杈逛笉浣跨敤(鑰冭檻鍚戝悗鍏煎)锛?br>ArrayList<String> al = new ArrayList<Object>(); //閿?br>//瑕佷繚璇佸乏鍙充袱杈圭殑娉涘瀷鍏蜂綋绫诲瀷涓€鑷村氨鍙互浜嗭紝杩欐牱涓嶅鏄撳嚭閿欍€?br>ArrayList<? extends Object> al = new ArrayList<String>();
al.add("aa"); //閿?br>//鍥犱负闆嗗悎鍏蜂綋瀵硅薄涓棦鍙瓨鍌⊿tring锛屼篃鍙互瀛樺偍Object鐨勫叾浠栧瓙绫伙紝鎵€浠ユ坊鍔犲叿浣撶殑绫诲瀷瀵硅薄涓嶅悎閫傦紝绫诲瀷妫€鏌ヤ細鍑虹幇瀹夊叏闂銆?锛焑xtends Object 浠h〃Object鐨勫瓙绫诲瀷涓嶇‘瀹氾紝鎬庝箞鑳芥坊鍔犲叿浣撶被鍨嬬殑瀵硅薄鍛紵
public static void method(ArrayList<? extends Object> al) {
al.add("abc"); //閿?br> //鍙兘瀵筧l闆嗗悎涓殑鍏冪礌璋冪敤Object绫讳腑鐨勬柟娉曪紝鍏蜂綋瀛愮被鍨嬬殑鏂规硶閮戒笉鑳界敤锛屽洜涓哄瓙绫诲瀷涓嶇‘瀹氥€?br>}
------------------------------------------------------------------------------------------------------------------------------------------------
IO娴侊細鈽呪槄鈽呪槄鈽咃紝
鐢ㄤ簬澶勭悊璁惧涓婃暟鎹€?br>娴侊細鍙互鐞嗚В鏁版嵁鐨勬祦鍔紝灏辨槸涓€涓暟鎹祦銆侷O娴佹渶缁堣浠ュ璞℃潵浣撶幇锛屽璞¢兘瀛樺湪IO鍖呬腑銆?br>娴佷篃杩涜鍒嗙被锛?br>1锛氳緭鍏ユ祦锛堣锛夊拰杈撳嚭娴侊紙鍐欙級銆?br>2锛氬洜涓哄鐞嗙殑鏁版嵁涓嶅悓锛屽垎涓哄瓧鑺傛祦鍜屽瓧绗︽祦銆?br>
瀛楄妭娴侊細
澶勭悊瀛楄妭鏁版嵁鐨勬祦瀵硅薄銆傝澶囦笂鐨勬暟鎹棤璁烘槸鍥剧墖鎴栬€卍vd锛屾枃瀛楋紝瀹冧滑閮戒互浜岃繘鍒跺瓨鍌ㄧ殑銆備簩杩涘埗鐨勬渶缁堥兘鏄互涓€涓?浣嶄负鏁版嵁鍗曞厓杩涜浣撶幇锛屾墍浠ヨ绠楁満涓殑鏈€灏忔暟鎹崟鍏冨氨鏄瓧鑺傘€傛剰鍛崇潃锛屽瓧鑺傛祦鍙互澶勭悊璁惧涓婄殑鎵€鏈夋暟鎹紝鎵€浠ュ瓧鑺傛祦涓€鏍峰彲浠ュ鐞嗗瓧绗︽暟鎹€?br>
閭d箞涓轰粈涔堣鏈夊瓧绗︽祦鍛紵鍥犱负瀛楃姣忎釜鍥藉閮戒笉涓€鏍凤紝鎵€浠ユ秹鍙婂埌浜嗗瓧绗︾紪鐮侀棶棰橈紝閭d箞GBK缂栫爜鐨勪腑鏂囩敤unicode缂栫爜瑙f瀽鏄湁闂鐨勶紝鎵€浠ラ渶瑕佽幏鍙栦腑鏂囧瓧鑺傛暟鎹殑鍚屾椂+ 鎸囧畾鐨勭紪鐮佽〃鎵嶅彲浠ヨВ鏋愭纭暟鎹€備负浜嗘柟渚夸簬鏂囧瓧鐨勮В鏋愶紝鎵€浠ュ皢瀛楄妭娴佸拰缂栫爜琛ㄥ皝瑁呮垚瀵硅薄锛岃繖涓璞″氨鏄瓧绗︽祦銆傚彧瑕佹搷浣滃瓧绗︽暟鎹紝浼樺厛鑰冭檻浣跨敤瀛楃娴佷綋绯汇€?br>
娉ㄦ剰锛氭祦鐨勬搷浣滃彧鏈変袱绉嶏細璇诲拰鍐欍€?br>
娴佺殑浣撶郴鍥犱负鍔熻兘涓嶅悓锛屼絾鏄湁鍏辨€у唴瀹癸紝涓嶆柇鎶藉彇锛屽舰鎴愮户鎵夸綋绯汇€傝浣撶郴涓€鍏辨湁鍥涗釜鍩虹被锛岃€屼笖閮芥槸鎶借薄绫汇€?br>
瀛楄妭娴侊細InputStream OutputStream
瀛楃娴侊細Reader Writer
鍦ㄨ繖鍥涗釜绯荤粺涓紝瀹冧滑鐨勫瓙绫伙紝閮芥湁涓€涓叡鎬х壒鐐癸細瀛愮被鍚嶅悗缂€閮芥槸鐖剁被鍚嶏紝鍓嶇紑鍚嶉兘鏄繖涓瓙绫荤殑鍔熻兘鍚嶇О銆?br>--------------------------------------------------------------------------------------------------------------------
public static void main(String[] args) throws IOException { //璇汇€佸啓閮戒細鍙戠敓IO寮傚父
/*
1锛氬垱寤轰竴涓瓧绗﹁緭鍑烘祦瀵硅薄锛岀敤浜庢搷浣滄枃浠躲€傝瀵硅薄涓€寤虹珛锛屽氨蹇呴』鏄庣‘鏁版嵁瀛樺偍浣嶇疆锛屾槸涓€涓枃浠躲€?br> 2锛氬璞′骇鐢熷悗锛屼細鍦ㄥ爢鍐呭瓨涓湁涓€涓疄浣擄紝鍚屾椂涔熻皟鐢ㄤ簡绯荤粺搴曞眰璧勬簮锛屽湪鎸囧畾鐨勪綅缃垱寤轰簡涓€涓瓨鍌ㄦ暟鎹殑鏂囦欢銆?br> 3锛氬鏋滄寚瀹氫綅缃紝鍑虹幇浜嗗悓鍚嶆枃浠讹紝鏂囦欢浼氳瑕嗙洊銆?br> */
FileWriter fw = new FileWriter("demo.txt"); // FileNotFoundException
/*
璋冪敤Writer绫讳腑鐨剋rite鏂规硶鍐欏叆瀛楃涓层€傚瓧绗︿覆骞舵湭鐩存帴鍐欏叆鍒扮洰鐨勫湴涓紝鑰屾槸鍐欏叆鍒颁簡娴佷腑锛?鍏跺疄鏄啓鍏ュ埌鍐呭瓨缂撳啿鍖轰腑)銆傛€庝箞鎶婃暟鎹紕鍒版枃浠朵腑锛?br> */
fw.write("abcde");
fw.flush(); // 鍒锋柊缂撳啿鍖猴紝灏嗙紦鍐插尯涓殑鏁版嵁鍒峰埌鐩殑鍦版枃浠朵腑銆?br> fw.close(); // 鍏抽棴娴侊紝鍏跺疄鍏抽棴鐨勫氨鏄痡ava璋冪敤鐨勭郴缁熷簳灞傝祫婧愩€傚湪鍏抽棴鍓嶏紝浼氬厛鍒锋柊璇ユ祦銆?br>}
close()鍜宖lush()鐨勫尯鍒細
flush()锛氬皢缂撳啿鍖虹殑鏁版嵁鍒峰埌鐩殑鍦颁腑鍚庯紝娴佸彲浠ヤ娇鐢ㄣ€?br>close()锛氬皢缂撳啿鍖虹殑鏁版嵁鍒峰埌鐩殑鍦颁腑鍚庯紝娴佸氨鍏抽棴浜嗭紝璇ユ柟娉曚富瑕佺敤浜庣粨鏉熻皟鐢ㄧ殑搴曞眰璧勬簮銆傝繖涓姩浣滀竴瀹氬仛銆?br>--------------------------------------------------------------------------------------------------------------------
io寮傚父鐨勫鐞嗘柟寮忥細
io涓€瀹氳鍐檉inally锛?br>
FileWriter鍐欏叆鏁版嵁鐨勭粏鑺傦細
1锛歸indow涓殑鎹㈣绗︼細
涓や釜绗﹀彿缁勬垚銆?linux锛歕n銆?br>2锛氱画鍐欐暟鎹紝鍙鍦ㄦ瀯閫犲嚱鏁颁腑浼犲叆鏂扮殑鍙傛暟true銆?br>3锛氱洰褰曞垎鍓茬锛歸indow \ /
public static void main(String[] args) {
FileWriter fw = null;
try {
fw = new FileWriter("demo.txt",true);
fw.write("abcde");
}
catch (IOException e ){
System.out.println(e.toString()+"....");
}
finally{
if(fw!=null)
try{
fw.close();
}
catch (IOException e){
System.out.println("close:"+e.toString());
}
}
}
--------------------------------------------------------------------------------------------------------------------
FileReader锛氫娇鐢≧eader浣撶郴锛岃鍙栦竴涓枃鏈枃浠朵腑鐨勬暟鎹€傝繑鍥?-1 锛屾爣蹇楄鍒扮粨灏俱€?br>import java.io.*;
class FileReaderDemo {
public static void main(String[] args) throws IOException {
/*
鍒涘缓鍙互璇诲彇鏂囨湰鏂囦欢鐨勬祦瀵硅薄锛孎ileReader璁╁垱寤哄ソ鐨勬祦瀵硅薄鍜屾寚瀹氱殑鏂囦欢鐩稿叧鑱斻€?br> */
FileReader fr = new FileReader("demo.txt");
int ch = 0;
while((ch = fr.read())!= -1) { //鏉′欢鏄病鏈夎鍒扮粨灏?br> System.out.println((char)ch); //璋冪敤璇诲彇娴佺殑read鏂规硶锛岃鍙栦竴涓瓧绗︺€?br> }
fr.close();
}
}
--------------------------------------------------------------------------------------------------------------------
璇诲彇鏁版嵁鐨勭浜岀鏂瑰紡锛氱浜岀鏂瑰紡杈冧负楂樻晥锛岃嚜瀹氫箟缂撳啿鍖恒€?br>import java.io.*;
class FileReaderDemo2 {
public static void main(String[] args) throws IOException {
FileReader fr = new FileReader("demo.txt"); //鍒涘缓璇诲彇娴佸璞″拰鎸囧畾鏂囦欢鍏宠仈銆?br> //鍥犱负瑕佷娇鐢╮ead(char[])鏂规硶锛屽皢璇诲彇鍒板瓧绗﹀瓨鍏ユ暟缁勩€傛墍浠ヨ鍒涘缓涓€涓瓧绗︽暟缁勶紝涓€鑸暟缁勭殑闀垮害閮芥槸1024鐨勬暣鏁板€嶃€?br> char[] buf = new char[1024];
int len = 0;
while(( len=fr.read(buf)) != -1) {
System.out.println(new String(buf,0,len));
}
fr.close();
}
}
--------------------------------------------------------------------------------------------------------------------
IO涓殑浣跨敤鍒颁簡涓€涓璁℃ā寮忥細瑁呴グ璁捐妯″紡銆?br>瑁呴グ璁捐妯″紡瑙e喅锛氬涓€缁勭被杩涜鍔熻兘鐨勫寮恒€?br>鍖呰锛氬啓涓€涓被(鍖呰绫?瀵硅鍖呰瀵硅薄杩涜鍖呰锛?br> * 1銆佸寘瑁呯被鍜岃鍖呰瀵硅薄瑕佸疄鐜板悓鏍风殑鎺ュ彛锛?br> * 2銆佸寘瑁呯被瑕佹寔鏈変竴涓鍖呰瀵硅薄锛?br> * 3銆佸寘瑁呯被鍦ㄥ疄鐜版帴鍙f椂锛屽ぇ閮ㄥ垎鏂规硶鏄潬璋冪敤琚寘瑁呭璞℃潵瀹炵幇鐨勶紝瀵逛簬闇€瑕佷慨鏀圭殑鏂规硶鎴戜滑鑷繁瀹炵幇锛?br>--------------------------------------------------------------------------------------------------------------------
瀛楃娴侊細
Reader锛氱敤浜庤鍙栧瓧绗︽祦鐨勬娊璞$被銆傚瓙绫诲繀椤诲疄鐜扮殑鏂规硶鍙湁 read(char[], int, int) 鍜?close()銆?br> |---BufferedReader锛氫粠瀛楃杈撳叆娴佷腑璇诲彇鏂囨湰锛岀紦鍐插悇涓瓧绗︼紝浠庤€屽疄鐜板瓧绗︺€佹暟缁勫拰琛岀殑楂樻晥璇诲彇銆?鍙互鎸囧畾缂撳啿鍖虹殑澶у皬锛屾垨鑰呭彲浣跨敤榛樿鐨勫ぇ灏忋€傚ぇ澶氭暟鎯呭喌涓嬶紝榛樿鍊煎氨瓒冲澶т簡銆?br> |---LineNumberReader锛氳窡韪鍙风殑缂撳啿瀛楃杈撳叆娴併€傛绫诲畾涔変簡鏂规硶 setLineNumber(int) 鍜?getLineNumber()锛屽畠浠彲鍒嗗埆鐢ㄤ簬璁剧疆鍜岃幏鍙栧綋鍓嶈鍙枫€?br> |---InputStreamReader锛氭槸瀛楄妭娴侀€氬悜瀛楃娴佺殑妗ユ锛氬畠浣跨敤鎸囧畾鐨?charset 璇诲彇瀛楄妭骞跺皢鍏惰В鐮佷负瀛楃銆傚畠浣跨敤鐨勫瓧绗﹂泦鍙互鐢卞悕绉版寚瀹氭垨鏄惧紡缁欏畾锛屾垨鑰呭彲浠ユ帴鍙楀钩鍙伴粯璁ょ殑瀛楃闆嗐€?br> |---FileReader锛氱敤鏉ヨ鍙栧瓧绗︽枃浠剁殑渚挎嵎绫汇€傛绫荤殑鏋勯€犳柟娉曞亣瀹氶粯璁ゅ瓧绗︾紪鐮佸拰榛樿瀛楄妭缂撳啿鍖哄ぇ灏忛兘鏄€傚綋鐨勩€傝鑷繁鎸囧畾杩欎簺鍊硷紝鍙互鍏堝湪 FileInputStream 涓婃瀯閫犱竴涓?InputStreamReader銆?br> |---CharArrayReader锛?br> |---StringReader锛?br>-------------------------------------------------
Writer锛氬啓鍏ュ瓧绗︽祦鐨勬娊璞$被銆傚瓙绫诲繀椤诲疄鐜扮殑鏂规硶浠呮湁 write(char[], int, int)銆乫lush() 鍜?close()銆?br> |---BufferedWriter锛氬皢鏂囨湰鍐欏叆瀛楃杈撳嚭娴侊紝缂撳啿鍚勪釜瀛楃锛屼粠鑰屾彁渚涘崟涓瓧绗︺€佹暟缁勫拰瀛楃涓茬殑楂樻晥鍐欏叆銆?br> |---OutputStreamWriter锛氭槸瀛楃娴侀€氬悜瀛楄妭娴佺殑妗ユ锛氬彲浣跨敤鎸囧畾鐨?charset 灏嗚鍐欏叆娴佷腑鐨勫瓧绗︾紪鐮佹垚瀛楄妭銆傚畠浣跨敤鐨勫瓧绗﹂泦鍙互鐢卞悕绉版寚瀹氭垨鏄惧紡缁欏畾锛屽惁鍒欏皢鎺ュ彈骞冲彴榛樿鐨勫瓧绗﹂泦銆?br> |---FileWriter锛氱敤鏉ュ啓鍏ュ瓧绗︽枃浠剁殑渚挎嵎绫汇€傛绫荤殑鏋勯€犳柟娉曞亣瀹氶粯璁ゅ瓧绗︾紪鐮佸拰榛樿瀛楄妭缂撳啿鍖哄ぇ灏忛兘鏄彲鎺ュ彈鐨勩€傝鑷繁鎸囧畾杩欎簺鍊硷紝鍙互鍏堝湪 FileOutputStream 涓婃瀯閫犱竴涓?OutputStreamWriter銆?br> |---PrintWriter锛?br> |---CharArrayWriter锛?br> |---StringWriter锛?br>---------------------------------
瀛楄妭娴侊細
InputStream锛氭槸琛ㄧず瀛楄妭杈撳叆娴佺殑鎵€鏈夌被鐨勮秴绫汇€?br> |--- FileInputStream锛氫粠鏂囦欢绯荤粺涓殑鏌愪釜鏂囦欢涓幏寰楄緭鍏ュ瓧鑺傘€傚摢浜涙枃浠跺彲鐢ㄥ彇鍐充簬涓绘満鐜銆侳ileInputStream 鐢ㄤ簬璇诲彇璇稿鍥惧儚鏁版嵁涔嬬被鐨勫師濮嬪瓧鑺傛祦銆傝璇诲彇瀛楃娴侊紝璇疯€冭檻浣跨敤 FileReader銆?br> |--- FilterInputStream锛氬寘鍚叾浠栦竴浜涜緭鍏ユ祦锛屽畠灏嗚繖浜涙祦鐢ㄤ綔鍏跺熀鏈暟鎹簮锛屽畠鍙互鐩存帴浼犺緭鏁版嵁鎴栨彁渚涗竴浜涢澶栫殑鍔熻兘銆?br> |--- BufferedInputStream锛氳绫诲疄鐜扮紦鍐茬殑杈撳叆娴併€?br> |--- Stream锛?br> |--- ObjectInputStream锛?br> |--- PipedInputStream锛?br>-----------------------------------------------
OutputStream锛氭鎶借薄绫绘槸琛ㄧず杈撳嚭瀛楄妭娴佺殑鎵€鏈夌被鐨勮秴绫汇€?br> |--- FileOutputStream锛氭枃浠惰緭鍑烘祦鏄敤浜庡皢鏁版嵁鍐欏叆 File 鎴?FileDescriptor 鐨勮緭鍑烘祦銆?br> |--- FilterOutputStream锛氭绫绘槸杩囨护杈撳嚭娴佺殑鎵€鏈夌被鐨勮秴绫汇€?br> |--- BufferedOutputStream锛氳绫诲疄鐜扮紦鍐茬殑杈撳嚭娴併€?br> |--- PrintStream锛?br> |--- DataOutputStream锛?br> |--- ObjectOutputStream锛?br> |--- PipedOutputStream锛?br>--------------------------------
缂撳啿鍖烘槸鎻愰珮鏁堢巼鐢ㄧ殑锛岀粰璋佹彁楂樺憿锛?br>BufferedWriter锛氭槸缁欏瓧绗﹁緭鍑烘祦鎻愰珮鏁堢巼鐢ㄧ殑锛岄偅灏辨剰鍛崇潃锛岀紦鍐插尯瀵硅薄寤虹珛鏃讹紝蹇呴』瑕佸厛鏈夋祦瀵硅薄銆傛槑纭鎻愰珮鍏蜂綋鐨勬祦瀵硅薄鐨勬晥鐜囥€?br> FileWriter fw = new FileWriter("bufdemo.txt");
BufferedWriter bufw = new BufferedWriter(fw);//璁╃紦鍐插尯鍜屾寚瀹氭祦鐩稿叧鑱斻€?br> for(int x=0; x<4; x++){
bufw.write(x+"abc");
bufw.newLine(); //鍐欏叆涓€涓崲琛岀锛岃繖涓崲琛岀鍙互渚濇嵁骞冲彴鐨勪笉鍚屽啓鍏ヤ笉鍚岀殑鎹㈣绗︺€?br> bufw.flush();//瀵圭紦鍐插尯杩涜鍒锋柊锛屽彲浠ヨ鏁版嵁鍒扮洰鐨勫湴涓€?br> }
bufw.close();//鍏抽棴缂撳啿鍖猴紝鍏跺疄灏辨槸鍦ㄥ叧闂叿浣撶殑娴併€?br>-----------------------------
BufferedReader锛?br> FileReader fr = new FileReader("bufdemo.txt");
BufferedReader bufr = new BufferedReader(fr);
String line = null;
while((line=bufr.readLine())!=null){ //readLine鏂规硶杩斿洖鐨勬椂鍊欐槸涓嶅甫鎹㈣绗︾殑銆?br> System.out.println(line);
}
bufr.close();
-----------------------------
//璁颁綇锛屽彧瑕佷竴璇诲彇閿洏褰曞叆锛屽氨鐢ㄨ繖鍙ヨ瘽銆?br>BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bufw = new BufferedWriter(new OutputStreamWriter(System.out));//杈撳嚭鍒版帶鍒跺彴
String line = null;
while((line=bufr.readLine())!=null){
if("over".equals(line))
break;
bufw.write(line.toUpperCase());//灏嗚緭鍏ョ殑瀛楃杞垚澶у啓瀛楃杈撳嚭
bufw.newLine();
bufw.flush();
}
bufw.close();
bufr.close();
------------------------------
娴佸璞★細
鍏跺疄寰堢畝鍗曪紝灏辨槸璇诲彇鍜屽啓鍏ャ€備絾鏄洜涓哄姛鑳界殑涓嶅悓锛屾祦鐨勪綋绯讳腑鎻愪緵N澶氱殑瀵硅薄銆傞偅涔堝紑濮嬫椂锛屽埌搴曡鐢ㄥ摢涓璞℃洿涓哄悎閫傚憿锛熻繖灏遍渶瑕佹槑纭祦鐨勬搷浣滆寰嬨€?br>
娴佺殑鎿嶄綔瑙勫緥锛?br>1锛屾槑纭簮鍜岀洰鐨勩€?br> 鏁版嵁婧愶細灏辨槸闇€瑕佽鍙栵紝鍙互浣跨敤涓や釜浣撶郴锛欼nputStream銆丷eader锛?br> 鏁版嵁姹囷細灏辨槸闇€瑕佸啓鍏ワ紝鍙互浣跨敤涓や釜浣撶郴锛歄utputStream銆乄riter锛?br>2锛屾搷浣滅殑鏁版嵁鏄惁鏄函鏂囨湰鏁版嵁锛?br> 濡傛灉鏄細鏁版嵁婧愶細Reader
鏁版嵁姹囷細Writer
濡傛灉涓嶆槸锛氭暟鎹簮锛欼nputStream
鏁版嵁姹囷細OutputStream
3锛岃櫧鐒剁‘瀹氫簡涓€涓綋绯伙紝浣嗘槸璇ヤ綋绯讳腑鏈夊お澶氱殑瀵硅薄锛屽埌搴曠敤鍝釜鍛紵
鏄庣‘鎿嶄綔鐨勬暟鎹澶囥€?br> 鏁版嵁婧愬搴旂殑璁惧锛氱‖鐩?File)锛屽唴瀛?鏁扮粍)锛岄敭鐩?System.in)
鏁版嵁姹囧搴旂殑璁惧锛氱‖鐩?File)锛屽唴瀛?鏁扮粍)锛屾帶鍒跺彴(System.out)銆?br>4锛岄渶瑕佸湪鍩烘湰鎿嶄綔涓婇檮鍔犲叾浠栧姛鑳藉悧锛熸瘮濡傜紦鍐层€?br> 濡傛灉闇€瑕佸氨杩涜瑁呴グ銆?br>
杞崲娴佺壒鏈夊姛鑳斤細杞崲娴佸彲浠ュ皢瀛楄妭杞垚瀛楃锛屽師鍥犲湪浜庯紝灏嗚幏鍙栧埌鐨勫瓧鑺傞€氳繃鏌ョ紪鐮佽〃鑾峰彇鍒版寚瀹氬搴斿瓧绗︺€?br>
杞崲娴佺殑鏈€寮哄姛鑳藉氨鏄熀浜?瀛楄妭娴?+ 缂栫爜琛?銆傛病鏈夎浆鎹紝娌℃湁瀛楃娴併€?br>
鍙戠幇杞崲娴佹湁涓€涓瓙绫诲氨鏄搷浣滄枃浠剁殑瀛楃娴佸璞★細
InputStreamReader
|--FileReader
OutputStreamWriter
|--FileWrier
鎯宠鎿嶄綔鏂囨湰鏂囦欢锛屽繀椤昏杩涜缂栫爜杞崲锛岃€岀紪鐮佽浆鎹㈠姩浣滆浆鎹㈡祦閮藉畬鎴愪簡銆傛墍浠ユ搷浣滄枃浠剁殑娴佸璞″彧瑕佺户鎵胯嚜杞崲娴佸氨鍙互璇诲彇涓€涓瓧绗︿簡銆?br>
浣嗘槸瀛愮被鏈変竴涓眬闄愭€э紝灏辨槸瀛愮被涓娇鐢ㄧ殑缂栫爜鏄浐瀹氱殑锛屾槸鏈満榛樿鐨勭紪鐮佽〃锛屽浜庣畝浣撲腑鏂囩増鐨勭郴缁熼粯璁ょ爜琛ㄦ槸GBK銆?br>FileReader fr = new FileReader("a.txt");
InputStreamReader isr = new InputStreamReader(new FileInputStream("a.txt"),"gbk");
浠ヤ笂涓ゅ彞浠g爜鍔熻兘涓€鑷达紝
濡傛灉浠呬粎浣跨敤骞冲彴榛樿鐮佽〃锛屽氨浣跨敤FileReader fr = new FileReader("a.txt"); //鍥犱负绠€鍖栥€?br>
濡傛灉闇€瑕佸埗瀹氱爜琛紝蹇呴』鐢ㄨ浆鎹㈡祦銆?br>杞崲娴?= 瀛楄妭娴?缂栫爜琛ㄣ€?br>杞崲娴佺殑瀛愮被File = 瀛楄妭娴?+ 榛樿缂栫爜琛ㄣ€?br>
鍑℃槸鎿嶄綔璁惧涓婄殑鏂囨湰鏁版嵁锛屾秹鍙婄紪鐮佽浆鎹紝蹇呴』浣跨敤杞崲娴併€?br>-----------------------------------------------------------------------------------------------
File绫伙細
灏嗘枃浠剁郴缁熶腑鐨勬枃浠跺拰鏂囦欢澶瑰皝瑁呮垚浜嗗璞°€傛彁渚涗簡鏇村鐨勫睘鎬у拰琛屼负鍙互瀵硅繖浜涙枃浠跺拰鏂囦欢澶硅繘琛屾搷浣溿€傝繖浜涙槸娴佸璞″姙涓嶅埌鐨勶紝鍥犱负娴佸彧鎿嶄綔鏁版嵁銆?br>
File绫诲父瑙佹柟娉曪細
1锛氬垱寤恒€?br> boolean createNewFile()锛氬湪鎸囧畾鐩綍涓嬪垱寤烘枃浠讹紝濡傛灉璇ユ枃浠跺凡瀛樺湪锛屽垯涓嶅垱寤恒€傝€屽鎿嶄綔鏂囦欢鐨勮緭鍑烘祦鑰岃█锛岃緭鍑烘祦瀵硅薄宸插缓绔嬶紝灏变細鍒涘缓鏂囦欢锛屽鏋滄枃浠跺凡瀛樺湪锛屼細瑕嗙洊銆傞櫎闈炵画鍐欍€?br> boolean mkdir()锛氬垱寤烘鎶借薄璺緞鍚嶆寚瀹氱殑鐩綍銆?br> boolean mkdirs()锛氬垱寤哄绾х洰褰曘€?br>2锛氬垹闄ゃ€?br> boolean delete()锛氬垹闄ゆ鎶借薄璺緞鍚嶈〃绀虹殑鏂囦欢鎴栫洰褰曘€?br> void deleteOnExit()锛氬湪铏氭嫙鏈洪€€鍑烘椂鍒犻櫎銆?br>娉ㄦ剰锛氬湪鍒犻櫎鏂囦欢澶规椂锛屽繀椤讳繚璇佽繖涓枃浠跺す涓病鏈変换浣曞唴瀹癸紝鎵嶅彲浠ュ皢璇ユ枃浠跺す鐢╠elete鍒犻櫎銆?br> window鐨勫垹闄ゅ姩浣滐紝鏄粠閲屽線澶栧垹銆傛敞鎰忥細java鍒犻櫎鏂囦欢涓嶈蛋鍥炴敹绔欍€傝鎱庣敤銆?br>3锛氳幏鍙?
long length()锛氳幏鍙栨枃浠跺ぇ灏忋€?br> String getName()锛氳繑鍥炵敱姝ゆ娊璞¤矾寰勫悕琛ㄧず鐨勬枃浠舵垨鐩綍鐨勫悕绉般€?br> String getPath()锛氬皢姝ゆ娊璞¤矾寰勫悕杞崲涓轰竴涓矾寰勫悕瀛楃涓层€?br> String getAbsolutePath()锛氳繑鍥炴鎶借薄璺緞鍚嶇殑缁濆璺緞鍚嶅瓧绗︿覆銆?br> String getParent()锛氳繑鍥炴鎶借薄璺緞鍚嶇埗鐩綍鐨勬娊璞¤矾寰勫悕锛屽鏋滄璺緞鍚嶆病鏈夋寚瀹氱埗鐩綍锛屽垯杩斿洖 null銆?br>long lastModified()锛氳繑鍥炴鎶借薄璺緞鍚嶈〃绀虹殑鏂囦欢鏈€鍚庝竴娆¤淇敼鐨勬椂闂淬€?br>File.pathSeparator锛氳繑鍥炲綋鍓嶇郴缁熼粯璁ょ殑璺緞鍒嗛殧绗︼紝windows榛樿涓?鈥滐紱鈥濄€?br>File.Separator锛氳繑鍥炲綋鍓嶇郴缁熼粯璁ょ殑鐩綍鍒嗛殧绗︼紝windows榛樿涓?鈥淺鈥濄€?br>4锛氬垽鏂細
boolean exists()锛氬垽鏂枃浠舵垨鑰呮枃浠跺す鏄惁瀛樺湪銆?br> boolean isDirectory()锛氭祴璇曟鎶借薄璺緞鍚嶈〃绀虹殑鏂囦欢鏄惁鏄竴涓洰褰曘€?br> boolean isFile()锛氭祴璇曟鎶借薄璺緞鍚嶈〃绀虹殑鏂囦欢鏄惁鏄竴涓爣鍑嗘枃浠躲€?br> boolean isHidden()锛氭祴璇曟鎶借薄璺緞鍚嶆寚瀹氱殑鏂囦欢鏄惁鏄竴涓殣钘忔枃浠躲€?br> boolean isAbsolute()锛氭祴璇曟鎶借薄璺緞鍚嶆槸鍚︿负缁濆璺緞鍚嶃€?br>5锛氶噸鍛藉悕銆?br> boolean renameTo(File dest)锛氬彲浠ュ疄鐜扮Щ鍔ㄧ殑鏁堟灉銆傚壀鍒?閲嶅懡鍚嶃€?br>
String[] list()锛氬垪鍑烘寚瀹氱洰褰曚笅鐨勫綋鍓嶇殑鏂囦欢鍜屾枃浠跺す鐨勫悕绉般€傚寘鍚殣钘忔枃浠躲€?br>濡傛灉璋冪敤list鏂规硶鐨凢ile 瀵硅薄涓皝瑁呯殑鏄竴涓枃浠讹紝閭d箞list鏂规硶杩斿洖鏁扮粍涓簄ull銆傚鏋滃皝瑁呯殑瀵硅薄涓嶅瓨鍦ㄤ篃浼氳繑鍥瀗ull銆傚彧鏈夊皝瑁呯殑瀵硅薄瀛樺湪骞朵笖鏄枃浠跺す鏃讹紝杩欎釜鏂规硶鎵嶆湁鏁堛€?br>------------------------------------------------------------------------------------------------
閫掑綊锛氬氨鏄嚱鏁拌嚜韬皟鐢ㄨ嚜韬€?/h1>
浠€涔堟椂鍊欑敤閫掑綊鍛紵
褰撲竴涓姛鑳借閲嶅浣跨敤锛岃€屾瘡涓€娆′娇鐢ㄨ鍔熻兘鏃剁殑鍙傛暟涓嶇‘瀹氾紝閮界敱涓婃鐨勫姛鑳藉厓绱犵粨鏋滄潵纭畾銆?br>绠€鍗曡锛氬姛鑳藉唴閮ㄥ張鐢ㄥ埌璇ュ姛鑳斤紝浣嗘槸浼犻€掔殑鍙傛暟鍊间笉纭畾銆?姣忔鍔熻兘鍙備笌杩愮畻鐨勬湭鐭ュ唴瀹逛笉纭畾)銆?br>
閫掑綊鐨勬敞鎰忎簨椤癸細
1锛氫竴瀹氳瀹氫箟閫掑綊鐨勬潯浠躲€?br>2锛氶€掑綊鐨勬鏁颁笉瑕佽繃澶氥€傚鏄撳嚭鐜?StackOverflowError 鏍堝唴瀛樻孩鍑洪敊璇€?br>鍏跺疄閫掑綊灏辨槸鍦ㄦ爤鍐呭瓨涓笉鏂殑鍔犺浇鍚屼竴涓嚱鏁般€?br>------------------------------------------------------------------------------------------------
Java.util.Properties锛?/h3>
|--TreeMap锛氬簳灞傛槸浜屽弶鏍戠粨鏋勶紝鍙互瀵筸ap闆嗗悎涓殑閿繘琛屾寚瀹氶『搴忕殑鎺掑簭銆?br>
Map闆嗗悎瀛樺偍鍜孋ollection鏈夌潃寰堝ぇ涓嶅悓锛?br>Collection涓€娆″瓨涓€涓厓绱狅紱Map涓€娆″瓨涓€瀵瑰厓绱犮€?br>Collection鏄崟鍒楅泦鍚堬紱Map鏄弻鍒楅泦鍚堛€?br>Map涓殑瀛樺偍鐨勪竴瀵瑰厓绱狅細涓€涓槸閿紝涓€涓槸鍊硷紝閿笌鍊间箣闂存湁瀵瑰簲(鏄犲皠)鍏崇郴銆?br>鐗圭偣锛氳淇濊瘉map闆嗗悎涓敭鐨勫敮涓€鎬с€?br>
1锛屾坊鍔犮€?br> put(key,value)锛氬綋瀛樺偍鐨勯敭鐩稿悓鏃讹紝鏂扮殑鍊间細鏇挎崲鑰佺殑鍊硷紝骞跺皢鑰佸€艰繑鍥炪€傚鏋滈敭娌℃湁閲嶅锛岃繑鍥瀗ull銆?br> void putAll(Map);
2锛屽垹闄ゃ€?br> void clear()锛氭竻绌?br> value remove(key) 锛氬垹闄ゆ寚瀹氶敭銆?br>3锛屽垽鏂€?br> boolean isEmpty()锛?br> boolean containsKey(key)锛氭槸鍚﹀寘鍚玨ey
boolean containsValue(value) 锛氭槸鍚﹀寘鍚玽alue
4锛屽彇鍑恒€?br> int size()锛氳繑鍥為暱搴?br> value get(key) 锛氶€氳繃鎸囧畾閿幏鍙栧搴旂殑鍊笺€傚鏋滆繑鍥瀗ull锛屽彲浠ュ垽鏂閿笉瀛樺湪銆傚綋鐒舵湁鐗规畩鎯呭喌锛屽氨鏄湪hashmap闆嗗悎涓紝鏄彲浠ュ瓨鍌╪ull閿畁ull鍊肩殑銆?br> Collection values()锛氳幏鍙杕ap闆嗗悎涓殑鎵€鏈夌殑鍊笺€?br>5锛屾兂瑕佽幏鍙杕ap涓殑鎵€鏈夊厓绱狅細
鍘熺悊锛歮ap涓槸娌℃湁杩唬鍣ㄧ殑锛宑ollection鍏峰杩唬鍣紝鍙灏唌ap闆嗗悎杞垚Set闆嗗悎锛屽彲浠ヤ娇鐢ㄨ凯浠e櫒浜嗐€備箣鎵€浠ヨ浆鎴恠et锛屾槸鍥犱负map闆嗗悎鍏峰鐫€閿殑鍞竴鎬э紝鍏跺疄set闆嗗悎灏辨潵鑷簬map锛宻et闆嗗悎搴曞眰鍏跺疄鐢ㄧ殑灏辨槸map鐨勬柟娉曘€?br>鎶妋ap闆嗗悎杞垚set鐨勬柟娉曪細
Set keySet();
Set entrySet();//鍙栫殑鏄敭鍜屽€肩殑鏄犲皠鍏崇郴銆?br>Entry灏辨槸Map鎺ュ彛涓殑鍐呴儴鎺ュ彛锛?br>涓轰粈涔堣瀹氫箟鍦╩ap鍐呴儴鍛紵entry鏄闂敭鍊煎叧绯荤殑鍏ュ彛锛屾槸map鐨勫叆鍙o紝璁块棶鐨勬槸map涓殑閿€煎銆?br>---------------------------------------------------------
鍙栧嚭map闆嗗悎涓墍鏈夊厓绱犵殑鏂瑰紡涓€锛歬eySet()鏂规硶銆?br>鍙互灏唌ap闆嗗悎涓殑閿兘鍙栧嚭瀛樻斁鍒皊et闆嗗悎涓€傚set闆嗗悎杩涜杩唬銆傝凯浠e畬鎴愶紝鍐嶉€氳繃get鏂规硶瀵硅幏鍙栧埌鐨勯敭杩涜鍊肩殑鑾峰彇銆?br> Set keySet = map.keySet();
Iterator it = keySet.iterator();
while(it.hasNext()) {
Object key = it.next();
Object value = map.get(key);
System.out.println(key+":"+value);
}
--------------------------------------------------------
鍙栧嚭map闆嗗悎涓墍鏈夊厓绱犵殑鏂瑰紡浜岋細entrySet()鏂规硶銆?br>Set entrySet = map.entrySet();
Iterator it = entrySet.iterator();
while(it.hasNext()) {
Map.Entry me = (Map.Entry)it.next();
System.out.println(me.getKey()+"::::"+me.getValue());
}
--------------------------------------------------------
浣跨敤闆嗗悎鐨勬妧宸э細
鐪嬪埌Array灏辨槸鏁扮粍缁撴瀯锛屾湁瑙掓爣锛屾煡璇㈤€熷害寰堝揩銆?br>鐪嬪埌link灏辨槸閾捐〃缁撴瀯锛氬鍒犻€熷害蹇紝鑰屼笖鏈夌壒鏈夋柟娉曘€俛ddFirst锛?addLast锛?removeFirst()锛?removeLast()锛?getFirst()锛沢etLast()锛?br>鐪嬪埌hash灏辨槸鍝堝笇琛紝灏辫鎯宠鍝堝笇鍊硷紝灏辫鎯冲埌鍞竴鎬э紝灏辫鎯冲埌瀛樺叆鍒拌缁撴瀯鐨勪腑鐨勫厓绱犲繀椤昏鐩杊ashCode锛宔quals鏂规硶銆?br>鐪嬪埌tree灏辨槸浜屽弶鏍戯紝灏辫鎯冲埌鎺掑簭锛屽氨鎯宠鐢ㄥ埌姣旇緝銆?br>姣旇緝鐨勪袱绉嶆柟寮忥細涓€涓槸Comparable锛氳鐩朿ompareTo鏂规硶锛?br>涓€涓槸Comparator锛氳鐩朿ompare鏂规硶銆?br>LinkedHashSet锛孡inkedHashMap:杩欎袱涓泦鍚堝彲浠ヤ繚璇佸搱甯岃〃鏈夊瓨鍏ラ『搴忓拰鍙栧嚭椤哄簭涓€鑷达紝淇濊瘉鍝堝笇琛ㄦ湁搴忋€?br>
闆嗗悎浠€涔堟椂鍊欑敤锛?br>褰撳瓨鍌ㄧ殑鏄竴涓厓绱犳椂锛屽氨鐢–ollection銆傚綋瀛樺偍瀵硅薄涔嬮棿瀛樺湪鐫€鏄犲皠鍏崇郴鏃讹紝灏变娇鐢∕ap闆嗗悎銆?br>
淇濊瘉鍞竴锛屽氨鐢⊿et銆備笉淇濊瘉鍞竴锛屽氨鐢↙ist銆?br>------------------------------------------------------------------------------------------------
Collections锛氬畠鐨勫嚭鐜扮粰闆嗗悎鎿嶄綔鎻愪緵浜嗘洿澶氱殑鍔熻兘銆傝繖涓被涓嶉渶瑕佸垱寤哄璞★紝鍐呴儴鎻愪緵鐨勯兘鏄潤鎬佹柟娉曘€?br>闈欐€佹柟娉曪細
Collections.sort(list);//list闆嗗悎杩涜鍏冪礌鐨勮嚜鐒堕『搴忔帓搴忋€?br>Collections.sort(list,new ComparatorByLen());//鎸夋寚瀹氱殑姣旇緝鍣ㄦ柟娉曟帓搴忋€?br>class ComparatorByLen implements Comparator<String>{
public int compare(String s1,String s2){
int temp = s1.length()-s2.length();
return temp==0?s1.compareTo(s2):temp;
}
}
Collections.max(list); //杩斿洖list涓瓧鍏搁『搴忔渶澶х殑鍏冪礌銆?br>int index = Collections.binarySearch(list,"zz");//浜屽垎鏌ユ壘锛岃繑鍥炶鏍囥€?br>Collections.reverseOrder();//閫嗗悜鍙嶈浆鎺掑簭銆?br>Collections.shuffle(list);//闅忔満瀵筶ist涓殑鍏冪礌杩涜浣嶇疆鐨勭疆鎹€?br>
灏嗛潪鍚屾闆嗗悎杞垚鍚屾闆嗗悎鐨勬柟娉曪細Collections涓殑 XXX synchronizedXXX(XXX);
List synchronizedList(list);
Map synchronizedMap(map);
鍘熺悊锛氬畾涔変竴涓被锛屽皢闆嗗悎鎵€鏈夌殑鏂规硶鍔犲悓涓€鎶婇攣鍚庤繑鍥炪€?br>
Collection 鍜?Collections鐨勫尯鍒細
Collections鏄釜java.util涓嬬殑绫伙紝鏄拡瀵归泦鍚堢被鐨勪竴涓伐鍏风被,鎻愪緵涓€绯诲垪闈欐€佹柟娉?瀹炵幇瀵归泦鍚堢殑鏌ユ壘銆佹帓搴忋€佹浛鎹€佺嚎绋嬪畨鍏ㄥ寲锛堝皢闈炲悓姝ョ殑闆嗗悎杞崲鎴愬悓姝ョ殑锛夌瓑鎿嶄綔銆?br>Collection鏄釜java.util涓嬬殑鎺ュ彛锛屽畠鏄悇绉嶉泦鍚堢粨鏋勭殑鐖舵帴鍙o紝缁ф壙浜庡畠鐨勬帴鍙d富瑕佹湁Set鍜孡ist,鎻愪緵浜嗗叧浜庨泦鍚堢殑涓€浜涙搷浣?濡傛彃鍏ャ€佸垹闄ゃ€佸垽鏂竴涓厓绱犳槸鍚﹀叾鎴愬憳銆侀亶鍘嗙瓑銆?br>-------------------------------------------------------
Arrays锛?br>鐢ㄤ簬鎿嶄綔鏁扮粍瀵硅薄鐨勫伐鍏风被锛岄噷闈㈤兘鏄潤鎬佹柟娉曘€?br>asList鏂规硶锛氬皢鏁扮粍杞崲鎴恖ist闆嗗悎銆?br>String[] arr = {"abc","kk","qq"};
List<String> list = Arrays.asList(arr);//灏哸rr鏁扮粍杞垚list闆嗗悎銆?br>灏嗘暟缁勮浆鎹㈡垚闆嗗悎锛屾湁浠€涔堝ソ澶勫憿锛熺敤aslist鏂规硶锛屽皢鏁扮粍鍙樻垚闆嗗悎锛?br>鍙互閫氳繃list闆嗗悎涓殑鏂规硶鏉ユ搷浣滄暟缁勪腑鐨勫厓绱狅細isEmpty()銆乧ontains銆乮ndexOf銆乻et锛?br>娉ㄦ剰锛堝眬闄愭€э級锛氭暟缁勬槸鍥哄畾闀垮害锛屼笉鍙互浣跨敤闆嗗悎瀵硅薄澧炲姞鎴栬€呭垹闄ょ瓑锛屼細鏀瑰彉鏁扮粍闀垮害鐨勫姛鑳芥柟娉曘€傛瘮濡俛dd銆乺emove銆乧lear銆傦紙浼氭姤涓嶆敮鎸佹搷浣滃紓甯窾nsupportedOperationException锛夛紱
濡傛灉鏁扮粍涓瓨鍌ㄧ殑寮曠敤鏁版嵁绫诲瀷锛岀洿鎺ヤ綔涓洪泦鍚堢殑鍏冪礌鍙互鐩存帴鐢ㄩ泦鍚堟柟娉曟搷浣溿€?br>濡傛灉鏁扮粍涓瓨鍌ㄧ殑鏄熀鏈暟鎹被鍨嬶紝asList浼氬皢鏁扮粍瀹炰綋浣滀负闆嗗悎鍏冪礌瀛樺湪銆?br>
闆嗗悎鍙樻暟缁勶細鐢ㄧ殑鏄疌ollection鎺ュ彛涓殑鏂规硶锛歵oArray();
濡傛灉缁檛oArray浼犻€掔殑鎸囧畾绫诲瀷鐨勬暟鎹暱搴﹀皬浜庝簡闆嗗悎鐨剆ize锛岄偅涔坱oArray鏂规硶锛屼細鑷畾鍐嶅垱寤轰竴涓绫诲瀷鐨勬暟鎹紝闀垮害涓洪泦鍚堢殑size銆?br> 濡傛灉浼犻€掔殑鎸囧畾鐨勭被鍨嬬殑鏁扮粍鐨勯暱搴﹀ぇ浜庝簡闆嗗悎鐨剆ize锛岄偅涔坱oArray鏂规硶锛屽氨涓嶄細鍒涘缓鏂版暟缁勶紝鐩存帴浣跨敤璇ユ暟缁勫嵆鍙紝骞跺皢闆嗗悎涓殑鍏冪礌瀛樺偍鍒版暟缁勪腑锛屽叾浠栦负瀛樺偍鍏冪礌鐨勪綅缃粯璁ゅ€糿ull銆?br> 鎵€浠ワ紝鍦ㄤ紶閫掓寚瀹氱被鍨嬫暟缁勬椂锛屾渶濂界殑鏂瑰紡灏辨槸鎸囧畾鐨勯暱搴﹀拰size鐩哥瓑鐨勬暟缁勩€?br>灏嗛泦鍚堝彉鎴愭暟缁勫悗鏈変粈涔堝ソ澶勶紵闄愬畾浜嗗闆嗗悎涓殑鍏冪礌杩涜澧炲垹鎿嶄綔锛屽彧瑕佽幏鍙栬繖浜涘厓绱犲嵆鍙€?br>------------------------------------------------------------------------------------------------
Jdk5.0鏂扮壒鎬э細
Collection鍦╦dk1.5浠ュ悗锛屾湁浜嗕竴涓埗鎺ュ彛Iterable锛岃繖涓帴鍙g殑鍑虹幇鐨勫皢iterator鏂规硶杩涜鎶藉彇锛屾彁楂樹簡鎵╁睍鎬с€?br>--------------------------------------------------
澧炲己for寰幆锛歠oreach璇彞锛宖oreach绠€鍖栦簡杩唬鍣ㄣ€?br>鏍煎紡锛?/ 澧炲己for寰幆鎷彿閲屽啓涓や釜鍙傛暟锛岀涓€涓槸澹版槑涓€涓彉閲忥紝绗簩涓氨鏄渶瑕佽凯浠g殑瀹瑰櫒
for( 鍏冪礌绫诲瀷 鍙橀噺鍚?: Collection闆嗗悎 & 鏁扮粍 ) {
鈥?br>}
楂樼骇for寰幆鍜屼紶缁焒or寰幆鐨勫尯鍒細
楂樼骇for寰幆鍦ㄤ娇鐢ㄦ椂锛屽繀椤昏鏄庣‘琚亶鍘嗙殑鐩爣銆傝繖涓洰鏍囷紝鍙互鏄疌ollection闆嗗悎鎴栬€呮暟缁勶紝濡傛灉閬嶅巻Collection闆嗗悎锛屽湪閬嶅巻杩囩▼涓繕闇€瑕佸鍏冪礌杩涜鎿嶄綔锛屾瘮濡傚垹闄わ紝闇€瑕佷娇鐢ㄨ凯浠e櫒銆?br>濡傛灉閬嶅巻鏁扮粍锛岃繕闇€瑕佸鏁扮粍鍏冪礌杩涜鎿嶄綔锛屽缓璁敤浼犵粺for寰幆鍥犱负鍙互瀹氫箟瑙掓爣閫氳繃瑙掓爣鎿嶄綔鍏冪礌銆傚鏋滃彧涓洪亶鍘嗚幏鍙栵紝鍙互绠€鍖栨垚楂樼骇for寰幆锛屽畠鐨勫嚭鐜颁负浜嗙畝鍖栦功鍐欍€?br>
楂樼骇for寰幆鍙互閬嶅巻map闆嗗悎鍚楋紵涓嶅彲浠ャ€備絾鏄彲浠ュ皢map杞垚set鍚庡啀浣跨敤foreach璇彞銆?br>
1)銆佷綔鐢細瀵瑰瓨鍌ㄥ璞$殑瀹瑰櫒杩涜杩唬锛?鏁扮粍 collection map
2)銆佸寮篺or寰幆杩唬鏁扮粍锛?br>String [] arr = {"a", "b", "c"};//鏁扮粍鐨勯潤鎬佸畾涔夋柟寮忥紝鍙瘯鐢ㄤ簬鏁扮粍棣栨瀹氫箟鐨勬椂鍊?br>for(String s : arr) {
System.out.println(s);
}
3)銆佸崟鍒楅泦鍚?Collection锛?br>List list = new ArrayList();
list.add("aaa");
// 澧炲己for寰幆, 娌℃湁浣跨敤娉涘瀷鐨勯泦鍚堣兘涓嶈兘浣跨敤澧炲己for寰幆杩唬锛熻兘
for(Object obj : list) {
String s = (String) obj;
System.out.println(s);
}
4)銆佸弻鍒楅泦鍚?Map锛?br>Map map = new HashMap();
map.put("a", "aaa");
// 浼犵粺鏂瑰紡锛氬繀椤绘帉鎻¤繖绉嶆柟寮?br>Set entrys = map.entrySet(); // 1.鑾峰緱鎵€鏈夌殑閿€煎Entry瀵硅薄
iter = entrys.iterator(); // 2.杩唬鍑烘墍鏈夌殑entry
while(iter.hasNext()) {
Map.Entry entry = (Entry) iter.next();
String key = (String) entry.getKey(); // 鍒嗗埆鑾峰緱key鍜寁alue
String value = (String) entry.getValue();
System.out.println(key + "=" + value);
}
// 澧炲己for寰幆杩唬锛氬師鍒欎笂map闆嗗悎鏄棤娉曚娇鐢ㄥ寮篺or寰幆鏉ヨ凯浠g殑锛屽洜涓哄寮篺or寰幆鍙兘閽堝瀹炵幇浜咺terable鎺ュ彛鐨勯泦鍚堣繘琛岃凯浠o紱Iterable鏄痡dk5涓柊瀹氫箟鐨勬帴鍙o紝灏变竴涓柟娉昳terator鏂规硶锛屽彧鏈夊疄鐜颁簡Iterable鎺ュ彛鐨勭被锛屾墠鑳戒繚璇佷竴瀹氭湁iterator鏂规硶锛宩ava鏈夎繖鏍风殑闄愬畾鏄洜涓哄寮篺or寰幆鍐呴儴杩樻槸鐢ㄨ凯浠e櫒瀹炵幇鐨勶紝鑰屽疄闄呬笂锛屾垜浠彲浠ラ€氳繃鏌愮鏂瑰紡鏉ヤ娇鐢ㄥ寮篺or寰幆銆?br>for(Object obj : map.entrySet()) {
Map.Entry entry = (Entry) obj; // obj 渚濇琛ㄧずEntry
System.out.println(entry.getKey() + "=" + entry.getValue());
}
5锛夈€侀泦鍚堣凯浠f敞鎰忛棶棰橈細鍦ㄨ凯浠i泦鍚堢殑杩囩▼涓紝涓嶈兘瀵归泦鍚堣繘琛屽鍒犳搷浣滐紙浼氭姤骞跺彂璁块棶寮傚父锛夛紱鍙互鐢ㄨ凯浠e櫒鐨勬柟娉曡繘琛屾搷浣滐紙瀛愮被listIterator锛氭湁澧炲垹鐨勬柟娉曪級銆?br>6)銆佸寮篺or寰幆娉ㄦ剰闂锛氬湪浣跨敤澧炲己for寰幆鏃讹紝涓嶈兘瀵瑰厓绱犺繘琛岃祴鍊硷紱
int[] arr = {1,2,3};
for(int num : arr) {
num = 0; //涓嶈兘鏀瑰彉鏁扮粍鐨勫€?br>}
System.out.println(arr[1]); //2
--------------------------------------------------
鍙彉鍙傛暟锛?..锛夛細鐢ㄥ埌鍑芥暟鐨勫弬鏁颁笂锛屽綋瑕佹搷浣滅殑鍚屼竴涓被鍨嬪厓绱犱釜鏁颁笉纭畾鐨勬椂鍊欙紝鍙槸鐢ㄨ繖涓柟寮忥紝杩欎釜鍙傛暟鍙互鎺ュ彈浠绘剰涓暟鐨勫悓涓€绫诲瀷鐨勬暟鎹€?br>
鍜屼互鍓嶆帴鏀舵暟缁勪笉涓€鏍风殑鏄細
浠ュ墠瀹氫箟鏁扮粍绫诲瀷锛岄渶瑕佸厛鍒涘缓涓€涓暟缁勫璞★紝鍐嶅皢杩欎釜鏁扮粍瀵硅薄浣滀负鍙傛暟浼犻€掔粰鍑芥暟銆傜幇鍦紝鐩存帴灏嗘暟缁勪腑鐨勫厓绱犱綔涓哄弬鏁颁紶閫掑嵆鍙€傚簳灞傚叾瀹炴槸灏嗚繖浜涘厓绱犺繘琛屾暟缁勭殑灏佽锛岃€岃繖涓皝瑁呭姩浣滐紝鏄湪搴曞眰瀹屾垚鐨勶紝琚殣钘忎簡銆傛墍浠ョ畝鍖栦簡鐢ㄦ埛鐨勪功鍐欙紝灏戜簡璋冪敤鑰呭畾涔夋暟缁勭殑鍔ㄤ綔銆?br>濡傛灉鍦ㄥ弬鏁板垪琛ㄤ腑浣跨敤浜嗗彲鍙樺弬鏁帮紝鍙彉鍙傛暟蹇呴』瀹氫箟鍦ㄥ弬鏁板垪琛ㄧ粨灏?涔熷氨鏄繀椤绘槸鏈€鍚庝竴涓弬鏁帮紝鍚﹀垯缂栬瘧浼氬け璐ャ€?銆?br>濡傛灉瑕佽幏鍙栧涓猧nt鏁扮殑鍜屽憿锛熷彲浠ヤ娇鐢ㄥ皢澶氫釜int鏁板皝瑁呭埌鏁扮粍涓紝鐩存帴瀵规暟缁勬眰鍜屽嵆鍙€?br>---------------------------------------------------
闈欐€佸鍏ワ細瀵煎叆浜嗙被涓殑鎵€鏈夐潤鎬佹垚鍛橈紝绠€鍖栭潤鎬佹垚鍛樼殑涔﹀啓銆?br>import static java.util.Collections.*; //瀵煎叆浜咰ollections绫讳腑鐨勬墍鏈夐潤鎬佹垚鍛?br>---------------------------------------------------
鏋氫妇锛氬叧閿瓧 enum
闂锛氬璞$殑鏌愪釜灞炴€х殑鍊间笉鑳芥槸浠绘剰鐨勶紝蹇呴』涓哄浐瀹氱殑涓€缁勫彇鍊煎叾涓殑鏌愪竴涓紱瑙e喅鍔炴硶锛?br>1锛夈€佸湪setGrade鏂规硶涓仛鍒ゆ柇锛屼笉绗﹀悎鏍煎紡瑕佹眰灏辨姏鍑哄紓甯革紱
2锛夈€佺洿鎺ラ檺瀹氱敤鎴风殑閫夋嫨锛岄€氳繃鑷畾涔夌被妯℃嫙鏋氫妇鐨勬柟寮忔潵闄愬畾鐢ㄦ埛鐨勮緭鍏ワ紝鍐欎竴涓狦rade绫伙紝绉佹湁鏋勯€犲嚱鏁帮紝瀵瑰鎻愪緵5涓潤鎬佺殑甯搁噺琛ㄧず绫荤殑瀹炰緥锛?br>3)銆乯dk5涓柊瀹氫箟浜嗘灇涓剧被鍨嬶紝涓撻棬鐢ㄤ簬瑙e喅姝ょ被闂锛?br>4)銆佹灇涓惧氨鏄竴涓壒娈婄殑java绫伙紝鍙互瀹氫箟灞炴€с€佹柟娉曘€佹瀯閫犲嚱鏁般€佸疄鐜版帴鍙c€佺户鎵跨被锛?br>------------------------------------------------------------------------------
鑷姩鎷嗚绠憋細java涓暟鎹被鍨嬪垎涓轰袱绉?锛?鍩烘湰鏁版嵁绫诲瀷 寮曠敤鏁版嵁绫诲瀷(瀵硅薄)
鍦?java绋嬪簭涓墍鏈夌殑鏁版嵁閮介渶瑕佸綋鍋氬璞℃潵澶勭悊锛岄拡瀵?绉嶅熀鏈暟鎹被鍨嬫彁渚涗簡鍖呰绫伙紝濡備笅锛?br>int --> Integer
byte --> Byte
short --> Short
long --> Long
char --> Character
double --> Double
float --> Float
boolean --> Boolean
jdk5浠ュ墠鍩烘湰鏁版嵁绫诲瀷鍜屽寘瑁呯被涔嬮棿闇€瑕佷簰杞細
鍩烘湰---寮曠敤 Integer x = new Integer(x);
寮曠敤---鍩烘湰 int num = x.intValue();
1)銆両nteger x = 1; x = x + 1; 缁忓巻浜嗕粈涔堣繃绋嬶紵瑁呯 脿 鎷嗙 脿 瑁呯锛?br>2)銆佷负浜嗕紭鍖栵紝铏氭嫙鏈轰负鍖呰绫绘彁渚涗簡缂撳啿姹狅紝Integer姹犵殑澶у皬 -128~127 涓€涓瓧鑺傜殑澶у皬锛?br>3)銆丼tring姹狅細Java涓轰簡浼樺寲瀛楃涓叉搷浣?鎻愪緵浜嗕竴涓紦鍐叉睜锛?br>----------------------------------------------------------
娉涘瀷锛歫dk1.5鐗堟湰浠ュ悗鍑虹幇鐨勪竴涓畨鍏ㄦ満鍒躲€傝〃鐜版牸寮忥細< >
濂藉锛?br>1锛氬皢杩愯鏃舵湡鐨勯棶棰楥lassCastException闂杞崲鎴愪簡缂栬瘧澶辫触锛屼綋鐜板湪缂栬瘧鏃舵湡锛岀▼搴忓憳灏卞彲浠ヨВ鍐抽棶棰樸€?br>2锛氶伩鍏嶄簡寮哄埗杞崲鐨勯夯鐑︺€?br>
鍙甯︽湁<>鐨勭被鎴栬€呮帴鍙o紝閮藉睘浜庡甫鏈夌被鍨嬪弬鏁扮殑绫绘垨鑰呮帴鍙o紝鍦ㄤ娇鐢ㄨ繖浜涚被鎴栬€呮帴鍙f椂锛屽繀椤荤粰<>涓紶閫掍竴涓叿浣撶殑寮曠敤鏁版嵁绫诲瀷銆?br>
娉涘瀷鎶€鏈細鍏跺疄搴旂敤鍦ㄧ紪璇戞椂鏈燂紝鏄粰缂栬瘧鍣ㄤ娇鐢ㄧ殑鎶€鏈紝鍒颁簡杩愯鏃舵湡锛屾硾鍨嬪氨涓嶅瓨鍦ㄤ簡銆?br>涓轰粈涔? 鍥犱负娉涘瀷鐨勬摝闄わ細涔熷氨鏄锛岀紪杈戝櫒妫€鏌ヤ簡娉涘瀷鐨勭被鍨嬫纭悗锛屽湪鐢熸垚鐨勭被鏂囦欢涓槸娌℃湁娉涘瀷鐨勩€?br>鍦ㄨ繍琛屾椂锛屽浣曠煡閬撹幏鍙栫殑鍏冪礌绫诲瀷鑰屼笉鐢ㄥ己杞憿锛?br>娉涘瀷鐨勮ˉ鍋匡細鍥犱负瀛樺偍鐨勬椂鍊欙紝绫诲瀷宸茬粡纭畾浜嗘槸鍚屼竴涓被鍨嬬殑鍏冪礌锛屾墍浠ュ湪杩愯鏃讹紝鍙鑾峰彇鍒拌鍏冪礌鐨勭被鍨嬶紝鍦ㄥ唴閮ㄨ繘琛屼竴娆¤浆鎹㈠嵆鍙紝鎵€浠ヤ娇鐢ㄨ€呬笉鐢ㄥ啀鍋氳浆鎹㈠姩浣滀簡銆?br>
浠€涔堟椂鍊欑敤娉涘瀷绫诲憿锛?br>褰撶被涓殑鎿嶄綔鐨勫紩鐢ㄦ暟鎹被鍨嬩笉纭畾鐨勬椂鍊欙紝浠ュ墠鐢ㄧ殑Object鏉ヨ繘琛屾墿灞曠殑锛岀幇鍦ㄥ彲浠ョ敤娉涘瀷鏉ヨ〃绀恒€傝繖鏍峰彲浠ラ伩鍏嶅己杞殑楹荤儲锛岃€屼笖灏嗚繍琛岄棶棰樿浆绉诲埌鐨勭紪璇戞椂鏈熴€?br>----------------------------------------------------------
娉涘瀷鍦ㄧ▼搴忓畾涔変笂鐨勪綋鐜帮細
//娉涘瀷绫伙細灏嗘硾鍨嬪畾涔夊湪绫讳笂銆?br>class Tool<Q> {
private Q obj;
public void setObject(Q obj) {
this.obj = obj;
}
public Q getObject() {
return obj;
}
}
//褰撴柟娉曟搷浣滅殑寮曠敤鏁版嵁绫诲瀷涓嶇‘瀹氱殑鏃跺€欙紝鍙互灏嗘硾鍨嬪畾涔夊湪鏂规硶涓娿€?br> public <W> void method(W w) {
System.out.println("method:"+w);
}
//闈欐€佹柟娉曚笂鐨勬硾鍨嬶細闈欐€佹柟娉曟棤娉曡闂被涓婂畾涔夌殑娉涘瀷銆傚鏋滈潤鎬佹柟娉曟搷浣滅殑寮曠敤鏁版嵁绫诲瀷涓嶇‘瀹氱殑鏃跺€欙紝蹇呴』瑕佸皢娉涘瀷瀹氫箟鍦ㄦ柟娉曚笂銆?br> public static <Q> void function(Q t) {
System.out.println("function:"+t);
}
//娉涘瀷鎺ュ彛.
interface Inter<T> {
void show(T t);
}
class InterImpl<R> implements Inter<R> {
public void show(R r) {
System.out.println("show:"+r);
}
}
------------------------------------------------------------
娉涘瀷涓殑閫氶厤绗︼細鍙互瑙e喅褰撳叿浣撶被鍨嬩笉纭畾鐨勬椂鍊欙紝杩欎釜閫氶厤绗﹀氨鏄?? 锛涘綋鎿嶄綔绫诲瀷鏃讹紝涓嶉渶瑕佷娇鐢ㄧ被鍨嬬殑鍏蜂綋鍔熻兘鏃讹紝鍙娇鐢∣bject绫讳腑鐨勫姛鑳姐€傞偅涔堝彲浠ョ敤 ? 閫氶厤绗︽潵琛ㄦ湭鐭ョ被鍨嬨€?br>
娉涘瀷闄愬畾锛?br> 涓婇檺锛氾紵extends E锛氬彲浠ユ帴鏀禘绫诲瀷鎴栬€匛鐨勫瓙绫诲瀷瀵硅薄銆?br> 涓嬮檺锛氾紵super E锛氬彲浠ユ帴鏀禘绫诲瀷鎴栬€匛鐨勭埗绫诲瀷瀵硅薄銆?br>
涓婇檺浠€涔堟椂鍊欑敤锛氬線闆嗗悎涓坊鍔犲厓绱犳椂锛屾棦鍙互娣诲姞E绫诲瀷瀵硅薄锛屽張鍙互娣诲姞E鐨勫瓙绫诲瀷瀵硅薄銆備负浠€涔堬紵鍥犱负鍙栫殑鏃跺€欙紝E绫诲瀷鏃㈠彲浠ユ帴鏀禘绫诲璞★紝鍙堝彲浠ユ帴鏀禘鐨勫瓙绫诲瀷瀵硅薄銆?br>
涓嬮檺浠€涔堟椂鍊欑敤锛氬綋浠庨泦鍚堜腑鑾峰彇鍏冪礌杩涜鎿嶄綔鐨勬椂鍊欙紝鍙互鐢ㄥ綋鍓嶅厓绱犵殑绫诲瀷鎺ユ敹锛屼篃鍙互鐢ㄥ綋鍓嶅厓绱犵殑鐖剁被鍨嬫帴鏀躲€?br>
娉涘瀷鐨勭粏鑺傦細
1锛夈€佹硾鍨嬪埌搴曚唬琛ㄤ粈涔堢被鍨嬪彇鍐充簬璋冪敤鑰呬紶鍏ョ殑绫诲瀷锛屽鏋滄病浼狅紝榛樿鏄疧bject绫诲瀷锛?br>2锛夈€佷娇鐢ㄥ甫娉涘瀷鐨勭被鍒涘缓瀵硅薄鏃讹紝绛夊紡涓よ竟鎸囧畾鐨勬硾鍨嬪繀椤讳竴鑷达紱
鍘熷洜锛氱紪璇戝櫒妫€鏌ュ璞¤皟鐢ㄦ柟娉曟椂鍙湅鍙橀噺锛岀劧鑰岀▼搴忚繍琛屾湡闂磋皟鐢ㄦ柟娉曟椂灏辫鑰冭檻瀵硅薄鍏蜂綋绫诲瀷浜嗭紱
3锛夈€佺瓑寮忎袱杈瑰彲浠ュ湪浠绘剰涓€杈逛娇鐢ㄦ硾鍨嬶紝鍦ㄥ彟涓€杈逛笉浣跨敤(鑰冭檻鍚戝悗鍏煎)锛?br>ArrayList<String> al = new ArrayList<Object>(); //閿?br>//瑕佷繚璇佸乏鍙充袱杈圭殑娉涘瀷鍏蜂綋绫诲瀷涓€鑷村氨鍙互浜嗭紝杩欐牱涓嶅鏄撳嚭閿欍€?br>ArrayList<? extends Object> al = new ArrayList<String>();
al.add("aa"); //閿?br>//鍥犱负闆嗗悎鍏蜂綋瀵硅薄涓棦鍙瓨鍌⊿tring锛屼篃鍙互瀛樺偍Object鐨勫叾浠栧瓙绫伙紝鎵€浠ユ坊鍔犲叿浣撶殑绫诲瀷瀵硅薄涓嶅悎閫傦紝绫诲瀷妫€鏌ヤ細鍑虹幇瀹夊叏闂銆?锛焑xtends Object 浠h〃Object鐨勫瓙绫诲瀷涓嶇‘瀹氾紝鎬庝箞鑳芥坊鍔犲叿浣撶被鍨嬬殑瀵硅薄鍛紵
public static void method(ArrayList<? extends Object> al) {
al.add("abc"); //閿?br> //鍙兘瀵筧l闆嗗悎涓殑鍏冪礌璋冪敤Object绫讳腑鐨勬柟娉曪紝鍏蜂綋瀛愮被鍨嬬殑鏂规硶閮戒笉鑳界敤锛屽洜涓哄瓙绫诲瀷涓嶇‘瀹氥€?br>}
------------------------------------------------------------------------------------------------------------------------------------------------
IO娴侊細鈽呪槄鈽呪槄鈽咃紝
鐢ㄤ簬澶勭悊璁惧涓婃暟鎹€?br>娴侊細鍙互鐞嗚В鏁版嵁鐨勬祦鍔紝灏辨槸涓€涓暟鎹祦銆侷O娴佹渶缁堣浠ュ璞℃潵浣撶幇锛屽璞¢兘瀛樺湪IO鍖呬腑銆?br>娴佷篃杩涜鍒嗙被锛?br>1锛氳緭鍏ユ祦锛堣锛夊拰杈撳嚭娴侊紙鍐欙級銆?br>2锛氬洜涓哄鐞嗙殑鏁版嵁涓嶅悓锛屽垎涓哄瓧鑺傛祦鍜屽瓧绗︽祦銆?br>瀛楄妭娴侊細
澶勭悊瀛楄妭鏁版嵁鐨勬祦瀵硅薄銆傝澶囦笂鐨勬暟鎹棤璁烘槸鍥剧墖鎴栬€卍vd锛屾枃瀛楋紝瀹冧滑閮戒互浜岃繘鍒跺瓨鍌ㄧ殑銆備簩杩涘埗鐨勬渶缁堥兘鏄互涓€涓?浣嶄负鏁版嵁鍗曞厓杩涜浣撶幇锛屾墍浠ヨ绠楁満涓殑鏈€灏忔暟鎹崟鍏冨氨鏄瓧鑺傘€傛剰鍛崇潃锛屽瓧鑺傛祦鍙互澶勭悊璁惧涓婄殑鎵€鏈夋暟鎹紝鎵€浠ュ瓧鑺傛祦涓€鏍峰彲浠ュ鐞嗗瓧绗︽暟鎹€?br>
閭d箞涓轰粈涔堣鏈夊瓧绗︽祦鍛紵鍥犱负瀛楃姣忎釜鍥藉閮戒笉涓€鏍凤紝鎵€浠ユ秹鍙婂埌浜嗗瓧绗︾紪鐮侀棶棰橈紝閭d箞GBK缂栫爜鐨勪腑鏂囩敤unicode缂栫爜瑙f瀽鏄湁闂鐨勶紝鎵€浠ラ渶瑕佽幏鍙栦腑鏂囧瓧鑺傛暟鎹殑鍚屾椂+ 鎸囧畾鐨勭紪鐮佽〃鎵嶅彲浠ヨВ鏋愭纭暟鎹€備负浜嗘柟渚夸簬鏂囧瓧鐨勮В鏋愶紝鎵€浠ュ皢瀛楄妭娴佸拰缂栫爜琛ㄥ皝瑁呮垚瀵硅薄锛岃繖涓璞″氨鏄瓧绗︽祦銆傚彧瑕佹搷浣滃瓧绗︽暟鎹紝浼樺厛鑰冭檻浣跨敤瀛楃娴佷綋绯汇€?br>
娉ㄦ剰锛氭祦鐨勬搷浣滃彧鏈変袱绉嶏細璇诲拰鍐欍€?br>
娴佺殑浣撶郴鍥犱负鍔熻兘涓嶅悓锛屼絾鏄湁鍏辨€у唴瀹癸紝涓嶆柇鎶藉彇锛屽舰鎴愮户鎵夸綋绯汇€傝浣撶郴涓€鍏辨湁鍥涗釜鍩虹被锛岃€屼笖閮芥槸鎶借薄绫汇€?br>
瀛楄妭娴侊細InputStream OutputStream
瀛楃娴侊細Reader Writer
鍦ㄨ繖鍥涗釜绯荤粺涓紝瀹冧滑鐨勫瓙绫伙紝閮芥湁涓€涓叡鎬х壒鐐癸細瀛愮被鍚嶅悗缂€閮芥槸鐖剁被鍚嶏紝鍓嶇紑鍚嶉兘鏄繖涓瓙绫荤殑鍔熻兘鍚嶇О銆?br>--------------------------------------------------------------------------------------------------------------------
public static void main(String[] args) throws IOException { //璇汇€佸啓閮戒細鍙戠敓IO寮傚父
/*
1锛氬垱寤轰竴涓瓧绗﹁緭鍑烘祦瀵硅薄锛岀敤浜庢搷浣滄枃浠躲€傝瀵硅薄涓€寤虹珛锛屽氨蹇呴』鏄庣‘鏁版嵁瀛樺偍浣嶇疆锛屾槸涓€涓枃浠躲€?br> 2锛氬璞′骇鐢熷悗锛屼細鍦ㄥ爢鍐呭瓨涓湁涓€涓疄浣擄紝鍚屾椂涔熻皟鐢ㄤ簡绯荤粺搴曞眰璧勬簮锛屽湪鎸囧畾鐨勪綅缃垱寤轰簡涓€涓瓨鍌ㄦ暟鎹殑鏂囦欢銆?br> 3锛氬鏋滄寚瀹氫綅缃紝鍑虹幇浜嗗悓鍚嶆枃浠讹紝鏂囦欢浼氳瑕嗙洊銆?br> */
FileWriter fw = new FileWriter("demo.txt"); // FileNotFoundException
/*
璋冪敤Writer绫讳腑鐨剋rite鏂规硶鍐欏叆瀛楃涓层€傚瓧绗︿覆骞舵湭鐩存帴鍐欏叆鍒扮洰鐨勫湴涓紝鑰屾槸鍐欏叆鍒颁簡娴佷腑锛?鍏跺疄鏄啓鍏ュ埌鍐呭瓨缂撳啿鍖轰腑)銆傛€庝箞鎶婃暟鎹紕鍒版枃浠朵腑锛?br> */
fw.write("abcde");
fw.flush(); // 鍒锋柊缂撳啿鍖猴紝灏嗙紦鍐插尯涓殑鏁版嵁鍒峰埌鐩殑鍦版枃浠朵腑銆?br> fw.close(); // 鍏抽棴娴侊紝鍏跺疄鍏抽棴鐨勫氨鏄痡ava璋冪敤鐨勭郴缁熷簳灞傝祫婧愩€傚湪鍏抽棴鍓嶏紝浼氬厛鍒锋柊璇ユ祦銆?br>}
close()鍜宖lush()鐨勫尯鍒細
flush()锛氬皢缂撳啿鍖虹殑鏁版嵁鍒峰埌鐩殑鍦颁腑鍚庯紝娴佸彲浠ヤ娇鐢ㄣ€?br>close()锛氬皢缂撳啿鍖虹殑鏁版嵁鍒峰埌鐩殑鍦颁腑鍚庯紝娴佸氨鍏抽棴浜嗭紝璇ユ柟娉曚富瑕佺敤浜庣粨鏉熻皟鐢ㄧ殑搴曞眰璧勬簮銆傝繖涓姩浣滀竴瀹氬仛銆?br>--------------------------------------------------------------------------------------------------------------------
io寮傚父鐨勫鐞嗘柟寮忥細
io涓€瀹氳鍐檉inally锛?br>
FileWriter鍐欏叆鏁版嵁鐨勭粏鑺傦細
1锛歸indow涓殑鎹㈣绗︼細 涓や釜绗﹀彿缁勬垚銆?linux锛歕n銆?br>2锛氱画鍐欐暟鎹紝鍙鍦ㄦ瀯閫犲嚱鏁颁腑浼犲叆鏂扮殑鍙傛暟true銆?br>3锛氱洰褰曞垎鍓茬锛歸indow \ /
public static void main(String[] args) {
FileWriter fw = null;
try {
fw = new FileWriter("demo.txt",true);
fw.write("abcde");
}
catch (IOException e ){
System.out.println(e.toString()+"....");
}
finally{
if(fw!=null)
try{
fw.close();
}
catch (IOException e){
System.out.println("close:"+e.toString());
}
}
}
--------------------------------------------------------------------------------------------------------------------
FileReader锛氫娇鐢≧eader浣撶郴锛岃鍙栦竴涓枃鏈枃浠朵腑鐨勬暟鎹€傝繑鍥?-1 锛屾爣蹇楄鍒扮粨灏俱€?br>import java.io.*;
class FileReaderDemo {
public static void main(String[] args) throws IOException {
/*
鍒涘缓鍙互璇诲彇鏂囨湰鏂囦欢鐨勬祦瀵硅薄锛孎ileReader璁╁垱寤哄ソ鐨勬祦瀵硅薄鍜屾寚瀹氱殑鏂囦欢鐩稿叧鑱斻€?br> */
FileReader fr = new FileReader("demo.txt");
int ch = 0;
while((ch = fr.read())!= -1) { //鏉′欢鏄病鏈夎鍒扮粨灏?br> System.out.println((char)ch); //璋冪敤璇诲彇娴佺殑read鏂规硶锛岃鍙栦竴涓瓧绗︺€?br> }
fr.close();
}
}
--------------------------------------------------------------------------------------------------------------------
璇诲彇鏁版嵁鐨勭浜岀鏂瑰紡锛氱浜岀鏂瑰紡杈冧负楂樻晥锛岃嚜瀹氫箟缂撳啿鍖恒€?br>import java.io.*;
class FileReaderDemo2 {
public static void main(String[] args) throws IOException {
FileReader fr = new FileReader("demo.txt"); //鍒涘缓璇诲彇娴佸璞″拰鎸囧畾鏂囦欢鍏宠仈銆?br> //鍥犱负瑕佷娇鐢╮ead(char[])鏂规硶锛屽皢璇诲彇鍒板瓧绗﹀瓨鍏ユ暟缁勩€傛墍浠ヨ鍒涘缓涓€涓瓧绗︽暟缁勶紝涓€鑸暟缁勭殑闀垮害閮芥槸1024鐨勬暣鏁板€嶃€?br> char[] buf = new char[1024];
int len = 0;
while(( len=fr.read(buf)) != -1) {
System.out.println(new String(buf,0,len));
}
fr.close();
}
}
--------------------------------------------------------------------------------------------------------------------
IO涓殑浣跨敤鍒颁簡涓€涓璁℃ā寮忥細瑁呴グ璁捐妯″紡銆?br>瑁呴グ璁捐妯″紡瑙e喅锛氬涓€缁勭被杩涜鍔熻兘鐨勫寮恒€?br>鍖呰锛氬啓涓€涓被(鍖呰绫?瀵硅鍖呰瀵硅薄杩涜鍖呰锛?br> * 1銆佸寘瑁呯被鍜岃鍖呰瀵硅薄瑕佸疄鐜板悓鏍风殑鎺ュ彛锛?br> * 2銆佸寘瑁呯被瑕佹寔鏈変竴涓鍖呰瀵硅薄锛?br> * 3銆佸寘瑁呯被鍦ㄥ疄鐜版帴鍙f椂锛屽ぇ閮ㄥ垎鏂规硶鏄潬璋冪敤琚寘瑁呭璞℃潵瀹炵幇鐨勶紝瀵逛簬闇€瑕佷慨鏀圭殑鏂规硶鎴戜滑鑷繁瀹炵幇锛?br>--------------------------------------------------------------------------------------------------------------------
瀛楃娴侊細
Reader锛氱敤浜庤鍙栧瓧绗︽祦鐨勬娊璞$被銆傚瓙绫诲繀椤诲疄鐜扮殑鏂规硶鍙湁 read(char[], int, int) 鍜?close()銆?br> |---BufferedReader锛氫粠瀛楃杈撳叆娴佷腑璇诲彇鏂囨湰锛岀紦鍐插悇涓瓧绗︼紝浠庤€屽疄鐜板瓧绗︺€佹暟缁勫拰琛岀殑楂樻晥璇诲彇銆?鍙互鎸囧畾缂撳啿鍖虹殑澶у皬锛屾垨鑰呭彲浣跨敤榛樿鐨勫ぇ灏忋€傚ぇ澶氭暟鎯呭喌涓嬶紝榛樿鍊煎氨瓒冲澶т簡銆?br> |---LineNumberReader锛氳窡韪鍙风殑缂撳啿瀛楃杈撳叆娴併€傛绫诲畾涔変簡鏂规硶 setLineNumber(int) 鍜?getLineNumber()锛屽畠浠彲鍒嗗埆鐢ㄤ簬璁剧疆鍜岃幏鍙栧綋鍓嶈鍙枫€?br> |---InputStreamReader锛氭槸瀛楄妭娴侀€氬悜瀛楃娴佺殑妗ユ锛氬畠浣跨敤鎸囧畾鐨?charset 璇诲彇瀛楄妭骞跺皢鍏惰В鐮佷负瀛楃銆傚畠浣跨敤鐨勫瓧绗﹂泦鍙互鐢卞悕绉版寚瀹氭垨鏄惧紡缁欏畾锛屾垨鑰呭彲浠ユ帴鍙楀钩鍙伴粯璁ょ殑瀛楃闆嗐€?br> |---FileReader锛氱敤鏉ヨ鍙栧瓧绗︽枃浠剁殑渚挎嵎绫汇€傛绫荤殑鏋勯€犳柟娉曞亣瀹氶粯璁ゅ瓧绗︾紪鐮佸拰榛樿瀛楄妭缂撳啿鍖哄ぇ灏忛兘鏄€傚綋鐨勩€傝鑷繁鎸囧畾杩欎簺鍊硷紝鍙互鍏堝湪 FileInputStream 涓婃瀯閫犱竴涓?InputStreamReader銆?br> |---CharArrayReader锛?br> |---StringReader锛?br>-------------------------------------------------
Writer锛氬啓鍏ュ瓧绗︽祦鐨勬娊璞$被銆傚瓙绫诲繀椤诲疄鐜扮殑鏂规硶浠呮湁 write(char[], int, int)銆乫lush() 鍜?close()銆?br> |---BufferedWriter锛氬皢鏂囨湰鍐欏叆瀛楃杈撳嚭娴侊紝缂撳啿鍚勪釜瀛楃锛屼粠鑰屾彁渚涘崟涓瓧绗︺€佹暟缁勫拰瀛楃涓茬殑楂樻晥鍐欏叆銆?br> |---OutputStreamWriter锛氭槸瀛楃娴侀€氬悜瀛楄妭娴佺殑妗ユ锛氬彲浣跨敤鎸囧畾鐨?charset 灏嗚鍐欏叆娴佷腑鐨勫瓧绗︾紪鐮佹垚瀛楄妭銆傚畠浣跨敤鐨勫瓧绗﹂泦鍙互鐢卞悕绉版寚瀹氭垨鏄惧紡缁欏畾锛屽惁鍒欏皢鎺ュ彈骞冲彴榛樿鐨勫瓧绗﹂泦銆?br> |---FileWriter锛氱敤鏉ュ啓鍏ュ瓧绗︽枃浠剁殑渚挎嵎绫汇€傛绫荤殑鏋勯€犳柟娉曞亣瀹氶粯璁ゅ瓧绗︾紪鐮佸拰榛樿瀛楄妭缂撳啿鍖哄ぇ灏忛兘鏄彲鎺ュ彈鐨勩€傝鑷繁鎸囧畾杩欎簺鍊硷紝鍙互鍏堝湪 FileOutputStream 涓婃瀯閫犱竴涓?OutputStreamWriter銆?br> |---PrintWriter锛?br> |---CharArrayWriter锛?br> |---StringWriter锛?br>---------------------------------
瀛楄妭娴侊細
InputStream锛氭槸琛ㄧず瀛楄妭杈撳叆娴佺殑鎵€鏈夌被鐨勮秴绫汇€?br> |--- FileInputStream锛氫粠鏂囦欢绯荤粺涓殑鏌愪釜鏂囦欢涓幏寰楄緭鍏ュ瓧鑺傘€傚摢浜涙枃浠跺彲鐢ㄥ彇鍐充簬涓绘満鐜銆侳ileInputStream 鐢ㄤ簬璇诲彇璇稿鍥惧儚鏁版嵁涔嬬被鐨勫師濮嬪瓧鑺傛祦銆傝璇诲彇瀛楃娴侊紝璇疯€冭檻浣跨敤 FileReader銆?br> |--- FilterInputStream锛氬寘鍚叾浠栦竴浜涜緭鍏ユ祦锛屽畠灏嗚繖浜涙祦鐢ㄤ綔鍏跺熀鏈暟鎹簮锛屽畠鍙互鐩存帴浼犺緭鏁版嵁鎴栨彁渚涗竴浜涢澶栫殑鍔熻兘銆?br> |--- BufferedInputStream锛氳绫诲疄鐜扮紦鍐茬殑杈撳叆娴併€?br> |--- Stream锛?br> |--- ObjectInputStream锛?br> |--- PipedInputStream锛?br>-----------------------------------------------
OutputStream锛氭鎶借薄绫绘槸琛ㄧず杈撳嚭瀛楄妭娴佺殑鎵€鏈夌被鐨勮秴绫汇€?br> |--- FileOutputStream锛氭枃浠惰緭鍑烘祦鏄敤浜庡皢鏁版嵁鍐欏叆 File 鎴?FileDescriptor 鐨勮緭鍑烘祦銆?br> |--- FilterOutputStream锛氭绫绘槸杩囨护杈撳嚭娴佺殑鎵€鏈夌被鐨勮秴绫汇€?br> |--- BufferedOutputStream锛氳绫诲疄鐜扮紦鍐茬殑杈撳嚭娴併€?br> |--- PrintStream锛?br> |--- DataOutputStream锛?br> |--- ObjectOutputStream锛?br> |--- PipedOutputStream锛?br>--------------------------------
缂撳啿鍖烘槸鎻愰珮鏁堢巼鐢ㄧ殑锛岀粰璋佹彁楂樺憿锛?br>BufferedWriter锛氭槸缁欏瓧绗﹁緭鍑烘祦鎻愰珮鏁堢巼鐢ㄧ殑锛岄偅灏辨剰鍛崇潃锛岀紦鍐插尯瀵硅薄寤虹珛鏃讹紝蹇呴』瑕佸厛鏈夋祦瀵硅薄銆傛槑纭鎻愰珮鍏蜂綋鐨勬祦瀵硅薄鐨勬晥鐜囥€?br> FileWriter fw = new FileWriter("bufdemo.txt");
BufferedWriter bufw = new BufferedWriter(fw);//璁╃紦鍐插尯鍜屾寚瀹氭祦鐩稿叧鑱斻€?br> for(int x=0; x<4; x++){
bufw.write(x+"abc");
bufw.newLine(); //鍐欏叆涓€涓崲琛岀锛岃繖涓崲琛岀鍙互渚濇嵁骞冲彴鐨勪笉鍚屽啓鍏ヤ笉鍚岀殑鎹㈣绗︺€?br> bufw.flush();//瀵圭紦鍐插尯杩涜鍒锋柊锛屽彲浠ヨ鏁版嵁鍒扮洰鐨勫湴涓€?br> }
bufw.close();//鍏抽棴缂撳啿鍖猴紝鍏跺疄灏辨槸鍦ㄥ叧闂叿浣撶殑娴併€?br>-----------------------------
BufferedReader锛?br> FileReader fr = new FileReader("bufdemo.txt");
BufferedReader bufr = new BufferedReader(fr);
String line = null;
while((line=bufr.readLine())!=null){ //readLine鏂规硶杩斿洖鐨勬椂鍊欐槸涓嶅甫鎹㈣绗︾殑銆?br> System.out.println(line);
}
bufr.close();
-----------------------------
//璁颁綇锛屽彧瑕佷竴璇诲彇閿洏褰曞叆锛屽氨鐢ㄨ繖鍙ヨ瘽銆?br>BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bufw = new BufferedWriter(new OutputStreamWriter(System.out));//杈撳嚭鍒版帶鍒跺彴
String line = null;
while((line=bufr.readLine())!=null){
if("over".equals(line))
break;
bufw.write(line.toUpperCase());//灏嗚緭鍏ョ殑瀛楃杞垚澶у啓瀛楃杈撳嚭
bufw.newLine();
bufw.flush();
}
bufw.close();
bufr.close();
------------------------------
娴佸璞★細
鍏跺疄寰堢畝鍗曪紝灏辨槸璇诲彇鍜屽啓鍏ャ€備絾鏄洜涓哄姛鑳界殑涓嶅悓锛屾祦鐨勪綋绯讳腑鎻愪緵N澶氱殑瀵硅薄銆傞偅涔堝紑濮嬫椂锛屽埌搴曡鐢ㄥ摢涓璞℃洿涓哄悎閫傚憿锛熻繖灏遍渶瑕佹槑纭祦鐨勬搷浣滆寰嬨€?br>娴佺殑鎿嶄綔瑙勫緥锛?br>1锛屾槑纭簮鍜岀洰鐨勩€?br> 鏁版嵁婧愶細灏辨槸闇€瑕佽鍙栵紝鍙互浣跨敤涓や釜浣撶郴锛欼nputStream銆丷eader锛?br> 鏁版嵁姹囷細灏辨槸闇€瑕佸啓鍏ワ紝鍙互浣跨敤涓や釜浣撶郴锛歄utputStream銆乄riter锛?br>2锛屾搷浣滅殑鏁版嵁鏄惁鏄函鏂囨湰鏁版嵁锛?br> 濡傛灉鏄細鏁版嵁婧愶細Reader
鏁版嵁姹囷細Writer
濡傛灉涓嶆槸锛氭暟鎹簮锛欼nputStream
鏁版嵁姹囷細OutputStream
3锛岃櫧鐒剁‘瀹氫簡涓€涓綋绯伙紝浣嗘槸璇ヤ綋绯讳腑鏈夊お澶氱殑瀵硅薄锛屽埌搴曠敤鍝釜鍛紵
鏄庣‘鎿嶄綔鐨勬暟鎹澶囥€?br> 鏁版嵁婧愬搴旂殑璁惧锛氱‖鐩?File)锛屽唴瀛?鏁扮粍)锛岄敭鐩?System.in)
鏁版嵁姹囧搴旂殑璁惧锛氱‖鐩?File)锛屽唴瀛?鏁扮粍)锛屾帶鍒跺彴(System.out)銆?br>4锛岄渶瑕佸湪鍩烘湰鎿嶄綔涓婇檮鍔犲叾浠栧姛鑳藉悧锛熸瘮濡傜紦鍐层€?br> 濡傛灉闇€瑕佸氨杩涜瑁呴グ銆?br>
杞崲娴佺壒鏈夊姛鑳斤細杞崲娴佸彲浠ュ皢瀛楄妭杞垚瀛楃锛屽師鍥犲湪浜庯紝灏嗚幏鍙栧埌鐨勫瓧鑺傞€氳繃鏌ョ紪鐮佽〃鑾峰彇鍒版寚瀹氬搴斿瓧绗︺€?br>
杞崲娴佺殑鏈€寮哄姛鑳藉氨鏄熀浜?瀛楄妭娴?+ 缂栫爜琛?銆傛病鏈夎浆鎹紝娌℃湁瀛楃娴併€?br>
鍙戠幇杞崲娴佹湁涓€涓瓙绫诲氨鏄搷浣滄枃浠剁殑瀛楃娴佸璞★細
InputStreamReader
|--FileReader
OutputStreamWriter
|--FileWrier
鎯宠鎿嶄綔鏂囨湰鏂囦欢锛屽繀椤昏杩涜缂栫爜杞崲锛岃€岀紪鐮佽浆鎹㈠姩浣滆浆鎹㈡祦閮藉畬鎴愪簡銆傛墍浠ユ搷浣滄枃浠剁殑娴佸璞″彧瑕佺户鎵胯嚜杞崲娴佸氨鍙互璇诲彇涓€涓瓧绗︿簡銆?br>
浣嗘槸瀛愮被鏈変竴涓眬闄愭€э紝灏辨槸瀛愮被涓娇鐢ㄧ殑缂栫爜鏄浐瀹氱殑锛屾槸鏈満榛樿鐨勭紪鐮佽〃锛屽浜庣畝浣撲腑鏂囩増鐨勭郴缁熼粯璁ょ爜琛ㄦ槸GBK銆?br>FileReader fr = new FileReader("a.txt");
InputStreamReader isr = new InputStreamReader(new FileInputStream("a.txt"),"gbk");
浠ヤ笂涓ゅ彞浠g爜鍔熻兘涓€鑷达紝
濡傛灉浠呬粎浣跨敤骞冲彴榛樿鐮佽〃锛屽氨浣跨敤FileReader fr = new FileReader("a.txt"); //鍥犱负绠€鍖栥€?br>
濡傛灉闇€瑕佸埗瀹氱爜琛紝蹇呴』鐢ㄨ浆鎹㈡祦銆?br>杞崲娴?= 瀛楄妭娴?缂栫爜琛ㄣ€?br>杞崲娴佺殑瀛愮被File = 瀛楄妭娴?+ 榛樿缂栫爜琛ㄣ€?br>
鍑℃槸鎿嶄綔璁惧涓婄殑鏂囨湰鏁版嵁锛屾秹鍙婄紪鐮佽浆鎹紝蹇呴』浣跨敤杞崲娴併€?br>-----------------------------------------------------------------------------------------------
File绫伙細
灏嗘枃浠剁郴缁熶腑鐨勬枃浠跺拰鏂囦欢澶瑰皝瑁呮垚浜嗗璞°€傛彁渚涗簡鏇村鐨勫睘鎬у拰琛屼负鍙互瀵硅繖浜涙枃浠跺拰鏂囦欢澶硅繘琛屾搷浣溿€傝繖浜涙槸娴佸璞″姙涓嶅埌鐨勶紝鍥犱负娴佸彧鎿嶄綔鏁版嵁銆?br>File绫诲父瑙佹柟娉曪細
1锛氬垱寤恒€?br> boolean createNewFile()锛氬湪鎸囧畾鐩綍涓嬪垱寤烘枃浠讹紝濡傛灉璇ユ枃浠跺凡瀛樺湪锛屽垯涓嶅垱寤恒€傝€屽鎿嶄綔鏂囦欢鐨勮緭鍑烘祦鑰岃█锛岃緭鍑烘祦瀵硅薄宸插缓绔嬶紝灏变細鍒涘缓鏂囦欢锛屽鏋滄枃浠跺凡瀛樺湪锛屼細瑕嗙洊銆傞櫎闈炵画鍐欍€?br> boolean mkdir()锛氬垱寤烘鎶借薄璺緞鍚嶆寚瀹氱殑鐩綍銆?br> boolean mkdirs()锛氬垱寤哄绾х洰褰曘€?br>2锛氬垹闄ゃ€?br> boolean delete()锛氬垹闄ゆ鎶借薄璺緞鍚嶈〃绀虹殑鏂囦欢鎴栫洰褰曘€?br> void deleteOnExit()锛氬湪铏氭嫙鏈洪€€鍑烘椂鍒犻櫎銆?br>娉ㄦ剰锛氬湪鍒犻櫎鏂囦欢澶规椂锛屽繀椤讳繚璇佽繖涓枃浠跺す涓病鏈変换浣曞唴瀹癸紝鎵嶅彲浠ュ皢璇ユ枃浠跺す鐢╠elete鍒犻櫎銆?br> window鐨勫垹闄ゅ姩浣滐紝鏄粠閲屽線澶栧垹銆傛敞鎰忥細java鍒犻櫎鏂囦欢涓嶈蛋鍥炴敹绔欍€傝鎱庣敤銆?br>3锛氳幏鍙?
long length()锛氳幏鍙栨枃浠跺ぇ灏忋€?br> String getName()锛氳繑鍥炵敱姝ゆ娊璞¤矾寰勫悕琛ㄧず鐨勬枃浠舵垨鐩綍鐨勫悕绉般€?br> String getPath()锛氬皢姝ゆ娊璞¤矾寰勫悕杞崲涓轰竴涓矾寰勫悕瀛楃涓层€?br> String getAbsolutePath()锛氳繑鍥炴鎶借薄璺緞鍚嶇殑缁濆璺緞鍚嶅瓧绗︿覆銆?br> String getParent()锛氳繑鍥炴鎶借薄璺緞鍚嶇埗鐩綍鐨勬娊璞¤矾寰勫悕锛屽鏋滄璺緞鍚嶆病鏈夋寚瀹氱埗鐩綍锛屽垯杩斿洖 null銆?br>long lastModified()锛氳繑鍥炴鎶借薄璺緞鍚嶈〃绀虹殑鏂囦欢鏈€鍚庝竴娆¤淇敼鐨勬椂闂淬€?br>File.pathSeparator锛氳繑鍥炲綋鍓嶇郴缁熼粯璁ょ殑璺緞鍒嗛殧绗︼紝windows榛樿涓?鈥滐紱鈥濄€?br>File.Separator锛氳繑鍥炲綋鍓嶇郴缁熼粯璁ょ殑鐩綍鍒嗛殧绗︼紝windows榛樿涓?鈥淺鈥濄€?br>4锛氬垽鏂細
boolean exists()锛氬垽鏂枃浠舵垨鑰呮枃浠跺す鏄惁瀛樺湪銆?br> boolean isDirectory()锛氭祴璇曟鎶借薄璺緞鍚嶈〃绀虹殑鏂囦欢鏄惁鏄竴涓洰褰曘€?br> boolean isFile()锛氭祴璇曟鎶借薄璺緞鍚嶈〃绀虹殑鏂囦欢鏄惁鏄竴涓爣鍑嗘枃浠躲€?br> boolean isHidden()锛氭祴璇曟鎶借薄璺緞鍚嶆寚瀹氱殑鏂囦欢鏄惁鏄竴涓殣钘忔枃浠躲€?br> boolean isAbsolute()锛氭祴璇曟鎶借薄璺緞鍚嶆槸鍚︿负缁濆璺緞鍚嶃€?br>5锛氶噸鍛藉悕銆?br> boolean renameTo(File dest)锛氬彲浠ュ疄鐜扮Щ鍔ㄧ殑鏁堟灉銆傚壀鍒?閲嶅懡鍚嶃€?br>
String[] list()锛氬垪鍑烘寚瀹氱洰褰曚笅鐨勫綋鍓嶇殑鏂囦欢鍜屾枃浠跺す鐨勫悕绉般€傚寘鍚殣钘忔枃浠躲€?br>濡傛灉璋冪敤list鏂规硶鐨凢ile 瀵硅薄涓皝瑁呯殑鏄竴涓枃浠讹紝閭d箞list鏂规硶杩斿洖鏁扮粍涓簄ull銆傚鏋滃皝瑁呯殑瀵硅薄涓嶅瓨鍦ㄤ篃浼氳繑鍥瀗ull銆傚彧鏈夊皝瑁呯殑瀵硅薄瀛樺湪骞朵笖鏄枃浠跺す鏃讹紝杩欎釜鏂规硶鎵嶆湁鏁堛€?br>------------------------------------------------------------------------------------------------
閫掑綊锛氬氨鏄嚱鏁拌嚜韬皟鐢ㄨ嚜韬€?/h1>
浠€涔堟椂鍊欑敤閫掑綊鍛紵
褰撲竴涓姛鑳借閲嶅浣跨敤锛岃€屾瘡涓€娆′娇鐢ㄨ鍔熻兘鏃剁殑鍙傛暟涓嶇‘瀹氾紝閮界敱涓婃鐨勫姛鑳藉厓绱犵粨鏋滄潵纭畾銆?br>绠€鍗曡锛氬姛鑳藉唴閮ㄥ張鐢ㄥ埌璇ュ姛鑳斤紝浣嗘槸浼犻€掔殑鍙傛暟鍊间笉纭畾銆?姣忔鍔熻兘鍙備笌杩愮畻鐨勬湭鐭ュ唴瀹逛笉纭畾)銆?br>
閫掑綊鐨勬敞鎰忎簨椤癸細
1锛氫竴瀹氳瀹氫箟閫掑綊鐨勬潯浠躲€?br>2锛氶€掑綊鐨勬鏁颁笉瑕佽繃澶氥€傚鏄撳嚭鐜?StackOverflowError 鏍堝唴瀛樻孩鍑洪敊璇€?br>鍏跺疄閫掑綊灏辨槸鍦ㄦ爤鍐呭瓨涓笉鏂殑鍔犺浇鍚屼竴涓嚱鏁般€?br>------------------------------------------------------------------------------------------------
Java.util.Properties锛?/h3>
|--Hashtable
|--Properties锛氱敤浜庡睘鎬ч厤缃枃浠讹紝閿拰鍊奸兘鏄瓧绗︿覆绫诲瀷銆?br>鐗圭偣锛?锛氬彲浠ユ寔涔呭寲瀛樺偍鏁版嵁銆?锛氶敭鍊奸兘鏄瓧绗︿覆銆?锛氫竴鑸敤浜庨厤缃枃浠躲€?br>
|-- load()锛氬皢娴佷腑鐨勬暟鎹姞杞借繘闆嗗悎銆?br>鍘熺悊锛氬叾瀹炲氨鏄皢璇诲彇娴佸拰鎸囧畾鏂囦欢鐩稿叧鑱旓紝骞惰鍙栦竴琛屾暟鎹紝鍥犱负鏁版嵁鏄鍒欑殑key=value锛屾墍浠ヨ幏鍙栦竴琛屽悗锛岄€氳繃 = 瀵硅琛屾暟鎹繘琛屽垏鍓诧紝宸﹁竟灏辨槸閿紝鍙宠竟灏辨槸鍊硷紝灏嗛敭銆佸€煎瓨鍌ㄥ埌properties闆嗗悎涓€?br>|-- store()锛氬啓鍏ュ悇涓」鍚庯紝鍒锋柊杈撳嚭娴併€?br>|-- list()锛氬皢闆嗗悎鐨勯敭鍊兼暟鎹垪鍑哄埌鎸囧畾鐨勭洰鐨勫湴銆?br>-------------------------------------------------------------------------------------------------
浠ヤ笅浠嬬粛IO鍖呬腑鎵╁睍鍔熻兘鐨勬祦瀵硅薄锛氬熀鏈兘鏄楗拌璁℃ā寮忋€?/h3>
Java.io.outputstream.PrintStream锛氭墦鍗版祦
1锛氭彁渚涗簡鏇村鐨勫姛鑳斤紝姣斿鎵撳嵃鏂规硶銆傚彲浠ョ洿鎺ユ墦鍗颁换鎰忕被鍨嬬殑鏁版嵁銆?br>2锛氬畠鏈変竴涓嚜鍔ㄥ埛鏂版満鍒讹紝鍒涘缓璇ュ璞★紝鎸囧畾鍙傛暟锛屽浜庢寚瀹氭柟娉曞彲浠ヨ嚜鍔ㄥ埛鏂般€?br>3锛氬畠浣跨敤鐨勬湰鏈洪粯璁ょ殑瀛楃缂栫爜.
4锛氳娴佺殑print鏂规硶涓嶆姏鍑篒OException銆?br>
璇ュ璞$殑鏋勯€犲嚱鏁般€?br>PrintStream(File file) 锛氬垱寤哄叿鏈夋寚瀹氭枃浠朵笖涓嶅甫鑷姩琛屽埛鏂扮殑鏂版墦鍗版祦銆?br>PrintStream(File file, String csn) 锛氬垱寤哄叿鏈夋寚瀹氭枃浠跺悕绉板拰瀛楃闆嗕笖涓嶅甫鑷姩琛屽埛鏂扮殑鏂版墦鍗版祦銆?br>PrintStream(OutputStream out) 锛氬垱寤烘柊鐨勬墦鍗版祦銆?br>PrintStream(OutputStream out, boolean autoFlush) 锛氬垱寤烘柊鐨勬墦鍗版祦銆?br>PrintStream(OutputStream out, boolean autoFlush, String encoding) 锛氬垱寤烘柊鐨勬墦鍗版祦銆?br>PrintStream(String fileName) 锛氬垱寤哄叿鏈夋寚瀹氭枃浠跺悕绉颁笖涓嶅甫鑷姩琛屽埛鏂扮殑鏂版墦鍗版祦銆?br>PrintStream(String fileName, String csn)
PrintStream鍙互鎿嶄綔鐩殑锛?锛欶ile瀵硅薄銆?锛氬瓧绗︿覆璺緞銆?锛氬瓧鑺傝緭鍑烘祦銆?br>鍓嶄袱涓兘JDK1.5鐗堟湰鎵嶅嚭鐜般€傝€屼笖鍦ㄦ搷浣滄枃鏈枃浠舵椂锛屽彲鎸囧畾瀛楃缂栫爜浜嗐€?br>
褰撶洰鐨勬槸涓€涓瓧鑺傝緭鍑烘祦鏃讹紝濡傛灉浣跨敤鐨刾rintln鏂规硶锛屽彲浠ュ湪printStream瀵硅薄涓婂姞鍏ヤ竴涓猼rue鍙傛暟銆傝繖鏍峰浜巔rintln鏂规硶鍙互杩涜鑷姩鐨勫埛鏂帮紝鑰屼笉鏄瓑寰呯紦鍐插尯婊′簡鍐嶅埛鏂般€傛渶缁坧rint鏂规硶閮藉皢鍏蜂綋鐨勬暟鎹浆鎴愬瓧绗︿覆锛岃€屼笖閮藉IO寮傚父杩涜浜嗗唴閮ㄥ鐞嗐€?br>
鏃㈢劧鎿嶄綔鐨勬暟鎹兘杞垚浜嗗瓧绗︿覆锛岄偅涔堜娇鐢≒rintWriter鏇村ソ涓€浜涖€傚洜涓篜rintWrite鏄瓧绗︽祦鐨勫瓙绫伙紝鍙互鐩存帴鎿嶄綔瀛楃鏁版嵁锛屽悓鏃朵篃鍙互鎸囧畾鍏蜂綋鐨勭紪鐮併€?br>--------------------------------------------------------
PrintWriter锛氬叿澶囦簡PrintStream鐨勭壒鐐瑰悓鏃讹紝杩樻湁鑷韩鐗圭偣锛?br>璇ュ璞$殑鐩殑鍦版湁鍥涗釜锛?锛欶ile瀵硅薄銆?锛氬瓧绗︿覆璺緞銆?锛氬瓧鑺傝緭鍑烘祦銆?锛氬瓧绗﹁緭鍑烘祦銆?br>
寮€鍙戞椂灏介噺浣跨敤PrintWriter銆?br>
鏂规硶涓洿鎺ユ搷浣滄枃浠剁殑绗簩鍙傛暟鏄紪鐮佽〃銆?br>鐩存帴鎿嶄綔杈撳嚭娴佺殑锛岀浜屽弬鏁版槸鑷姩鍒锋柊銆?br>
//璇诲彇閿洏褰曞叆灏嗘暟鎹浆鎴愬ぇ鍐欐樉绀哄湪鎺у埗鍙?
BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));//婧愶細閿洏杈撳叆
//鐩殑锛氭妸鏁版嵁鍐欏埌鏂囦欢涓紝杩樻兂鑷姩鍒锋柊銆?br>PrintWriter out = new PrintWriter(new FileWriter("out.txt"),true);//璁剧疆true鍚庤嚜鍔ㄥ埛鏂?br>String line = null;
while((line=bufr.readLine())!=null){
if("over".equals(line))
break;
out.println(line.toUpperCase());//杞ぇ鍐欒緭鍑?br>}
//娉ㄦ剰锛歋ystem.in锛孲ystem.out杩欎袱涓爣鍑嗙殑杈撳叆杈撳嚭娴侊紝鍦╦vm鍚姩鏃跺凡缁忓瓨鍦ㄤ簡銆傞殢鏃跺彲浠ヤ娇鐢ㄣ€傚綋jvm缁撴潫浜嗭紝杩欎袱涓祦灏辩粨鏉熶簡銆備絾鏄紝褰撲娇鐢ㄤ簡鏄剧ず鐨刢lose鏂规硶鍏抽棴鏃讹紝杩欎袱涓祦鍦ㄦ彁鍓嶇粨鏉熶簡銆?br>out.close();
bufr.close();
------------------------------------------------------------------------------------------------
SequenceInputStream锛氬簭鍒楁祦锛屼綔鐢ㄥ氨鏄皢澶氫釜璇诲彇娴佸悎骞舵垚涓€涓鍙栨祦銆傚疄鐜版暟鎹悎骞躲€?br>琛ㄧず鍏朵粬杈撳叆娴佺殑閫昏緫涓茶仈銆傚畠浠庤緭鍏ユ祦鐨勬湁搴忛泦鍚堝紑濮嬶紝骞朵粠绗竴涓緭鍏ユ祦寮€濮嬭鍙栵紝鐩村埌鍒拌揪鏂囦欢鏈熬锛屾帴鐫€浠庣浜屼釜杈撳叆娴佽鍙栵紝渚濇绫绘帹锛岀洿鍒板埌杈惧寘鍚殑鏈€鍚庝竴涓緭鍏ユ祦鐨勬枃浠舵湯灏句负姝€?br>杩欐牱鍋氾紝鍙互鏇存柟渚跨殑鎿嶄綔澶氫釜璇诲彇娴侊紝鍏跺疄杩欎釜搴忓垪娴佸唴閮ㄤ細鏈変竴涓湁搴忕殑闆嗗悎瀹瑰櫒锛岀敤浜庡瓨鍌ㄥ涓鍙栨祦瀵硅薄銆?br>璇ュ璞$殑鏋勯€犲嚱鏁板弬鏁版槸鏋氫妇锛屾兂瑕佽幏鍙栨灇涓撅紝闇€瑕佹湁Vector闆嗗悎锛屼絾涓嶉珮鏁堛€傞渶鐢ˋrrayList锛屼絾ArrayList涓病鏈夋灇涓撅紝鍙湁鑷繁鍘诲垱寤烘灇涓惧璞°€?br>浣嗘槸鏂规硶鎬庝箞瀹炵幇鍛紵鍥犱负鏋氫妇鎿嶄綔鐨勬槸鍏蜂綋闆嗗悎涓殑鍏冪礌锛屾墍浠ユ棤娉曞叿浣撳疄鐜帮紝浣嗘槸鏋氫妇鍜岃凯浠e櫒鏄姛鑳戒竴鏍风殑锛屾墍浠ワ紝鍙互鐢ㄨ凯浠f浛浠f灇涓俱€?br>
鍚堝苟鍘熺悊锛氬涓鍙栨祦瀵瑰簲涓€涓緭鍑烘祦銆?br>鍒囧壊鍘熺悊锛氫竴涓鍙栨祦瀵瑰簲澶氫釜杈撳嚭娴併€?br>
import java.io.*;
import java.util.*;
class SplitFileDemo{
private static final String CFG = ".properties";
private static final String SP = ".part";
public static void main(String[] args) throws IOException{
File file = new File("c:\0.bmp");
File dir = new File("c:\partfiles");
meger(dir);
}
//鏁版嵁鐨勫悎骞躲€?br> public static void meger(File dir)throws IOException{
if(!(dir.exists() && dir.isDirectory()))
throw new RuntimeException("鎸囧畾鐨勭洰褰曚笉瀛樺湪锛屾垨鑰呬笉鏄纭殑鐩綍");
File[] files = dir.listFiles(new SuffixFilter(CFG));
if(files.length==0)
throw new RuntimeException("鎵╁睍鍚?proerpties鐨勬枃浠朵笉瀛樺湪");
//鑾峰彇鍒伴厤缃枃浠?br> File config = files[0];
//鑾峰彇閰嶇疆鏂囦欢鐨勪俊鎭€?br> Properties prop = new Properties();
FileInputStream fis = new FileInputStream(config);
prop.load(fis);
String fileName = prop.getProperty("filename");
int partcount = Integer.parseInt(prop.getProperty("partcount"));
//--------------------------
File[] partFiles = dir.listFiles(new SuffixFilter(SP));
if(partFiles.length!=partcount)
throw new RuntimeException("缂哄皯纰庣墖鏂囦欢");
//---------------------
ArrayList<FileInputStream> al = new ArrayList<FileInputStream>();
for(int x=0; x<partcount; x++){
al.add(new FileInputStream(new File(dir,x+SP)));
}
Enumeration<FileInputStream> en = Collections.enumeration(al);
SequenceInputStream sis = new SequenceInputStream(en);
File file = new File(dir,fileName);
FileOutputStream fos = new FileOutputStream(file);
byte[] buf = new byte[1024];
int len = 0;
while((len=sis.read(buf))!=-1){
fos.write(buf,0,len);
}
fos.close();
sis.close();
}
//甯︽湁閰嶇疆淇℃伅鐨勬暟鎹垏鍓层€?br> public static void splitFile(File file)throws IOException{
//鐢ㄤ竴涓鍙栨祦鍜屾枃浠跺叧鑱斻€?br> FileInputStream fis = new FileInputStream(file);
//鍒涘缓鐩殑鍦般€傚洜涓烘湁澶氫釜銆傛墍浠ュ厛鍒涘缓寮曠敤銆?br> FileOutputStream fos = null;
//鎸囧畾纰庣墖鐨勪綅缃€?br> File dir = new File("c:\partfiles");
if(!dir.exists())
dir.mkdir();
//纰庣墖鏂囦欢澶у皬寮曠敤銆?br> File f = null;
byte[] buf = new byte[1024*1024];
//鍥犱负鍒囧壊瀹岀殑鏂囦欢閫氬父閮芥湁瑙勫緥鐨勩€備负浜嗙畝鍗曟爣璁拌寰嬩娇鐢ㄨ鏁板櫒銆?br> int count = 0;
int len = 0;
while((len=fis.read(buf))!=-1){
f = new File(dir,(count++)+".part");
fos = new FileOutputStream(f);
fos.write(buf,0,len);
fos.close();
}
//纰庣墖鏂囦欢鐢熸垚鍚庯紝杩橀渶瑕佸畾涔夐厤缃枃浠惰褰曠敓鎴愮殑纰庣墖鏂囦欢涓暟銆備互鍙婅鍒囧壊鏂囦欢鐨勫悕绉般€?br> //瀹氫箟绠€鍗曠殑閿€间俊鎭紝鍙槸鐢≒roperties銆?br> String filename = file.getName();
Properties prop = new Properties();
prop.setProperty("filename",filename);
prop.setProperty("partcount",count+"");
File config = new File(dir,count+".properties");
fos = new FileOutputStream(config);
prop.store(fos,"");
fos.close();
fis.close();
}
}
class SuffixFilter implements FileFilter{
private String suffix;
SuffixFilter(String suffix){
this.suffix = suffix;
}
public boolean accept(File file){
return file.getName().endsWith(suffix);
}
}
-----------------------------------------------------------------------------------------------
RandomAccessFile:
鐗圭偣锛?br>1锛氳瀵硅薄鍗冲彲璇诲彇锛屽張鍙啓鍏ャ€?br>2锛氳瀵硅薄涓殑瀹氫箟浜嗕竴涓ぇ鍨嬬殑byte鏁扮粍锛岄€氳繃瀹氫箟鎸囬拡鏉ユ搷浣滆繖涓暟缁勩€?br>3锛氬彲浠ラ€氳繃璇ュ璞$殑getFilePointer()鑾峰彇鎸囬拡鐨勪綅缃紝閫氳繃seek()鏂规硶璁剧疆鎸囬拡鐨勪綅缃€?br>4锛氳瀵硅薄鎿嶄綔鐨勬簮鍜岀洰鐨勫繀椤绘槸鏂囦欢銆?br>5锛氬叾瀹炶瀵硅薄鍐呴儴灏佽浜嗗瓧鑺傝鍙栨祦鍜屽瓧鑺傚啓鍏ユ祦銆?br>娉ㄦ剰锛氬疄鐜伴殢鏈鸿闂紝鏈€濂芥槸鏁版嵁鏈夎寰嬨€?br>
class RandomAccessFileDemo{
public static void main(String[] args) throws IOException{
write();
read();
randomWrite();
}
//闅忔満鍐欏叆鏁版嵁锛屽彲浠ュ疄鐜板凡鏈夋暟鎹殑淇敼銆?br> public static void randomWrite()throws IOException{
RandomAccessFile raf = new RandomAccessFile("random.txt","rw");
raf.seek(8*4);
System.out.println("pos :"+raf.getFilePointer());
raf.write("鐜嬫".getBytes());
raf.writeInt(102);
raf.close();
}
public static void read()throws IOException{
RandomAccessFile raf = new RandomAccessFile("random.txt","r");//鍙妯″紡銆?br> //鎸囧畾鎸囬拡鐨勪綅缃€?br> raf.seek(8*1);//瀹炵幇闅忔満璇诲彇鏂囦欢涓殑鏁版嵁銆傛敞鎰忥細鏁版嵁鏈€濂芥湁瑙勫緥銆?br> System.out.println("pos1 :"+raf.getFilePointer());
byte[] buf = new byte[4];
raf.read(buf);
String name = new String(buf);
int age = raf.readInt();
System.out.println(name+"::"+age);
System.out.println("pos2 :"+raf.getFilePointer());
raf.close();
}
public static void write()throws IOException{
//rw锛氬綋杩欎釜鏂囦欢涓嶅瓨鍦紝浼氬垱寤鸿鏂囦欢銆傚綋鏂囦欢宸插瓨鍦紝涓嶄細鍒涘缓銆傛墍浠ヤ笉浼氬儚杈撳嚭娴佷竴鏍疯鐩栥€?br> RandomAccessFile raf = new RandomAccessFile("random.txt","rw");//rw璇诲啓妯″紡
//寰€鏂囦欢涓啓鍏ヤ汉鐨勫熀鏈俊鎭紝濮撳悕锛屽勾榫勩€?br> raf.write("寮犱笁".getBytes());
raf.writeInt(97);
raf.close();
}
}
------------------------------------------------------------------------------------------------
绠¢亾娴侊細
绠¢亾璇诲彇娴佸拰绠¢亾鍐欏叆娴佸彲浠ュ儚绠¢亾涓€鏍峰鎺ヤ笂锛岀閬撹鍙栨祦灏卞彲浠ヨ鍙栫閬撳啓鍏ユ祦鍐欏叆鐨勬暟鎹€?br>娉ㄦ剰锛氶渶瑕佸姞鍏ュ绾跨▼鎶€鏈紝鍥犱负鍗曠嚎绋嬶紝鍏堟墽琛宺ead锛屼細鍙戠敓姝婚攣锛屽洜涓簉ead鏂规硶鏄樆濉炲紡鐨勶紝娌℃湁鏁版嵁鐨剅ead鏂规硶浼氳绾跨▼绛夊緟銆?br>public static void main(String[] args) throws IOException{
PipedInputStream pipin = new PipedInputStream();
PipedOutputStream pipout = new PipedOutputStream();
pipin.connect(pipout);
new Thread(new Input(pipin)).start();
new Thread(new Output(pipout)).start();
}
------------------------------------------------------------------------------------------------
瀵硅薄鐨勫簭鍒楀寲锛氱洰鐨勶細灏嗕竴涓叿浣撶殑瀵硅薄杩涜鎸佷箙鍖栵紝鍐欏叆鍒扮‖鐩樹笂銆?br>娉ㄦ剰锛氶潤鎬佹暟鎹笉鑳借搴忓垪鍖栵紝鍥犱负闈欐€佹暟鎹笉鍦ㄥ爢鍐呭瓨涓紝鏄瓨鍌ㄥ湪闈欐€佹柟娉曞尯涓€?br>
濡備綍灏嗛潪闈欐€佺殑鏁版嵁涓嶈繘琛屽簭鍒楀寲锛熺敤transient 鍏抽敭瀛椾慨楗版鍙橀噺鍗冲彲銆?br>
Serializable锛氱敤浜庡惎鍔ㄥ璞$殑搴忓垪鍖栧姛鑳斤紝鍙互寮哄埗璁╂寚瀹氱被鍏峰搴忓垪鍖栧姛鑳斤紝璇ユ帴鍙d腑娌℃湁鎴愬憳锛岃繖鏄竴涓爣璁版帴鍙c€傝繖涓爣璁版帴鍙g敤浜庣粰搴忓垪鍖栫被鎻愪緵UID銆傝繖涓猽id鏄緷鎹被涓殑鎴愬憳鐨勬暟瀛楃鍚嶈繘琛岃繍琛岃幏鍙栫殑銆傚鏋滀笉闇€瑕佽嚜鍔ㄨ幏鍙栦竴涓猽id锛屽彲浠ュ湪绫讳腑锛屾墜鍔ㄦ寚瀹氫竴涓悕绉颁负serialVersionUID id鍙枫€備緷鎹紪璇戝櫒鐨勪笉鍚岋紝鎴栬€呭淇℃伅鐨勯珮搴︽晱鎰熸€с€傛渶濂芥瘡涓€涓簭鍒楀寲鐨勭被閮借繘琛屾墜鍔ㄦ樉绀虹殑UID鐨勬寚瀹氥€?br>
import java.io.*;
class ObjectStreamDemo {
public static void main(String[] args) throws Exception{
writeObj();
readObj();
}
public static void readObj()throws Exception{
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("obj.txt"));
Object obj = ois.readObject();//璇诲彇涓€涓璞°€?br> System.out.println(obj.toString());
}
public static void writeObj()throws IOException{
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("obj.txt"));
oos.writeObject(new Person("lisi",25)); //鍐欏叆涓€涓璞°€?br> oos.close();
}
}
class Person implements Serializable{
private static final long serialVersionUID = 42L;
private transient String name;//鐢╰ransient淇グ鍚巒ame灏嗕笉浼氳繘琛屽簭鍒楀寲
public int age;
Person(String name,int age){
this.name = name;
this.age = age;
}
public String toString(){
return name+"::"+age;
}
}
-----------------------------------------------------------------------------------------------
DataOutputStream銆丏ataInputStream锛氫笓闂ㄧ敤浜庢搷浣滃熀鏈暟鎹被鍨嬫暟鎹殑瀵硅薄銆?/h3>
DataOutputStream dos = new DataOutputStream(new FileOutputStream("data.txt"));
dos.writeInt(256);
dos.close();
DataInputStream dis = new DataInputStream(new FileInputStream("data.txt"));
int num = dis.readInt();
System.out.println(num);
dis.close();
-----------------------------------------------------------------------------------------------
ByteArrayInputStream锛氭簮锛氬唴瀛?
ByteArrayOutputStream锛氱洰鐨勶細鍐呭瓨銆?
杩欎袱涓祦瀵硅薄涓嶆秹鍙婂簳灞傝祫婧愯皟鐢紝鎿嶄綔鐨勯兘鏄唴瀛樹腑鏁扮粍锛屾墍浠ヤ笉闇€瑕佸叧闂€?br>鐩存帴鎿嶄綔瀛楄妭鏁扮粍灏卞彲浠ヤ簡锛屼负浠€涔堣繕瑕佹妸鏁扮粍灏佽鍒版祦瀵硅薄涓憿锛熷洜涓烘暟缁勬湰韬病鏈夋柟娉曪紝鍙湁涓€涓猯ength灞炴€с€備负浜嗕究浜庢暟缁勭殑鎿嶄綔锛屽皢鏁扮粍杩涜灏佽锛屽澶栨彁渚涙柟娉曟搷浣滄暟缁勪腑鐨勫厓绱犮€?br>
瀵逛簬鏁扮粍鍏冪礌鎿嶄綔鏃犻潪涓ょ鎿嶄綔锛氳缃紙鍐欙級鍜岃幏鍙栵紙璇伙級锛岃€岃繖涓ゆ搷浣滄濂藉搴旀祦鐨勮鍐欐搷浣溿€傝繖涓や釜瀵硅薄灏辨槸浣跨敤浜嗘祦鐨勮鍐欐€濇兂鏉ユ搷浣滄暟缁勩€?br>//鍒涘缓婧愶細
ByteArrayInputStream bis = new ByteArrayInputStream("abcdef".getBytes());
//鍒涘缓鐩殑锛?br> ByteArrayOutputStream bos = new ByteArrayOutputStream();
int ch = 0;
while((ch=bis.read())!=-1){
bos.write(ch);
}
System.out.println(bos.toString());
-----------------------------------------------------------------------------------------------
缃戠粶缂栫▼锛?/h1>
绔彛锛?br>鐗╃悊绔彛锛?br>閫昏緫绔彛锛氱敤浜庢爣璇嗚繘绋嬬殑閫昏緫鍦板潃锛屼笉鍚岃繘绋嬬殑鏍囪瘑锛涙湁鏁堢鍙o細0~65535锛屽叾涓?~1024绯荤粺浣跨敤鎴栦繚鐣欑鍙c€?br>
java 涓璱p瀵硅薄锛欼netAddress.
import java.net.*;
class IPDemo{
public static void main(String[] args) throws UnknownHostException{
//閫氳繃鍚嶇О(ip瀛楃涓瞣r涓绘満鍚?鏉ヨ幏鍙栦竴涓猧p瀵硅薄銆?br> InetAddress ip = InetAddress.getByName("www.baidu.com");//java.net.UnknownHostException
System.out.println("addr:"+ip.getHostAddress());
System.out.println("name:"+ip.getHostName());
}
}
Socket锛氣槄鈽呪槄鈽咃紝濂楁帴瀛楋紝閫氫俊鐨勭鐐广€?br>灏辨槸涓虹綉缁滄湇鍔℃彁渚涚殑涓€绉嶆満鍒讹紝閫氫俊鐨勪袱绔兘鏈塖ocket锛岀綉缁滈€氫俊鍏跺疄灏辨槸Socket闂寸殑閫氫俊锛屾暟鎹湪涓や釜Socket闂撮€氳繃IO浼犺緭銆?br>
UDP浼犺緭锛?/h2>
1锛屽彧瑕佹槸缃戠粶浼犺緭锛屽繀椤绘湁socket 銆?br>2锛屾暟鎹竴瀹氳灏佽鍒版暟鎹寘涓紝鏁版嵁鍖呬腑鍖呮嫭鐩殑鍦板潃銆佺鍙c€佹暟鎹瓑淇℃伅銆?br>
鐩存帴鎿嶄綔udp涓嶅彲鑳斤紝瀵逛簬java璇█搴旇灏唘dp灏佽鎴愬璞★紝鏄撲簬鎴戜滑鐨勪娇鐢紝杩欎釜瀵硅薄灏辨槸DatagramSocket. 灏佽浜唘dp浼犺緭鍗忚鐨剆ocket瀵硅薄銆?br>
鍥犱负鏁版嵁鍖呬腑鍖呭惈鐨勪俊鎭緝澶氾紝涓轰簡鎿嶄綔杩欎簺淇℃伅鏂逛究锛屼篃涓€鏍蜂細灏嗗叾灏佽鎴愬璞°€傝繖涓暟鎹寘瀵硅薄灏辨槸锛欴atagramPacket.閫氳繃杩欎釜瀵硅薄涓殑鏂规硶锛屽氨鍙互鑾峰彇鍒版暟鎹寘涓殑鍚勭淇℃伅銆?br>
DatagramSocket鍏峰鍙戦€佸拰鎺ュ彈鍔熻兘锛屽湪杩涜udp浼犺緭鏃讹紝闇€瑕佹槑纭竴涓槸鍙戦€佺锛屼竴涓槸鎺ユ敹绔€?br>
udp鐨勫彂閫佺锛?/h3>
1锛屽缓绔媢dp鐨剆ocket鏈嶅姟锛屽垱寤哄璞℃椂濡傛灉娌℃湁鏄庣‘绔彛锛岀郴缁熶細鑷姩鍒嗛厤涓€涓湭琚娇鐢ㄧ殑绔彛銆?br>2锛屾槑纭鍙戦€佺殑鍏蜂綋鏁版嵁銆?br>3锛屽皢鏁版嵁灏佽鎴愪簡鏁版嵁鍖呫€?br>4锛岀敤socket鏈嶅姟鐨剆end鏂规硶灏嗘暟鎹寘鍙戦€佸嚭鍘汇€?br>5锛屽叧闂祫婧愩€?br>--------------------------------------------------------------
import java.net.*;
class UdpSend{
public static void main(String[] args)throws Exception {
// 1锛屽缓绔媢dp鐨剆ocket鏈嶅姟銆?br> DatagramSocket ds = new DatagramSocket(8888);//鎸囧畾鍙戦€佺鍙o紝涓嶆寚瀹氱郴缁熶細闅忔満鍒嗛厤銆?br>// 2锛屾槑纭鍙戦€佺殑鍏蜂綋鏁版嵁銆?br> String text = "udp浼犺緭婕旂ず 鍝ヤ滑鏉ヤ簡";
byte[] buf = text.getBytes();
// 3锛屽皢鏁版嵁灏佽鎴愪簡鏁版嵁鍖呫€?br> DatagramPacket dp = new DatagramPacket(buf,
buf.length,InetAddress.getByName("10.1.31.127"),10000);
// 4锛岀敤socket鏈嶅姟鐨剆end鏂规硶灏嗘暟鎹寘鍙戦€佸嚭鍘汇€?br> ds.send(dp);
// 5锛屽叧闂祫婧愩€?br> ds.close();
}
}
-------------------------------------------------------------
udp鐨勬帴鏀剁锛?/h2>
1锛屽垱寤簎dp鐨剆ocket鏈嶅姟锛屽繀椤昏鏄庣‘涓€涓鍙o紝浣滅敤鍦ㄤ簬锛屽彧鏈夊彂閫佸埌杩欎釜绔彛鐨勬暟鎹墠鏄繖涓帴鏀剁鍙互澶勭悊鐨勬暟鎹€?br>2锛屽畾涔夋暟鎹寘锛岀敤浜庡瓨鍌ㄦ帴鏀跺埌鏁版嵁銆?br>3锛岄€氳繃socket鏈嶅姟鐨勬帴鏀舵柟娉曞皢鏀跺埌鐨勬暟鎹瓨鍌ㄥ埌鏁版嵁鍖呬腑銆?br>4锛岄€氳繃鏁版嵁鍖呯殑鏂规硶鑾峰彇鏁版嵁鍖呬腑鐨勫叿浣撴暟鎹唴瀹癸紝姣斿ip銆佺鍙c€佹暟鎹瓑绛夈€?br>5锛屽叧闂祫婧愩€?br>-------------------------------------------------------------
class UdpRece {
public static void main(String[] args) throws Exception{
// 1锛屽垱寤簎dp鐨剆ocket鏈嶅姟銆?br> DatagramSocket ds = new DatagramSocket(10000);
// 2锛屽畾涔夋暟鎹寘锛岀敤浜庡瓨鍌ㄦ帴鏀跺埌鏁版嵁銆傚厛瀹氫箟瀛楄妭鏁扮粍锛屾暟鎹寘浼氭妸鏁版嵁瀛樺偍鍒板瓧鑺傛暟缁勪腑銆?br> byte[] buf = new byte[1024];
DatagramPacket dp = new DatagramPacket(buf,buf.length);
// 3锛岄€氳繃socket鏈嶅姟鐨勬帴鏀舵柟娉曞皢鏀跺埌鐨勬暟鎹瓨鍌ㄥ埌鏁版嵁鍖呬腑銆?br> ds.receive(dp);//璇ユ柟娉曟槸闃诲寮忔柟娉曘€?br>// 4锛岄€氳繃鏁版嵁鍖呯殑鏂规硶鑾峰彇鏁版嵁鍖呬腑鐨勫叿浣撴暟鎹唴瀹癸紝姣斿ip锛岀鍙o紝鏁版嵁绛夌瓑銆?br> String ip = dp.getAddress().getHostAddress();
int port = dp.getPort();
String text = new String(dp.getData(),0,dp.getLength());//灏嗗瓧鑺傛暟缁勪腑鐨勬湁鏁堥儴鍒嗚浆鎴愬瓧绗︿覆銆?br> System.out.println(ip+":"+port+"--"+text);
// 5锛屽叧闂祫婧愩€?br> ds.close();
}
}
-------------------------------------------------------------
TCP浼犺緭锛氫袱涓鐐圭殑寤虹珛杩炴帴鍚庝細鏈変竴涓紶杈撴暟鎹殑閫氶亾锛岃繖閫氶亾绉颁负娴侊紝鑰屼笖鏄缓绔嬪湪缃戠粶鍩虹涓婄殑娴侊紝绉颁箣涓簊ocket娴併€傝娴佷腑鏃㈡湁璇诲彇锛屼篃鏈夊啓鍏ャ€?br>
tcp鐨勪袱涓鐐癸細涓€涓槸瀹㈡埛绔紝涓€涓槸鏈嶅姟绔€?br>瀹㈡埛绔細瀵瑰簲鐨勫璞★紝Socket
鏈嶅姟绔細瀵瑰簲鐨勫璞★紝ServerSocket
TCP瀹㈡埛绔細
1锛屽缓绔媡cp鐨剆ocket鏈嶅姟锛屾渶濂芥槑纭叿浣撶殑鍦板潃鍜岀鍙c€傝繖涓璞″湪鍒涘缓鏃讹紝灏卞凡缁忓彲浠ュ鎸囧畾ip鍜岀鍙h繘琛岃繛鎺?涓夋鎻℃墜)銆?br>2锛屽鏋滆繛鎺ユ垚鍔燂紝灏辨剰鍛崇潃閫氶亾寤虹珛浜嗭紝socket娴佸氨宸茬粡浜х敓浜嗐€傚彧瑕佽幏鍙栧埌socket娴佷腑鐨勮鍙栨祦鍜屽啓鍏ユ祦鍗冲彲锛屽彧瑕侀€氳繃getInputStream鍜実etOutputStream灏卞彲浠ヨ幏鍙栦袱涓祦瀵硅薄銆?br>3锛屽叧闂祫婧愩€?br>--------------------------------------------------------------
import java.net.*;
import java.io.*;
//闇€姹傦細瀹㈡埛绔粰鏈嶅姟鍣ㄧ鍙戦€佷竴涓暟鎹€?br>class TcpClient{
public static void main(String[] args) throws Exception{
Socket s = new Socket("10.1.31.69",10002);
OutputStream out = s.getOutputStream();//鑾峰彇浜唖ocket娴佷腑鐨勮緭鍑烘祦瀵硅薄銆?br> out.write("tcp婕旂ず锛屽摜浠張鏉ヤ簡!".getBytes());
s.close();
}
}
--------------------------------------------------------------
TCP鏈嶅姟绔細
1锛屽垱寤烘湇鍔$socket鏈嶅姟锛屽苟鐩戝惉涓€涓鍙c€?br>2锛屾湇鍔$涓轰簡缁欏鎴风鎻愪緵鏈嶅姟锛岃幏鍙栧鎴风鐨勫唴瀹癸紝鍙互閫氳繃accept鏂规硶鑾峰彇杩炴帴杩囨潵鐨勫鎴风瀵硅薄銆?br>3锛屽彲浠ラ€氳繃鑾峰彇鍒扮殑socket瀵硅薄涓殑socket娴佸拰鍏蜂綋鐨勫鎴风杩涜閫氳銆?br>4锛屽鏋滈€氳缁撴潫锛屽叧闂祫婧愩€傛敞鎰忥細瑕佸厛鍏冲鎴风锛屽啀鍏虫湇鍔$銆?br>--------------------------------------------------------------
class TcpServer{
public static void main(String[] args) throws Exception{
ServerSocket ss = new ServerSocket(10002);//寤虹珛鏈嶅姟绔殑socket鏈嶅姟
Socket s = ss.accept();//鑾峰彇瀹㈡埛绔璞?br> String ip = s.getInetAddress().getHostAddress();
System.out.println(ip+".....connected");
// 鍙互閫氳繃鑾峰彇鍒扮殑socket瀵硅薄涓殑socket娴佸拰鍏蜂綋鐨勫鎴风杩涜閫氳銆?br> InputStream in = s.getInputStream();//璇诲彇瀹㈡埛绔殑鏁版嵁锛屼娇鐢ㄥ鎴风瀵硅薄鐨剆ocket璇诲彇娴?br> byte[] buf = new byte[1024];
int len = in.read(buf);
String text = new String(buf,0,len);
System.out.println(text);
// 濡傛灉閫氳缁撴潫锛屽叧闂祫婧愩€傛敞鎰忥細瑕佸厛鍏冲鎴风锛屽湪鍏虫湇鍔$銆?br> s.close();
ss.close();
}
}
-----------------------------------------------------------------------------------------------
鍙嶅皠鎶€鏈細
鍏跺疄灏辨槸鍔ㄦ€佸姞杞戒竴涓寚瀹氱殑绫伙紝骞惰幏鍙栬绫讳腑鐨勬墍鏈夌殑鍐呭銆傝€屼笖灏嗗瓧鑺傜爜鏂囦欢灏佽鎴愬璞★紝骞跺皢瀛楄妭鐮佹枃浠朵腑鐨勫唴瀹归兘灏佽鎴愬璞★紝杩欐牱渚夸簬鎿嶄綔杩欎簺鎴愬憳銆傜畝鍗曡锛氬弽灏勬妧鏈彲浠ュ涓€涓被杩涜瑙e墫銆?br>
鍙嶅皠鐨勫ソ澶勶細澶уぇ鐨勫寮轰簡绋嬪簭鐨勬墿灞曟€с€?br>
鍙嶅皠鐨勫熀鏈楠わ細
1銆佽幏寰桟lass瀵硅薄锛屽氨鏄幏鍙栧埌鎸囧畾鐨勫悕绉扮殑瀛楄妭鐮佹枃浠跺璞°€?br>2銆佸疄渚嬪寲瀵硅薄锛岃幏寰楃被鐨勫睘鎬с€佹柟娉曟垨鏋勯€犲嚱鏁般€?br>3銆佽闂睘鎬с€佽皟鐢ㄦ柟娉曘€佽皟鐢ㄦ瀯閫犲嚱鏁板垱寤哄璞°€?br>
鑾峰彇杩欎釜Class瀵硅薄锛屾湁涓夌鏂瑰紡锛?br>1锛氶€氳繃姣忎釜瀵硅薄閮藉叿澶囩殑鏂规硶getClass鏉ヨ幏鍙栥€傚紛绔細蹇呴』瑕佸垱寤鸿绫诲璞★紝鎵嶅彲浠ヨ皟鐢╣etClass鏂规硶銆?br>2锛氭瘡涓€涓暟鎹被鍨?鍩烘湰鏁版嵁绫诲瀷鍜屽紩鐢ㄦ暟鎹被鍨?閮芥湁涓€涓潤鎬佺殑灞炴€lass銆傚紛绔細蹇呴』瑕佸厛鏄庣‘璇ョ被銆?br> 鍓嶄袱绉嶆柟寮忎笉鍒╀簬绋嬪簭鐨勬墿灞曪紝鍥犱负閮介渶瑕佸湪绋嬪簭浣跨敤鍏蜂綋鐨勭被鏉ュ畬鎴愩€?br>3锛氫娇鐢ㄧ殑Class绫讳腑鐨勬柟娉曪紝闈欐€佺殑forName鏂规硶銆?br> 鎸囧畾浠€涔堢被鍚嶏紝灏辫幏鍙栦粈涔堢被瀛楄妭鐮佹枃浠跺璞★紝杩欑鏂瑰紡鐨勬墿灞曟€ф渶寮猴紝鍙灏嗙被鍚嶇殑瀛楃涓蹭紶鍏ュ嵆鍙€?br>// 1. 鏍规嵁缁欏畾鐨勭被鍚嶆潵鑾峰緱 鐢ㄤ簬绫诲姞杞?br>String classname = "cn.itcast.reflect.Person";// 鏉ヨ嚜閰嶇疆鏂囦欢
Class clazz = Class.forName(classname);// 姝ゅ璞′唬琛≒erson.class
// 2. 濡傛灉鎷垮埌浜嗗璞★紝涓嶇煡閬撴槸浠€涔堢被鍨?nbsp; 鐢ㄤ簬鑾峰緱瀵硅薄鐨勭被鍨?br>Object obj = new Person();
Class clazz1 = obj.getClass();// 鑾峰緱瀵硅薄鍏蜂綋鐨勭被鍨?br>// 3. 濡傛灉鏄槑纭湴鑾峰緱鏌愪釜绫荤殑Class瀵硅薄 涓昏鐢ㄤ簬浼犲弬
Class clazz2 = Person.class;
鍙嶅皠鐨勭敤娉曪細
1锛夈€侀渶瑕佽幏寰梛ava绫荤殑鍚勪釜缁勬垚閮ㄥ垎锛岄鍏堥渶瑕佽幏寰楃被鐨凜lass瀵硅薄锛岃幏寰桟lass瀵硅薄鐨勪笁绉嶆柟寮忥細
Class.forName(classname) 鐢ㄤ簬鍋氱被鍔犺浇
obj.getClass() 鐢ㄤ簬鑾峰緱瀵硅薄鐨勭被鍨?br> 绫诲悕.class 鐢ㄤ簬鑾峰緱鎸囧畾鐨勭被鍨嬶紝浼犲弬鐢?br>
2)銆佸弽灏勭被鐨勬垚鍛樻柟娉曪細
Class clazz = Person.class;
Method method = clazz.getMethod(methodName, new Class[]{paramClazz1, paramClazz2});
method.invoke();
3)銆佸弽灏勭被鐨勬瀯閫犲嚱鏁帮細
Constructor con = clazz.getConstructor(new Class[]{paramClazz1, paramClazz2,...})
con.newInstance(params...)
4)銆佸弽灏勭被鐨勫睘鎬э細
Field field = clazz.getField(fieldName);
field.setAccessible(true);
field.setObject(value);
鑾峰彇浜嗗瓧鑺傜爜鏂囦欢瀵硅薄鍚庯紝鏈€缁堥兘闇€瑕佸垱寤烘寚瀹氱被鐨勫璞★細
鍒涘缓瀵硅薄鐨勪袱绉嶆柟寮?鍏跺疄灏辨槸瀵硅薄鍦ㄨ繘琛屽疄渚嬪寲鏃剁殑鍒濆鍖栨柟寮?锛?br>1锛岃皟鐢ㄧ┖鍙傛暟鐨勬瀯閫犲嚱鏁帮細浣跨敤浜咰lass绫讳腑鐨刵ewInstance()鏂规硶銆?br>2锛岃皟鐢ㄥ甫鍙傛暟鐨勬瀯閫犲嚱鏁帮細鍏堣鑾峰彇鎸囧畾鍙傛暟鍒楄〃鐨勬瀯閫犲嚱鏁板璞★紝鐒跺悗閫氳繃璇ユ瀯閫犲嚱鏁扮殑瀵硅薄鐨刵ewInstance(瀹為檯鍙傛暟) 杩涜瀵硅薄鐨勫垵濮嬪寲銆?br>
缁间笂鎵€杩帮紝绗簩绉嶆柟寮忥紝蹇呴』瑕佸厛鏄庣‘鍏蜂綋鐨勬瀯閫犲嚱鏁扮殑鍙傛暟绫诲瀷锛屼笉渚夸簬鎵╁睍銆傛墍浠ヤ竴鑸儏鍐典笅锛岃鍙嶅皠鐨勭被锛屽唴閮ㄩ€氬父閮戒細鎻愪緵涓€涓叕鏈夌殑绌哄弬鏁扮殑鏋勯€犲嚱鏁般€?br>------------------------------------------------------
// 濡備綍鐢熸垚鑾峰彇鍒板瓧鑺傜爜鏂囦欢瀵硅薄鐨勫疄渚嬪璞°€?br> Class clazz = Class.forName("cn.itcast.bean.Person");//绫诲姞杞?br>// 鐩存帴鑾峰緱鎸囧畾鐨勭被鍨?br> clazz = Person.class;
// 鏍规嵁瀵硅薄鑾峰緱绫诲瀷
Object obj = new Person("zhangsan", 19);
clazz = obj.getClass();
Object obj = clazz.newInstance();//璇ュ疄渚嬪寲瀵硅薄鐨勬柟娉曡皟鐢ㄥ氨鏄寚瀹氱被涓殑绌哄弬鏁版瀯閫犲嚱鏁帮紝缁欏垱寤哄璞¤繘琛屽垵濮嬪寲銆傚綋鎸囧畾绫讳腑娌℃湁绌哄弬鏁版瀯閫犲嚱鏁版椂锛岃濡備綍鍒涘缓璇ョ被瀵硅薄鍛紵璇风湅method_2();
public static void method_2() throws Exception {
Class clazz = Class.forName("cn.itcast.bean.Person");
//鏃㈢劧绫讳腑娌℃湁绌哄弬鏁扮殑鏋勯€犲嚱鏁?閭d箞鍙湁鑾峰彇鎸囧畾鍙傛暟鐨勬瀯閫犲嚱鏁?鐢ㄨ鍑芥暟鏉ヨ繘琛屽疄渚嬪寲銆?br> //鑾峰彇涓€涓甫鍙傛暟鐨勬瀯閫犲櫒銆?br> Constructor constructor = clazz.getConstructor(String.class,int.class);
//鎯宠瀵瑰璞¤繘琛屽垵濮嬪寲锛屼娇鐢ㄦ瀯閫犲櫒鐨勬柟娉昻ewInstance();
Object obj = constructor.newInstance("zhagnsan",30);
//鑾峰彇鎵€鏈夋瀯閫犲櫒銆?br> Constructor[] constructors = clazz.getConstructors();//鍙寘鍚叕鍏辩殑
constructors = clazz.getDeclaredConstructors();//鍖呭惈绉佹湁鐨?br> for(Constructor con : constructors) {
System.out.println(con);
}
}
------------------------------------------------------
鍙嶅皠鎸囧畾绫讳腑鐨勬柟娉曪細
//鑾峰彇绫讳腑鎵€鏈夌殑鏂规硶銆?br> public static void method_1() throws Exception {
Class clazz = Class.forName("cn.itcast.bean.Person");
Method[] methods = clazz.getMethods();//鑾峰彇鐨勬槸璇ョ被涓殑鍏湁鏂规硶鍜岀埗绫讳腑鐨勫叕鏈夋柟娉曘€?br> methods = clazz.getDeclaredMethods();//鑾峰彇鏈被涓殑鏂规硶锛屽寘鍚鏈夋柟娉曘€?br> for(Method method : methods) {
System.out.println(method);
}
}
//鑾峰彇鎸囧畾鏂规硶锛?br> public static void method_2() throws Exception {
Class clazz = Class.forName("cn.itcast.bean.Person");
//鑾峰彇鎸囧畾鍚嶇О鐨勬柟娉曘€?br> Method method = clazz.getMethod("show", int.class,String.class);
//鎯宠杩愯鎸囧畾鏂规硶锛屽綋鐒舵槸鏂规硶瀵硅薄鏈€娓呮锛屼负浜嗚鏂规硶杩愯锛岃皟鐢ㄦ柟娉曞璞$殑invoke鏂规硶鍗冲彲锛屼絾鏄柟娉曡繍琛屽繀椤昏鏄庣‘鎵€灞炵殑瀵硅薄鍜屽叿浣撶殑瀹為檯鍙傛暟銆?br> Object obj = clazz.newInstance();
method.invoke(obj, 39,"hehehe");//鎵ц涓€涓柟娉?br> }
//鎯宠杩愯绉佹湁鏂规硶銆?br> public static void method_3() throws Exception {
Class clazz = Class.forName("cn.itcast.bean.Person");
//鎯宠鑾峰彇绉佹湁鏂规硶銆傚繀椤荤敤getDeclearMethod();
Method method = clazz.getDeclaredMethod("method", null);
// 绉佹湁鏂规硶涓嶈兘鐩存帴璁块棶锛屽洜涓烘潈闄愪笉澶熴€傞潪瑕佽闂紝鍙互閫氳繃鏆村姏鐨勬柟寮忋€?br> method.setAccessible(true);//涓€鑸緢灏戠敤锛屽洜涓虹鏈夊氨鏄殣钘忚捣鏉ワ紝鎵€浠ュ敖閲忎笉瑕佽闂€?br> }
//鍙嶅皠闈欐€佹柟娉曘€?br> public static void method_4() throws Exception {
Class clazz = Class.forName("cn.itcast.bean.Person");
Method method = clazz.getMethod("function",null);
method.invoke(null,null);
}
------------------------------------------------------------------------------------------------
dos.writeInt(256);
dos.close();
DataInputStream dis = new DataInputStream(new FileInputStream("data.txt"));
int num = dis.readInt();
System.out.println(num);
dis.close();
-----------------------------------------------------------------------------------------------
ByteArrayInputStream锛氭簮锛氬唴瀛?
ByteArrayOutputStream锛氱洰鐨勶細鍐呭瓨銆?
杩欎袱涓祦瀵硅薄涓嶆秹鍙婂簳灞傝祫婧愯皟鐢紝鎿嶄綔鐨勯兘鏄唴瀛樹腑鏁扮粍锛屾墍浠ヤ笉闇€瑕佸叧闂€?br>鐩存帴鎿嶄綔瀛楄妭鏁扮粍灏卞彲浠ヤ簡锛屼负浠€涔堣繕瑕佹妸鏁扮粍灏佽鍒版祦瀵硅薄涓憿锛熷洜涓烘暟缁勬湰韬病鏈夋柟娉曪紝鍙湁涓€涓猯ength灞炴€с€備负浜嗕究浜庢暟缁勭殑鎿嶄綔锛屽皢鏁扮粍杩涜灏佽锛屽澶栨彁渚涙柟娉曟搷浣滄暟缁勪腑鐨勫厓绱犮€?br>
瀵逛簬鏁扮粍鍏冪礌鎿嶄綔鏃犻潪涓ょ鎿嶄綔锛氳缃紙鍐欙級鍜岃幏鍙栵紙璇伙級锛岃€岃繖涓ゆ搷浣滄濂藉搴旀祦鐨勮鍐欐搷浣溿€傝繖涓や釜瀵硅薄灏辨槸浣跨敤浜嗘祦鐨勮鍐欐€濇兂鏉ユ搷浣滄暟缁勩€?br>//鍒涘缓婧愶細
ByteArrayInputStream bis = new ByteArrayInputStream("abcdef".getBytes());
//鍒涘缓鐩殑锛?br> ByteArrayOutputStream bos = new ByteArrayOutputStream();
int ch = 0;
while((ch=bis.read())!=-1){
bos.write(ch);
}
System.out.println(bos.toString());
-----------------------------------------------------------------------------------------------
缃戠粶缂栫▼锛?/h1>
绔彛锛?br>鐗╃悊绔彛锛?br>閫昏緫绔彛锛氱敤浜庢爣璇嗚繘绋嬬殑閫昏緫鍦板潃锛屼笉鍚岃繘绋嬬殑鏍囪瘑锛涙湁鏁堢鍙o細0~65535锛屽叾涓?~1024绯荤粺浣跨敤鎴栦繚鐣欑鍙c€?br>
java 涓璱p瀵硅薄锛欼netAddress.
import java.net.*;
class IPDemo{
public static void main(String[] args) throws UnknownHostException{
//閫氳繃鍚嶇О(ip瀛楃涓瞣r涓绘満鍚?鏉ヨ幏鍙栦竴涓猧p瀵硅薄銆?br> InetAddress ip = InetAddress.getByName("www.baidu.com");//java.net.UnknownHostException
System.out.println("addr:"+ip.getHostAddress());
System.out.println("name:"+ip.getHostName());
}
}
Socket锛氣槄鈽呪槄鈽咃紝濂楁帴瀛楋紝閫氫俊鐨勭鐐广€?br>灏辨槸涓虹綉缁滄湇鍔℃彁渚涚殑涓€绉嶆満鍒讹紝閫氫俊鐨勪袱绔兘鏈塖ocket锛岀綉缁滈€氫俊鍏跺疄灏辨槸Socket闂寸殑閫氫俊锛屾暟鎹湪涓や釜Socket闂撮€氳繃IO浼犺緭銆?br>
UDP浼犺緭锛?/h2>
1锛屽彧瑕佹槸缃戠粶浼犺緭锛屽繀椤绘湁socket 銆?br>2锛屾暟鎹竴瀹氳灏佽鍒版暟鎹寘涓紝鏁版嵁鍖呬腑鍖呮嫭鐩殑鍦板潃銆佺鍙c€佹暟鎹瓑淇℃伅銆?br>
鐩存帴鎿嶄綔udp涓嶅彲鑳斤紝瀵逛簬java璇█搴旇灏唘dp灏佽鎴愬璞★紝鏄撲簬鎴戜滑鐨勪娇鐢紝杩欎釜瀵硅薄灏辨槸DatagramSocket. 灏佽浜唘dp浼犺緭鍗忚鐨剆ocket瀵硅薄銆?br>
鍥犱负鏁版嵁鍖呬腑鍖呭惈鐨勪俊鎭緝澶氾紝涓轰簡鎿嶄綔杩欎簺淇℃伅鏂逛究锛屼篃涓€鏍蜂細灏嗗叾灏佽鎴愬璞°€傝繖涓暟鎹寘瀵硅薄灏辨槸锛欴atagramPacket.閫氳繃杩欎釜瀵硅薄涓殑鏂规硶锛屽氨鍙互鑾峰彇鍒版暟鎹寘涓殑鍚勭淇℃伅銆?br>
DatagramSocket鍏峰鍙戦€佸拰鎺ュ彈鍔熻兘锛屽湪杩涜udp浼犺緭鏃讹紝闇€瑕佹槑纭竴涓槸鍙戦€佺锛屼竴涓槸鎺ユ敹绔€?br>
udp鐨勫彂閫佺锛?/h3>
1锛屽缓绔媢dp鐨剆ocket鏈嶅姟锛屽垱寤哄璞℃椂濡傛灉娌℃湁鏄庣‘绔彛锛岀郴缁熶細鑷姩鍒嗛厤涓€涓湭琚娇鐢ㄧ殑绔彛銆?br>2锛屾槑纭鍙戦€佺殑鍏蜂綋鏁版嵁銆?br>3锛屽皢鏁版嵁灏佽鎴愪簡鏁版嵁鍖呫€?br>4锛岀敤socket鏈嶅姟鐨剆end鏂规硶灏嗘暟鎹寘鍙戦€佸嚭鍘汇€?br>5锛屽叧闂祫婧愩€?br>--------------------------------------------------------------
import java.net.*;
class UdpSend{
public static void main(String[] args)throws Exception {
// 1锛屽缓绔媢dp鐨剆ocket鏈嶅姟銆?br> DatagramSocket ds = new DatagramSocket(8888);//鎸囧畾鍙戦€佺鍙o紝涓嶆寚瀹氱郴缁熶細闅忔満鍒嗛厤銆?br>// 2锛屾槑纭鍙戦€佺殑鍏蜂綋鏁版嵁銆?br> String text = "udp浼犺緭婕旂ず 鍝ヤ滑鏉ヤ簡";
byte[] buf = text.getBytes();
// 3锛屽皢鏁版嵁灏佽鎴愪簡鏁版嵁鍖呫€?br> DatagramPacket dp = new DatagramPacket(buf,
buf.length,InetAddress.getByName("10.1.31.127"),10000);
// 4锛岀敤socket鏈嶅姟鐨剆end鏂规硶灏嗘暟鎹寘鍙戦€佸嚭鍘汇€?br> ds.send(dp);
// 5锛屽叧闂祫婧愩€?br> ds.close();
}
}
-------------------------------------------------------------
udp鐨勬帴鏀剁锛?/h2>
1锛屽垱寤簎dp鐨剆ocket鏈嶅姟锛屽繀椤昏鏄庣‘涓€涓鍙o紝浣滅敤鍦ㄤ簬锛屽彧鏈夊彂閫佸埌杩欎釜绔彛鐨勬暟鎹墠鏄繖涓帴鏀剁鍙互澶勭悊鐨勬暟鎹€?br>2锛屽畾涔夋暟鎹寘锛岀敤浜庡瓨鍌ㄦ帴鏀跺埌鏁版嵁銆?br>3锛岄€氳繃socket鏈嶅姟鐨勬帴鏀舵柟娉曞皢鏀跺埌鐨勬暟鎹瓨鍌ㄥ埌鏁版嵁鍖呬腑銆?br>4锛岄€氳繃鏁版嵁鍖呯殑鏂规硶鑾峰彇鏁版嵁鍖呬腑鐨勫叿浣撴暟鎹唴瀹癸紝姣斿ip銆佺鍙c€佹暟鎹瓑绛夈€?br>5锛屽叧闂祫婧愩€?br>-------------------------------------------------------------
class UdpRece {
public static void main(String[] args) throws Exception{
// 1锛屽垱寤簎dp鐨剆ocket鏈嶅姟銆?br> DatagramSocket ds = new DatagramSocket(10000);
// 2锛屽畾涔夋暟鎹寘锛岀敤浜庡瓨鍌ㄦ帴鏀跺埌鏁版嵁銆傚厛瀹氫箟瀛楄妭鏁扮粍锛屾暟鎹寘浼氭妸鏁版嵁瀛樺偍鍒板瓧鑺傛暟缁勪腑銆?br> byte[] buf = new byte[1024];
DatagramPacket dp = new DatagramPacket(buf,buf.length);
// 3锛岄€氳繃socket鏈嶅姟鐨勬帴鏀舵柟娉曞皢鏀跺埌鐨勬暟鎹瓨鍌ㄥ埌鏁版嵁鍖呬腑銆?br> ds.receive(dp);//璇ユ柟娉曟槸闃诲寮忔柟娉曘€?br>// 4锛岄€氳繃鏁版嵁鍖呯殑鏂规硶鑾峰彇鏁版嵁鍖呬腑鐨勫叿浣撴暟鎹唴瀹癸紝姣斿ip锛岀鍙o紝鏁版嵁绛夌瓑銆?br> String ip = dp.getAddress().getHostAddress();
int port = dp.getPort();
String text = new String(dp.getData(),0,dp.getLength());//灏嗗瓧鑺傛暟缁勪腑鐨勬湁鏁堥儴鍒嗚浆鎴愬瓧绗︿覆銆?br> System.out.println(ip+":"+port+"--"+text);
// 5锛屽叧闂祫婧愩€?br> ds.close();
}
}
-------------------------------------------------------------
TCP浼犺緭锛氫袱涓鐐圭殑寤虹珛杩炴帴鍚庝細鏈変竴涓紶杈撴暟鎹殑閫氶亾锛岃繖閫氶亾绉颁负娴侊紝鑰屼笖鏄缓绔嬪湪缃戠粶鍩虹涓婄殑娴侊紝绉颁箣涓簊ocket娴併€傝娴佷腑鏃㈡湁璇诲彇锛屼篃鏈夊啓鍏ャ€?br>
tcp鐨勪袱涓鐐癸細涓€涓槸瀹㈡埛绔紝涓€涓槸鏈嶅姟绔€?br>瀹㈡埛绔細瀵瑰簲鐨勫璞★紝Socket
鏈嶅姟绔細瀵瑰簲鐨勫璞★紝ServerSocket
TCP瀹㈡埛绔細
1锛屽缓绔媡cp鐨剆ocket鏈嶅姟锛屾渶濂芥槑纭叿浣撶殑鍦板潃鍜岀鍙c€傝繖涓璞″湪鍒涘缓鏃讹紝灏卞凡缁忓彲浠ュ鎸囧畾ip鍜岀鍙h繘琛岃繛鎺?涓夋鎻℃墜)銆?br>2锛屽鏋滆繛鎺ユ垚鍔燂紝灏辨剰鍛崇潃閫氶亾寤虹珛浜嗭紝socket娴佸氨宸茬粡浜х敓浜嗐€傚彧瑕佽幏鍙栧埌socket娴佷腑鐨勮鍙栨祦鍜屽啓鍏ユ祦鍗冲彲锛屽彧瑕侀€氳繃getInputStream鍜実etOutputStream灏卞彲浠ヨ幏鍙栦袱涓祦瀵硅薄銆?br>3锛屽叧闂祫婧愩€?br>--------------------------------------------------------------
import java.net.*;
import java.io.*;
//闇€姹傦細瀹㈡埛绔粰鏈嶅姟鍣ㄧ鍙戦€佷竴涓暟鎹€?br>class TcpClient{
public static void main(String[] args) throws Exception{
Socket s = new Socket("10.1.31.69",10002);
OutputStream out = s.getOutputStream();//鑾峰彇浜唖ocket娴佷腑鐨勮緭鍑烘祦瀵硅薄銆?br> out.write("tcp婕旂ず锛屽摜浠張鏉ヤ簡!".getBytes());
s.close();
}
}
--------------------------------------------------------------
TCP鏈嶅姟绔細
1锛屽垱寤烘湇鍔$socket鏈嶅姟锛屽苟鐩戝惉涓€涓鍙c€?br>2锛屾湇鍔$涓轰簡缁欏鎴风鎻愪緵鏈嶅姟锛岃幏鍙栧鎴风鐨勫唴瀹癸紝鍙互閫氳繃accept鏂规硶鑾峰彇杩炴帴杩囨潵鐨勫鎴风瀵硅薄銆?br>3锛屽彲浠ラ€氳繃鑾峰彇鍒扮殑socket瀵硅薄涓殑socket娴佸拰鍏蜂綋鐨勫鎴风杩涜閫氳銆?br>4锛屽鏋滈€氳缁撴潫锛屽叧闂祫婧愩€傛敞鎰忥細瑕佸厛鍏冲鎴风锛屽啀鍏虫湇鍔$銆?br>--------------------------------------------------------------
class TcpServer{
public static void main(String[] args) throws Exception{
ServerSocket ss = new ServerSocket(10002);//寤虹珛鏈嶅姟绔殑socket鏈嶅姟
Socket s = ss.accept();//鑾峰彇瀹㈡埛绔璞?br> String ip = s.getInetAddress().getHostAddress();
System.out.println(ip+".....connected");
// 鍙互閫氳繃鑾峰彇鍒扮殑socket瀵硅薄涓殑socket娴佸拰鍏蜂綋鐨勫鎴风杩涜閫氳銆?br> InputStream in = s.getInputStream();//璇诲彇瀹㈡埛绔殑鏁版嵁锛屼娇鐢ㄥ鎴风瀵硅薄鐨剆ocket璇诲彇娴?br> byte[] buf = new byte[1024];
int len = in.read(buf);
String text = new String(buf,0,len);
System.out.println(text);
// 濡傛灉閫氳缁撴潫锛屽叧闂祫婧愩€傛敞鎰忥細瑕佸厛鍏冲鎴风锛屽湪鍏虫湇鍔$銆?br> s.close();
ss.close();
}
}
-----------------------------------------------------------------------------------------------
鍙嶅皠鎶€鏈細
鍏跺疄灏辨槸鍔ㄦ€佸姞杞戒竴涓寚瀹氱殑绫伙紝骞惰幏鍙栬绫讳腑鐨勬墍鏈夌殑鍐呭銆傝€屼笖灏嗗瓧鑺傜爜鏂囦欢灏佽鎴愬璞★紝骞跺皢瀛楄妭鐮佹枃浠朵腑鐨勫唴瀹归兘灏佽鎴愬璞★紝杩欐牱渚夸簬鎿嶄綔杩欎簺鎴愬憳銆傜畝鍗曡锛氬弽灏勬妧鏈彲浠ュ涓€涓被杩涜瑙e墫銆?br>
鍙嶅皠鐨勫ソ澶勶細澶уぇ鐨勫寮轰簡绋嬪簭鐨勬墿灞曟€с€?br>
鍙嶅皠鐨勫熀鏈楠わ細
1銆佽幏寰桟lass瀵硅薄锛屽氨鏄幏鍙栧埌鎸囧畾鐨勫悕绉扮殑瀛楄妭鐮佹枃浠跺璞°€?br>2銆佸疄渚嬪寲瀵硅薄锛岃幏寰楃被鐨勫睘鎬с€佹柟娉曟垨鏋勯€犲嚱鏁般€?br>3銆佽闂睘鎬с€佽皟鐢ㄦ柟娉曘€佽皟鐢ㄦ瀯閫犲嚱鏁板垱寤哄璞°€?br>
鑾峰彇杩欎釜Class瀵硅薄锛屾湁涓夌鏂瑰紡锛?br>1锛氶€氳繃姣忎釜瀵硅薄閮藉叿澶囩殑鏂规硶getClass鏉ヨ幏鍙栥€傚紛绔細蹇呴』瑕佸垱寤鸿绫诲璞★紝鎵嶅彲浠ヨ皟鐢╣etClass鏂规硶銆?br>2锛氭瘡涓€涓暟鎹被鍨?鍩烘湰鏁版嵁绫诲瀷鍜屽紩鐢ㄦ暟鎹被鍨?閮芥湁涓€涓潤鎬佺殑灞炴€lass銆傚紛绔細蹇呴』瑕佸厛鏄庣‘璇ョ被銆?br> 鍓嶄袱绉嶆柟寮忎笉鍒╀簬绋嬪簭鐨勬墿灞曪紝鍥犱负閮介渶瑕佸湪绋嬪簭浣跨敤鍏蜂綋鐨勭被鏉ュ畬鎴愩€?br>3锛氫娇鐢ㄧ殑Class绫讳腑鐨勬柟娉曪紝闈欐€佺殑forName鏂规硶銆?br> 鎸囧畾浠€涔堢被鍚嶏紝灏辫幏鍙栦粈涔堢被瀛楄妭鐮佹枃浠跺璞★紝杩欑鏂瑰紡鐨勬墿灞曟€ф渶寮猴紝鍙灏嗙被鍚嶇殑瀛楃涓蹭紶鍏ュ嵆鍙€?br>// 1. 鏍规嵁缁欏畾鐨勭被鍚嶆潵鑾峰緱 鐢ㄤ簬绫诲姞杞?br>String classname = "cn.itcast.reflect.Person";// 鏉ヨ嚜閰嶇疆鏂囦欢
Class clazz = Class.forName(classname);// 姝ゅ璞′唬琛≒erson.class
// 2. 濡傛灉鎷垮埌浜嗗璞★紝涓嶇煡閬撴槸浠€涔堢被鍨?nbsp; 鐢ㄤ簬鑾峰緱瀵硅薄鐨勭被鍨?br>Object obj = new Person();
Class clazz1 = obj.getClass();// 鑾峰緱瀵硅薄鍏蜂綋鐨勭被鍨?br>// 3. 濡傛灉鏄槑纭湴鑾峰緱鏌愪釜绫荤殑Class瀵硅薄 涓昏鐢ㄤ簬浼犲弬
Class clazz2 = Person.class;
鍙嶅皠鐨勭敤娉曪細
1锛夈€侀渶瑕佽幏寰梛ava绫荤殑鍚勪釜缁勬垚閮ㄥ垎锛岄鍏堥渶瑕佽幏寰楃被鐨凜lass瀵硅薄锛岃幏寰桟lass瀵硅薄鐨勪笁绉嶆柟寮忥細
Class.forName(classname) 鐢ㄤ簬鍋氱被鍔犺浇
obj.getClass() 鐢ㄤ簬鑾峰緱瀵硅薄鐨勭被鍨?br> 绫诲悕.class 鐢ㄤ簬鑾峰緱鎸囧畾鐨勭被鍨嬶紝浼犲弬鐢?br>
2)銆佸弽灏勭被鐨勬垚鍛樻柟娉曪細
Class clazz = Person.class;
Method method = clazz.getMethod(methodName, new Class[]{paramClazz1, paramClazz2});
method.invoke();
3)銆佸弽灏勭被鐨勬瀯閫犲嚱鏁帮細
Constructor con = clazz.getConstructor(new Class[]{paramClazz1, paramClazz2,...})
con.newInstance(params...)
4)銆佸弽灏勭被鐨勫睘鎬э細
Field field = clazz.getField(fieldName);
field.setAccessible(true);
field.setObject(value);
鑾峰彇浜嗗瓧鑺傜爜鏂囦欢瀵硅薄鍚庯紝鏈€缁堥兘闇€瑕佸垱寤烘寚瀹氱被鐨勫璞★細
鍒涘缓瀵硅薄鐨勪袱绉嶆柟寮?鍏跺疄灏辨槸瀵硅薄鍦ㄨ繘琛屽疄渚嬪寲鏃剁殑鍒濆鍖栨柟寮?锛?br>1锛岃皟鐢ㄧ┖鍙傛暟鐨勬瀯閫犲嚱鏁帮細浣跨敤浜咰lass绫讳腑鐨刵ewInstance()鏂规硶銆?br>2锛岃皟鐢ㄥ甫鍙傛暟鐨勬瀯閫犲嚱鏁帮細鍏堣鑾峰彇鎸囧畾鍙傛暟鍒楄〃鐨勬瀯閫犲嚱鏁板璞★紝鐒跺悗閫氳繃璇ユ瀯閫犲嚱鏁扮殑瀵硅薄鐨刵ewInstance(瀹為檯鍙傛暟) 杩涜瀵硅薄鐨勫垵濮嬪寲銆?br>
缁间笂鎵€杩帮紝绗簩绉嶆柟寮忥紝蹇呴』瑕佸厛鏄庣‘鍏蜂綋鐨勬瀯閫犲嚱鏁扮殑鍙傛暟绫诲瀷锛屼笉渚夸簬鎵╁睍銆傛墍浠ヤ竴鑸儏鍐典笅锛岃鍙嶅皠鐨勭被锛屽唴閮ㄩ€氬父閮戒細鎻愪緵涓€涓叕鏈夌殑绌哄弬鏁扮殑鏋勯€犲嚱鏁般€?br>------------------------------------------------------
// 濡備綍鐢熸垚鑾峰彇鍒板瓧鑺傜爜鏂囦欢瀵硅薄鐨勫疄渚嬪璞°€?br> Class clazz = Class.forName("cn.itcast.bean.Person");//绫诲姞杞?br>// 鐩存帴鑾峰緱鎸囧畾鐨勭被鍨?br> clazz = Person.class;
// 鏍规嵁瀵硅薄鑾峰緱绫诲瀷
Object obj = new Person("zhangsan", 19);
clazz = obj.getClass();
Object obj = clazz.newInstance();//璇ュ疄渚嬪寲瀵硅薄鐨勬柟娉曡皟鐢ㄥ氨鏄寚瀹氱被涓殑绌哄弬鏁版瀯閫犲嚱鏁帮紝缁欏垱寤哄璞¤繘琛屽垵濮嬪寲銆傚綋鎸囧畾绫讳腑娌℃湁绌哄弬鏁版瀯閫犲嚱鏁版椂锛岃濡備綍鍒涘缓璇ョ被瀵硅薄鍛紵璇风湅method_2();
public static void method_2() throws Exception {
Class clazz = Class.forName("cn.itcast.bean.Person");
//鏃㈢劧绫讳腑娌℃湁绌哄弬鏁扮殑鏋勯€犲嚱鏁?閭d箞鍙湁鑾峰彇鎸囧畾鍙傛暟鐨勬瀯閫犲嚱鏁?鐢ㄨ鍑芥暟鏉ヨ繘琛屽疄渚嬪寲銆?br> //鑾峰彇涓€涓甫鍙傛暟鐨勬瀯閫犲櫒銆?br> Constructor constructor = clazz.getConstructor(String.class,int.class);
//鎯宠瀵瑰璞¤繘琛屽垵濮嬪寲锛屼娇鐢ㄦ瀯閫犲櫒鐨勬柟娉昻ewInstance();
Object obj = constructor.newInstance("zhagnsan",30);
//鑾峰彇鎵€鏈夋瀯閫犲櫒銆?br> Constructor[] constructors = clazz.getConstructors();//鍙寘鍚叕鍏辩殑
constructors = clazz.getDeclaredConstructors();//鍖呭惈绉佹湁鐨?br> for(Constructor con : constructors) {
System.out.println(con);
}
}
------------------------------------------------------
鍙嶅皠鎸囧畾绫讳腑鐨勬柟娉曪細
//鑾峰彇绫讳腑鎵€鏈夌殑鏂规硶銆?br> public static void method_1() throws Exception {
Class clazz = Class.forName("cn.itcast.bean.Person");
Method[] methods = clazz.getMethods();//鑾峰彇鐨勬槸璇ョ被涓殑鍏湁鏂规硶鍜岀埗绫讳腑鐨勫叕鏈夋柟娉曘€?br> methods = clazz.getDeclaredMethods();//鑾峰彇鏈被涓殑鏂规硶锛屽寘鍚鏈夋柟娉曘€?br> for(Method method : methods) {
System.out.println(method);
}
}
//鑾峰彇鎸囧畾鏂规硶锛?br> public static void method_2() throws Exception {
Class clazz = Class.forName("cn.itcast.bean.Person");
//鑾峰彇鎸囧畾鍚嶇О鐨勬柟娉曘€?br> Method method = clazz.getMethod("show", int.class,String.class);
//鎯宠杩愯鎸囧畾鏂规硶锛屽綋鐒舵槸鏂规硶瀵硅薄鏈€娓呮锛屼负浜嗚鏂规硶杩愯锛岃皟鐢ㄦ柟娉曞璞$殑invoke鏂规硶鍗冲彲锛屼絾鏄柟娉曡繍琛屽繀椤昏鏄庣‘鎵€灞炵殑瀵硅薄鍜屽叿浣撶殑瀹為檯鍙傛暟銆?br> Object obj = clazz.newInstance();
method.invoke(obj, 39,"hehehe");//鎵ц涓€涓柟娉?br> }
//鎯宠杩愯绉佹湁鏂规硶銆?br> public static void method_3() throws Exception {
Class clazz = Class.forName("cn.itcast.bean.Person");
//鎯宠鑾峰彇绉佹湁鏂规硶銆傚繀椤荤敤getDeclearMethod();
Method method = clazz.getDeclaredMethod("method", null);
// 绉佹湁鏂规硶涓嶈兘鐩存帴璁块棶锛屽洜涓烘潈闄愪笉澶熴€傞潪瑕佽闂紝鍙互閫氳繃鏆村姏鐨勬柟寮忋€?br> method.setAccessible(true);//涓€鑸緢灏戠敤锛屽洜涓虹鏈夊氨鏄殣钘忚捣鏉ワ紝鎵€浠ュ敖閲忎笉瑕佽闂€?br> }
//鍙嶅皠闈欐€佹柟娉曘€?br> public static void method_4() throws Exception {
Class clazz = Class.forName("cn.itcast.bean.Person");
Method method = clazz.getMethod("function",null);
method.invoke(null,null);
}
------------------------------------------------------------------------------------------------
鐩存帴鎿嶄綔udp涓嶅彲鑳斤紝瀵逛簬java璇█搴旇灏唘dp灏佽鎴愬璞★紝鏄撲簬鎴戜滑鐨勪娇鐢紝杩欎釜瀵硅薄灏辨槸DatagramSocket. 灏佽浜唘dp浼犺緭鍗忚鐨剆ocket瀵硅薄銆?br>
鍥犱负鏁版嵁鍖呬腑鍖呭惈鐨勪俊鎭緝澶氾紝涓轰簡鎿嶄綔杩欎簺淇℃伅鏂逛究锛屼篃涓€鏍蜂細灏嗗叾灏佽鎴愬璞°€傝繖涓暟鎹寘瀵硅薄灏辨槸锛欴atagramPacket.閫氳繃杩欎釜瀵硅薄涓殑鏂规硶锛屽氨鍙互鑾峰彇鍒版暟鎹寘涓殑鍚勭淇℃伅銆?br>
DatagramSocket鍏峰鍙戦€佸拰鎺ュ彈鍔熻兘锛屽湪杩涜udp浼犺緭鏃讹紝闇€瑕佹槑纭竴涓槸鍙戦€佺锛屼竴涓槸鎺ユ敹绔€?br>
udp鐨勫彂閫佺锛?/h3>
1锛屽缓绔媢dp鐨剆ocket鏈嶅姟锛屽垱寤哄璞℃椂濡傛灉娌℃湁鏄庣‘绔彛锛岀郴缁熶細鑷姩鍒嗛厤涓€涓湭琚娇鐢ㄧ殑绔彛銆?br>2锛屾槑纭鍙戦€佺殑鍏蜂綋鏁版嵁銆?br>3锛屽皢鏁版嵁灏佽鎴愪簡鏁版嵁鍖呫€?br>4锛岀敤socket鏈嶅姟鐨剆end鏂规硶灏嗘暟鎹寘鍙戦€佸嚭鍘汇€?br>5锛屽叧闂祫婧愩€?br>--------------------------------------------------------------
import java.net.*;
class UdpSend{
public static void main(String[] args)throws Exception {
// 1锛屽缓绔媢dp鐨剆ocket鏈嶅姟銆?br> DatagramSocket ds = new DatagramSocket(8888);//鎸囧畾鍙戦€佺鍙o紝涓嶆寚瀹氱郴缁熶細闅忔満鍒嗛厤銆?br>// 2锛屾槑纭鍙戦€佺殑鍏蜂綋鏁版嵁銆?br> String text = "udp浼犺緭婕旂ず 鍝ヤ滑鏉ヤ簡";
byte[] buf = text.getBytes();
// 3锛屽皢鏁版嵁灏佽鎴愪簡鏁版嵁鍖呫€?br> DatagramPacket dp = new DatagramPacket(buf,
buf.length,InetAddress.getByName("10.1.31.127"),10000);
// 4锛岀敤socket鏈嶅姟鐨剆end鏂规硶灏嗘暟鎹寘鍙戦€佸嚭鍘汇€?br> ds.send(dp);
// 5锛屽叧闂祫婧愩€?br> ds.close();
}
}
-------------------------------------------------------------
udp鐨勬帴鏀剁锛?/h2>
1锛屽垱寤簎dp鐨剆ocket鏈嶅姟锛屽繀椤昏鏄庣‘涓€涓鍙o紝浣滅敤鍦ㄤ簬锛屽彧鏈夊彂閫佸埌杩欎釜绔彛鐨勬暟鎹墠鏄繖涓帴鏀剁鍙互澶勭悊鐨勬暟鎹€?br>2锛屽畾涔夋暟鎹寘锛岀敤浜庡瓨鍌ㄦ帴鏀跺埌鏁版嵁銆?br>3锛岄€氳繃socket鏈嶅姟鐨勬帴鏀舵柟娉曞皢鏀跺埌鐨勬暟鎹瓨鍌ㄥ埌鏁版嵁鍖呬腑銆?br>4锛岄€氳繃鏁版嵁鍖呯殑鏂规硶鑾峰彇鏁版嵁鍖呬腑鐨勫叿浣撴暟鎹唴瀹癸紝姣斿ip銆佺鍙c€佹暟鎹瓑绛夈€?br>5锛屽叧闂祫婧愩€?br>-------------------------------------------------------------
class UdpRece {
public static void main(String[] args) throws Exception{
// 1锛屽垱寤簎dp鐨剆ocket鏈嶅姟銆?br> DatagramSocket ds = new DatagramSocket(10000);
// 2锛屽畾涔夋暟鎹寘锛岀敤浜庡瓨鍌ㄦ帴鏀跺埌鏁版嵁銆傚厛瀹氫箟瀛楄妭鏁扮粍锛屾暟鎹寘浼氭妸鏁版嵁瀛樺偍鍒板瓧鑺傛暟缁勪腑銆?br> byte[] buf = new byte[1024];
DatagramPacket dp = new DatagramPacket(buf,buf.length);
// 3锛岄€氳繃socket鏈嶅姟鐨勬帴鏀舵柟娉曞皢鏀跺埌鐨勬暟鎹瓨鍌ㄥ埌鏁版嵁鍖呬腑銆?br> ds.receive(dp);//璇ユ柟娉曟槸闃诲寮忔柟娉曘€?br>// 4锛岄€氳繃鏁版嵁鍖呯殑鏂规硶鑾峰彇鏁版嵁鍖呬腑鐨勫叿浣撴暟鎹唴瀹癸紝姣斿ip锛岀鍙o紝鏁版嵁绛夌瓑銆?br> String ip = dp.getAddress().getHostAddress();
int port = dp.getPort();
String text = new String(dp.getData(),0,dp.getLength());//灏嗗瓧鑺傛暟缁勪腑鐨勬湁鏁堥儴鍒嗚浆鎴愬瓧绗︿覆銆?br> System.out.println(ip+":"+port+"--"+text);
// 5锛屽叧闂祫婧愩€?br> ds.close();
}
}
-------------------------------------------------------------
TCP浼犺緭锛氫袱涓鐐圭殑寤虹珛杩炴帴鍚庝細鏈変竴涓紶杈撴暟鎹殑閫氶亾锛岃繖閫氶亾绉颁负娴侊紝鑰屼笖鏄缓绔嬪湪缃戠粶鍩虹涓婄殑娴侊紝绉颁箣涓簊ocket娴併€傝娴佷腑鏃㈡湁璇诲彇锛屼篃鏈夊啓鍏ャ€?br>
tcp鐨勪袱涓鐐癸細涓€涓槸瀹㈡埛绔紝涓€涓槸鏈嶅姟绔€?br>瀹㈡埛绔細瀵瑰簲鐨勫璞★紝Socket
鏈嶅姟绔細瀵瑰簲鐨勫璞★紝ServerSocket
TCP瀹㈡埛绔細
1锛屽缓绔媡cp鐨剆ocket鏈嶅姟锛屾渶濂芥槑纭叿浣撶殑鍦板潃鍜岀鍙c€傝繖涓璞″湪鍒涘缓鏃讹紝灏卞凡缁忓彲浠ュ鎸囧畾ip鍜岀鍙h繘琛岃繛鎺?涓夋鎻℃墜)銆?br>2锛屽鏋滆繛鎺ユ垚鍔燂紝灏辨剰鍛崇潃閫氶亾寤虹珛浜嗭紝socket娴佸氨宸茬粡浜х敓浜嗐€傚彧瑕佽幏鍙栧埌socket娴佷腑鐨勮鍙栨祦鍜屽啓鍏ユ祦鍗冲彲锛屽彧瑕侀€氳繃getInputStream鍜実etOutputStream灏卞彲浠ヨ幏鍙栦袱涓祦瀵硅薄銆?br>3锛屽叧闂祫婧愩€?br>--------------------------------------------------------------
import java.net.*;
import java.io.*;
//闇€姹傦細瀹㈡埛绔粰鏈嶅姟鍣ㄧ鍙戦€佷竴涓暟鎹€?br>class TcpClient{
public static void main(String[] args) throws Exception{
Socket s = new Socket("10.1.31.69",10002);
OutputStream out = s.getOutputStream();//鑾峰彇浜唖ocket娴佷腑鐨勮緭鍑烘祦瀵硅薄銆?br> out.write("tcp婕旂ず锛屽摜浠張鏉ヤ簡!".getBytes());
s.close();
}
}
--------------------------------------------------------------
TCP鏈嶅姟绔細
1锛屽垱寤烘湇鍔$socket鏈嶅姟锛屽苟鐩戝惉涓€涓鍙c€?br>2锛屾湇鍔$涓轰簡缁欏鎴风鎻愪緵鏈嶅姟锛岃幏鍙栧鎴风鐨勫唴瀹癸紝鍙互閫氳繃accept鏂规硶鑾峰彇杩炴帴杩囨潵鐨勫鎴风瀵硅薄銆?br>3锛屽彲浠ラ€氳繃鑾峰彇鍒扮殑socket瀵硅薄涓殑socket娴佸拰鍏蜂綋鐨勫鎴风杩涜閫氳銆?br>4锛屽鏋滈€氳缁撴潫锛屽叧闂祫婧愩€傛敞鎰忥細瑕佸厛鍏冲鎴风锛屽啀鍏虫湇鍔$銆?br>--------------------------------------------------------------
class TcpServer{
public static void main(String[] args) throws Exception{
ServerSocket ss = new ServerSocket(10002);//寤虹珛鏈嶅姟绔殑socket鏈嶅姟
Socket s = ss.accept();//鑾峰彇瀹㈡埛绔璞?br> String ip = s.getInetAddress().getHostAddress();
System.out.println(ip+".....connected");
// 鍙互閫氳繃鑾峰彇鍒扮殑socket瀵硅薄涓殑socket娴佸拰鍏蜂綋鐨勫鎴风杩涜閫氳銆?br> InputStream in = s.getInputStream();//璇诲彇瀹㈡埛绔殑鏁版嵁锛屼娇鐢ㄥ鎴风瀵硅薄鐨剆ocket璇诲彇娴?br> byte[] buf = new byte[1024];
int len = in.read(buf);
String text = new String(buf,0,len);
System.out.println(text);
// 濡傛灉閫氳缁撴潫锛屽叧闂祫婧愩€傛敞鎰忥細瑕佸厛鍏冲鎴风锛屽湪鍏虫湇鍔$銆?br> s.close();
ss.close();
}
}
-----------------------------------------------------------------------------------------------
鍙嶅皠鎶€鏈細
鍏跺疄灏辨槸鍔ㄦ€佸姞杞戒竴涓寚瀹氱殑绫伙紝骞惰幏鍙栬绫讳腑鐨勬墍鏈夌殑鍐呭銆傝€屼笖灏嗗瓧鑺傜爜鏂囦欢灏佽鎴愬璞★紝骞跺皢瀛楄妭鐮佹枃浠朵腑鐨勫唴瀹归兘灏佽鎴愬璞★紝杩欐牱渚夸簬鎿嶄綔杩欎簺鎴愬憳銆傜畝鍗曡锛氬弽灏勬妧鏈彲浠ュ涓€涓被杩涜瑙e墫銆?br>
鍙嶅皠鐨勫ソ澶勶細澶уぇ鐨勫寮轰簡绋嬪簭鐨勬墿灞曟€с€?br>
鍙嶅皠鐨勫熀鏈楠わ細
1銆佽幏寰桟lass瀵硅薄锛屽氨鏄幏鍙栧埌鎸囧畾鐨勫悕绉扮殑瀛楄妭鐮佹枃浠跺璞°€?br>2銆佸疄渚嬪寲瀵硅薄锛岃幏寰楃被鐨勫睘鎬с€佹柟娉曟垨鏋勯€犲嚱鏁般€?br>3銆佽闂睘鎬с€佽皟鐢ㄦ柟娉曘€佽皟鐢ㄦ瀯閫犲嚱鏁板垱寤哄璞°€?br>
鑾峰彇杩欎釜Class瀵硅薄锛屾湁涓夌鏂瑰紡锛?br>1锛氶€氳繃姣忎釜瀵硅薄閮藉叿澶囩殑鏂规硶getClass鏉ヨ幏鍙栥€傚紛绔細蹇呴』瑕佸垱寤鸿绫诲璞★紝鎵嶅彲浠ヨ皟鐢╣etClass鏂规硶銆?br>2锛氭瘡涓€涓暟鎹被鍨?鍩烘湰鏁版嵁绫诲瀷鍜屽紩鐢ㄦ暟鎹被鍨?閮芥湁涓€涓潤鎬佺殑灞炴€lass銆傚紛绔細蹇呴』瑕佸厛鏄庣‘璇ョ被銆?br> 鍓嶄袱绉嶆柟寮忎笉鍒╀簬绋嬪簭鐨勬墿灞曪紝鍥犱负閮介渶瑕佸湪绋嬪簭浣跨敤鍏蜂綋鐨勭被鏉ュ畬鎴愩€?br>3锛氫娇鐢ㄧ殑Class绫讳腑鐨勬柟娉曪紝闈欐€佺殑forName鏂规硶銆?br> 鎸囧畾浠€涔堢被鍚嶏紝灏辫幏鍙栦粈涔堢被瀛楄妭鐮佹枃浠跺璞★紝杩欑鏂瑰紡鐨勬墿灞曟€ф渶寮猴紝鍙灏嗙被鍚嶇殑瀛楃涓蹭紶鍏ュ嵆鍙€?br>// 1. 鏍规嵁缁欏畾鐨勭被鍚嶆潵鑾峰緱 鐢ㄤ簬绫诲姞杞?br>String classname = "cn.itcast.reflect.Person";// 鏉ヨ嚜閰嶇疆鏂囦欢
Class clazz = Class.forName(classname);// 姝ゅ璞′唬琛≒erson.class
// 2. 濡傛灉鎷垮埌浜嗗璞★紝涓嶇煡閬撴槸浠€涔堢被鍨?nbsp; 鐢ㄤ簬鑾峰緱瀵硅薄鐨勭被鍨?br>Object obj = new Person();
Class clazz1 = obj.getClass();// 鑾峰緱瀵硅薄鍏蜂綋鐨勭被鍨?br>// 3. 濡傛灉鏄槑纭湴鑾峰緱鏌愪釜绫荤殑Class瀵硅薄 涓昏鐢ㄤ簬浼犲弬
Class clazz2 = Person.class;
鍙嶅皠鐨勭敤娉曪細
1锛夈€侀渶瑕佽幏寰梛ava绫荤殑鍚勪釜缁勬垚閮ㄥ垎锛岄鍏堥渶瑕佽幏寰楃被鐨凜lass瀵硅薄锛岃幏寰桟lass瀵硅薄鐨勪笁绉嶆柟寮忥細
Class.forName(classname) 鐢ㄤ簬鍋氱被鍔犺浇
obj.getClass() 鐢ㄤ簬鑾峰緱瀵硅薄鐨勭被鍨?br> 绫诲悕.class 鐢ㄤ簬鑾峰緱鎸囧畾鐨勭被鍨嬶紝浼犲弬鐢?br>
2)銆佸弽灏勭被鐨勬垚鍛樻柟娉曪細
Class clazz = Person.class;
Method method = clazz.getMethod(methodName, new Class[]{paramClazz1, paramClazz2});
method.invoke();
3)銆佸弽灏勭被鐨勬瀯閫犲嚱鏁帮細
Constructor con = clazz.getConstructor(new Class[]{paramClazz1, paramClazz2,...})
con.newInstance(params...)
4)銆佸弽灏勭被鐨勫睘鎬э細
Field field = clazz.getField(fieldName);
field.setAccessible(true);
field.setObject(value);
鑾峰彇浜嗗瓧鑺傜爜鏂囦欢瀵硅薄鍚庯紝鏈€缁堥兘闇€瑕佸垱寤烘寚瀹氱被鐨勫璞★細
鍒涘缓瀵硅薄鐨勪袱绉嶆柟寮?鍏跺疄灏辨槸瀵硅薄鍦ㄨ繘琛屽疄渚嬪寲鏃剁殑鍒濆鍖栨柟寮?锛?br>1锛岃皟鐢ㄧ┖鍙傛暟鐨勬瀯閫犲嚱鏁帮細浣跨敤浜咰lass绫讳腑鐨刵ewInstance()鏂规硶銆?br>2锛岃皟鐢ㄥ甫鍙傛暟鐨勬瀯閫犲嚱鏁帮細鍏堣鑾峰彇鎸囧畾鍙傛暟鍒楄〃鐨勬瀯閫犲嚱鏁板璞★紝鐒跺悗閫氳繃璇ユ瀯閫犲嚱鏁扮殑瀵硅薄鐨刵ewInstance(瀹為檯鍙傛暟) 杩涜瀵硅薄鐨勫垵濮嬪寲銆?br>
缁间笂鎵€杩帮紝绗簩绉嶆柟寮忥紝蹇呴』瑕佸厛鏄庣‘鍏蜂綋鐨勬瀯閫犲嚱鏁扮殑鍙傛暟绫诲瀷锛屼笉渚夸簬鎵╁睍銆傛墍浠ヤ竴鑸儏鍐典笅锛岃鍙嶅皠鐨勭被锛屽唴閮ㄩ€氬父閮戒細鎻愪緵涓€涓叕鏈夌殑绌哄弬鏁扮殑鏋勯€犲嚱鏁般€?br>------------------------------------------------------
// 濡備綍鐢熸垚鑾峰彇鍒板瓧鑺傜爜鏂囦欢瀵硅薄鐨勫疄渚嬪璞°€?br> Class clazz = Class.forName("cn.itcast.bean.Person");//绫诲姞杞?br>// 鐩存帴鑾峰緱鎸囧畾鐨勭被鍨?br> clazz = Person.class;
// 鏍规嵁瀵硅薄鑾峰緱绫诲瀷
Object obj = new Person("zhangsan", 19);
clazz = obj.getClass();
Object obj = clazz.newInstance();//璇ュ疄渚嬪寲瀵硅薄鐨勬柟娉曡皟鐢ㄥ氨鏄寚瀹氱被涓殑绌哄弬鏁版瀯閫犲嚱鏁帮紝缁欏垱寤哄璞¤繘琛屽垵濮嬪寲銆傚綋鎸囧畾绫讳腑娌℃湁绌哄弬鏁版瀯閫犲嚱鏁版椂锛岃濡備綍鍒涘缓璇ョ被瀵硅薄鍛紵璇风湅method_2();
public static void method_2() throws Exception {
Class clazz = Class.forName("cn.itcast.bean.Person");
//鏃㈢劧绫讳腑娌℃湁绌哄弬鏁扮殑鏋勯€犲嚱鏁?閭d箞鍙湁鑾峰彇鎸囧畾鍙傛暟鐨勬瀯閫犲嚱鏁?鐢ㄨ鍑芥暟鏉ヨ繘琛屽疄渚嬪寲銆?br> //鑾峰彇涓€涓甫鍙傛暟鐨勬瀯閫犲櫒銆?br> Constructor constructor = clazz.getConstructor(String.class,int.class);
//鎯宠瀵瑰璞¤繘琛屽垵濮嬪寲锛屼娇鐢ㄦ瀯閫犲櫒鐨勬柟娉昻ewInstance();
Object obj = constructor.newInstance("zhagnsan",30);
//鑾峰彇鎵€鏈夋瀯閫犲櫒銆?br> Constructor[] constructors = clazz.getConstructors();//鍙寘鍚叕鍏辩殑
constructors = clazz.getDeclaredConstructors();//鍖呭惈绉佹湁鐨?br> for(Constructor con : constructors) {
System.out.println(con);
}
}
------------------------------------------------------
鍙嶅皠鎸囧畾绫讳腑鐨勬柟娉曪細
//鑾峰彇绫讳腑鎵€鏈夌殑鏂规硶銆?br> public static void method_1() throws Exception {
Class clazz = Class.forName("cn.itcast.bean.Person");
Method[] methods = clazz.getMethods();//鑾峰彇鐨勬槸璇ョ被涓殑鍏湁鏂规硶鍜岀埗绫讳腑鐨勫叕鏈夋柟娉曘€?br> methods = clazz.getDeclaredMethods();//鑾峰彇鏈被涓殑鏂规硶锛屽寘鍚鏈夋柟娉曘€?br> for(Method method : methods) {
System.out.println(method);
}
}
//鑾峰彇鎸囧畾鏂规硶锛?br> public static void method_2() throws Exception {
Class clazz = Class.forName("cn.itcast.bean.Person");
//鑾峰彇鎸囧畾鍚嶇О鐨勬柟娉曘€?br> Method method = clazz.getMethod("show", int.class,String.class);
//鎯宠杩愯鎸囧畾鏂规硶锛屽綋鐒舵槸鏂规硶瀵硅薄鏈€娓呮锛屼负浜嗚鏂规硶杩愯锛岃皟鐢ㄦ柟娉曞璞$殑invoke鏂规硶鍗冲彲锛屼絾鏄柟娉曡繍琛屽繀椤昏鏄庣‘鎵€灞炵殑瀵硅薄鍜屽叿浣撶殑瀹為檯鍙傛暟銆?br> Object obj = clazz.newInstance();
method.invoke(obj, 39,"hehehe");//鎵ц涓€涓柟娉?br> }
//鎯宠杩愯绉佹湁鏂规硶銆?br> public static void method_3() throws Exception {
Class clazz = Class.forName("cn.itcast.bean.Person");
//鎯宠鑾峰彇绉佹湁鏂规硶銆傚繀椤荤敤getDeclearMethod();
Method method = clazz.getDeclaredMethod("method", null);
// 绉佹湁鏂规硶涓嶈兘鐩存帴璁块棶锛屽洜涓烘潈闄愪笉澶熴€傞潪瑕佽闂紝鍙互閫氳繃鏆村姏鐨勬柟寮忋€?br> method.setAccessible(true);//涓€鑸緢灏戠敤锛屽洜涓虹鏈夊氨鏄殣钘忚捣鏉ワ紝鎵€浠ュ敖閲忎笉瑕佽闂€?br> }
//鍙嶅皠闈欐€佹柟娉曘€?br> public static void method_4() throws Exception {
Class clazz = Class.forName("cn.itcast.bean.Person");
Method method = clazz.getMethod("function",null);
method.invoke(null,null);
}
------------------------------------------------------------------------------------------------
class UdpRece {
public static void main(String[] args) throws Exception{
// 1锛屽垱寤簎dp鐨剆ocket鏈嶅姟銆?br> DatagramSocket ds = new DatagramSocket(10000);
// 2锛屽畾涔夋暟鎹寘锛岀敤浜庡瓨鍌ㄦ帴鏀跺埌鏁版嵁銆傚厛瀹氫箟瀛楄妭鏁扮粍锛屾暟鎹寘浼氭妸鏁版嵁瀛樺偍鍒板瓧鑺傛暟缁勪腑銆?br> byte[] buf = new byte[1024];
DatagramPacket dp = new DatagramPacket(buf,buf.length);
// 3锛岄€氳繃socket鏈嶅姟鐨勬帴鏀舵柟娉曞皢鏀跺埌鐨勬暟鎹瓨鍌ㄥ埌鏁版嵁鍖呬腑銆?br> ds.receive(dp);//璇ユ柟娉曟槸闃诲寮忔柟娉曘€?br>// 4锛岄€氳繃鏁版嵁鍖呯殑鏂规硶鑾峰彇鏁版嵁鍖呬腑鐨勫叿浣撴暟鎹唴瀹癸紝姣斿ip锛岀鍙o紝鏁版嵁绛夌瓑銆?br> String ip = dp.getAddress().getHostAddress();
int port = dp.getPort();
String text = new String(dp.getData(),0,dp.getLength());//灏嗗瓧鑺傛暟缁勪腑鐨勬湁鏁堥儴鍒嗚浆鎴愬瓧绗︿覆銆?br> System.out.println(ip+":"+port+"--"+text);
// 5锛屽叧闂祫婧愩€?br> ds.close();
}
}
-------------------------------------------------------------
TCP浼犺緭锛氫袱涓鐐圭殑寤虹珛杩炴帴鍚庝細鏈変竴涓紶杈撴暟鎹殑閫氶亾锛岃繖閫氶亾绉颁负娴侊紝鑰屼笖鏄缓绔嬪湪缃戠粶鍩虹涓婄殑娴侊紝绉颁箣涓簊ocket娴併€傝娴佷腑鏃㈡湁璇诲彇锛屼篃鏈夊啓鍏ャ€?br>
tcp鐨勪袱涓鐐癸細涓€涓槸瀹㈡埛绔紝涓€涓槸鏈嶅姟绔€?br>瀹㈡埛绔細瀵瑰簲鐨勫璞★紝Socket
鏈嶅姟绔細瀵瑰簲鐨勫璞★紝ServerSocket
TCP瀹㈡埛绔細
1锛屽缓绔媡cp鐨剆ocket鏈嶅姟锛屾渶濂芥槑纭叿浣撶殑鍦板潃鍜岀鍙c€傝繖涓璞″湪鍒涘缓鏃讹紝灏卞凡缁忓彲浠ュ鎸囧畾ip鍜岀鍙h繘琛岃繛鎺?涓夋鎻℃墜)銆?br>2锛屽鏋滆繛鎺ユ垚鍔燂紝灏辨剰鍛崇潃閫氶亾寤虹珛浜嗭紝socket娴佸氨宸茬粡浜х敓浜嗐€傚彧瑕佽幏鍙栧埌socket娴佷腑鐨勮鍙栨祦鍜屽啓鍏ユ祦鍗冲彲锛屽彧瑕侀€氳繃getInputStream鍜実etOutputStream灏卞彲浠ヨ幏鍙栦袱涓祦瀵硅薄銆?br>3锛屽叧闂祫婧愩€?br>--------------------------------------------------------------import java.net.*;
import java.io.*;
//闇€姹傦細瀹㈡埛绔粰鏈嶅姟鍣ㄧ鍙戦€佷竴涓暟鎹€?br>class TcpClient{
public static void main(String[] args) throws Exception{
Socket s = new Socket("10.1.31.69",10002);
OutputStream out = s.getOutputStream();//鑾峰彇浜唖ocket娴佷腑鐨勮緭鍑烘祦瀵硅薄銆?br> out.write("tcp婕旂ず锛屽摜浠張鏉ヤ簡!".getBytes());
s.close();
}
}
--------------------------------------------------------------
TCP鏈嶅姟绔細
1锛屽垱寤烘湇鍔$socket鏈嶅姟锛屽苟鐩戝惉涓€涓鍙c€?br>2锛屾湇鍔$涓轰簡缁欏鎴风鎻愪緵鏈嶅姟锛岃幏鍙栧鎴风鐨勫唴瀹癸紝鍙互閫氳繃accept鏂规硶鑾峰彇杩炴帴杩囨潵鐨勫鎴风瀵硅薄銆?br>3锛屽彲浠ラ€氳繃鑾峰彇鍒扮殑socket瀵硅薄涓殑socket娴佸拰鍏蜂綋鐨勫鎴风杩涜閫氳銆?br>4锛屽鏋滈€氳缁撴潫锛屽叧闂祫婧愩€傛敞鎰忥細瑕佸厛鍏冲鎴风锛屽啀鍏虫湇鍔$銆?br>--------------------------------------------------------------class TcpServer{
public static void main(String[] args) throws Exception{
ServerSocket ss = new ServerSocket(10002);//寤虹珛鏈嶅姟绔殑socket鏈嶅姟
Socket s = ss.accept();//鑾峰彇瀹㈡埛绔璞?br> String ip = s.getInetAddress().getHostAddress();
System.out.println(ip+".....connected");
// 鍙互閫氳繃鑾峰彇鍒扮殑socket瀵硅薄涓殑socket娴佸拰鍏蜂綋鐨勫鎴风杩涜閫氳銆?br> InputStream in = s.getInputStream();//璇诲彇瀹㈡埛绔殑鏁版嵁锛屼娇鐢ㄥ鎴风瀵硅薄鐨剆ocket璇诲彇娴?br> byte[] buf = new byte[1024];
int len = in.read(buf);
String text = new String(buf,0,len);
System.out.println(text);
// 濡傛灉閫氳缁撴潫锛屽叧闂祫婧愩€傛敞鎰忥細瑕佸厛鍏冲鎴风锛屽湪鍏虫湇鍔$銆?br> s.close();
ss.close();
}
}
-----------------------------------------------------------------------------------------------
鍙嶅皠鎶€鏈細
鍏跺疄灏辨槸鍔ㄦ€佸姞杞戒竴涓寚瀹氱殑绫伙紝骞惰幏鍙栬绫讳腑鐨勬墍鏈夌殑鍐呭銆傝€屼笖灏嗗瓧鑺傜爜鏂囦欢灏佽鎴愬璞★紝骞跺皢瀛楄妭鐮佹枃浠朵腑鐨勫唴瀹归兘灏佽鎴愬璞★紝杩欐牱渚夸簬鎿嶄綔杩欎簺鎴愬憳銆傜畝鍗曡锛氬弽灏勬妧鏈彲浠ュ涓€涓被杩涜瑙e墫銆?br>鍙嶅皠鐨勫ソ澶勶細澶уぇ鐨勫寮轰簡绋嬪簭鐨勬墿灞曟€с€?br>
鍙嶅皠鐨勫熀鏈楠わ細
1銆佽幏寰桟lass瀵硅薄锛屽氨鏄幏鍙栧埌鎸囧畾鐨勫悕绉扮殑瀛楄妭鐮佹枃浠跺璞°€?br>2銆佸疄渚嬪寲瀵硅薄锛岃幏寰楃被鐨勫睘鎬с€佹柟娉曟垨鏋勯€犲嚱鏁般€?br>3銆佽闂睘鎬с€佽皟鐢ㄦ柟娉曘€佽皟鐢ㄦ瀯閫犲嚱鏁板垱寤哄璞°€?br>
鑾峰彇杩欎釜Class瀵硅薄锛屾湁涓夌鏂瑰紡锛?br>1锛氶€氳繃姣忎釜瀵硅薄閮藉叿澶囩殑鏂规硶getClass鏉ヨ幏鍙栥€傚紛绔細蹇呴』瑕佸垱寤鸿绫诲璞★紝鎵嶅彲浠ヨ皟鐢╣etClass鏂规硶銆?br>2锛氭瘡涓€涓暟鎹被鍨?鍩烘湰鏁版嵁绫诲瀷鍜屽紩鐢ㄦ暟鎹被鍨?閮芥湁涓€涓潤鎬佺殑灞炴€lass銆傚紛绔細蹇呴』瑕佸厛鏄庣‘璇ョ被銆?br> 鍓嶄袱绉嶆柟寮忎笉鍒╀簬绋嬪簭鐨勬墿灞曪紝鍥犱负閮介渶瑕佸湪绋嬪簭浣跨敤鍏蜂綋鐨勭被鏉ュ畬鎴愩€?br>3锛氫娇鐢ㄧ殑Class绫讳腑鐨勬柟娉曪紝闈欐€佺殑forName鏂规硶銆?br> 鎸囧畾浠€涔堢被鍚嶏紝灏辫幏鍙栦粈涔堢被瀛楄妭鐮佹枃浠跺璞★紝杩欑鏂瑰紡鐨勬墿灞曟€ф渶寮猴紝鍙灏嗙被鍚嶇殑瀛楃涓蹭紶鍏ュ嵆鍙€?br>// 1. 鏍规嵁缁欏畾鐨勭被鍚嶆潵鑾峰緱 鐢ㄤ簬绫诲姞杞?br>String classname = "cn.itcast.reflect.Person";// 鏉ヨ嚜閰嶇疆鏂囦欢
Class clazz = Class.forName(classname);// 姝ゅ璞′唬琛≒erson.class
// 2. 濡傛灉鎷垮埌浜嗗璞★紝涓嶇煡閬撴槸浠€涔堢被鍨?nbsp; 鐢ㄤ簬鑾峰緱瀵硅薄鐨勭被鍨?br>Object obj = new Person();
Class clazz1 = obj.getClass();// 鑾峰緱瀵硅薄鍏蜂綋鐨勭被鍨?br>// 3. 濡傛灉鏄槑纭湴鑾峰緱鏌愪釜绫荤殑Class瀵硅薄 涓昏鐢ㄤ簬浼犲弬
Class clazz2 = Person.class;
鍙嶅皠鐨勭敤娉曪細
1锛夈€侀渶瑕佽幏寰梛ava绫荤殑鍚勪釜缁勬垚閮ㄥ垎锛岄鍏堥渶瑕佽幏寰楃被鐨凜lass瀵硅薄锛岃幏寰桟lass瀵硅薄鐨勪笁绉嶆柟寮忥細Class.forName(classname) 鐢ㄤ簬鍋氱被鍔犺浇
obj.getClass() 鐢ㄤ簬鑾峰緱瀵硅薄鐨勭被鍨?br> 绫诲悕.class 鐢ㄤ簬鑾峰緱鎸囧畾鐨勭被鍨嬶紝浼犲弬鐢?br>
2)銆佸弽灏勭被鐨勬垚鍛樻柟娉曪細
Class clazz = Person.class;
Method method = clazz.getMethod(methodName, new Class[]{paramClazz1, paramClazz2});
method.invoke();
3)銆佸弽灏勭被鐨勬瀯閫犲嚱鏁帮細
Constructor con = clazz.getConstructor(new Class[]{paramClazz1, paramClazz2,...})
con.newInstance(params...)
4)銆佸弽灏勭被鐨勫睘鎬э細
Field field = clazz.getField(fieldName);
field.setAccessible(true);
field.setObject(value);
鑾峰彇浜嗗瓧鑺傜爜鏂囦欢瀵硅薄鍚庯紝鏈€缁堥兘闇€瑕佸垱寤烘寚瀹氱被鐨勫璞★細
鍒涘缓瀵硅薄鐨勪袱绉嶆柟寮?鍏跺疄灏辨槸瀵硅薄鍦ㄨ繘琛屽疄渚嬪寲鏃剁殑鍒濆鍖栨柟寮?锛?br>1锛岃皟鐢ㄧ┖鍙傛暟鐨勬瀯閫犲嚱鏁帮細浣跨敤浜咰lass绫讳腑鐨刵ewInstance()鏂规硶銆?br>2锛岃皟鐢ㄥ甫鍙傛暟鐨勬瀯閫犲嚱鏁帮細鍏堣鑾峰彇鎸囧畾鍙傛暟鍒楄〃鐨勬瀯閫犲嚱鏁板璞★紝鐒跺悗閫氳繃璇ユ瀯閫犲嚱鏁扮殑瀵硅薄鐨刵ewInstance(瀹為檯鍙傛暟) 杩涜瀵硅薄鐨勫垵濮嬪寲銆?br>
缁间笂鎵€杩帮紝绗簩绉嶆柟寮忥紝蹇呴』瑕佸厛鏄庣‘鍏蜂綋鐨勬瀯閫犲嚱鏁扮殑鍙傛暟绫诲瀷锛屼笉渚夸簬鎵╁睍銆傛墍浠ヤ竴鑸儏鍐典笅锛岃鍙嶅皠鐨勭被锛屽唴閮ㄩ€氬父閮戒細鎻愪緵涓€涓叕鏈夌殑绌哄弬鏁扮殑鏋勯€犲嚱鏁般€?br>------------------------------------------------------
// 濡備綍鐢熸垚鑾峰彇鍒板瓧鑺傜爜鏂囦欢瀵硅薄鐨勫疄渚嬪璞°€?br> Class clazz = Class.forName("cn.itcast.bean.Person");//绫诲姞杞?br>// 鐩存帴鑾峰緱鎸囧畾鐨勭被鍨?br> clazz = Person.class;
// 鏍规嵁瀵硅薄鑾峰緱绫诲瀷
Object obj = new Person("zhangsan", 19);
clazz = obj.getClass();
Object obj = clazz.newInstance();//璇ュ疄渚嬪寲瀵硅薄鐨勬柟娉曡皟鐢ㄥ氨鏄寚瀹氱被涓殑绌哄弬鏁版瀯閫犲嚱鏁帮紝缁欏垱寤哄璞¤繘琛屽垵濮嬪寲銆傚綋鎸囧畾绫讳腑娌℃湁绌哄弬鏁版瀯閫犲嚱鏁版椂锛岃濡備綍鍒涘缓璇ョ被瀵硅薄鍛紵璇风湅method_2();
public static void method_2() throws Exception {
Class clazz = Class.forName("cn.itcast.bean.Person");
//鏃㈢劧绫讳腑娌℃湁绌哄弬鏁扮殑鏋勯€犲嚱鏁?閭d箞鍙湁鑾峰彇鎸囧畾鍙傛暟鐨勬瀯閫犲嚱鏁?鐢ㄨ鍑芥暟鏉ヨ繘琛屽疄渚嬪寲銆?br> //鑾峰彇涓€涓甫鍙傛暟鐨勬瀯閫犲櫒銆?br> Constructor constructor = clazz.getConstructor(String.class,int.class);
//鎯宠瀵瑰璞¤繘琛屽垵濮嬪寲锛屼娇鐢ㄦ瀯閫犲櫒鐨勬柟娉昻ewInstance();
Object obj = constructor.newInstance("zhagnsan",30);
//鑾峰彇鎵€鏈夋瀯閫犲櫒銆?br> Constructor[] constructors = clazz.getConstructors();//鍙寘鍚叕鍏辩殑
constructors = clazz.getDeclaredConstructors();//鍖呭惈绉佹湁鐨?br> for(Constructor con : constructors) {
System.out.println(con);
}
}
------------------------------------------------------
鍙嶅皠鎸囧畾绫讳腑鐨勬柟娉曪細
//鑾峰彇绫讳腑鎵€鏈夌殑鏂规硶銆?br> public static void method_1() throws Exception {Class clazz = Class.forName("cn.itcast.bean.Person");
Method[] methods = clazz.getMethods();//鑾峰彇鐨勬槸璇ョ被涓殑鍏湁鏂规硶鍜岀埗绫讳腑鐨勫叕鏈夋柟娉曘€?br> methods = clazz.getDeclaredMethods();//鑾峰彇鏈被涓殑鏂规硶锛屽寘鍚鏈夋柟娉曘€?br> for(Method method : methods) {
System.out.println(method);
}
}
//鑾峰彇鎸囧畾鏂规硶锛?br> public static void method_2() throws Exception {
Class clazz = Class.forName("cn.itcast.bean.Person");
//鑾峰彇鎸囧畾鍚嶇О鐨勬柟娉曘€?br> Method method = clazz.getMethod("show", int.class,String.class);
//鎯宠杩愯鎸囧畾鏂规硶锛屽綋鐒舵槸鏂规硶瀵硅薄鏈€娓呮锛屼负浜嗚鏂规硶杩愯锛岃皟鐢ㄦ柟娉曞璞$殑invoke鏂规硶鍗冲彲锛屼絾鏄柟娉曡繍琛屽繀椤昏鏄庣‘鎵€灞炵殑瀵硅薄鍜屽叿浣撶殑瀹為檯鍙傛暟銆?br> Object obj = clazz.newInstance();
method.invoke(obj, 39,"hehehe");//鎵ц涓€涓柟娉?br> }
//鎯宠杩愯绉佹湁鏂规硶銆?br> public static void method_3() throws Exception {
Class clazz = Class.forName("cn.itcast.bean.Person");
//鎯宠鑾峰彇绉佹湁鏂规硶銆傚繀椤荤敤getDeclearMethod();
Method method = clazz.getDeclaredMethod("method", null);
// 绉佹湁鏂规硶涓嶈兘鐩存帴璁块棶锛屽洜涓烘潈闄愪笉澶熴€傞潪瑕佽闂紝鍙互閫氳繃鏆村姏鐨勬柟寮忋€?br> method.setAccessible(true);//涓€鑸緢灏戠敤锛屽洜涓虹鏈夊氨鏄殣钘忚捣鏉ワ紝鎵€浠ュ敖閲忎笉瑕佽闂€?br> }
//鍙嶅皠闈欐€佹柟娉曘€?br> public static void method_4() throws Exception {
Class clazz = Class.forName("cn.itcast.bean.Person");
Method method = clazz.getMethod("function",null);
method.invoke(null,null);
}
------------------------------------------------------------------------------------------------