1. Java鍩虹閮ㄥ垎
鍩虹閮ㄥ垎鐨勯『搴忥細鍩烘湰璇硶锛岀被鐩稿叧鐨勮娉曪紝鍐呴儴绫荤殑璇硶锛岀户鎵跨浉鍏崇殑璇硶锛屽紓甯哥殑璇硶锛岀嚎绋嬬殑璇硶锛岄泦鍚堢殑璇硶锛宨o鐨勮娉曪紝铏氭嫙鏈烘柟闈㈢殑璇硶銆�
1銆佷竴涓�".java"婧愭枃浠朵腑鏄惁鍙互鍖呮嫭澶氫釜绫伙紙涓嶆槸鍐呴儴绫伙級锛熸湁浠�涔堥檺鍒讹紵
鍙互鏈夊涓被锛屼絾鍙兘鏈変竴涓猵ublic鐨勭被锛屽苟涓攑ublic鐨勭被鍚嶅繀椤讳笌鏂囦欢鍚嶇浉涓�鑷淬��
2銆丣ava鏈夋病鏈塯oto?
java涓殑淇濈暀瀛楋紝鐜板湪娌℃湁鍦╦ava涓娇鐢ㄣ��
3銆佽璇�&鍜�&&鐨勫尯鍒��
&鍜�&&閮藉彲浠ョ敤浣滈�昏緫涓庣殑杩愮畻绗︼紝琛ㄧず閫昏緫涓庯紙and锛夛紝褰撹繍绠楃涓よ竟鐨勮〃杈惧紡鐨勭粨鏋滈兘涓簍rue鏃讹紝鏁翠釜杩愮畻缁撴灉鎵嶄负true锛屽惁鍒欙紝鍙鏈変竴鏂逛负false锛屽垯缁撴灉涓篺alse銆�
&&杩樺叿鏈夌煭璺殑鍔熻兘锛屽嵆濡傛灉绗竴涓〃杈惧紡涓篺alse锛屽垯涓嶅啀璁$畻绗簩涓〃杈惧紡锛屼緥濡傦紝瀵逛簬if(str != null&& !str.equals(鈥溾��))琛ㄨ揪寮忥紝褰搒tr涓簄ull鏃讹紝鍚庨潰鐨勮〃杈惧紡涓嶄細鎵ц锛屾墍浠ヤ笉浼氬嚭鐜癗ullPointerException濡傛灉灏�&&鏀逛负&锛屽垯浼氭姏鍑篘ullPointerException寮傚父銆侷f(x==33 &++y>0) y浼氬闀匡紝If(x==33 && ++y>0)涓嶄細澧為暱
&杩樺彲浠ョ敤浣滀綅杩愮畻绗︼紝褰�&鎿嶄綔绗︿袱杈圭殑琛ㄨ揪寮忎笉鏄痓oolean绫诲瀷鏃讹紝&琛ㄧず鎸変綅涓庢搷浣滐紝鎴戜滑閫氬父浣跨敤0x0f鏉ヤ笌涓�涓暣鏁拌繘琛�&杩愮畻锛屾潵鑾峰彇璇ユ暣鏁扮殑鏈�浣�4涓猙it浣嶏紝渚嬪锛�0x31 & 0x0f鐨勭粨鏋滀负0x01銆�
澶囨敞锛氳繖閬撻鍏堣涓よ�呯殑鍏卞悓鐐癸紝鍐嶈鍑�&&鍜�&鐨勭壒娈婁箣澶勶紝骞跺垪涓句竴浜涚粡鍏哥殑渚嬪瓙鏉ヨ〃鏄庤嚜宸辩悊瑙i�忓交娣卞叆銆佸疄闄呯粡楠屼赴瀵屻�� f
4銆佸湪JAVA涓浣曡烦鍑哄綋鍓嶇殑澶氶噸宓屽寰幆锛�
鍦↗ava涓紝瑕佹兂璺冲嚭澶氶噸寰幆锛屽彲浠ュ湪澶栭潰鐨勫惊鐜鍙ュ墠瀹氫箟涓�涓爣鍙凤紝鐒跺悗鍦ㄩ噷灞傚惊鐜綋鐨勪唬鐮佷腑浣跨敤甯︽湁鏍囧彿鐨刡reak璇彞锛屽嵆鍙烦鍑哄灞傚惊鐜�備緥濡傦紝
ok:
for(int i=0;i<10;i++) 聽 聽 {
for(int j=0;j<10;j++) 聽 聽 聽 聽 聽 聽{
System.out.println(鈥渋=鈥� + i + 鈥�,j=鈥� + j);
if(j == 5) break ok;
}
}
鍙﹀锛屾垜涓汉閫氬父骞朵笉浣跨敤鏍囧彿杩欑鏂瑰紡锛岃�屾槸璁╁灞傜殑寰幆鏉′欢琛ㄨ揪寮忕殑缁撴灉鍙互鍙楀埌閲屽眰寰幆浣撲唬鐮佺殑鎺у埗锛屼緥濡傦紝瑕佸湪浜岀淮鏁扮粍涓煡鎵惧埌鏌愪釜鏁板瓧銆�
int arr[][] ={{1,2,3},{4,5,6,7},{9}};
boolean found = false;
for(int i=0;i
for(int j=0;j
System.out.println(鈥渋=鈥� + i + 鈥�,j=鈥� + j);
if(arr[i][j] 聽==5) {
found = true;
break;
}
}
}
5銆乻witch璇彞鑳藉惁浣滅敤鍦╞yte涓婏紝鑳藉惁浣滅敤鍦╨ong涓婏紝鑳藉惁浣滅敤鍦⊿tring涓�?
鍦╯witch锛坋xpr1锛変腑锛宔xpr1鍙兘鏄竴涓暣鏁拌〃杈惧紡鎴栬�呮灇涓惧父閲忥紙鏇村ぇ瀛椾綋锛夛紝鏁存暟琛ㄨ揪寮忓彲浠ユ槸int鍩烘湰绫诲瀷鎴朓nteger鍖呰绫诲瀷锛岀敱浜庯紝byte,short,char閮藉彲浠ラ殣鍚浆鎹负int锛屾墍浠ワ紝杩欎簺绫诲瀷浠ュ強杩欎簺绫诲瀷鐨勫寘瑁呯被鍨嬩篃鏄彲浠ョ殑銆傛樉鐒讹紝long鍜孲tring绫诲瀷閮戒笉绗﹀悎switch鐨勮娉曡瀹氾紝骞朵笖涓嶈兘琚殣寮忚浆鎹㈡垚int绫诲瀷锛屾墍浠ワ紝瀹冧滑涓嶈兘浣滅敤浜巗wtich璇彞涓��
6銆乻hort s1 = 1; s1 = s1 + 1;鏈変粈涔堥敊? short s1 = 1; s1 += 1;鏈変粈涔堥敊?
瀵逛簬short s1 = 1; s1 = s1 + 1;鐢变簬s1+1杩愮畻鏃朵細鑷姩鎻愬崌琛ㄨ揪寮忕殑绫诲瀷锛屾墍浠ョ粨鏋滄槸int鍨嬶紝鍐嶈祴鍊肩粰short绫诲瀷s1鏃讹紝缂栬瘧鍣ㄥ皢鎶ュ憡闇�瑕佸己鍒惰浆鎹㈢被鍨嬬殑閿欒銆�
瀵逛簬short s1 = 1; s1 += 1;鐢变簬 +=鏄痡ava璇█瑙勫畾鐨勮繍绠楃锛宩ava缂栬瘧鍣ㄤ細瀵瑰畠杩涜鐗规畩澶勭悊锛屽洜姝ゅ彲浠ユ纭紪璇戙��
7銆乧har鍨嬪彉閲忎腑鑳戒笉鑳藉瓨璐竴涓腑鏂囨眽瀛�?涓轰粈涔�?
char鍨嬪彉閲忔槸鐢ㄦ潵瀛樺偍Unicode缂栫爜鐨勫瓧绗︾殑锛寀nicode缂栫爜瀛楃闆嗕腑鍖呭惈浜嗘眽瀛楋紝鎵�浠ワ紝char鍨嬪彉閲忎腑褰撶劧鍙互瀛樺偍姹夊瓧鍟︺�備笉杩囷紝濡傛灉鏌愪釜鐗规畩鐨勬眽瀛楁病鏈夎鍖呭惈鍦╱nicode缂栫爜瀛楃闆嗕腑锛岄偅涔堬紝杩欎釜char鍨嬪彉閲忎腑灏变笉鑳藉瓨鍌ㄨ繖涓壒娈婃眽瀛椼�傝ˉ鍏呰鏄庯細unicode缂栫爜鍗犵敤涓や釜瀛楄妭锛屾墍浠ワ紝char绫诲瀷鐨勫彉閲忎篃鏄崰鐢ㄤ袱涓瓧鑺傘��
澶囨敞锛氬悗闈竴閮ㄥ垎鍥炵瓟铏界劧涓嶆槸鍦ㄦ闈㈠洖绛旈鐩紝浣嗘槸锛屼负浜嗗睍鐜拌嚜宸辩殑瀛﹁瘑鍜岃〃鐜拌嚜宸卞闂鐞嗚В鐨勯�忓交娣卞叆锛屽彲浠ュ洖绛斾竴浜涚浉鍏崇殑鐭ヨ瘑锛屽仛鍒扮煡鏃犱笉瑷�锛岃█鏃犱笉灏姐��
8銆佺敤鏈�鏈夋晥鐜囩殑鏂规硶绠楀嚭2涔樹互8绛夋柤鍑�?
2 << 3锛�
鍥犱负灏嗕竴涓暟宸︾Щn浣嶏紝灏辩浉褰撲簬涔樹互浜�2鐨刵娆℃柟锛岄偅涔堬紝涓�涓暟涔樹互8鍙灏嗗叾宸︾Щ3浣嶅嵆鍙紝鑰屼綅杩愮畻cpu鐩存帴鏀寔鐨勶紝鏁堢巼鏈�楂橈紝鎵�浠ワ紝2涔樹互8绛夋柤鍑犵殑鏈�鏁堢巼鐨勬柟娉曟槸2 << 3銆�
9銆佽璁捐涓�涓竴鐧句嚎鐨勮绠楀櫒
棣栧厛瑕佹槑鐧借繖閬撻鐩殑鑰冩煡鐐规槸浠�涔堬紝涓�鏄ぇ瀹堕鍏堣瀵硅绠楁満鍘熺悊鐨勫簳灞傜粏鑺傝娓呮銆佽鐭ラ亾鍔犲噺娉曠殑浣嶈繍绠楀師鐞嗗拰鐭ラ亾璁$畻鏈轰腑鐨勭畻鏈繍绠椾細鍙戠敓瓒婄晫鐨勬儏鍐碉紝浜屾槸瑕佸叿澶囦竴瀹氱殑闈㈠悜瀵硅薄鐨勮璁℃�濇兂銆�
棣栧厛锛岃绠楁満涓敤鍥哄畾鏁伴噺鐨勫嚑涓瓧鑺傛潵瀛樺偍鐨勬暟鍊硷紝鎵�浠ヨ绠楁満涓兘澶熻〃绀虹殑鏁板�兼槸鏈変竴瀹氱殑鑼冨洿鐨勶紝涓轰簡渚夸簬璁茶В鍜岀悊瑙o紝鎴戜滑鍏堜互byte绫诲瀷鐨勬暣鏁颁负渚嬶紝瀹冪敤1涓瓧鑺傝繘琛屽瓨鍌紝琛ㄧず鐨勬渶澶ф暟鍊艰寖鍥翠负-128鍒�+127銆�-1鍦ㄥ唴瀛樹腑瀵瑰簲鐨勪簩杩涘埗鏁版嵁涓�11111111锛屽鏋滀袱涓�-1鐩稿姞锛屼笉鑰冭檻Java杩愮畻鏃剁殑绫诲瀷鎻愬崌锛岃繍绠楀悗浼氫骇鐢熻繘浣嶏紝浜岃繘鍒剁粨鏋滀负1,11111110锛岀敱浜庤繘浣嶅悗瓒呰繃浜哹yte绫诲瀷鐨勫瓨鍌ㄧ┖闂达紝鎵�浠ヨ繘浣嶉儴鍒嗚鑸嶅純锛屽嵆鏈�缁堢殑缁撴灉涓�11111110锛屼篃灏辨槸-2锛岃繖姝eソ鍒╃敤婧綅鐨勬柟寮忓疄鐜颁簡璐熸暟鐨勮繍绠椼��-128鍦ㄥ唴瀛樹腑瀵瑰簲鐨勪簩杩涘埗鏁版嵁涓�10000000锛屽鏋滀袱涓�-128鐩稿姞锛屼笉鑰冭檻Java杩愮畻鏃剁殑绫诲瀷鎻愬崌锛岃繍绠楀悗浼氫骇鐢熻繘浣嶏紝浜岃繘鍒剁粨鏋滀负1,00000000锛岀敱浜庤繘浣嶅悗瓒呰繃浜哹yte绫诲瀷鐨勫瓨鍌ㄧ┖闂达紝鎵�浠ヨ繘浣嶉儴鍒嗚鑸嶅純锛屽嵆鏈�缁堢殑缁撴灉涓�00000000锛屼篃灏辨槸0锛岃繖鏍风殑缁撴灉鏄剧劧涓嶆槸鎴戜滑鏈熸湜鐨勶紝杩欒鏄�璁$畻鏈轰腑鐨勭畻鏈繍绠楁槸浼氬彂鐢熻秺鐣屾儏鍐电殑锛屼袱涓暟鍊肩殑杩愮畻缁撴灉涓嶈兘瓒呰繃璁$畻鏈轰腑鐨勮绫诲瀷鐨勬暟鍊艰寖鍥淬��鐢变簬Java涓秹鍙婅〃杈惧紡杩愮畻鏃剁殑绫诲瀷鑷姩鎻愬崌锛屾垜浠棤娉曠敤byte绫诲瀷鏉ュ仛婕旂ず杩欑闂鍜岀幇璞$殑瀹為獙锛屽ぇ瀹跺彲浠ョ敤涓嬮潰涓�涓娇鐢ㄦ暣鏁板仛瀹為獙鐨勪緥瀛愮▼搴忎綋楠屼竴涓嬶細
int a = Integer.MAX_VALUE;
int b = Integer.MAX_VALUE;
int sum = a + b;
System.out.println(鈥渁=鈥�+a+鈥�,b=鈥�+b+鈥�,sum=鈥�+sum);
鍏堜笉鑰冭檻long绫诲瀷锛岀敱浜巌nt鐨勬鏁拌寖鍥翠负2鐨�31娆℃柟锛岃〃绀虹殑鏈�澶ф暟鍊肩害绛変簬2*1000*1000*1000锛屼篃灏辨槸20浜跨殑澶у皬锛屾墍浠ワ紝瑕佸疄鐜颁竴涓竴鐧句嚎鐨勮绠楀櫒锛屾垜浠緱鑷繁璁捐涓�涓被鍙互鐢ㄤ簬琛ㄧず寰堝ぇ鐨勬暣鏁帮紝骞朵笖鎻愪緵浜嗕笌鍙﹀涓�涓暣鏁拌繘琛屽姞鍑忎箻闄ょ殑鍔熻兘锛屽ぇ姒傚姛鑳藉涓嬶細
锛堬級杩欎釜绫诲唴閮ㄦ湁涓や釜鎴愬憳鍙橀噺锛屼竴涓〃绀虹鍙凤紝鍙︿竴涓敤瀛楄妭鏁扮粍琛ㄧず鏁板�肩殑浜岃繘鍒舵暟
锛堬級鏈変竴涓瀯閫犳柟娉曪紝鎶婁竴涓寘鍚湁澶氫綅鏁板�肩殑瀛楃涓茶浆鎹㈠埌鍐呴儴鐨勭鍙峰拰瀛楄妭鏁扮粍涓�
锛堬級鎻愪緵鍔犲噺涔橀櫎鐨勫姛鑳�
public class BigInteger{
int sign;
byte[] val;
public Biginteger(String val) {
sign = ;
val = ;
}
public BigInteger add(BigInteger other) 聽 聽 聽 {
}
public BigInteger subtract(BigInteger other) {
}
public BigInteger multiply(BigInteger other){
}
public BigInteger divide(BigInteger other){
}
}
澶囨敞锛氳鎯冲啓鍑鸿繖涓被鐨勫畬鏁翠唬鐮侊紝鏄潪甯稿鏉傜殑锛屽鏋滄湁鍏磋叮鐨勮瘽锛屽彲浠ュ弬鐪媕dk涓嚜甯︾殑java.math.BigInteger绫荤殑婧愮爜銆傞潰璇曠殑浜轰篃鐭ラ亾璋侀兘涓嶅彲鑳藉湪鐭椂闂村唴鍐欏嚭杩欎釜绫荤殑瀹屾暣浠g爜鐨勶紝浠栬鐨勬槸浣犳槸鍚︽湁杩欐柟闈㈢殑姒傚康鍜屾剰璇嗭紝浠栨渶閲嶈鐨勮繕鏄�冩煡浣犵殑鑳藉姏锛屾墍浠ワ紝浣犱笉瑕佸洜涓鸿嚜宸辨棤娉曞啓鍑哄畬鏁寸殑鏈�缁堢粨鏋滃氨鏀惧純绛旇繖閬撻锛屼綘瑕佸仛鐨勫氨鏄綘姣斿埆浜哄啓寰楀锛岃瘉鏄庝綘姣斿埆浜哄己锛屼綘鏈夎繖鏂归潰鐨勬�濇兂鎰忚瘑灏卞彲浠ヤ簡锛屾瘯绔熷埆浜哄彲鑳借繛棰樼洰鐨勬剰鎬濋兘鐪嬩笉鎳傦紝浠�涔堥兘娌″啓锛屼綘瑕佹暍浜庣瓟杩欓亾棰橈紝鍗充娇鍙瓟浜嗕竴閮ㄥ垎锛岄偅涔熶笌閭d簺浠�涔堥兘涓嶆噦鐨勪汉鍖哄埆鍑烘潵锛屾媺寮�浜嗚窛绂伙紝绠楁槸鐭瓙涓殑楂樹釜锛屾満浼氬綋鐒跺氨灞炰簬浣犱簡銆傚彟澶栵紝绛旀涓殑妗嗘灦浠g爜涔熷緢閲嶈锛屼綋鐜颁簡涓�浜涢潰鍚戝璞¤璁$殑鍔熷簳锛岀壒鍒槸鍏朵腑鐨勬柟娉曞懡鍚嶅緢涓撲笟锛岀敤鐨勮嫳鏂囧崟璇嶅緢绮惧噯锛岃繖涔熸槸鑳藉姏銆佺粡楠屻�佷笓涓氭�с�佽嫳璇按骞崇瓑澶氫釜鏂归潰鐨勪綋鐜帮紝浼氱粰浜虹暀涓嬪緢濂界殑鍗拌薄锛屽湪缂栫▼鑳藉姏鍜屽叾浠栨柟闈㈡潯浠跺樊涓嶅鐨勬儏鍐典笅锛岃嫳璇ソ闄や簡鍙互浣夸綘鑾峰緱鏇村鏈轰細澶栵紝钖按鍙互楂樺嚭涓�鍗冨厓銆�
10銆佷娇鐢╢inal鍏抽敭瀛椾慨楗颁竴涓彉閲忔椂锛屾槸寮曠敤涓嶈兘鍙橈紝杩樻槸寮曠敤鐨勫璞′笉鑳藉彉锛�
浣跨敤final鍏抽敭瀛椾慨楗颁竴涓彉閲忔椂锛屾槸鎸囧紩鐢ㄥ彉閲忎笉鑳藉彉锛屽紩鐢ㄥ彉閲忔墍鎸囧悜鐨勫璞′腑鐨勫唴瀹硅繕鏄彲浠ユ敼鍙樼殑銆備緥濡傦紝瀵逛簬濡備笅璇彞锛�
final StringBuffer a=new StringBuffer("immutable");
鎵ц濡備笅璇彞灏嗘姤鍛婄紪璇戞湡閿欒锛�
a=new StringBuffer("");
浣嗘槸锛屾墽琛屽涓嬭鍙ュ垯鍙互閫氳繃缂栬瘧锛�
a.append(" broken!");
鏈変汉鍦ㄥ畾涔夋柟娉曠殑鍙傛暟鏃讹紝鍙兘鎯抽噰鐢ㄥ涓嬪舰寮忔潵闃绘鏂规硶鍐呴儴淇敼浼犺繘鏉ョ殑鍙傛暟瀵硅薄锛�
public void method(final 聽StringBuffer param){
}
瀹為檯涓婏紝杩欐槸鍔炰笉鍒扮殑锛屽湪璇ユ柟娉曞唴閮ㄤ粛鐒跺彲浠ュ鍔犲涓嬩唬鐮佹潵淇敼鍙傛暟瀵硅薄锛�
param.append("a");
11銆�"=="鍜宔quals鏂规硶绌剁珶鏈変粈涔堝尯鍒紵
锛堝崟鐙妸涓�涓笢瑗胯娓呮锛岀劧鍚庡啀璇存竻妤氬彟涓�涓紝杩欐牱锛屽畠浠殑鍖哄埆鑷劧灏卞嚭鏉ヤ簡锛屾贩鍦ㄤ竴璧疯锛屽垯寰堥毦璇存竻妤氾級
==鎿嶄綔绗︿笓闂ㄧ敤鏉ユ瘮杈冧袱涓彉閲忕殑鍊兼槸鍚︾浉绛夛紝涔熷氨鏄敤浜庢瘮杈冨彉閲忔墍瀵瑰簲鐨勫唴瀛樹腑鎵�瀛樺偍鐨勬暟鍊兼槸鍚︾浉鍚岋紝瑕佹瘮杈冧袱涓熀鏈被鍨嬬殑鏁版嵁鎴栦袱涓紩鐢ㄥ彉閲忔槸鍚︾浉绛夛紝鍙兘鐢�==鎿嶄綔绗︺��
濡傛灉涓�涓彉閲忔寚鍚戠殑鏁版嵁鏄璞$被鍨嬬殑锛岄偅涔堬紝杩欐椂鍊欐秹鍙婁簡涓ゅ潡鍐呭瓨锛屽璞℃湰韬崰鐢ㄤ竴鍧楀唴瀛橈紙鍫嗗唴瀛橈級锛屽彉閲忎篃鍗犵敤涓�鍧楀唴瀛橈紝渚嬪Objet obj = new Object();鍙橀噺obj鏄竴涓唴瀛橈紝new Object()鏄彟涓�涓唴瀛橈紝姝ゆ椂锛屽彉閲弌bj鎵�瀵瑰簲鐨勫唴瀛樹腑瀛樺偍鐨勬暟鍊煎氨鏄璞″崰鐢ㄧ殑閭e潡鍐呭瓨鐨勯鍦板潃銆傚浜庢寚鍚戝璞$被鍨嬬殑鍙橀噺锛屽鏋滆姣旇緝涓や釜鍙橀噺鏄惁鎸囧悜鍚屼竴涓璞★紝鍗宠鐪嬭繖涓や釜鍙橀噺鎵�瀵瑰簲鐨勫唴瀛樹腑鐨勬暟鍊兼槸鍚︾浉绛夛紝杩欐椂鍊欏氨闇�瑕佺敤==鎿嶄綔绗﹁繘琛屾瘮杈冦��
equals鏂规硶鏄敤浜庢瘮杈冧袱涓嫭绔嬪璞$殑鍐呭鏄惁鐩稿悓锛屽氨濂芥瘮鍘绘瘮杈冧袱涓汉鐨勯暱鐩告槸鍚︾浉鍚岋紝瀹冩瘮杈冪殑涓や釜瀵硅薄鏄嫭绔嬬殑銆備緥濡傦紝瀵逛簬涓嬮潰鐨勪唬鐮侊細
String a=new String("foo");
String b=new String("foo");
涓ゆ潯new璇彞鍒涘缓浜嗕袱涓璞★紝鐒跺悗鐢╝/b杩欎袱涓彉閲忓垎鍒寚鍚戜簡鍏朵腑涓�涓璞★紝杩欐槸涓や釜涓嶅悓鐨勫璞★紝瀹冧滑鐨勯鍦板潃鏄笉鍚岀殑锛屽嵆a鍜宐涓瓨鍌ㄧ殑鏁板�兼槸涓嶇浉鍚岀殑锛屾墍浠ワ紝琛ㄨ揪寮廰==b灏嗚繑鍥瀎alse锛岃�岃繖涓や釜瀵硅薄涓殑鍐呭鏄浉鍚岀殑锛屾墍浠ワ紝琛ㄨ揪寮廰.equals(b)灏嗚繑鍥瀟rue銆�
鍦ㄥ疄闄呭紑鍙戜腑锛屾垜浠粡甯歌姣旇緝浼犻�掕繘琛屾潵鐨勫瓧绗︿覆鍐呭鏄惁绛夛紝渚嬪锛孲tring input = 鈥�;input.equals(鈥渜uit鈥�)锛岃澶氫汉绋嶄笉娉ㄦ剰灏变娇鐢�==杩涜姣旇緝浜嗭紝杩欐槸閿欒鐨勶紝闅忎究浠庣綉涓婃壘鍑犱釜椤圭洰瀹炴垬鐨勬暀瀛﹁棰戠湅鐪嬶紝閲岄潰灏辨湁澶ч噺杩欐牱鐨勯敊璇�傝浣忥紝瀛楃涓茬殑姣旇緝鍩烘湰涓婇兘鏄娇鐢╡quals鏂规硶銆�
濡傛灉涓�涓被娌℃湁鑷繁瀹氫箟equals鏂规硶锛岄偅涔堝畠灏嗙户鎵縊bject绫荤殑equals鏂规硶锛孫bject绫荤殑equals鏂规硶鐨勫疄鐜颁唬鐮佸涓嬶細
boolean equals(Object o){
return this==o;
}
杩欒鏄庯紝濡傛灉涓�涓被娌℃湁鑷繁瀹氫箟equals鏂规硶锛屽畠榛樿鐨別quals鏂规硶锛堜粠Object绫荤户鎵跨殑锛夊氨鏄娇鐢�==鎿嶄綔绗︼紝涔熸槸鍦ㄦ瘮杈冧袱涓彉閲忔寚鍚戠殑瀵硅薄鏄惁鏄悓涓�瀵硅薄锛岃繖鏃跺�欎娇鐢╡quals鍜屼娇鐢�==浼氬緱鍒板悓鏍风殑缁撴灉锛屽鏋滄瘮杈冪殑鏄袱涓嫭绔嬬殑瀵硅薄鍒欐�昏繑鍥瀎alse銆傚鏋滀綘缂栧啓鐨勭被甯屾湜鑳藉姣旇緝璇ョ被鍒涘缓鐨勪袱涓疄渚嬪璞$殑鍐呭鏄惁鐩稿悓锛岄偅涔堜綘蹇呴』瑕嗙洊equals鏂规硶锛岀敱浣犺嚜宸卞啓浠g爜鏉ュ喅瀹氬湪浠�涔堟儏鍐靛嵆鍙涓轰袱涓璞$殑鍐呭鏄浉鍚岀殑銆�
12銆侀潤鎬佸彉閲忓拰瀹炰緥鍙橀噺鐨勫尯鍒紵
鍦ㄨ娉曞畾涔変笂鐨勫尯鍒細闈欐�佸彉閲忓墠瑕佸姞static鍏抽敭瀛楋紝鑰屽疄渚嬪彉閲忓墠鍒欎笉鍔犮��
鍦ㄧ▼搴忚繍琛屾椂鐨勫尯鍒細瀹炰緥鍙橀噺灞炰簬鏌愪釜瀵硅薄鐨勫睘鎬э紝蹇呴』鍒涘缓浜嗗疄渚嬪璞★紝鍏朵腑鐨勫疄渚嬪彉閲忔墠浼氳鍒嗛厤绌洪棿锛屾墠鑳戒娇鐢ㄨ繖涓疄渚嬪彉閲忋�傞潤鎬佸彉閲忎笉灞炰簬鏌愪釜瀹炰緥瀵硅薄锛岃�屾槸灞炰簬绫伙紝鎵�浠ヤ篃绉颁负绫诲彉閲忥紝鍙绋嬪簭鍔犺浇浜嗙被鐨勫瓧鑺傜爜锛屼笉鐢ㄥ垱寤轰换浣曞疄渚嬪璞★紝闈欐�佸彉閲忓氨浼氳鍒嗛厤绌洪棿锛岄潤鎬佸彉閲忓氨鍙互琚娇鐢ㄤ簡銆傛�讳箣锛屽疄渚嬪彉閲忓繀椤诲垱寤哄璞″悗鎵嶅彲浠ラ�氳繃杩欎釜瀵硅薄鏉ヤ娇鐢紝闈欐�佸彉閲忓垯鍙互鐩存帴浣跨敤绫诲悕鏉ュ紩鐢ㄣ��
渚嬪锛屽浜庝笅闈㈢殑绋嬪簭锛屾棤璁哄垱寤哄灏戜釜瀹炰緥瀵硅薄锛屾案杩滈兘鍙垎閰嶄簡涓�涓猻taticVar鍙橀噺锛屽苟涓旀瘡鍒涘缓涓�涓疄渚嬪璞★紝杩欎釜staticVar灏变細鍔�1锛涗絾鏄紝姣忓垱寤轰竴涓疄渚嬪璞★紝灏变細鍒嗛厤涓�涓猧nstanceVar锛屽嵆鍙兘鍒嗛厤澶氫釜instanceVar锛屽苟涓旀瘡涓猧nstanceVar鐨勫�奸兘鍙嚜鍔犱簡1娆°��
public class VariantTest{
public static int staticVar = 0;
public int instanceVar = 0;
public VariantTest(){
staticVar++;
instanceVar++;
System.out.println(鈥渟taticVar=鈥� + staticVar + 鈥�,instanceVar=鈥�+ instanceVar);
}
}
澶囨敞锛氳繖涓В绛旈櫎浜嗚娓呮涓よ�呯殑鍖哄埆澶栵紝鏈�鍚庤繕鐢ㄤ竴涓叿浣撶殑搴旂敤渚嬪瓙鏉ヨ鏄庝袱鑰呯殑宸紓锛屼綋鐜颁簡鑷繁鏈夊緢濂界殑瑙h闂鍜岃璁℃渚嬬殑鑳藉姏锛屾�濈淮鏁忔嵎锛岃秴杩囦竴鑸▼搴忓憳锛屾湁鍐欎綔鑳藉姏锛�
13銆佹槸鍚﹀彲浠ヤ粠涓�涓猻tatic鏂规硶鍐呴儴鍙戝嚭瀵归潪static鏂规硶鐨勮皟鐢紵
涓嶅彲浠ャ�傚洜涓洪潪static鏂规硶鏄涓庡璞″叧鑱斿湪涓�璧风殑锛屽繀椤诲垱寤轰竴涓璞″悗锛屾墠鍙互鍦ㄨ瀵硅薄涓婅繘琛屾柟娉曡皟鐢紝鑰宻tatic鏂规硶璋冪敤鏃朵笉闇�瑕佸垱寤哄璞★紝鍙互鐩存帴璋冪敤銆備篃灏辨槸璇达紝褰撲竴涓猻tatic鏂规硶琚皟鐢ㄦ椂锛屽彲鑳借繕娌℃湁鍒涘缓浠讳綍瀹炰緥瀵硅薄锛屽鏋滀粠涓�涓猻tatic鏂规硶涓彂鍑哄闈瀞tatic鏂规硶鐨勮皟鐢紝閭d釜闈瀞tatic鏂规硶鏄叧鑱斿埌鍝釜瀵硅薄涓婄殑鍛紵杩欎釜閫昏緫鏃犳硶鎴愮珛锛屾墍浠ワ紝涓�涓猻tatic鏂规硶鍐呴儴鍙戝嚭瀵归潪static鏂规硶鐨勮皟鐢ㄣ��
14銆両nteger涓巌nt鐨勫尯鍒�
int鏄痡ava鎻愪緵鐨�8绉嶅師濮嬫暟鎹被鍨嬩箣涓�銆侸ava涓烘瘡涓師濮嬬被鍨嬫彁渚涗簡灏佽绫伙紝Integer鏄痡ava涓篿nt鎻愪緵鐨勫皝瑁呯被銆俰nt鐨勯粯璁ゅ�间负0锛岃�孖nteger鐨勯粯璁ゅ�间负null锛屽嵆Integer鍙互鍖哄垎鍑烘湭璧嬪�煎拰鍊间负0鐨勫尯鍒紝int鍒欐棤娉曡〃杈惧嚭鏈祴鍊肩殑鎯呭喌锛屼緥濡傦紝瑕佹兂琛ㄨ揪鍑烘病鏈夊弬鍔犺�冭瘯鍜岃�冭瘯鎴愮哗涓�0鐨勫尯鍒紝鍒欏彧鑳戒娇鐢↖nteger銆傚湪JSP寮�鍙戜腑锛孖nteger鐨勯粯璁や负null锛屾墍浠ョ敤el琛ㄨ揪寮忓湪鏂囨湰妗嗕腑鏄剧ず鏃讹紝鍊间负绌虹櫧瀛楃涓诧紝鑰宨nt榛樿鐨勯粯璁ゅ�间负0锛屾墍浠ョ敤el琛ㄨ揪寮忓湪鏂囨湰妗嗕腑鏄剧ず鏃讹紝缁撴灉涓�0锛屾墍浠ワ紝int涓嶉�傚悎浣滀负web灞傜殑琛ㄥ崟鏁版嵁鐨勭被鍨嬨��
鍦℉ibernate涓紝濡傛灉灏哋ID瀹氫箟涓篒nteger绫诲瀷锛岄偅涔圚ibernate灏卞彲浠ユ牴鎹叾鍊兼槸鍚︿负null鑰屽垽鏂竴涓璞℃槸鍚︽槸涓存椂鐨勶紝濡傛灉灏哋ID瀹氫箟涓轰簡int绫诲瀷锛岃繕闇�瑕佸湪hbm鏄犲皠鏂囦欢涓缃叾unsaved-value灞炴�т负0銆�
鍙﹀锛孖nteger鎻愪緵浜嗗涓笌鏁存暟鐩稿叧鐨勬搷浣滄柟娉曪紝渚嬪锛屽皢涓�涓瓧绗︿覆杞崲鎴愭暣鏁帮紝Integer涓繕瀹氫箟浜嗚〃绀烘暣鏁扮殑鏈�澶у�煎拰鏈�灏忓�肩殑甯搁噺銆�
15銆丮ath.round(11.5)绛夋柤澶氬皯? Math.round(-11.5)绛夋柤澶氬皯?
Math绫讳腑鎻愪緵浜嗕笁涓笌鍙栨暣鏈夊叧鐨勬柟娉曪細ceil銆乫loor銆乺ound锛岃繖浜涙柟娉曠殑浣滅敤涓庡畠浠殑鑻辨枃鍚嶇О鐨勫惈涔夌浉瀵瑰簲锛屼緥濡傦紝ceil鐨勮嫳鏂囨剰涔夋槸澶╄姳鏉匡紝璇ユ柟娉曞氨琛ㄧず鍚戜笂鍙栨暣锛孧ath.ceil(11.3)鐨勭粨鏋滀负12,Math.ceil(-11.3)鐨勭粨鏋滄槸-11锛沠loor鐨勮嫳鏂囨剰涔夋槸鍦版澘锛岃鏂规硶灏辫〃绀哄悜涓嬪彇鏁达紝Math.ceil(11.6)鐨勭粨鏋滀负11,Math.ceil(-11.6)鐨勭粨鏋滄槸-12锛涙渶闅炬帉鎻$殑鏄痳ound鏂规硶锛屽畠琛ㄧず鈥滃洓鑸嶄簲鍏モ�濓紝绠楁硶涓篗ath.floor(x+0.5)锛屽嵆灏嗗師鏉ョ殑鏁板瓧鍔犱笂0.5鍚庡啀鍚戜笅鍙栨暣锛屾墍浠ワ紝Math.round(11.5)鐨勭粨鏋滀负12锛孧ath.round(-11.5)鐨勭粨鏋滀负-11銆�
16銆佷笅闈㈢殑浠g爜鏈変粈涔堜笉濡ヤ箣澶�?
1. if(username.equals(鈥渮xx鈥�){}
username鍙兘涓篘ULL,浼氭姤绌烘寚閽堥敊璇紱鏀逛负"zxx".equals(username)
2.intx = 1;
return x==1?true:false; 聽杩欎釜鏀规垚return x==1;灏卞彲浠�!
17銆佽璇村嚭浣滅敤鍩焢ublic锛宲rivate锛宲rotected锛屼互鍙婁笉鍐欐椂鐨勫尯鍒�
杩欏洓涓綔鐢ㄥ煙鐨勫彲瑙佽寖鍥村涓嬭〃鎵�绀恒��
璇存槑锛氬鏋滃湪淇グ鐨勫厓绱犱笂闈㈡病鏈夊啓浠讳綍璁块棶淇グ绗︼紝鍒欒〃绀篺riendly銆�
浣滅敤鍩� 聽 聽褰撳墠绫诲悓涓�package瀛愬瓩绫诲叾浠杙ackage
public 聽 聽鈭� 聽 聽鈭� 聽 聽 聽 聽 鈭� 聽 聽 聽鈭�
protected 聽鈭� 聽 聽鈭� 聽 聽 聽 聽 鈭� 聽 聽 脳
friendly 聽 鈭� 聽 聽鈭� 聽 聽 聽 聽 脳 聽 聽 脳
private 聽 聽鈭� 聽 聽脳 聽 聽 聽 聽 脳 聽 聽 脳
澶囨敞锛氬彧瑕佽浣忎簡鏈�4绉嶈闂潈闄愶紝4涓闂寖鍥达紝鐒跺悗灏嗗叏閫夊拰鑼冨洿鍦ㄦ按骞冲拰鍨傜洿鏂瑰悜涓婂垎鍒寜鎺掍粠灏忓埌澶ф垨浠庡ぇ鍒板皬鐨勯『搴忔帓鍒楋紝灏卞緢瀹规槗鐢诲嚭涓婇潰鐨勫浘浜嗐��
18銆丱verload鍜孫verride鐨勫尯鍒�侽verloaded鐨勬柟娉曟槸鍚﹀彲浠ユ敼鍙樿繑鍥炲�肩殑绫诲瀷?
Overload鏄噸杞界殑鎰忔�濓紝Override鏄鐩栫殑鎰忔�濓紝涔熷氨鏄噸鍐欍��
閲嶈浇Overload琛ㄧず鍚屼竴涓被涓彲浠ユ湁澶氫釜鍚嶇О鐩稿悓鐨勬柟娉曪紝浣嗚繖浜涙柟娉曠殑鍙傛暟鍒楄〃鍚勪笉鐩稿悓锛堝嵆鍙傛暟涓暟鎴栫被鍨嬩笉鍚岋級銆�
閲嶅啓Override琛ㄧず瀛愮被涓殑鏂规硶鍙互涓庣埗绫讳腑鐨勬煇涓柟娉曠殑鍚嶇О鍜屽弬鏁板畬鍏ㄧ浉鍚岋紝閫氳繃瀛愮被鍒涘缓鐨勫疄渚嬪璞¤皟鐢ㄨ繖涓柟娉曟椂锛屽皢璋冪敤瀛愮被涓殑瀹氫箟鏂规硶锛岃繖鐩稿綋浜庢妸鐖剁被涓畾涔夌殑閭d釜瀹屽叏鐩稿悓鐨勬柟娉曠粰瑕嗙洊浜嗭紝杩欎篃鏄潰鍚戝璞$紪绋嬬殑澶氭�佹�х殑涓�绉嶈〃鐜般�傚瓙绫昏鐩栫埗绫荤殑鏂规硶鏃讹紝鍙兘姣旂埗绫绘姏鍑烘洿灏戠殑寮傚父锛屾垨鑰呮槸鎶涘嚭鐖剁被鎶涘嚭鐨勫紓甯哥殑瀛愬紓甯革紝鍥犱负瀛愮被鍙互瑙e喅鐖剁被鐨勪竴浜涢棶棰橈紝涓嶈兘姣旂埗绫绘湁鏇村鐨勯棶棰樸�傚瓙绫绘柟娉曠殑璁块棶鏉冮檺鍙兘姣旂埗绫荤殑鏇村ぇ锛屼笉鑳芥洿灏忋�傚鏋滅埗绫荤殑鏂规硶鏄痯rivate绫诲瀷锛岄偅涔堬紝瀛愮被鍒欎笉瀛樺湪瑕嗙洊鐨勯檺鍒讹紝鐩稿綋浜庡瓙绫讳腑澧炲姞浜嗕竴涓叏鏂扮殑鏂规硶銆�
鑷充簬Overloaded鐨勬柟娉曟槸鍚﹀彲浠ユ敼鍙樿繑鍥炲�肩殑绫诲瀷杩欎釜闂锛岃鐪嬩綘鍊掑簳鎯抽棶浠�涔堝憿锛熻繖涓鐩緢妯$硦銆傚鏋滃嚑涓狾verloaded鐨勬柟娉曠殑鍙傛暟鍒楄〃涓嶄竴鏍凤紝瀹冧滑鐨勮繑鍥炶�呯被鍨嬪綋鐒朵篃鍙互涓嶄竴鏍枫�備絾鎴戜及璁′綘鎯抽棶鐨勯棶棰樻槸锛氬鏋滀袱涓柟娉曠殑鍙傛暟鍒楄〃瀹屽叏涓�鏍凤紝鏄惁鍙互璁╁畠浠殑杩斿洖鍊间笉鍚屾潵瀹炵幇閲嶈浇Overload銆傝繖鏄笉琛岀殑锛屾垜浠彲浠ョ敤鍙嶈瘉娉曟潵璇存槑杩欎釜闂锛屽洜涓烘垜浠湁鏃跺�欒皟鐢ㄤ竴涓柟娉曟椂涔熷彲浠ヤ笉瀹氫箟杩斿洖缁撴灉鍙橀噺锛屽嵆涓嶈鍏冲績鍏惰繑鍥炵粨鏋滐紝渚嬪锛屾垜浠皟鐢╩ap.remove(key)鏂规硶鏃讹紝铏界劧remove鏂规硶鏈夎繑鍥炲�硷紝浣嗘槸鎴戜滑閫氬父閮戒笉浼氬畾涔夋帴鏀惰繑鍥炵粨鏋滅殑鍙橀噺锛岃繖鏃跺�欏亣璁捐绫讳腑鏈変袱涓悕绉板拰鍙傛暟鍒楄〃瀹屽叏鐩稿悓鐨勬柟娉曪紝浠呬粎鏄繑鍥炵被鍨嬩笉鍚岋紝java灏辨棤娉曠‘瀹氱紪绋嬭�呭�掑簳鏄兂璋冪敤鍝釜鏂规硶浜嗭紝鍥犱负瀹冩棤娉曢�氳繃杩斿洖缁撴灉绫诲瀷鏉ュ垽鏂��
override鍙互缈昏瘧涓鸿鐩栵紝浠庡瓧闈㈠氨鍙互鐭ラ亾锛屽畠鏄鐩栦簡涓�涓柟娉曞苟涓斿鍏堕噸鍐欙紝浠ユ眰杈惧埌涓嶅悓鐨勪綔鐢ㄣ�傚鎴戜滑鏉ヨ鏈�鐔熸倝鐨勮鐩栧氨鏄鎺ュ彛鏂规硶鐨勫疄鐜帮紝鍦ㄦ帴鍙d腑涓�鑸彧鏄鏂规硶杩涜浜嗗0鏄庯紝鑰屾垜浠湪瀹炵幇鏃讹紝灏遍渶瑕佸疄鐜版帴鍙e0鏄庣殑鎵�鏈夋柟娉曘�傞櫎浜嗚繖涓吀鍨嬬殑鐢ㄦ硶浠ュ锛屾垜浠湪缁ф壙涓篃鍙兘浼氬湪瀛愮被瑕嗙洊鐖剁被涓殑鏂规硶銆傚湪瑕嗙洊瑕佹敞鎰忎互涓嬬殑鍑犵偣锛�
1銆佽鐩栫殑鏂规硶鐨勬爣蹇楀繀椤昏鍜岃瑕嗙洊鐨勬柟娉曠殑鏍囧織瀹屽叏鍖归厤锛屾墠鑳借揪鍒拌鐩栫殑鏁堟灉锛�
2銆佽鐩栫殑鏂规硶鐨勮繑鍥炲�煎繀椤诲拰琚鐩栫殑鏂规硶鐨勮繑鍥炰竴鑷达紱
3銆佽鐩栫殑鏂规硶鎵�鎶涘嚭鐨勫紓甯稿繀椤诲拰琚鐩栨柟娉曠殑鎵�鎶涘嚭鐨勫紓甯镐竴鑷达紝鎴栬�呮槸鍏跺瓙绫伙紱
4銆佽瑕嗙洊鐨勬柟娉曚笉鑳戒负private锛屽惁鍒欏湪鍏跺瓙绫讳腑鍙槸鏂板畾涔変簡涓�涓柟娉曪紝骞舵病鏈夊鍏惰繘琛岃鐩栥��
overload瀵规垜浠潵璇村彲鑳芥瘮杈冪啛鎮夛紝鍙互缈昏瘧涓洪噸杞斤紝瀹冩槸鎸囨垜浠彲浠ュ畾涔変竴浜涘悕绉扮浉鍚岀殑鏂规硶锛岄�氳繃瀹氫箟涓嶅悓鐨勮緭鍏ュ弬鏁版潵鍖哄垎杩欎簺鏂规硶锛岀劧鍚庡啀璋冪敤鏃讹紝VM灏变細鏍规嵁涓嶅悓鐨勫弬鏁版牱寮忥紝鏉ラ�夋嫨鍚堥�傜殑鏂规硶鎵ц銆傚湪浣跨敤閲嶈浇瑕佹敞鎰忎互涓嬬殑鍑犵偣锛�
1銆佸湪浣跨敤閲嶈浇鏃跺彧鑳介�氳繃涓嶅悓鐨勫弬鏁版牱寮忋�備緥濡傦紝涓嶅悓鐨勫弬鏁扮被鍨嬶紝涓嶅悓鐨勫弬鏁颁釜鏁帮紝涓嶅悓鐨勫弬鏁伴『搴忥紙褰撶劧锛屽悓涓�鏂规硶鍐呯殑鍑犱釜鍙傛暟绫诲瀷蹇呴』涓嶄竴鏍凤紝渚嬪鍙互鏄痜un(int,float)锛屼絾鏄笉鑳戒负fun(int,int)锛夛紱
2銆佷笉鑳介�氳繃璁块棶鏉冮檺銆佽繑鍥炵被鍨嬨�佹姏鍑虹殑寮傚父杩涜閲嶈浇锛�
3銆佹柟娉曠殑寮傚父绫诲瀷鍜屾暟鐩笉浼氬閲嶈浇閫犳垚褰卞搷锛�
4銆佸浜庣户鎵挎潵璇达紝濡傛灉鏌愪竴鏂规硶鍦ㄧ埗绫讳腑鏄闂潈闄愭槸priavte锛岄偅涔堝氨涓嶈兘鍦ㄥ瓙绫诲鍏惰繘琛岄噸杞斤紝濡傛灉瀹氫箟鐨勮瘽锛屼篃鍙槸瀹氫箟浜嗕竴涓柊鏂规硶锛岃�屼笉浼氳揪鍒伴噸杞界殑鏁堟灉銆�
19銆佹瀯閫犲櫒Constructor鏄惁鍙override?
鏋勯�犲櫒Constructor涓嶈兘琚户鎵匡紝鍥犳涓嶈兘閲嶅啓Override锛屼絾鍙互琚噸杞絆verload銆�
20銆佹帴鍙f槸鍚﹀彲缁ф壙鎺ュ彛?鎶借薄绫绘槸鍚﹀彲瀹炵幇(implements)鎺ュ彛?鎶借薄绫绘槸鍚﹀彲缁ф壙鍏蜂綋绫�(concrete class)?鎶借薄绫讳腑鏄惁鍙互鏈夐潤鎬佺殑main鏂规硶锛�
鎺ュ彛鍙互缁ф壙鎺ュ彛銆傛娊璞$被鍙互瀹炵幇(implements)鎺ュ彛锛屾娊璞$被鍙互缁ф壙鍏蜂綋绫汇�傛娊璞$被涓彲浠ユ湁闈欐�佺殑main鏂规硶銆�
澶囨敞锛氬彧瑕佹槑鐧戒簡鎺ュ彛鍜屾娊璞$被鐨勬湰璐ㄥ拰浣滅敤锛岃繖浜涢棶棰橀兘寰堝ソ鍥炵瓟锛屼綘鎯虫兂锛屽鏋滀綘鏄痡ava璇█鐨勮璁¤�咃紝浣犳槸鍚︿細鎻愪緵杩欐牱鐨勬敮鎸侊紝濡傛灉涓嶆彁渚涚殑璇濓紝鏈変粈涔堢悊鐢卞悧锛熷鏋滀綘娌℃湁閬撶悊涓嶆彁渚涳紝閭g瓟妗堝氨鏄偗瀹氱殑浜嗐��
鍙湁璁颁綇鎶借薄绫讳笌鏅�氱被鐨勫敮涓�鍖哄埆灏辨槸涓嶈兘鍒涘缓瀹炰緥瀵硅薄鍜屽厑璁告湁abstract鏂规硶銆�
21銆佸啓clone()鏂规硶鏃讹紝閫氬父閮芥湁涓�琛屼唬鐮侊紝鏄粈涔堬紵
clone 鏈夌己鐪佽涓猴紝super.clone();鍥犱负棣栧厛瑕佹妸鐖剁被涓殑鎴愬憳澶嶅埗鍒颁綅锛岀劧鍚庢墠鏄鍒惰嚜宸辩殑鎴愬憳銆�
22銆侀潰鍚戝璞$殑鐗瑰緛鏈夊摢浜涙柟闈�
璁$畻鏈鸿蒋浠剁郴缁熸槸鐜板疄鐢熸椿涓殑涓氬姟鍦ㄨ绠楁満涓殑鏄犲皠锛岃�岀幇瀹炵敓娲讳腑鐨勪笟鍔″叾瀹炲氨鏄竴涓釜瀵硅薄鍗忎綔鐨勮繃绋嬨�傞潰鍚戝璞$紪绋嬪氨鏄寜鐜板疄涓氬姟涓�鏍风殑鏂瑰紡灏嗙▼搴忎唬鐮佹寜涓�涓釜瀵硅薄杩涜缁勭粐鍜岀紪鍐欙紝璁╄绠楁満绯荤粺鑳藉璇嗗埆鍜岀悊瑙g敤瀵硅薄鏂瑰紡缁勭粐鍜岀紪鍐欑殑绋嬪簭浠g爜锛岃繖鏍峰氨鍙互鎶婄幇瀹炵敓娲讳腑鐨勪笟鍔″璞℃槧灏勫埌璁$畻鏈虹郴缁熶腑銆�
闈㈠悜瀵硅薄鐨勭紪绋嬭瑷�鏈�4涓富瑕佺殑鐗瑰緛銆�
1灏佽锛�
灏佽鏄繚璇佽蒋浠堕儴浠跺叿鏈変紭鑹殑妯″潡鎬х殑鍩虹锛屽皝瑁呯殑鐩爣灏辨槸瑕佸疄鐜拌蒋浠堕儴浠剁殑鈥滈珮鍐呰仛銆佷綆鑰﹀悎鈥濓紝闃叉绋嬪簭鐩镐簰渚濊禆鎬ц�屽甫鏉ョ殑鍙樺姩褰卞搷銆傚湪闈㈠悜瀵硅薄鐨勭紪绋嬭瑷�涓紝瀵硅薄鏄皝瑁呯殑鏈�鍩烘湰鍗曚綅锛岄潰鍚戝璞$殑灏佽姣斾紶缁熻瑷�鐨勫皝瑁呮洿涓烘竻鏅般�佹洿涓烘湁鍔涖�傞潰鍚戝璞$殑灏佽灏辨槸鎶婃弿杩颁竴涓璞$殑灞炴�у拰琛屼负鐨勪唬鐮佸皝瑁呭湪涓�涓�滄ā鍧椻�濅腑锛屼篃灏辨槸涓�涓被涓紝灞炴�х敤鍙橀噺瀹氫箟锛岃涓虹敤鏂规硶杩涜瀹氫箟锛屾柟娉曞彲浠ョ洿鎺ヨ闂悓涓�涓璞′腑鐨勫睘鎬с�傞�氬父鎯呭喌涓嬶紝鍙璁颁綇璁╁彉閲忓拰璁块棶杩欎釜鍙橀噺鐨勬柟娉曟斁鍦ㄤ竴璧凤紝灏嗕竴涓被涓殑鎴愬憳鍙橀噺鍏ㄩ儴瀹氫箟鎴愮鏈夌殑锛屽彧鏈夎繖涓被鑷繁鐨勬柟娉曟墠鍙互璁块棶鍒拌繖浜涙垚鍛樺彉閲忥紝杩欏氨鍩烘湰涓婂疄鐜板璞$殑灏佽锛屽氨寰堝鏄撴壘鍑鸿鍒嗛厤鍒拌繖涓被涓婄殑鏂规硶浜嗭紝灏卞熀鏈笂绠楁槸浼氶潰鍚戝璞$殑缂栫▼浜嗐�傛妸鎻′竴涓師鍒欙細鎶婂鍚屼竴浜嬬墿杩涜鎿嶄綔鐨勬柟娉曞拰鐩稿叧鐨勬柟娉曟斁鍦ㄥ悓涓�涓被涓紝鎶婃柟娉曞拰瀹冩搷浣滅殑鏁版嵁鏀惧湪鍚屼竴涓被涓��
渚嬪锛屼汉瑕佸湪榛戞澘涓婄敾鍦嗭紝杩欎竴鍏辨秹鍙婁笁涓璞★細浜恒�侀粦鏉裤�佸渾锛岀敾鍦嗙殑鏂规硶瑕佸垎閰嶇粰鍝釜瀵硅薄鍛紵鐢变簬鐢诲渾闇�瑕佷娇鐢ㄥ埌鍦嗗績鍜屽崐寰勶紝鍦嗗績鍜屽崐寰勬樉鐒舵槸鍦嗙殑灞炴�э紝濡傛灉灏嗗畠浠湪绫讳腑瀹氫箟鎴愪簡绉佹湁鐨勬垚鍛樺彉閲忥紝閭d箞锛岀敾鍦嗙殑鏂规硶蹇呴』鍒嗛厤缁欏渾锛屽畠鎵嶈兘璁块棶鍒板渾蹇冨拰鍗婂緞杩欎袱涓睘鎬э紝浜轰互鍚庡彧鏄皟鐢ㄥ渾鐨勭敾鍦嗘柟娉曘�佽〃绀虹粰鍦嗗彂缁欐秷鎭�屽凡锛岀敾鍦嗚繖涓柟娉曚笉搴旇鍒嗛厤鍦ㄤ汉杩欎釜瀵硅薄涓婏紝杩欏氨鏄潰鍚戝璞$殑灏佽鎬э紝鍗冲皢瀵硅薄灏佽鎴愪竴涓珮搴﹁嚜娌诲拰鐩稿灏侀棴鐨勪釜浣擄紝瀵硅薄鐘舵�侊紙灞炴�э級鐢辫繖涓璞¤嚜宸辩殑琛屼负锛堟柟娉曪級鏉ヨ鍙栧拰鏀瑰彉銆�涓�涓洿渚夸簬鐞嗚В鐨勪緥瀛愬氨鏄紝鍙告満灏嗙伀杞﹀埞浣忎簡锛屽埞杞︾殑鍔ㄤ綔鏄垎閰嶇粰鍙告満锛岃繕鏄垎閰嶇粰鐏溅锛屾樉鐒讹紝搴旇鍒嗛厤缁欑伀杞︼紝鍥犱负鍙告満鑷韩鏄笉鍙兘鏈夐偅涔堝ぇ鐨勫姏姘斿皢涓�涓伀杞︾粰鍋滀笅鏉ョ殑锛屽彧鏈夌伀杞﹁嚜宸辨墠鑳藉畬鎴愯繖涓�鍔ㄤ綔锛岀伀杞﹂渶瑕佽皟鐢ㄥ唴閮ㄧ殑绂诲悎鍣ㄥ拰鍒硅溅鐗囩瓑澶氫釜鍣ㄤ欢鍗忎綔鎵嶈兘瀹屾垚鍒硅溅杩欎釜鍔ㄤ綔锛屽徃鏈哄埞杞︾殑杩囩▼鍙槸缁欑伀杞﹀彂浜嗕竴涓秷鎭紝閫氱煡鐏溅瑕佹墽琛屽埞杞﹀姩浣滆�屽凡銆�
鎶借薄锛�
鎶借薄灏辨槸鎵惧嚭涓�浜涗簨鐗╃殑鐩镐技鍜屽叡鎬т箣澶勶紝鐒跺悗灏嗚繖浜涗簨鐗╁綊涓轰竴涓被锛岃繖涓被鍙�冭檻杩欎簺浜嬬墿鐨勭浉浼煎拰鍏辨�т箣澶勶紝骞朵笖浼氬拷鐣ヤ笌褰撳墠涓婚鍜岀洰鏍囨棤鍏崇殑閭d簺鏂归潰锛屽皢娉ㄦ剰鍔涢泦涓湪涓庡綋鍓嶇洰鏍囨湁鍏崇殑鏂归潰銆備緥濡傦紝鐪嬪埌涓�鍙殏铓佸拰澶ц薄锛屼綘鑳藉鎯宠薄鍑哄畠浠殑鐩稿悓涔嬪锛岄偅灏辨槸鎶借薄銆傛娊璞″寘鎷涓烘娊璞″拰鐘舵�佹娊璞′袱涓柟闈€�備緥濡傦紝瀹氫箟涓�涓狿erson绫伙紝濡備笅锛�
Class Person{
String name;
int age;
}
浜烘湰鏉ユ槸寰堝鏉傜殑浜嬬墿锛屾湁寰堝鏂归潰锛屼絾鍥犱负褰撳墠绯荤粺鍙渶瑕佷簡瑙d汉鐨勫鍚嶅拰骞撮緞锛屾墍浠ヤ笂闈㈠畾涔夌殑绫讳腑鍙寘鍚鍚嶅拰骞撮緞杩欎袱涓睘鎬э紝杩欏氨鏄竴绉嶆娊鍍忥紝浣跨敤鎶借薄鍙互閬垮厤鑰冭檻涓�浜涗笌鐩爣鏃犲叧鐨勭粏鑺傘�傛垜瀵规娊璞$殑鐞嗚В灏辨槸涓嶈鐢ㄦ樉寰暅鍘荤湅涓�涓簨鐗╃殑鎵�鏈夋柟闈紝杩欐牱娑夊強鐨勫唴瀹瑰氨澶浜嗭紝鑰屾槸瑕佸杽浜庡垝鍒嗛棶棰樼殑杈圭晫锛屽綋鍓嶇郴缁熼渶瑕佷粈涔堬紝灏卞彧鑰冭檻浠�涔堛��
缁ф壙锛�
鍦ㄥ畾涔夊拰瀹炵幇涓�涓被鐨勬椂鍊欙紝鍙互鍦ㄤ竴涓凡缁忓瓨鍦ㄧ殑绫荤殑鍩虹涔嬩笂鏉ヨ繘琛岋紝鎶婅繖涓凡缁忓瓨鍦ㄧ殑绫绘墍瀹氫箟鐨勫唴瀹逛綔涓鸿嚜宸辩殑鍐呭锛屽苟鍙互鍔犲叆鑻ュ共鏂扮殑鍐呭锛屾垨淇敼鍘熸潵鐨勬柟娉曚娇涔嬫洿閫傚悎鐗规畩鐨勯渶瑕侊紝杩欏氨鏄户鎵裤�傜户鎵挎槸瀛愮被鑷姩鍏变韩鐖剁被鏁版嵁鍜屾柟娉曠殑鏈哄埗锛岃繖鏄被涔嬮棿鐨勪竴绉嶅叧绯伙紝鎻愰珮浜嗚蒋浠剁殑鍙噸鐢ㄦ�у拰鍙墿灞曟�с��
澶氭�侊細
澶氭�佹槸鎸囩▼搴忎腑瀹氫箟鐨勫紩鐢ㄥ彉閲忔墍鎸囧悜鐨勫叿浣撶被鍨嬪拰閫氳繃璇ュ紩鐢ㄥ彉閲忓彂鍑虹殑鏂规硶璋冪敤鍦ㄧ紪绋嬫椂骞朵笉纭畾锛岃�屾槸鍦ㄧ▼搴忚繍琛屾湡闂存墠纭畾锛屽嵆涓�涓紩鐢ㄥ彉閲忓�掑簳浼氭寚鍚戝摢涓被鐨勫疄渚嬪璞★紝璇ュ紩鐢ㄥ彉閲忓彂鍑虹殑鏂规硶璋冪敤鍒板簳鏄摢涓被涓疄鐜扮殑鏂规硶锛屽繀椤诲湪鐢辩▼搴忚繍琛屾湡闂存墠鑳藉喅瀹氥�傚洜涓哄湪绋嬪簭杩愯鏃舵墠纭畾鍏蜂綋鐨勭被锛岃繖鏍凤紝涓嶇敤淇敼婧愮▼搴忎唬鐮侊紝灏卞彲浠ヨ寮曠敤鍙橀噺缁戝畾鍒板悇绉嶄笉鍚岀殑绫诲疄鐜颁笂锛屼粠鑰屽鑷磋寮曠敤璋冪敤鐨勫叿浣撴柟娉曢殢涔嬫敼鍙橈紝鍗充笉淇敼绋嬪簭浠g爜灏卞彲浠ユ敼鍙樼▼搴忚繍琛屾椂鎵�缁戝畾鐨勫叿浣撲唬鐮侊紝璁╃▼搴忓彲浠ラ�夋嫨澶氫釜杩愯鐘舵�侊紝杩欏氨鏄鎬佹�с�傚鎬佹�у寮轰簡杞欢鐨勭伒娲绘�у拰鎵╁睍鎬с�備緥濡傦紝涓嬮潰浠g爜涓殑UserDao鏄竴涓帴鍙o紝瀹冨畾涔夊紩鐢ㄥ彉閲弖serDao鎸囧悜鐨勫疄渚嬪璞$敱daofactory.getDao()鍦ㄦ墽琛岀殑鏃跺�欒繑鍥烇紝鏈夋椂鍊欐寚鍚戠殑鏄疷serJdbcDao杩欎釜瀹炵幇锛屾湁鏃跺�欐寚鍚戠殑鏄疷serHibernateDao杩欎釜瀹炵幇锛岃繖鏍凤紝涓嶇敤淇敼婧愪唬鐮侊紝灏卞彲浠ユ敼鍙榰serDao鎸囧悜鐨勫叿浣撶被瀹炵幇锛屼粠鑰屽鑷磚serDao.insertUser()鏂规硶璋冪敤鐨勫叿浣撲唬鐮佷篃闅忎箣鏀瑰彉锛屽嵆鏈夋椂鍊欒皟鐢ㄧ殑鏄疷serJdbcDao鐨刬nsertUser鏂规硶锛屾湁鏃跺�欒皟鐢ㄧ殑鏄疷serHibernateDao鐨刬nsertUser鏂规硶锛�
UserDao userDao =daofactory.getDao();
userDao.insertUser(user);
姣斿柣锛氫汉鍚冮キ锛屼綘鐪嬪埌鐨勬槸宸︽墜锛岃繕鏄彸鎵嬶紵
23銆乯ava涓疄鐜板鎬佺殑鏈哄埗鏄粈涔堬紵
闈犵殑鏄埗绫绘垨鎺ュ彛瀹氫箟鐨勫紩鐢ㄥ彉閲忓彲浠ユ寚鍚戝瓙绫绘垨鍏蜂綋瀹炵幇绫荤殑瀹炰緥瀵硅薄锛岃�岀▼搴忚皟鐢ㄧ殑鏂规硶鍦ㄨ繍琛屾湡鎵嶅姩鎬佺粦瀹氾紝灏辨槸寮曠敤鍙橀噺鎵�鎸囧悜鐨勫叿浣撳疄渚嬪璞$殑鏂规硶锛屼篃灏辨槸鍐呭瓨閲屾鍦ㄨ繍琛岀殑閭d釜瀵硅薄鐨勬柟娉曪紝鑰屼笉鏄紩鐢ㄥ彉閲忕殑绫诲瀷涓畾涔夌殑鏂规硶銆�
24銆乤bstract class鍜宨nterface鏈変粈涔堝尯鍒�?
鍚湁abstract淇グ绗︾殑class鍗充负鎶借薄绫伙紝abstract绫讳笉鑳藉垱寤哄疄渚嬪璞°�傚惈鏈塧bstract鏂规硶鐨勭被蹇呴』瀹氫箟涓篴bstract class锛宎bstract class绫讳腑鐨勬柟娉曚笉蹇呮槸鎶借薄鐨勩�俛bstract class绫讳腑瀹氫箟鎶借薄鏂规硶蹇呴』鍦ㄥ叿浣�(Concrete)瀛愮被涓疄鐜帮紝鎵�浠ワ紝涓嶈兘鏈夋娊璞℃瀯閫犳柟娉曟垨鎶借薄闈欐�佹柟娉曘�傚鏋滅殑瀛愮被娌℃湁瀹炵幇鎶借薄鐖剁被涓殑鎵�鏈夋娊璞℃柟娉曪紝閭d箞瀛愮被涔熷繀椤诲畾涔変负abstract绫诲瀷銆�
鎺ュ彛锛坕nterface锛夊彲浠ヨ鎴愭槸鎶借薄绫荤殑涓�绉嶇壒渚嬶紝鎺ュ彛涓殑鎵�鏈夋柟娉曢兘蹇呴』鏄娊璞$殑銆傛帴鍙d腑鐨勬柟娉曞畾涔夐粯璁や负public abstract绫诲瀷锛屾帴鍙d腑鐨勬垚鍛樺彉閲忕被鍨嬮粯璁や负public static final銆�
涓嬮潰姣旇緝涓�涓嬩袱鑰呯殑璇硶鍖哄埆锛�
1.鎶借薄绫诲彲浠ユ湁鏋勯�犳柟娉曪紝鎺ュ彛涓笉鑳芥湁鏋勯�犳柟娉曘��
2.鎶借薄绫讳腑鍙互鏈夋櫘閫氭垚鍛樺彉閲忥紝鎺ュ彛涓病鏈夋櫘閫氭垚鍛樺彉閲�
3.鎶借薄绫讳腑鍙互鍖呭惈闈炴娊璞$殑鏅�氭柟娉曪紝鎺ュ彛涓殑鎵�鏈夋柟娉曞繀椤婚兘鏄娊璞$殑锛屼笉鑳芥湁闈炴娊璞$殑鏅�氭柟娉曘��
4.鎶借薄绫讳腑鐨勬娊璞℃柟娉曠殑璁块棶绫诲瀷鍙互鏄痯ublic锛宲rotected鍜岋紙榛樿绫诲瀷,铏界劧
eclipse涓嬩笉鎶ラ敊锛屼絾搴旇涔熶笉琛岋級锛屼絾鎺ュ彛涓殑鎶借薄鏂规硶鍙兘鏄痯ublic绫诲瀷鐨勶紝骞朵笖榛樿鍗充负public abstract绫诲瀷銆�
5.鎶借薄绫讳腑鍙互鍖呭惈闈欐�佹柟娉曪紝鎺ュ彛涓笉鑳藉寘鍚潤鎬佹柟娉�
6.鎶借薄绫诲拰鎺ュ彛涓兘鍙互鍖呭惈闈欐�佹垚鍛樺彉閲忥紝鎶借薄绫讳腑鐨勯潤鎬佹垚鍛樺彉閲忕殑璁块棶绫诲瀷鍙互浠绘剰锛屼絾鎺ュ彛涓畾涔夌殑鍙橀噺鍙兘鏄痯ublic static final绫诲瀷锛屽苟涓旈粯璁ゅ嵆涓簆ublic static final绫诲瀷銆�
7. 涓�涓被鍙互瀹炵幇澶氫釜鎺ュ彛锛屼絾鍙兘缁ф壙涓�涓娊璞$被銆�
涓嬮潰鎺ョ潃鍐嶈璇翠袱鑰呭湪搴旂敤涓婄殑鍖哄埆锛�
鎺ュ彛鏇村鐨勬槸鍦ㄧ郴缁熸灦鏋勮璁℃柟娉曞彂鎸ヤ綔鐢紝涓昏鐢ㄤ簬瀹氫箟妯″潡涔嬮棿鐨勯�氫俊濂戠害銆傝�屾娊璞$被鍦ㄤ唬鐮佸疄鐜版柟闈㈠彂鎸ヤ綔鐢紝鍙互瀹炵幇浠g爜鐨勯噸鐢紝渚嬪锛屾ā鏉挎柟娉曡璁℃ā寮忔槸鎶借薄绫荤殑涓�涓吀鍨嬪簲鐢紝鍋囪鏌愪釜椤圭洰鐨勬墍鏈塖ervlet绫婚兘瑕佺敤鐩稿悓鐨勬柟寮忚繘琛屾潈闄愬垽鏂�佽褰曡闂棩蹇楀拰澶勭悊寮傚父锛岄偅涔堝氨鍙互瀹氫箟涓�涓娊璞$殑鍩虹被锛岃鎵�鏈夌殑Servlet閮界户鎵胯繖涓娊璞″熀绫伙紝鍦ㄦ娊璞″熀绫荤殑service鏂规硶涓畬鎴愭潈闄愬垽鏂�佽褰曡闂棩蹇楀拰澶勭悊寮傚父鐨勪唬鐮侊紝鍦ㄥ悇涓瓙绫讳腑鍙槸瀹屾垚鍚勮嚜鐨勪笟鍔¢�昏緫浠g爜锛屼吉浠g爜濡備笅锛�
public abstract classBaseServlet extends HttpServlet{
public final void service(HttpServletRequest request,HttpServletResponse response) throws IOExcetion,ServletException 聽 聽 聽 {
璁板綍璁块棶鏃ュ織
杩涜鏉冮檺鍒ゆ柇
if(鍏锋湁鏉冮檺){
try{
doService(request,response);
}
catch(Excetpion e) 聽{
璁板綍寮傚父淇℃伅
}
}
}
protected abstract void doService(HttpServletRequest request,HttpServletResponse response) throws IOExcetion,ServletException;
//娉ㄦ剰璁块棶鏉冮檺瀹氫箟鎴恜rotected锛屾樉寰楁棦涓撲笟锛屽張涓ヨ皑锛屽洜涓哄畠鏄笓闂ㄧ粰瀛愮被鐢ㄧ殑
}
public class MyServlet1 extendsBaseServlet
{
protected voiddoService(HttpServletRequest request, HttpServletResponse response) throwsIOExcetion,ServletException
{
鏈琒ervlet鍙鐞嗙殑鍏蜂綋涓氬姟閫昏緫浠g爜
}
}
鐖剁被鏂规硶涓棿鐨勬煇娈典唬鐮佷笉纭畾锛岀暀缁欏瓙绫诲共锛屽氨鐢ㄦā鏉挎柟娉曡璁℃ā寮忋��
澶囨敞锛氳繖閬撻鐨勬�濊矾鏄厛浠庢�讳綋瑙i噴鎶借薄绫诲拰鎺ュ彛鐨勫熀鏈蹇碉紝鐒跺悗鍐嶆瘮杈冧袱鑰呯殑璇硶缁嗚妭锛屾渶鍚庡啀璇翠袱鑰呯殑搴旂敤鍖哄埆銆傛瘮杈冧袱鑰呰娉曠粏鑺傚尯鍒殑鏉$悊鏄細鍏堜粠涓�涓被涓殑鏋勯�犳柟娉曘�佹櫘閫氭垚鍛樺彉閲忓拰鏂规硶锛堝寘鎷娊璞℃柟娉曪級锛岄潤鎬佸彉閲忓拰鏂规硶锛岀户鎵挎�х瓑6涓柟闈㈤�愪竴鍘绘瘮杈冨洖绛旓紝鎺ョ潃浠庣涓夎�呯户鎵跨殑瑙掑害鐨勫洖绛旓紝鐗瑰埆鏄渶鍚庣敤浜嗕竴涓吀鍨嬬殑渚嬪瓙鏉ュ睍鐜拌嚜宸辨繁鍘氱殑鎶�鏈姛搴曘��
25銆乤bstract鐨刴ethod鏄惁鍙悓鏃舵槸static,鏄惁鍙悓鏃舵槸native锛屾槸鍚﹀彲鍚屾椂鏄痵ynchronized?
abstract鐨刴ethod涓嶅彲浠ユ槸static鐨勶紝鍥犱负鎶借薄鐨勬柟娉曟槸瑕佽瀛愮被瀹炵幇鐨勶紝鑰宻tatic涓庡瓙绫绘壇涓嶄笂鍏崇郴锛�
native鏂规硶琛ㄧず璇ユ柟娉曡鐢ㄥ彟澶栦竴绉嶄緷璧栧钩鍙扮殑缂栫▼璇█瀹炵幇鐨勶紝涓嶅瓨鍦ㄧ潃琚瓙绫诲疄鐜扮殑闂锛屾墍浠ワ紝瀹冧篃涓嶈兘鏄娊璞$殑锛屼笉鑳戒笌abstract娣风敤銆備緥濡傦紝FileOutputSteam绫昏纭欢鎵撲氦閬擄紝搴曞眰鐨勫疄鐜扮敤鐨勬槸鎿嶄綔绯荤粺鐩稿叧鐨刟pi瀹炵幇锛屼緥濡傦紝鍦╳indows鐢╟璇█瀹炵幇鐨勶紝鎵�浠ワ紝鏌ョ湅jdk鐨勬簮浠g爜锛屽彲浠ュ彂鐜癋ileOutputStream鐨刼pen鏂规硶鐨勫畾涔夊涓嬶細
private native void open(Stringname) throws FileNotFoundException;
濡傛灉鎴戜滑瑕佺敤java璋冪敤鍒汉鍐欑殑c璇█鍑芥暟锛屾垜浠槸鏃犳硶鐩存帴璋冪敤鐨勶紝鎴戜滑闇�瑕佹寜鐓ava鐨勮姹傚啓涓�涓猚璇█鐨勫嚱鏁帮紝鍙堟垜浠殑杩欎釜c璇█鍑芥暟鍘昏皟鐢ㄥ埆浜虹殑c璇█鍑芥暟銆傜敱浜庢垜浠殑c璇█鍑芥暟鏄寜java鐨勮姹傛潵鍐欑殑锛屾垜浠繖涓猚璇█鍑芥暟灏卞彲浠ヤ笌java瀵规帴涓婏紝java閭h竟鐨勫鎺ユ柟寮忓氨鏄畾涔夊嚭涓庢垜浠繖涓猚鍑芥暟鐩稿搴旂殑鏂规硶锛宩ava涓搴旂殑鏂规硶涓嶉渶瑕佸啓鍏蜂綋鐨勪唬鐮侊紝浣嗛渶瑕佸湪鍓嶉潰澹版槑native銆�
鍏充簬synchronized涓巃bstract鍚堢敤鐨勯棶棰橈紝鎴戣寰椾篃涓嶈锛屽洜涓哄湪鎴戝嚑骞寸殑瀛︿範鍜屽紑鍙戜腑锛屼粠鏉ユ病瑙佸埌杩囪繖绉嶆儏鍐碉紝骞朵笖鎴戣寰梥ynchronized搴旇鏄綔鐢ㄥ湪涓�涓叿浣撶殑鏂规硶涓婃墠鏈夋剰涔夈�傝�屼笖锛屾柟娉曚笂鐨剆ynchronized鍚屾鎵�浣跨敤鐨勫悓姝ラ攣瀵硅薄鏄痶his锛岃�屾娊璞℃柟娉曚笂鏃犳硶纭畾this鏄粈涔堛��
26銆佷粈涔堟槸鍐呴儴绫伙紵Static Nested Class鍜孖nner Class鐨勪笉鍚屻��
鍐呴儴绫诲氨鏄湪涓�涓被鐨勫唴閮ㄥ畾涔夌殑绫伙紝鍐呴儴绫讳腑涓嶈兘瀹氫箟闈欐�佹垚鍛橈紙闈欐�佹垚鍛樹笉鏄璞$殑鐗规�э紝鍙槸涓轰簡鎵句竴涓韬箣澶勶紝鎵�浠ラ渶瑕佹斁鍒颁竴涓被涓�屽凡锛岃繖涔堜竴鐐瑰皬浜嬶紝浣犺繕瑕佹妸瀹冩斁鍒扮被鍐呴儴鐨勪竴涓被涓紝杩囧垎浜嗗晩锛佹彁渚涘唴閮ㄧ被锛屼笉鏄负璁╀綘骞茶繖绉嶄簨鎯咃紝鏃犺亰锛屼笉璁╀綘骞层�傛垜鎯冲彲鑳芥槸鏃㈢劧闈欐�佹垚鍛樼被浼糲璇█鐨勫叏灞�鍙橀噺锛岃�屽唴閮ㄧ被閫氬父鏄敤浜庡垱寤哄唴閮ㄥ璞$敤鐨勶紝鎵�浠ワ紝鎶娾�滃叏灞�鍙橀噺鈥濇斁鍦ㄥ唴閮ㄧ被涓氨鏄鏃犳剰涔夌殑浜嬫儏锛屾棦鐒舵槸姣棤鎰忎箟鐨勪簨鎯咃紝灏卞簲璇ヨ绂佹锛夛紝鍐呴儴绫诲彲浠ョ洿鎺ヨ闂閮ㄧ被涓殑鎴愬憳鍙橀噺锛屽唴閮ㄧ被鍙互瀹氫箟鍦ㄥ閮ㄧ被鐨勬柟娉曞闈紝涔熷彲浠ュ畾涔夊湪澶栭儴绫荤殑鏂规硶浣撲腑锛屽涓嬫墍绀猴細
public class Outer
{
int out_x 聽= 0;
public void method()
{
Inner1 inner1 = new Inner1();
public class Inner2 聽//鍦ㄦ柟娉曚綋鍐呴儴瀹氫箟鐨勫唴閮ㄧ被
{
public method()
{
out_x = 3;
}
}
Inner2 inner2 = new Inner2();
}
public class Inner1 聽//鍦ㄦ柟娉曚綋澶栭潰瀹氫箟鐨勫唴閮ㄧ被
{
}
}
鍦ㄦ柟娉曚綋澶栭潰瀹氫箟鐨勫唴閮ㄧ被鐨勮闂被鍨嬪彲浠ユ槸public,protecte,榛樿鐨勶紝private绛�4绉嶇被鍨嬶紝杩欏氨濂藉儚绫讳腑瀹氫箟鐨勬垚鍛樺彉閲忔湁4绉嶈闂被鍨嬩竴鏍凤紝瀹冧滑鍐冲畾杩欎釜鍐呴儴绫荤殑瀹氫箟瀵瑰叾浠栫被鏄惁鍙锛涘浜庤繖绉嶆儏鍐碉紝鎴戜滑涔熷彲浠ュ湪澶栭潰鍒涘缓鍐呴儴绫荤殑瀹炰緥瀵硅薄锛屽垱寤哄唴閮ㄧ被鐨勫疄渚嬪璞℃椂锛屼竴瀹氳鍏堝垱寤哄閮ㄧ被鐨勫疄渚嬪璞★紝鐒跺悗鐢ㄨ繖涓閮ㄧ被鐨勫疄渚嬪璞″幓鍒涘缓鍐呴儴绫荤殑瀹炰緥瀵硅薄锛屼唬鐮佸涓嬶細
Outer outer = new Outer();
Outer.Inner1 inner1 = outer.new Innner1();
鍦ㄦ柟娉曞唴閮ㄥ畾涔夌殑鍐呴儴绫诲墠闈笉鑳芥湁璁块棶绫诲瀷淇グ绗︼紝灏卞ソ鍍忔柟娉曚腑瀹氫箟鐨勫眬閮ㄥ彉閲忎竴鏍凤紝浣嗚繖绉嶅唴閮ㄧ被鐨勫墠闈㈠彲浠ヤ娇鐢╢inal鎴朼bstract淇グ绗︺�傝繖绉嶅唴閮ㄧ被瀵瑰叾浠栫被鏄笉鍙鐨勫叾浠栫被鏃犳硶寮曠敤杩欑鍐呴儴绫伙紝浣嗘槸杩欑鍐呴儴绫诲垱寤虹殑瀹炰緥瀵硅薄鍙互浼犻�掔粰鍏朵粬绫昏闂�傝繖绉嶅唴閮ㄧ被蹇呴』鏄厛瀹氫箟锛屽悗浣跨敤锛屽嵆鍐呴儴绫荤殑瀹氫箟浠g爜蹇呴』鍑虹幇鍦ㄤ娇鐢ㄨ绫讳箣鍓嶏紝杩欎笌鏂规硶涓殑灞�閮ㄥ彉閲忓繀椤诲厛瀹氫箟鍚庝娇鐢ㄧ殑閬撶悊涔熸槸涓�鏍风殑銆傝繖绉嶅唴閮ㄧ被鍙互璁块棶鏂规硶浣撲腑鐨勫眬閮ㄥ彉閲忥紝浣嗘槸锛岃灞�閮ㄥ彉閲忓墠蹇呴』鍔爁inal淇グ绗︺��
瀵逛簬杩欎簺缁嗚妭锛屽彧瑕佸湪eclipse鍐欎唬鐮佽瘯璇曪紝鏍规嵁寮�鍙戝伐鍏锋彁绀虹殑鍚勭被閿欒淇℃伅灏卞彲浠ラ┈涓婁簡瑙e埌銆�
鍦ㄦ柟娉曚綋鍐呴儴杩樺彲浠ラ噰鐢ㄥ涓嬭娉曟潵鍒涘缓涓�绉嶅尶鍚嶅唴閮ㄧ被锛屽嵆瀹氫箟鏌愪竴鎺ュ彛鎴栫被鐨勫瓙绫荤殑鍚屾椂锛岃繕鍒涘缓浜嗚瀛愮被鐨勫疄渚嬪璞★紝鏃犻渶涓鸿瀛愮被瀹氫箟鍚嶇О锛�
public class Outer
{
public void start()
{
new Thread(
new Runable(){
public void run(){};
}
).start();
}
}
鏈�鍚庯紝鍦ㄦ柟娉曞閮ㄥ畾涔夌殑鍐呴儴绫诲墠闈㈠彲浠ュ姞涓妔tatic鍏抽敭瀛楋紝浠庤�屾垚涓篠tatic Nested Class锛屽畠涓嶅啀鍏锋湁鍐呴儴绫荤殑鐗规�э紝鎵�鏈夛紝浠庣嫮涔変笂璁诧紝瀹冧笉鏄唴閮ㄧ被銆係tatic Nested Class涓庢櫘閫氱被鍦ㄨ繍琛屾椂鐨勮涓哄拰鍔熻兘涓婃病鏈変粈涔堝尯鍒紝鍙槸鍦ㄧ紪绋嬪紩鐢ㄦ椂鐨勮娉曚笂鏈変竴浜涘樊鍒紝瀹冨彲浠ュ畾涔夋垚public銆乸rotected銆侀粯璁ょ殑銆乸rivate绛夊绉嶇被鍨嬶紝鑰屾櫘閫氱被鍙兘瀹氫箟鎴恜ublic鍜岄粯璁ょ殑杩欎袱绉嶇被鍨嬨�傚湪澶栭潰寮曠敤Static Nested Class绫荤殑鍚嶇О涓衡�滃閮ㄧ被鍚�.鍐呴儴绫诲悕鈥濄�傚湪澶栭潰涓嶉渶瑕佸垱寤哄閮ㄧ被鐨勫疄渚嬪璞★紝灏卞彲浠ョ洿鎺ュ垱寤篠tatic Nested Class锛屼緥濡傦紝鍋囪Inner鏄畾涔夊湪Outer绫讳腑鐨凷tatic Nested Class锛岄偅涔堝彲浠ヤ娇鐢ㄥ涓嬭鍙ュ垱寤篒nner绫伙細
Outer.Inner inner = new Outer.Inner();
鐢变簬static Nested Class涓嶄緷璧栦簬澶栭儴绫荤殑瀹炰緥瀵硅薄锛屾墍浠ワ紝static Nested Class鑳借闂閮ㄧ被鐨勯潪static鎴愬憳鍙橀噺(涓嶈兘鐩存帴璁块棶锛岄渶瑕佸垱寤哄閮ㄧ被瀹炰緥鎵嶈兘璁块棶闈為潤鎬佸彉閲�)銆�褰撳湪澶栭儴绫讳腑璁块棶Static Nested Class鏃讹紝鍙互鐩存帴浣跨敤Static Nested Class鐨勫悕瀛楋紝鑰屼笉闇�瑕佸姞涓婂閮ㄧ被鐨勫悕瀛椾簡锛屽湪Static Nested Class涓篃鍙互鐩存帴寮曠敤澶栭儴绫荤殑static鐨勬垚鍛樺彉閲忥紝涓嶉渶瑕佸姞涓婂閮ㄧ被鐨勫悕瀛椼��
鍦ㄩ潤鎬佹柟娉曚腑瀹氫箟鐨勫唴閮ㄧ被涔熸槸Static Nested Class锛岃繖鏃跺�欎笉鑳藉湪绫诲墠闈㈠姞static鍏抽敭瀛楋紝闈欐�佹柟娉曚腑鐨凷tatic Nested Class涓庢櫘閫氭柟娉曚腑鐨勫唴閮ㄧ被鐨勫簲鐢ㄦ柟寮忓緢鐩镐技锛屽畠闄や簡鍙互鐩存帴璁块棶澶栭儴绫讳腑鐨剆tatic鐨勬垚鍛樺彉閲忥紝杩樺彲浠ヨ闂潤鎬佹柟娉曚腑鐨勫眬閮ㄥ彉閲忥紝浣嗘槸锛岃灞�閮ㄥ彉閲忓墠蹇呴』鍔爁inal淇グ绗︺��
澶囨敞锛氶鍏堟牴鎹綘鐨勫嵃璞¤鍑轰綘瀵瑰唴閮ㄧ被鐨勬�讳綋鏂归潰鐨勭壒鐐癸細渚嬪锛屽湪涓や釜鍦版柟鍙互瀹氫箟锛屽彲浠ヨ闂閮ㄧ被鐨勬垚鍛樺彉閲忥紝涓嶈兘瀹氫箟闈欐�佹垚鍛橈紝杩欐槸澶х殑鐗圭偣銆傜劧鍚庡啀璇翠竴浜涚粏鑺傛柟闈㈢殑鐭ヨ瘑锛屼緥濡傦紝鍑犵瀹氫箟鏂瑰紡鐨勮娉曞尯鍒紝闈欐�佸唴閮ㄧ被锛屼互鍙婂尶鍚嶅唴閮ㄧ被銆�
27銆佸唴閮ㄧ被鍙互寮曠敤瀹冪殑鍖呭惈绫荤殑鎴愬憳鍚楋紵鏈夋病鏈変粈涔堥檺鍒讹紵
瀹屽叏鍙互銆傚鏋滀笉鏄潤鎬佸唴閮ㄧ被锛岄偅娌℃湁浠�涔堥檺鍒讹紒
濡傛灉浣犳妸闈欐�佸祵濂楃被褰撲綔鍐呴儴绫荤殑涓�绉嶇壒渚嬶紝閭e湪杩欑鎯呭喌涓嬩笉鍙互璁块棶澶栭儴绫荤殑鏅�氭垚鍛樺彉閲忥紝鑰屽彧鑳借闂閮ㄧ被涓殑闈欐�佹垚鍛橈紝渚嬪锛屼笅闈㈢殑浠g爜锛�
class Outer
{
static int x;
static class Inner
{
void test()
{
syso(x);
}
}
}
绛旈鏃讹紝涔熻鑳藉療瑷�瑙傝壊锛屾彛鎽╂彁闂�呯殑蹇冩�濓紝鏄剧劧浜哄甯屾湜浣犺鐨勬槸闈欐�佸唴閮ㄧ被涓嶈兘璁块棶澶栭儴绫荤殑鎴愬憳锛屼絾浣犱竴涓婃潵灏遍《鐗涳紝杩欎笉濂斤紝瑕佸厛椤虹潃浜哄锛岃浜哄婊℃剰锛岀劧鍚庡啀璇寸壒娈婃儏鍐碉紝璁╀汉瀹跺悆鎯娿��
28銆丄nonymous Inner Class (鍖垮悕鍐呴儴绫�)鏄惁鍙互extends(缁ф壙)鍏跺畠绫伙紝鏄惁鍙互implements(瀹炵幇)interface(鎺ュ彛)?
鍙互缁ф壙鍏朵粬绫绘垨瀹炵幇鍏朵粬鎺ュ彛銆備笉浠呮槸鍙互锛岃�屾槸蹇呴』!
29銆乻uper.getClass()鏂规硶璋冪敤
涓嬮潰绋嬪簭鐨勮緭鍑虹粨鏋滄槸澶氬皯锛�
Import java.util.Date;
publicclassTestextendsDate{
public static voidmain(String[] args) {
newTest().test();
}
public voidtest(){
System.out.println(super.getClass().getName());
}
}
寰堝鎬紝缁撴灉鏄疶est
杩欏睘浜庤剳绛嬫�ヨ浆寮殑棰樼洰锛屽湪涓�涓猶q缇ゆ湁涓綉鍙嬫濂介棶杩囪繖涓棶棰橈紝鎴戣寰楁尯鏈夎叮锛屽氨鐮旂┒浜嗕竴涓嬶紝娌℃兂鍒颁粖澶╄繕琚綘闈㈠埌浜嗭紝鍝堝搱銆�
鍦╰est鏂规硶涓紝鐩存帴璋冪敤getClass().getName()鏂规硶锛岃繑鍥炵殑鏄疶est绫诲悕
鐢变簬getClass()鍦∣bject绫讳腑瀹氫箟鎴愪簡final锛屽瓙绫讳笉鑳借鐩栬鏂规硶锛屾墍浠ワ紝鍦�
test鏂规硶涓皟鐢╣etClass().getName()鏂规硶锛屽叾瀹炲氨鏄湪璋冪敤浠庣埗绫荤户鎵跨殑getClass()鏂规硶锛岀瓑鏁堜簬璋冪敤super.getClass().getName()鏂规硶锛屾墍浠ワ紝super.getClass().getName()鏂规硶杩斿洖鐨勪篃搴旇鏄疶est銆�
濡傛灉鎯冲緱鍒扮埗绫荤殑鍚嶇О锛屽簲璇ョ敤濡備笅浠g爜锛�
getClass().getSuperClass().getName();
30銆丼tring鏄渶鍩烘湰鐨勬暟鎹被鍨嬪悧?
鍩烘湰鏁版嵁绫诲瀷鍖呮嫭byte銆乮nt銆乧har銆乴ong銆乫loat銆乨ouble銆乥oolean鍜宻hort銆�
java.lang.String绫绘槸final绫诲瀷鐨勶紝鍥犳涓嶅彲浠ョ户鎵胯繖涓被銆佷笉鑳戒慨鏀硅繖涓被銆備负浜嗘彁楂樻晥鐜囪妭鐪佺┖闂达紝鎴戜滑搴旇鐢⊿tringBuffer绫�
31銆丼tring s = "Hello";s = s + " world!";杩欎袱琛屼唬鐮佹墽琛屽悗锛屽師濮嬬殑String瀵硅薄涓殑鍐呭鍒板簳鍙樹簡娌℃湁锛�
娌℃湁銆傚洜涓篠tring琚璁℃垚涓嶅彲鍙�(immutable)绫伙紝鎵�浠ュ畠鐨勬墍鏈夊璞¢兘鏄笉鍙彉瀵硅薄銆傚湪杩欐浠g爜涓紝s鍘熷厛鎸囧悜涓�涓猄tring瀵硅薄锛屽唴瀹规槸 "Hello"锛岀劧鍚庢垜浠s杩涜浜�+鎿嶄綔锛岄偅涔坰鎵�鎸囧悜鐨勯偅涓璞℃槸鍚﹀彂鐢熶簡鏀瑰彉鍛紵绛旀鏄病鏈夈�傝繖鏃讹紝s涓嶆寚鍚戝師鏉ラ偅涓璞′簡锛岃�屾寚鍚戜簡鍙︿竴涓� String瀵硅薄锛屽唴瀹逛负"Hello world!"锛屽師鏉ラ偅涓璞¤繕瀛樺湪浜庡唴瀛樹箣涓紝鍙槸s杩欎釜寮曠敤鍙橀噺涓嶅啀鎸囧悜瀹冧簡銆�
閫氳繃涓婇潰鐨勮鏄庯紝鎴戜滑寰堝鏄撳鍑哄彟涓�涓粨璁猴紝濡傛灉缁忓父瀵瑰瓧绗︿覆杩涜鍚勭鍚勬牱鐨勪慨鏀癸紝鎴栬�呰锛屼笉鍙瑙佺殑淇敼锛岄偅涔堜娇鐢⊿tring鏉ヤ唬琛ㄥ瓧绗︿覆鐨勮瘽浼氬紩璧峰緢澶х殑鍐呭瓨寮�閿�銆傚洜涓� String瀵硅薄寤虹珛涔嬪悗涓嶈兘鍐嶆敼鍙橈紝鎵�浠ュ浜庢瘡涓�涓笉鍚岀殑瀛楃涓诧紝閮介渶瑕佷竴涓猄tring瀵硅薄鏉ヨ〃绀恒�傝繖鏃讹紝搴旇鑰冭檻浣跨敤StringBuffer绫伙紝瀹冨厑璁镐慨鏀癸紝鑰屼笉鏄瘡涓笉鍚岀殑瀛楃涓查兘瑕佺敓鎴愪竴涓柊鐨勫璞°�傚苟涓旓紝杩欎袱绉嶇被鐨勫璞¤浆鎹㈠崄鍒嗗鏄撱��
鍚屾椂锛屾垜浠繕鍙互鐭ラ亾锛屽鏋滆浣跨敤鍐呭鐩稿悓鐨勫瓧绗︿覆锛屼笉蹇呮瘡娆¢兘new涓�涓猄tring銆備緥濡傛垜浠鍦ㄦ瀯閫犲櫒涓涓�涓悕鍙玸鐨凷tring寮曠敤鍙橀噺杩涜鍒濆鍖栵紝鎶婂畠璁剧疆涓哄垵濮嬪�硷紝搴斿綋杩欐牱鍋氾細
public class Demo {
private String s;
...
public Demo {
s = "Initial Value";
}
...
}
鑰岄潪
s = new String("Initial Value");
鍚庤�呮瘡娆¢兘浼氳皟鐢ㄦ瀯閫犲櫒锛岀敓鎴愭柊瀵硅薄锛屾�ц兘浣庝笅涓斿唴瀛樺紑閿�澶э紝骞朵笖娌℃湁鎰忎箟锛屽洜涓篠tring瀵硅薄涓嶅彲鏀瑰彉锛屾墍浠ュ浜庡唴瀹圭浉鍚岀殑瀛楃涓诧紝鍙涓�涓猄tring瀵硅薄鏉ヨ〃绀哄氨鍙互浜嗐�備篃灏辫锛屽娆¤皟鐢ㄤ笂闈㈢殑鏋勯�犲櫒鍒涘缓澶氫釜瀵硅薄锛屼粬浠殑String绫诲瀷灞炴�閮芥寚鍚戝悓涓�涓璞°��
涓婇潰鐨勭粨璁鸿繕鍩轰簬杩欐牱涓�涓簨瀹烇細瀵逛簬瀛楃涓插父閲忥紝濡傛灉鍐呭鐩稿悓锛孞ava璁や负瀹冧滑浠h〃鍚屼竴涓猄tring瀵硅薄銆傝�岀敤鍏抽敭瀛梟ew璋冪敤鏋勯�犲櫒锛屾�绘槸浼氬垱寤轰竴涓柊鐨勫璞★紝鏃犺鍐呭鏄惁鐩稿悓銆�
鑷充簬涓轰粈涔堣鎶奡tring绫昏璁℃垚涓嶅彲鍙樼被锛屾槸瀹冪殑鐢ㄩ�斿喅瀹氱殑銆傚叾瀹炰笉鍙猄tring锛屽緢澶欽ava鏍囧噯绫诲簱涓殑绫婚兘鏄笉鍙彉鐨勩�傚湪寮�鍙戜竴涓郴缁熺殑鏃跺�欙紝鎴戜滑鏈夋椂鍊欎篃闇�瑕佽璁′笉鍙彉绫伙紝鏉ヤ紶閫掍竴缁勭浉鍏崇殑鍊硷紝杩欎篃鏄潰鍚戝璞℃�濇兂鐨勪綋鐜般�備笉鍙彉绫绘湁涓�浜涗紭鐐癸紝姣斿鍥犱负瀹冪殑瀵硅薄鏄彧璇荤殑锛屾墍浠ュ绾跨▼骞跺彂璁块棶涔熶笉浼氭湁浠讳綍闂銆傚綋鐒朵篃鏈変竴浜涚己鐐癸紝姣斿姣忎釜涓嶅悓鐨勭姸鎬侀兘瑕佷竴涓璞℃潵浠h〃锛屽彲鑳戒細閫犳垚鎬ц兘涓婄殑闂銆傛墍浠ava鏍囧噯绫诲簱杩樻彁渚涗簡涓�涓彲鍙樼増鏈紝鍗� StringBuffer銆�
32銆佹槸鍚﹀彲浠ョ户鎵縎tring绫�?
String绫绘槸final绫绘晠涓嶅彲浠ョ户鎵裤��
33銆丼tring s = new String("xyz");鍒涘缓浜嗗嚑涓猄tring Object?浜岃�呬箣闂存湁浠�涔堝尯鍒紵
涓や釜鎴栦竴涓紝鈥漻yz鈥濆搴斾竴涓璞★紝杩欎釜瀵硅薄鏀惧湪瀛楃涓插父閲忕紦鍐插尯锛屽父閲忊�漻yz鈥濅笉绠″嚭鐜板灏戦亶锛岄兘鏄紦鍐插尯涓殑閭d竴涓�侼ew String姣忓啓涓�閬嶏紝灏卞垱寤轰竴涓柊鐨勫璞★紝瀹冧竴鍙ラ偅涓父閲忊�漻yz鈥濆璞$殑鍐呭鏉ュ垱寤哄嚭涓�涓柊String瀵硅薄銆傚鏋滀互鍓嶅氨鐢ㄨ繃鈥檟yz鈥欙紝杩欏彞浠h〃灏变笉浼氬垱寤衡�漻yz鈥濊嚜宸变簡锛岀洿鎺ヤ粠缂撳啿鍖烘嬁銆�
34銆丼tring鍜孲tringBuffer鐨勫尯鍒�
JAVA骞冲彴鎻愪緵浜嗕袱涓被锛歋tring鍜孲tringBuffer锛屽畠浠彲浠ュ偍瀛樺拰鎿嶄綔瀛楃涓诧紝鍗冲寘鍚涓瓧绗︾殑瀛楃鏁版嵁銆傝繖涓猄tring绫绘彁渚涗簡鏁板�间笉鍙敼鍙樼殑瀛楃涓层�傝�岃繖涓猄tringBuffer绫绘彁渚涚殑瀛楃涓茶繘琛屼慨鏀广�傚綋浣犵煡閬撳瓧绗︽暟鎹鏀瑰彉鐨勬椂鍊欎綘灏卞彲浠ヤ娇鐢⊿tringBuffer銆傚吀鍨嬪湴锛屼綘鍙互浣跨敤StringBuffers鏉ュ姩鎬佹瀯閫犲瓧绗︽暟鎹�傚彟澶栵紝String瀹炵幇浜唀quals鏂规硶锛宯ew String(鈥渁bc鈥�).equals(newString(鈥渁bc鈥�)鐨勭粨鏋滀负true,鑰孲tringBuffer娌℃湁瀹炵幇equals鏂规硶锛屾墍浠ワ紝new StringBuffer(鈥渁bc鈥�).equals(newStringBuffer(鈥渁bc鈥�)鐨勭粨鏋滀负false銆�
鎺ョ潃瑕佷妇涓�涓叿浣撶殑渚嬪瓙鏉ヨ鏄庯紝鎴戜滑瑕佹妸1鍒�100鐨勬墍鏈夋暟瀛楁嫾璧锋潵锛岀粍鎴愪竴涓覆銆�
StringBuffer sbf = new StringBuffer();
for(int i=0;i<100;i++)
{
sbf.append(i);
}
涓婇潰鐨勪唬鐮佹晥鐜囧緢楂橈紝鍥犱负鍙垱寤轰簡涓�涓猄tringBuffer瀵硅薄锛岃�屼笅闈㈢殑浠g爜鏁堢巼寰堜綆锛屽洜涓哄垱寤轰簡101涓璞°��
String str = new String();
for(int i=0;i<100;i++)
{
str = str + i;
}
鍦ㄨ涓よ�呭尯鍒椂锛屽簲鎶婂惊鐜殑娆℃暟鎼炴垚10000锛岀劧鍚庣敤endTime-beginTime鏉ユ瘮杈冧袱鑰呮墽琛岀殑鏃堕棿宸紓锛屾渶鍚庤繕瑕佽璁睸tringBuilder涓嶴tringBuffer鐨勫尯鍒��
String瑕嗙洊浜唀quals鏂规硶鍜宧ashCode鏂规硶锛岃�孲tringBuffer娌℃湁瑕嗙洊equals鏂规硶鍜宧ashCode鏂规硶锛屾墍浠ワ紝灏�StringBuffer瀵硅薄瀛樺偍杩汮ava闆嗗悎绫讳腑鏃朵細鍑虹幇闂銆�
35銆佸浣曟妸涓�娈甸�楀彿鍒嗗壊鐨勫瓧绗︿覆杞崲鎴愪竴涓暟缁�?
濡傛灉涓嶆煡jdk api锛屾垜寰堥毦鍐欏嚭鏉ワ紒鎴戝彲浠ヨ璇存垜鐨勬�濊矾锛�
1鐢ㄦ鍒欒〃杈惧紡锛屼唬鐮佸ぇ姒備负锛歋tring [] result = orgStr.split(鈥�,鈥�);
2鐢� StingTokenizer ,浠g爜涓猴細StringTokenizer 聽tokener = StringTokenizer(orgStr,鈥�,鈥�);
String [] result =new String[tokener .countTokens()];
Int i=0;
while(tokener.hasNext(){
result[i++]=toker.nextToken();
}
36銆佹暟缁勬湁娌℃湁length()杩欎釜鏂规硶? String鏈夋病鏈塴ength()杩欎釜鏂规硶锛�
鏁扮粍娌℃湁length()杩欎釜鏂规硶锛屾湁length鐨勫睘鎬с�係tring鏈夋湁length()杩欎釜鏂规硶銆�
37銆佷笅闈㈣繖鏉¤鍙ヤ竴鍏卞垱寤轰簡澶氬皯涓璞★細String s="a"+"b"+"c"+"d";
绛旓細瀵逛簬濡備笅浠g爜锛�
String s1 = "a";
String s2 = s1 + "b";
String s3 = "a" + "b";
System.out.println(s2 == "ab");
System.out.println(s3 == "ab");
绗竴鏉¤鍙ユ墦鍗扮殑缁撴灉涓篺alse锛岀浜屾潯璇彞鎵撳嵃鐨勭粨鏋滀负true锛岃繖璇存槑javac缂栬瘧鍙互瀵瑰瓧绗︿覆甯搁噺鐩存帴鐩稿姞鐨勮〃杈惧紡杩涜浼樺寲锛屼笉蹇呰绛夊埌杩愯鏈熷幓杩涜鍔犳硶杩愮畻澶勭悊锛岃�屾槸鍦ㄧ紪璇戞椂鍘绘帀鍏朵腑鐨勫姞鍙凤紝鐩存帴灏嗗叾缂栬瘧鎴愪竴涓繖浜涘父閲忕浉杩炵殑缁撴灉銆�
棰樼洰涓殑绗竴琛屼唬鐮佽缂栬瘧鍣ㄥ湪缂栬瘧鏃朵紭鍖栧悗锛岀浉褰撲簬鐩存帴瀹氫箟浜嗕竴涓�漚bcd鈥濈殑瀛楃涓诧紝鎵�浠ワ紝涓婇潰鐨勪唬鐮佸簲璇ュ彧鍒涘缓浜嗕竴涓猄tring瀵硅薄銆傚啓濡備笅涓よ浠g爜锛�
String s ="a" + "b" + "c" + "d";
System.out.println(s== "abcd");
鏈�缁堟墦鍗扮殑缁撴灉搴旇涓簍rue銆�
38銆乼ry {}閲屾湁涓�涓猺eturn璇彞锛岄偅涔堢揣璺熷湪杩欎釜try鍚庣殑finally {}閲岀殑code浼氫笉浼氳鎵ц锛屼粈涔堟椂鍊欒鎵ц锛屽湪return鍓嶈繕鏄悗?
涔熻浣犵殑绛旀鏄湪return涔嬪墠锛屼絾寰�鏇寸粏鍦拌锛屾垜鐨勭瓟妗堟槸鍦╮eturn涓棿鎵ц锛岃鐪嬩笅闈㈢▼搴忎唬鐮佺殑杩愯缁撴灉锛�
publicclassTest {
/**
*@paramargs add by zxx ,Dec 9, 2008
*/
public static voidmain(String[] args) {
//TODOAuto-generated method stub
System.out.println(newTest().test());;
}
staticinttest()
{
intx = 1;
try
{
returnx;
}
finally
{
++x;
}
}
}
---------鎵ц缁撴灉 ---------
1
杩愯缁撴灉鏄�1锛屼负浠�涔堝憿锛熶富鍑芥暟璋冪敤瀛愬嚱鏁板苟寰楀埌缁撴灉鐨勮繃绋嬶紝濂芥瘮涓诲嚱鏁板噯澶囦竴涓┖缃愬瓙锛屽綋瀛愬嚱鏁拌杩斿洖缁撴灉鏃讹紝鍏堟妸缁撴灉鏀惧湪缃愬瓙閲岋紝鐒跺悗鍐嶅皢绋嬪簭閫昏緫杩斿洖鍒颁富鍑芥暟銆傛墍璋撹繑鍥烇紝灏辨槸瀛愬嚱鏁拌锛屾垜涓嶈繍琛屼簡锛屼綘涓诲嚱鏁扮户缁繍琛屽惂锛岃繖娌′粈涔堢粨鏋滃彲瑷�锛岀粨鏋滄槸鍦ㄨ杩欒瘽涔嬪墠鏀捐繘缃愬瓙閲岀殑銆�
39銆佷笅闈㈢殑绋嬪簭浠g爜杈撳嚭鐨勭粨鏋滄槸澶氬皯锛�
public classsmallT
{
public staticvoid 聽main(String args[])
{
smallT t 聽= new smallT();
int 聽b = 聽t.get();
System.out.println(b);
}
public int 聽get()
{
try
{
return1 ;
}
finally
{
return2 ;
}
}
}
杩斿洖鐨勭粨鏋滄槸2銆�
鎴戝彲浠ラ�氳繃涓嬮潰涓�涓緥瀛愮▼搴忔潵甯姪鎴戣В閲婅繖涓瓟妗堬紝浠庝笅闈緥瀛愮殑杩愯缁撴灉涓彲浠ュ彂鐜帮紝try涓殑return璇彞璋冪敤鐨勫嚱鏁板厛浜巉inally涓皟鐢ㄧ殑鍑芥暟鎵ц锛屼篃灏辨槸璇磖eturn璇彞鍏堟墽琛岋紝finally璇彞鍚庢墽琛岋紝鎵�浠ワ紝杩斿洖鐨勭粨鏋滄槸2銆俁eturn骞朵笉鏄鍑芥暟椹笂杩斿洖锛岃�屾槸return璇彞鎵ц鍚庯紝灏嗘妸杩斿洖缁撴灉鏀剧疆杩涘嚱鏁版爤涓紝姝ゆ椂鍑芥暟骞朵笉鏄┈涓婅繑鍥烇紝瀹冭鎵цfinally璇彞鍚庢墠鐪熸寮�濮嬭繑鍥炪��
鍦ㄨ瑙g瓟妗堟椂鍙互鐢ㄤ笅闈㈢殑绋嬪簭鏉ュ府鍔╁垎鏋愶細
publicclassTest {
/**
*@paramargs add by zxx ,Dec 9, 2008
*/
public static voidmain(String[] args) {
//TODOAuto-generated method stub
System.out.println(newTest().test());;
}
inttest()
{
try
{
returnfunc1();
}
finally
{
returnfunc2();
}
}
intfunc1()
{
System.out.println("func1");
return1;
}
intfunc2()
{
System.out.println("func2");
return2;
}
}
-----------鎵ц缁撴灉-----------------
func1
func2
2
缁撹锛歠inally涓殑浠g爜姣攔eturn鍜宐reak璇彞鍚庢墽琛�
40銆乫inal, finally, finalize鐨勫尯鍒��
final 鐢ㄤ簬澹版槑灞炴�э紝鏂规硶鍜岀被锛屽垎鍒〃绀哄睘鎬т笉鍙彉锛屾柟娉曚笉鍙鐩栵紝绫讳笉鍙户鎵裤��
鍐呴儴绫昏璁块棶灞�閮ㄥ彉閲忥紝灞�閮ㄥ彉閲忓繀椤诲畾涔夋垚final绫诲瀷锛屼緥濡傦紝涓�娈典唬鐮佲�︹��
finally鏄紓甯稿鐞嗚鍙ョ粨鏋勭殑涓�閮ㄥ垎锛岃〃绀烘�绘槸鎵ц銆�
finalize鏄疧bject绫荤殑涓�涓柟娉曪紝鍦ㄥ瀮鍦炬敹闆嗗櫒鎵ц鐨勬椂鍊欎細璋冪敤琚洖鏀跺璞$殑姝ゆ柟娉曪紝鍙互瑕嗙洊姝ゆ柟娉曟彁渚涘瀮鍦炬敹闆嗘椂鐨勫叾浠栬祫婧愬洖鏀讹紝渚嬪鍏抽棴鏂囦欢绛夈�侸VM涓嶄繚璇佹鏂规硶鎬昏璋冪敤
41銆佽繍琛屾椂寮傚父涓庝竴鑸紓甯告湁浣曞紓鍚岋紵
寮傚父琛ㄧず绋嬪簭杩愯杩囩▼涓彲鑳藉嚭鐜扮殑闈炴甯哥姸鎬侊紝杩愯鏃跺紓甯歌〃绀鸿櫄鎷熸満鐨勯�氬父鎿嶄綔涓彲鑳介亣鍒扮殑寮傚父锛屾槸涓�绉嶅父瑙佽繍琛岄敊璇�俲ava缂栬瘧鍣ㄨ姹傛柟娉曞繀椤诲0鏄庢姏鍑哄彲鑳藉彂鐢熺殑闈炶繍琛屾椂寮傚父锛屼絾鏄苟涓嶈姹傚繀椤诲0鏄庢姏鍑烘湭琚崟鑾风殑杩愯鏃跺紓甯搞��
42銆乪rror鍜宔xception鏈変粈涔堝尯鍒�?
error 琛ㄧず鎭㈠涓嶆槸涓嶅彲鑳戒絾寰堝洶闅剧殑鎯呭喌涓嬬殑涓�绉嶄弗閲嶉棶棰樸�傛瘮濡傝鍐呭瓨婧㈠嚭銆備笉鍙兘鎸囨湜绋嬪簭鑳藉鐞嗚繖鏍风殑鎯呭喌銆� exception琛ㄧず涓�绉嶈璁℃垨瀹炵幇闂銆備篃灏辨槸璇达紝瀹冭〃绀哄鏋滅▼搴忚繍琛屾甯革紝浠庝笉浼氬彂鐢熺殑鎯呭喌銆�
43銆丣ava涓殑寮傚父澶勭悊鏈哄埗鐨勭畝鍗曞師鐞嗗拰搴旂敤銆�
寮傚父鏄寚java绋嬪簭杩愯鏃讹紙闈炵紪璇戯級鎵�鍙戠敓鐨勯潪姝e父鎯呭喌鎴栭敊璇紝涓庣幇瀹炵敓娲讳腑鐨勪簨浠跺緢鐩镐技锛岀幇瀹炵敓娲讳腑鐨勪簨浠跺彲浠ュ寘鍚簨浠跺彂鐢熺殑鏃堕棿銆佸湴鐐广�佷汉鐗┿�佹儏鑺傜瓑淇℃伅锛屽彲浠ョ敤涓�涓璞℃潵琛ㄧず锛孞ava浣跨敤闈㈠悜瀵硅薄鐨勬柟寮忔潵澶勭悊寮傚父锛屽畠鎶婄▼搴忎腑鍙戠敓鐨勬瘡涓紓甯镐篃閮藉垎鍒皝瑁呭埌涓�涓璞℃潵琛ㄧず鐨勶紝璇ュ璞′腑鍖呭惈鏈夊紓甯哥殑淇℃伅銆�
Java瀵瑰紓甯歌繘琛屼簡鍒嗙被锛屼笉鍚岀被鍨嬬殑寮傚父鍒嗗埆鐢ㄤ笉鍚岀殑Java绫昏〃绀猴紝鎵�鏈夊紓甯哥殑鏍圭被涓簀ava.lang.Throwable锛孴hrowable涓嬮潰鍙堟淳鐢熶簡涓や釜瀛愮被锛欵rror鍜孍xception锛孍rror琛ㄧず搴旂敤绋嬪簭鏈韩鏃犳硶鍏嬫湇鍜屾仮澶嶇殑涓�绉嶄弗閲嶉棶棰橈紝绋嬪簭鍙湁姝荤殑浠戒簡锛屼緥濡傦紝璇村唴瀛樻孩鍑哄拰绾跨▼姝婚攣绛夌郴缁熼棶棰樸�侲xception琛ㄧず绋嬪簭杩樿兘澶熷厠鏈嶅拰鎭㈠鐨勯棶棰橈紝鍏朵腑鍙堝垎涓虹郴缁熷紓甯稿拰鏅�氬紓甯革紝绯荤粺寮傚父鏄蒋浠舵湰韬己闄锋墍瀵艰嚧鐨勯棶棰橈紝涔熷氨鏄蒋浠跺紑鍙戜汉鍛樿�冭檻涓嶅懆鎵�瀵艰嚧鐨勯棶棰橈紝杞欢浣跨敤鑰呮棤娉曞厠鏈嶅拰鎭㈠杩欑闂锛屼絾鍦ㄨ繖绉嶉棶棰樹笅杩樺彲浠ヨ杞欢绯荤粺缁х画杩愯鎴栬�呰杞欢姝绘帀锛屼緥濡傦紝鏁扮粍鑴氭湰瓒婄晫锛圓rrayIndexOutOfBoundsException锛夛紝绌烘寚閽堝紓甯革紙NullPointerException锛夈�佺被杞崲寮傚父锛圕lassCastException锛夛紱鏅�氬紓甯告槸杩愯鐜鐨勫彉鍖栨垨寮傚父鎵�瀵艰嚧鐨勯棶棰橈紝鏄敤鎴疯兘澶熷厠鏈嶇殑闂锛屼緥濡傦紝缃戠粶鏂嚎锛岀‖鐩樼┖闂翠笉澶燂紝鍙戠敓杩欐牱鐨勫紓甯稿悗锛岀▼搴忎笉搴旇姝绘帀銆�
java涓虹郴缁熷紓甯稿拰鏅�氬紓甯告彁渚涗簡涓嶅悓鐨勮В鍐虫柟妗堬紝缂栬瘧鍣ㄥ己鍒舵櫘閫氬紓甯稿繀椤籺ry..catch澶勭悊鎴栫敤throws澹版槑缁х画鎶涚粰涓婂眰璋冪敤鏂规硶澶勭悊锛屾墍浠ユ櫘閫氬紓甯镐篃绉颁负checked寮傚父锛岃�岀郴缁熷紓甯稿彲浠ュ鐞嗕篃鍙互涓嶅鐞嗭紝鎵�浠ワ紝缂栬瘧鍣ㄤ笉寮哄埗鐢╰ry..catch澶勭悊鎴栫敤throws澹版槑锛屾墍浠ョ郴缁熷紓甯镐篃绉颁负unchecked寮傚父銆�
鎻愮ず绛旈鑰咃細灏辨寜鐓т笁涓骇鍒幓鎬濊�冿細铏氭嫙鏈哄繀椤诲畷鏈虹殑閿欒锛岀▼搴忓彲浠ユ鎺変篃鍙互涓嶆鎺夌殑閿欒锛岀▼搴忎笉搴旇姝绘帀鐨勯敊璇紱
44銆佽鍐欏嚭浣犳渶甯歌鍒扮殑5涓猺untime exception銆�
杩欓亾棰樹富瑕佽�冧綘鐨勪唬鐮侀噺鍒板簳澶氬ぇ锛屽鏋滀綘闀挎湡鍐欎唬鐮佺殑锛屽簲璇ョ粡甯搁兘鐪嬪埌杩囦竴浜涚郴缁熸柟闈㈢殑寮傚父锛屼綘涓嶄竴瀹氱湡瑕佸洖绛斿嚭5涓叿浣撶殑绯荤粺寮傚父锛屼絾浣犺鑳藉璇村嚭浠�涔堟槸绯荤粺寮傚父锛屼互鍙婂嚑涓郴缁熷紓甯稿氨鍙互浜嗭紝褰撶劧锛岃繖浜涘紓甯稿畬鍏ㄧ敤鍏惰嫳鏂囧悕绉版潵鍐欐槸鏈�濂界殑锛屽鏋滃疄鍦ㄥ啓涓嶅嚭锛岄偅灏辩敤涓枃鍚э紝鏈夋�绘瘮娌℃湁寮猴紒
鎵�璋撶郴缁熷紓甯革紝灏辨槸鈥�..锛屽畠浠兘鏄疪untimeException鐨勫瓙绫伙紝鍦╦dk doc涓煡RuntimeException绫伙紝灏卞彲浠ョ湅鍒板叾鎵�鏈夌殑瀛愮被鍒楄〃锛屼篃灏辨槸鐪嬪埌浜嗘墍鏈夌殑绯荤粺寮傚父銆傛垜姣旇緝鏈夊嵃璞$殑绯荤粺寮傚父鏈夛細NullPointerException銆丄rrayIndexOutOfBoundsException銆丆lassCastException銆�
45銆丣AVA璇█濡備綍杩涜寮傚父澶勭悊锛屽叧閿瓧锛歵hrows,throw,try,catch,finally鍒嗗埆浠h〃浠�涔堟剰涔夛紵鍦╰ry鍧椾腑鍙互鎶涘嚭寮傚父鍚楋紵
46銆乯ava涓湁鍑犵鏂规硶鍙互瀹炵幇涓�涓嚎绋嬶紵鐢ㄤ粈涔堝叧閿瓧淇グ鍚屾鏂规硶? stop()鍜宻uspend()鏂规硶涓轰綍涓嶆帹鑽愪娇鐢紵
java5浠ュ墠锛屾湁濡備笅涓ょ锛�
绗竴绉嶏細
new Thread(){}.start();杩欒〃绀鸿皟鐢═hread瀛愮被瀵硅薄鐨剅un鏂规硶锛宯ew Thread(){}琛ㄧず涓�涓猅hread鐨勫尶鍚嶅瓙绫荤殑瀹炰緥瀵硅薄锛屽瓙绫诲姞涓妑un鏂规硶鍚庣殑浠g爜濡備笅锛�
new Thread(){
public void run(){
}
}.start();
绗簩绉嶏細
new Thread(new Runnable(){}).start();杩欒〃绀鸿皟鐢═hread瀵硅薄鎺ュ彈鐨凴unnable瀵硅薄鐨剅un鏂规硶锛宯ew Runnable(){}琛ㄧず涓�涓猂unnable鐨勫尶鍚嶅瓙绫荤殑瀹炰緥瀵硅薄,runnable鐨勫瓙绫诲姞涓妑un鏂规硶鍚庣殑浠g爜濡備笅锛�
new Thread(new Runnable(){
public voidrun(){
}
}
).start();
浠巎ava5寮�濮嬶紝杩樻湁濡備笅涓�浜涚嚎绋嬫睜鍒涘缓澶氱嚎绋嬬殑鏂瑰紡锛�
ExecutorService pool = Executors.newFixedThreadPool(3)
for(int i=0;i<10;i++)
{
pool.execute(newRunable(){public void run(){}});
}
Executors.newCachedThreadPool().execute(new Runable(){publicvoid run(){}});
Executors.newSingleThreadExecutor().execute(new Runable(){publicvoid run(){}});
鏈変袱绉嶅疄鐜版柟娉曪紝鍒嗗埆浣跨敤new Thread()鍜宯ew Thread(runnable)褰㈠紡锛岀涓�绉嶇洿鎺ヨ皟鐢╰hread鐨剅un鏂规硶锛屾墍浠ワ紝鎴戜滑寰�寰�浣跨敤Thread瀛愮被锛屽嵆new SubThread()銆傜浜岀璋冪敤runnable鐨剅un鏂规硶銆�
鏈変袱绉嶅疄鐜版柟娉曪紝鍒嗗埆鏄户鎵縏hread绫讳笌瀹炵幇Runnable鎺ュ彛
鐢╯ynchronized鍏抽敭瀛椾慨楗板悓姝ユ柟娉�
鍙嶅浣跨敤stop()锛屾槸鍥犱负瀹冧笉瀹夊叏銆傚畠浼氳В闄ょ敱绾跨▼鑾峰彇鐨勬墍鏈夐攣瀹氾紝鑰屼笖濡傛灉瀵硅薄澶勪簬涓�绉嶄笉杩炶疮鐘舵�侊紝閭d箞鍏朵粬绾跨▼鑳藉湪閭g鐘舵�佷笅妫�鏌ュ拰淇敼瀹冧滑銆傜粨鏋滃緢闅炬鏌ュ嚭鐪熸鐨勯棶棰樻墍鍦ㄣ�俿uspend()鏂规硶瀹规槗鍙戠敓姝婚攣銆傝皟鐢╯uspend()鐨勬椂鍊欙紝鐩爣绾跨▼浼氬仠涓嬫潵锛屼絾鍗翠粛鐒舵寔鏈夊湪杩欎箣鍓嶈幏寰楃殑閿佸畾銆傛鏃讹紝鍏朵粬浠讳綍绾跨▼閮戒笉鑳借闂攣瀹氱殑璧勬簮锛岄櫎闈炶"鎸傝捣"鐨勭嚎绋嬫仮澶嶈繍琛屻�傚浠讳綍绾跨▼鏉ヨ锛屽鏋滃畠浠兂鎭㈠鐩爣绾跨▼锛屽悓鏃跺張璇曞浘浣跨敤浠讳綍涓�涓攣瀹氱殑璧勬簮锛屽氨浼氶�犳垚姝婚攣銆傛墍浠ヤ笉搴旇浣跨敤suspend()锛岃�屽簲鍦ㄨ嚜宸辩殑Thread绫讳腑缃叆涓�涓爣蹇楋紝鎸囧嚭绾跨▼搴旇娲诲姩杩樻槸鎸傝捣銆傝嫢鏍囧織鎸囧嚭绾跨▼搴旇鎸傝捣锛屼究鐢╳ait()鍛藉叾杩涘叆绛夊緟鐘舵�併�傝嫢鏍囧織鎸囧嚭绾跨▼搴斿綋鎭㈠锛屽垯鐢ㄤ竴涓猲otify()閲嶆柊鍚姩绾跨▼銆�
47銆乻leep()鍜� wait()鏈変粈涔堝尯鍒�?
锛堢綉涓婄殑绛旀锛歴leep鏄嚎绋嬬被锛圱hread锛夌殑鏂规硶锛屽鑷存绾跨▼鏆傚仠鎵ц鎸囧畾鏃堕棿锛岀粰鎵ц鏈轰細缁欏叾浠栫嚎绋嬶紝浣嗘槸鐩戞帶鐘舵�佷緷鐒朵繚鎸侊紝鍒版椂鍚庝細鑷姩鎭㈠銆傝皟鐢╯leep涓嶄細閲婃斁瀵硅薄閿併�� wait鏄疧bject绫荤殑鏂规硶锛屽姝ゅ璞¤皟鐢╳ait鏂规硶瀵艰嚧鏈嚎绋嬫斁寮冨璞¢攣锛岃繘鍏ョ瓑寰呮瀵硅薄鐨勭瓑寰呴攣瀹氭睜锛屽彧鏈夐拡瀵规瀵硅薄鍙戝嚭notify鏂规硶锛堟垨notifyAll锛夊悗鏈嚎绋嬫墠杩涘叆瀵硅薄閿佸畾姹犲噯澶囪幏寰楀璞¢攣杩涘叆杩愯鐘舵�併�傦級
sleep灏辨槸姝e湪鎵ц鐨勭嚎绋嬩富鍔ㄨ鍑篶pu锛宑pu鍘绘墽琛屽叾浠栫嚎绋嬶紝鍦╯leep鎸囧畾鐨勬椂闂磋繃鍚庯紝cpu鎵嶄細鍥炲埌杩欎釜绾跨▼涓婄户缁線涓嬫墽琛岋紝濡傛灉褰撳墠绾跨▼杩涘叆浜嗗悓姝ラ攣锛宻leep鏂规硶骞朵笉浼氶噴鏀鹃攣锛屽嵆浣垮綋鍓嶇嚎绋嬩娇鐢╯leep鏂规硶璁╁嚭浜哻pu锛屼絾鍏朵粬琚悓姝ラ攣鎸′綇浜嗙殑绾跨▼涔熸棤娉曞緱鍒版墽琛屻�倃ait鏄寚鍦ㄤ竴涓凡缁忚繘鍏ヤ簡鍚屾閿佺殑绾跨▼鍐咃紝璁╄嚜宸辨殏鏃惰鍑哄悓姝ラ攣锛屼互渚垮叾浠栨鍦ㄧ瓑寰呮閿佺殑绾跨▼鍙互寰楀埌鍚屾閿佸苟杩愯锛屽彧鏈夊叾浠栫嚎绋嬭皟鐢ㄤ簡notify鏂规硶锛坣otify骞朵笉閲婃斁閿侊紝鍙槸鍛婅瘔璋冪敤杩噖ait鏂规硶鐨勭嚎绋嬪彲浠ュ幓鍙備笌鑾峰緱閿佺殑绔炰簤浜嗭紝浣嗕笉鏄┈涓婂緱鍒伴攣锛屽洜涓洪攣杩樺湪鍒汉鎵嬮噷锛屽埆浜鸿繕娌¢噴鏀俱�傚鏋渘otify鏂规硶鍚庨潰鐨勪唬鐮佽繕鏈夊緢澶氾紝闇�瑕佽繖浜涗唬鐮佹墽琛屽畬鍚庢墠浼氶噴鏀鹃攣锛屽彲浠ュ湪notfiy鏂规硶鍚庡鍔犱竴涓瓑寰呭拰涓�浜涗唬鐮侊紝鐪嬬湅鏁堟灉锛夛紝璋冪敤wait鏂规硶鐨勭嚎绋嬪氨浼氳В闄ait鐘舵�佸拰绋嬪簭鍙互鍐嶆寰楀埌閿佸悗缁х画鍚戜笅杩愯銆傚浜巜ait鐨勮瑙d竴瀹氳閰嶅悎渚嬪瓙浠g爜鏉ヨ鏄庯紝鎵嶆樉寰楄嚜宸辩湡鏄庣櫧銆�
packagecom.huawei.interview;
publicclassMultiThread {
/**
*@paramargs
*/
public static voidmain(String[] args) {
//TODOAuto-generated method stub
newThread(newThread1()).start();
try{
Thread.sleep(10);
}catch(InterruptedException e) {
//TODOAuto-generated catchblock
e.printStackTrace();
}
newThread(newThread2()).start();
}
private static classThread1implementsRunnable
{
@Override
public voidrun() {
//TODOAuto-generated methodstub
//鐢变簬杩欓噷鐨凾hread1鍜屼笅闈㈢殑Thread2鍐呴儴run鏂规硶瑕佺敤鍚屼竴瀵硅薄浣滀负鐩戣鍣紝鎴戜滑杩欓噷涓嶈兘鐢╰his锛屽洜涓哄湪Thread2閲岄潰鐨則his鍜岃繖涓猅hread1鐨則his涓嶆槸鍚屼竴涓璞°�傛垜浠敤MultiThread.class杩欎釜瀛楄妭鐮佸璞★紝褰撳墠铏氭嫙鏈洪噷寮曠敤杩欎釜鍙橀噺鏃讹紝鎸囧悜鐨勯兘鏄悓涓�涓璞°��
synchronized(MultiThread.class){
System.out.println("enterthread1...");
System.out.println("thread1is waiting");
try{
//閲婃斁閿佹湁涓ょ鏂瑰紡锛岀涓�绉嶆柟寮忔槸绋嬪簭鑷劧绂诲紑鐩戣鍣ㄧ殑鑼冨洿锛屼篃灏辨槸绂诲紑浜唖ynchronized鍏抽敭瀛楃杈栫殑浠g爜鑼冨洿锛屽彟涓�绉嶆柟寮忓氨鏄湪synchronized鍏抽敭瀛楃杈栫殑浠g爜鍐呴儴璋冪敤鐩戣鍣ㄥ璞$殑wait鏂规硶銆傝繖閲岋紝浣跨敤wait鏂规硶閲婃斁閿併��
MultiThread.class.wait();
}catch(InterruptedException e) {
//TODOAuto-generatedcatch block
e.printStackTrace();
}
System.out.println("thread1is going on...");
System.out.println("thread1is being over!");
}
}
}
private static classThread2implementsRunnable
{
@Override
public voidrun() {
//TODOAuto-generated methodstub
synchronized(MultiThread.class){
System.out.println("enterthread2...");
System.out.println("thread2notify other thread can release wait status..");
//鐢变簬notify鏂规硶骞朵笉閲婃斁閿侊紝鍗充娇thread2璋冪敤涓嬮潰鐨剆leep鏂规硶浼戞伅浜�10姣锛屼絾thread1浠嶇劧涓嶄細鎵ц锛屽洜涓簍hread2娌℃湁閲婃斁閿侊紝鎵�浠hread1鏃犳硶寰椾笉鍒伴攣銆�
MultiThread.class.notify();
System.out.println("thread2is sleeping ten millisecond...");
try{
Thread.sleep(10);
}catch(InterruptedExceptione) {
//TODOAuto-generatedcatch block
e.printStackTrace();
}
System.out.println("thread2is going on...");
System.out.println("thread2is being over!");
}
}
}
}
48銆佸悓姝ュ拰寮傛鏈変綍寮傚悓锛屽湪浠�涔堟儏鍐典笅鍒嗗埆浣跨敤浠栦滑锛熶妇渚嬭鏄庛��
濡傛灉鏁版嵁灏嗗湪绾跨▼闂村叡浜�備緥濡傛鍦ㄥ啓鐨勬暟鎹互鍚庡彲鑳借鍙︿竴涓嚎绋嬭鍒帮紝鎴栬�呮鍦ㄨ鐨勬暟鎹彲鑳藉凡缁忚鍙︿竴涓嚎绋嬪啓杩囦簡锛岄偅涔堣繖浜涙暟鎹氨鏄叡浜暟鎹紝蹇呴』杩涜鍚屾瀛樺彇銆�
褰撳簲鐢ㄧ▼搴忓湪瀵硅薄涓婅皟鐢ㄤ簡涓�涓渶瑕佽姳璐瑰緢闀挎椂闂存潵鎵ц鐨勬柟娉曪紝骞朵笖涓嶅笇鏈涜绋嬪簭绛夊緟鏂规硶鐨勮繑鍥炴椂锛屽氨搴旇浣跨敤寮傛缂栫▼锛屽湪寰堝鎯呭喌涓嬮噰鐢ㄥ紓姝ラ�斿緞寰�寰�鏇存湁鏁堢巼銆�
49. 涓嬮潰涓や釜鏂规硶鍚屾鍚楋紵锛堣嚜宸卞彂鏄庯級
class Test
{
synchronizedstatic voidsayHello3()
{
}
synchronizedvoidgetX(){}
}
50銆佸绾跨▼鏈夊嚑绉嶅疄鐜版柟娉�?鍚屾鏈夊嚑绉嶅疄鐜版柟娉�?
澶氱嚎绋嬫湁涓ょ瀹炵幇鏂规硶锛屽垎鍒槸缁ф壙Thread绫讳笌瀹炵幇Runnable鎺ュ彛
鍚屾鐨勫疄鐜版柟闈㈡湁涓ょ锛屽垎鍒槸synchronized,wait涓巒otify
wait():浣夸竴涓嚎绋嬪浜庣瓑寰呯姸鎬侊紝骞朵笖閲婃斁鎵�鎸佹湁鐨勫璞$殑lock銆�
sleep():浣夸竴涓鍦ㄨ繍琛岀殑绾跨▼澶勪簬鐫$湢鐘舵�侊紝鏄竴涓潤鎬佹柟娉曪紝璋冪敤姝ゆ柟娉曡鎹曟崏InterruptedException寮傚父銆�
notify():鍞ら啋涓�涓浜庣瓑寰呯姸鎬佺殑绾跨▼锛屾敞鎰忕殑鏄湪璋冪敤姝ゆ柟娉曠殑鏃跺�欙紝骞朵笉鑳界‘鍒囩殑鍞ら啋鏌愪竴涓瓑寰呯姸鎬佺殑绾跨▼锛岃�屾槸鐢盝VM纭畾鍞ら啋鍝釜绾跨▼锛岃�屼笖涓嶆槸鎸変紭鍏堢骇銆�
Allnotity():鍞ら啋鎵�鏈夊鍏ョ瓑寰呯姸鎬佺殑绾跨▼锛屾敞鎰忓苟涓嶆槸缁欐墍鏈夊敜閱掔嚎绋嬩竴涓璞$殑閿侊紝鑰屾槸璁╁畠浠珵浜夈��
51銆佸惎鍔ㄤ竴涓嚎绋嬫槸鐢╮un()杩樻槸start()? .
鍚姩涓�涓嚎绋嬫槸璋冪敤start()鏂规硶锛屼娇绾跨▼灏辩华鐘舵�侊紝浠ュ悗鍙互琚皟搴︿负杩愯鐘舵�侊紝涓�涓嚎绋嬪繀椤诲叧鑱斾竴浜涘叿浣撶殑鎵ц浠g爜锛宺un()鏂规硶鏄绾跨▼鎵�鍏宠仈鐨勬墽琛屼唬鐮併��
52銆佸綋涓�涓嚎绋嬭繘鍏ヤ竴涓璞$殑涓�涓猻ynchronized鏂规硶鍚庯紝鍏跺畠绾跨▼鏄惁鍙�杩涘叆姝ゅ璞$殑鍏跺畠鏂规硶?
鍒嗗嚑绉嶆儏鍐碉細
1.鍏朵粬鏂规硶鍓嶆槸鍚﹀姞浜唖ynchronized鍏抽敭瀛楋紝濡傛灉娌″姞锛屽垯鑳姐��
2.濡傛灉杩欎釜鏂规硶鍐呴儴璋冪敤浜唚ait锛屽垯鍙互杩涘叆鍏朵粬synchronized鏂规硶銆�
3.濡傛灉鍏朵粬涓柟娉曢兘鍔犱簡synchronized鍏抽敭瀛楋紝骞朵笖鍐呴儴娌℃湁璋冪敤wait锛屽垯涓嶈兘銆�
4.濡傛灉鍏朵粬鏂规硶鏄痵tatic锛屽畠鐢ㄧ殑鍚屾閿佹槸褰撳墠绫荤殑瀛楄妭鐮侊紝涓庨潪闈欐�佺殑鏂规硶涓嶈兘鍚屾锛屽洜涓洪潪闈欐�佺殑鏂规硶鐢ㄧ殑鏄痶his銆�
53銆佺嚎绋嬬殑鍩烘湰姒傚康銆佺嚎绋嬬殑鍩烘湰鐘舵�佷互鍙婄姸鎬佷箣闂寸殑鍏崇郴
涓�涓▼搴忎腑鍙互鏈夊鏉℃墽琛岀嚎绱㈠悓鏃舵墽琛岋紝涓�涓嚎绋嬪氨鏄▼搴忎腑鐨勪竴鏉℃墽琛岀嚎绱紝姣忎釜绾跨▼涓婇兘鍏宠仈鏈夎鎵ц鐨勪唬鐮侊紝鍗冲彲浠ユ湁澶氭绋嬪簭浠g爜鍚屾椂杩愯锛屾瘡涓▼搴忚嚦灏戦兘鏈変竴涓嚎绋嬶紝鍗砿ain鏂规硶鎵ц鐨勯偅涓嚎绋嬨�傚鏋滃彧鏄竴涓猚pu锛屽畠鎬庝箞鑳藉鍚屾椂鎵ц澶氭绋嬪簭鍛紵杩欐槸浠庡畯瑙備笂鏉ョ湅鐨勶紝cpu涓�浼氭墽琛宎绾跨储锛屼竴浼氭墽琛宐绾跨储锛屽垏鎹㈡椂闂村緢蹇紝缁欎汉鐨勬劅瑙夋槸a,b鍦ㄥ悓鏃舵墽琛岋紝濂芥瘮澶у鍦ㄥ悓涓�涓姙鍏涓婄綉锛屽彧鏈変竴鏉¢摼鎺ュ埌澶栭儴缃戠嚎锛屽叾瀹烇紝杩欐潯缃戠嚎涓�浼氫负a浼犳暟鎹紝涓�浼氫负b浼犳暟鎹紝鐢变簬鍒囨崲鏃堕棿寰堢煭鏆傦紝鎵�浠ワ紝澶у鎰熻閮藉湪鍚屾椂涓婄綉銆�
鐘舵�侊細灏辩华锛岃繍琛岋紝synchronize闃诲锛寃ait鍜宻leep鎸傝捣锛岀粨鏉熴�倃ait蹇呴』鍦╯ynchronized鍐呴儴璋冪敤銆�
璋冪敤绾跨▼鐨剆tart鏂规硶鍚庣嚎绋嬭繘鍏ュ氨缁姸鎬侊紝绾跨▼璋冨害绯荤粺灏嗗氨缁姸鎬佺殑绾跨▼杞负杩愯鐘舵�侊紝閬囧埌synchronized璇彞鏃讹紝鐢辫繍琛岀姸鎬佽浆涓洪樆濉烇紝褰搒ynchronized鑾峰緱閿佸悗锛岀敱闃诲杞负杩愯锛屽湪杩欑鎯呭喌鍙互璋冪敤wait鏂规硶杞负鎸傝捣鐘舵�侊紝褰撶嚎绋嬪叧鑱旂殑浠g爜鎵ц瀹屽悗锛岀嚎绋嬪彉涓虹粨鏉熺姸鎬併��
54銆佺畝杩皊ynchronized鍜宩ava.util.concurrent.locks.Lock鐨勫紓鍚岋紵
涓昏鐩稿悓鐐癸細Lock鑳藉畬鎴恠ynchronized鎵�瀹炵幇鐨勬墍鏈夊姛鑳�
涓昏涓嶅悓鐐癸細Lock鏈夋瘮synchronized鏇寸簿纭殑绾跨▼璇箟鍜屾洿濂界殑鎬ц兘銆俿ynchronized浼氳嚜鍔ㄩ噴鏀鹃攣锛岃�孡ock涓�瀹氳姹傜▼搴忓憳鎵嬪伐閲婃斁锛屽苟涓斿繀椤诲湪finally浠庡彞涓噴鏀俱�侺ock杩樻湁鏇村己澶х殑鍔熻兘锛屼緥濡傦紝瀹冪殑tryLock鏂规硶鍙互闈為樆濉炴柟寮忓幓鎷块攣銆�
涓句緥璇存槑锛堝涓嬮潰鐨勯鐢╨ock杩涜浜嗘敼鍐欙級锛�
packagecom.huawei.interview;
importjava.util.concurrent.locks.Lock;
importjava.util.concurrent.locks.ReentrantLock;
publicclassThreadTest {
/**
*@paramargs
*/
private intj;
privateLock lock =newReentrantLock();
public static voidmain(String[] args) {
//TODOAuto-generated method stub
ThreadTest tt =newThreadTest();
for(inti=0;i<2;i++)
{
newThread(tt.newAdder()).start();
newThread(tt.newSubtractor()).start();
}
}
private classSubtractorimplementsRunnable
{
@Override
public voidrun() {
//TODOAuto-generated methodstub
while(true)
{
/*synchronized (ThreadTest.this) {
System.out.println("j--="+ j--);
//杩欓噷鎶涘紓甯镐簡锛岄攣鑳介噴鏀惧悧锛�
}*/
lock.lock();
try
{
System.out.println("j--="+ j--);
}finally
{
lock.unlock();
}
}
}
}
private classAdderimplementsRunnable
{
@Override
public voidrun() {
//TODOAuto-generated methodstub
while(true)
{
/*synchronized (ThreadTest.this) {
System.out.println("j++="+ j++);
}*/
lock.lock();
try
{
System.out.println("j++="+ j++);
}finally
{
lock.unlock();
}
}
}
}
}
55銆佽璁�4涓嚎绋嬶紝鍏朵腑涓や釜绾跨▼姣忔瀵筳澧炲姞1锛屽彟澶栦袱涓嚎绋嬪j姣忔鍑忓皯1銆傚啓鍑虹▼搴忋��
浠ヤ笅绋嬪簭浣跨敤鍐呴儴绫诲疄鐜扮嚎绋嬶紝瀵筳澧炲噺鐨勬椂鍊欐病鏈夎�冭檻椤哄簭闂銆�
public class ThreadTest1
{
private int j;
public static void main(String args[]){
ThreadTest1 tt=newThreadTest1();
Inc inc=tt.new Inc();
Dec dec=tt.new Dec();
for(inti=0;i<2;i++){
Thread t=newThread(inc);
t.start();
t=new Thread(dec);
t.start();
}
}
private synchronized void inc(){
j++;
System.out.println(Thread.currentThread().getName()+"-inc:"+j);
}
private synchronized void dec(){
j--;
System.out.println(Thread.currentThread().getName()+"-dec:"+j);
}
class Inc implements Runnable{
public void run(){
for(inti=0;i<100;i++){
inc();
}
}
}
class Dec implements Runnable{
public void run(){
for(inti=0;i<100;i++){
dec();
}
}
}
}
----------闅忔墜鍐嶅啓鐨勪竴涓�-------------
class A
{
JManger j =new JManager();
main()
{
new A().call();
}
void call
{
for(int i=0;i<2;i++)
{
new Thread(
newRunnable(){ public void run(){while(true){j.accumulate()}}}
).start();
new Thread(newRunnable(){ public void run(){while(true){j.sub()}}}).start();
}
}
}
class JManager
{
private j = 0;
public synchronized voidsubtract()
{
j--
}
public synchronized voidaccumulate()
{
j++;
}
}
56銆佸瓙绾跨▼寰幆10娆★紝鎺ョ潃涓荤嚎绋嬪惊鐜�100锛屾帴鐫�鍙堝洖鍒板瓙绾跨▼寰幆10娆★紝鎺ョ潃鍐嶅洖鍒颁富绾跨▼鍙堝惊鐜�100锛屽姝ゅ惊鐜�50娆★紝璇峰啓鍑虹▼搴忋��
鏈�缁堢殑绋嬪簭浠g爜濡備笅锛�
publicclassThreadTest {
/**
*@paramargs
*/
public static voidmain(String[] args) {
//TODOAuto-generated method stub
newThreadTest().init();
}
public voidinit()
{
finalBusiness business =newBusiness();
newThread(
newRunnable()
{
public voidrun() {
for(inti=0;i<50;i++)
{
business.SubThread(i);
}
}
}
).start();
for(inti=0;i<50;i++)
{
business.MainThread(i);
}
}
private classBusiness
{
booleanbShouldSub =true;//杩欓噷鐩稿綋浜庡畾涔変簡鎺у埗璇ヨ皝鎵ц鐨勪竴涓俊鍙风伅
public synchronized voidMainThread(inti)
{
if(bShouldSub)
try{
this.wait();
}catch(InterruptedException e) {
//TODOAuto-generatedcatch block
e.printStackTrace();
}
for(intj=0;j<5;j++)
{
System.out.println(Thread.currentThread().getName()+ ":i=" + i +",j=" + j);
}
bShouldSub =true;
this.notify();
}
public synchronized voidSubThread(inti)
{
if(!bShouldSub)
try{
this.wait();
}catch(InterruptedExceptione) {
//TODOAuto-generatedcatch block
e.printStackTrace();
}
for(intj=0;j<10;j++)
{
System.out.println(Thread.currentThread().getName()+ ":i=" + i +",j=" + j);
}
bShouldSub =false;
this.notify();
}
}
}
澶囨敞锛氫笉鍙兘涓�涓婃潵灏卞啓鍑轰笂闈㈢殑瀹屾暣浠g爜锛屾渶鍒濆啓鍑烘潵鐨勪唬鐮佸涓嬶紝闂鍦ㄤ簬涓や釜绾跨▼鐨勪唬鐮佽鍙傜収鍚屼竴涓彉閲忥紝鍗宠繖涓や釜绾跨▼鐨勪唬鐮佽鍏变韩鏁版嵁锛屾墍浠ワ紝鎶婅繖涓や釜绾跨▼鐨勬墽琛屼唬鐮佹惉鍒板悓涓�涓被涓幓锛�
packagecom.huawei.interview.lym;
publicclassThreadTest {
private static booleanbShouldMain=false;
public static voidmain(String[]args) {
//TODOAuto-generated method stub
/*new Thread(){
public void run()
{
for(int i=0;i<50;i++)
{
for(int j=0;j<10;j++)
{
System.out.println("i="+ i + ",j=" + j);
}
}
}
}.start();*/
//final String str = newString("");
newThread(
newRunnable()
{
public voidrun()
{
for(inti=0;i<50;i++)
{
synchronized(ThreadTest.class) {
if(bShouldMain)
{
try{
ThreadTest.class.wait();}
catch(InterruptedException e) {
e.printStackTrace();
}
}
for(intj=0;j<10;j++)
{
System.out.println(
Thread.currentThread().getName()+
"i="+ i + ",j=" + j);
}
bShouldMain=true;
ThreadTest.class.notify();
}
}
}
}
).start();
for(inti=0;i<50;i++)
{
synchronized(ThreadTest.class){
if(!bShouldMain)
{
try{
ThreadTest.class.wait();}
catch(InterruptedException e) {
e.printStackTrace();
}
}
for(intj=0;j<5;j++)
{
System.out.println(
Thread.currentThread().getName()+
"i=" + i +",j=" + j);
}
bShouldMain=false;
ThreadTest.class.notify();
}
}
}
}
涓嬮潰浣跨敤jdk5涓殑骞跺彂搴撴潵瀹炵幇鐨勶細
import java.util.concurrent.Executors;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.locks.Condition;
public class ThreadTest
{
private static Locklock = new ReentrantLock();
private staticCondition subThreadCondition = lock.newCondition();
private staticboolean bBhouldSubThread = false;
public static voidmain(String [] args)
{
ExecutorServicethreadPool = Executors.newFixedThreadPool(3);
threadPool.execute(newRunnable(){
publicvoid run()
{
for(inti=0;i<50;i++)
{
lock.lock();
try
{
if(!bBhouldSubThread)
subThreadCondition.await();
for(intj=0;j<10;j++)
{
System.out.println(Thread.currentThread().getName()+ ",j=" + j);
}
bBhouldSubThread= false;
subThreadCondition.signal();
}catch(Exceptione)
{
}
finally
{
lock.unlock();
}
}
}
});
threadPool.shutdown();
for(inti=0;i<50;i++)
{
lock.lock();
try
{
if(bBhouldSubThread)
subThreadCondition.await();
for(intj=0;j<10;j++)
{
System.out.println(Thread.currentThread().getName()+ ",j=" + j);
}
bBhouldSubThread= true;
subThreadCondition.signal();
}catch(Exceptione)
{
}
finally
{
lock.unlock();
}
}
}
}
57銆佷粙缁岰ollection妗嗘灦鐨勭粨鏋�
绛旓細闅忔剰鍙戞尌棰橈紝澶╁崡娴峰寳璋佷究璋堬紝鍙璁╁埆瑙夊緱浣犵煡璇嗘笂鍗氾紝鐞嗚В閫忓交鍗冲彲銆�
58銆丆ollection妗嗘灦涓疄鐜版瘮杈冭瀹炵幇浠�涔堟帴鍙�
comparable/comparator
59銆丄rrayList鍜孷ector鐨勫尯鍒�
绛旓細
杩欎袱涓被閮藉疄鐜颁簡List鎺ュ彛锛圠ist鎺ュ彛缁ф壙浜咰ollection鎺ュ彛锛夛紝浠栦滑閮芥槸鏈夊簭闆嗗悎锛屽嵆瀛樺偍鍦ㄨ繖涓や釜闆嗗悎涓殑鍏冪礌鐨勪綅缃兘鏄湁椤哄簭鐨勶紝鐩稿綋浜庝竴绉嶅姩鎬佺殑鏁扮粍锛屾垜浠互鍚庡彲浠ユ寜浣嶇疆绱㈠紩鍙峰彇鍑烘煇涓厓绱狅紝锛屽苟涓斿叾涓殑鏁版嵁鏄厑璁搁噸澶嶇殑锛岃繖鏄疕ashSet涔嬬被鐨勯泦鍚堢殑鏈�澶т笉鍚屽锛孒ashSet涔嬬被鐨勯泦鍚堜笉鍙互鎸夌储寮曞彿鍘绘绱㈠叾涓殑鍏冪礌锛屼篃涓嶅厑璁告湁閲嶅鐨勫厓绱狅紙鏈潵棰樼洰闂殑涓巋ashset娌℃湁浠讳綍鍏崇郴锛屼絾涓轰簡璇存竻妤欰rrayList涓嶸ector鐨勫姛鑳斤紝鎴戜滑浣跨敤瀵规瘮鏂瑰紡锛屾洿鏈夊埄浜庤鏄庨棶棰橈級銆�
鎺ョ潃鎵嶈ArrayList涓嶸ector鐨勫尯鍒紝杩欎富瑕佸寘鎷袱涓柟闈細.
锛�1锛夊悓姝ユ�э細
Vector鏄嚎绋嬪畨鍏ㄧ殑锛屼篃灏辨槸璇存槸瀹冪殑鏂规硶涔嬮棿鏄嚎绋嬪悓姝ョ殑锛岃�孉rrayList鏄嚎绋嬪簭涓嶅畨鍏ㄧ殑锛屽畠鐨勬柟娉曚箣闂存槸绾跨▼涓嶅悓姝ョ殑銆傚鏋滃彧鏈変竴涓嚎绋嬩細璁块棶鍒伴泦鍚堬紝閭f渶濂芥槸浣跨敤ArrayList锛屽洜涓哄畠涓嶈�冭檻绾跨▼瀹夊叏锛屾晥鐜囦細楂樹簺锛涘鏋滄湁澶氫釜绾跨▼浼氳闂埌闆嗗悎锛岄偅鏈�濂芥槸浣跨敤Vector锛屽洜涓轰笉闇�瑕佹垜浠嚜宸卞啀鍘昏�冭檻鍜岀紪鍐欑嚎绋嬪畨鍏ㄧ殑浠g爜銆�
澶囨敞锛氬浜嶸ector&ArrayList銆丠ashtable&HashMap锛岃璁颁綇绾跨▼瀹夊叏鐨勯棶棰橈紝璁颁綇Vector涓嶩ashtable鏄棫鐨勶紝鏄痡ava涓�璇炵敓灏辨彁渚涗簡鐨勶紝瀹冧滑鏄嚎绋嬪畨鍏ㄧ殑锛孉rrayList涓嶩ashMap鏄痡ava2鏃舵墠鎻愪緵鐨勶紝瀹冧滑鏄嚎绋嬩笉瀹夊叏鐨勩�傛墍浠ワ紝鎴戜滑璁茶鏃跺厛璁茶�佺殑銆�
锛�2锛夋暟鎹闀匡細
ArrayList涓嶸ector閮芥湁涓�涓垵濮嬬殑瀹归噺澶у皬锛屽綋瀛樺偍杩涘畠浠噷闈㈢殑鍏冪礌鐨勪釜鏁拌秴杩囦簡瀹归噺鏃讹紝灏遍渶瑕佸鍔燗rrayList涓嶸ector鐨勫瓨鍌ㄧ┖闂达紝姣忔瑕佸鍔犲瓨鍌ㄧ┖闂存椂锛屼笉鏄彧澧炲姞涓�涓瓨鍌ㄥ崟鍏冿紝鑰屾槸澧炲姞澶氫釜瀛樺偍鍗曞厓锛屾瘡娆″鍔犵殑瀛樺偍鍗曞厓鐨勪釜鏁板湪鍐呭瓨绌洪棿鍒╃敤涓庣▼搴忔晥鐜囦箣闂磋鍙栧緱涓�瀹氱殑骞宠 銆俈ector榛樿澧為暱涓哄師鏉ヤ袱鍊嶏紝鑰孉rrayList鐨勫闀跨瓥鐣ュ湪鏂囨。涓病鏈夋槑纭瀹氾紙浠庢簮浠g爜鐪嬪埌鐨勬槸澧為暱涓哄師鏉ョ殑1.5鍊嶏級銆侫rrayList涓嶸ector閮藉彲浠ヨ缃垵濮嬬殑绌洪棿澶у皬锛孷ector杩樺彲浠ヨ缃闀跨殑绌洪棿澶у皬锛岃�孉rrayList娌℃湁鎻愪緵璁剧疆澧為暱绌洪棿鐨勬柟娉曘��
鎬荤粨锛氬嵆Vector澧為暱鍘熸潵鐨勪竴鍊嶏紝ArrayList澧炲姞鍘熸潵鐨�0.5鍊嶃��
60銆丠ashMap鍜孒ashtable鐨勫尯鍒�
锛堟潯鐞嗕笂杩橀渶瑕佹暣鐞嗭紝涔熸槸鍏堣鐩稿悓鐐癸紝鍐嶈涓嶅悓鐐癸級
HashMap鏄疕ashtable鐨勮交閲忕骇瀹炵幇锛堥潪绾跨▼瀹夊叏鐨勫疄鐜帮級锛屼粬浠兘瀹屾垚浜哅ap鎺ュ彛锛屼富瑕佸尯鍒湪浜嶩ashMap鍏佽绌猴紙null锛夐敭鍊硷紙key锛�,鐢变簬闈炵嚎绋嬪畨鍏紝鍦ㄥ彧鏈変竴涓嚎绋嬭闂殑鎯呭喌涓嬶紝鏁堢巼瑕侀珮浜嶩ashtable銆�
HashMap鍏佽灏唍ull浣滀负涓�涓猠ntry鐨刱ey鎴栬�卾alue锛岃�孒ashtable涓嶅厑璁搞��
HashMap鎶奌ashtable鐨刢ontains鏂规硶鍘绘帀浜嗭紝鏀规垚containsvalue鍜宑ontainsKey銆傚洜涓篶ontains鏂规硶瀹规槗璁╀汉寮曡捣璇В銆�
Hashtable缁ф壙鑷狣ictionary绫伙紝鑰孒ashMap鏄疛ava1.2寮曡繘鐨凪ap interface鐨勪竴涓疄鐜般��
鏈�澶х殑涓嶅悓鏄紝Hashtable鐨勬柟娉曟槸Synchronize鐨勶紝鑰孒ashMap涓嶆槸锛屽湪澶氫釜绾跨▼璁块棶Hashtable鏃讹紝涓嶉渶瑕佽嚜宸变负瀹冪殑鏂规硶瀹炵幇鍚屾锛岃�孒ashMap灏卞繀椤讳负涔嬫彁渚涘鍚屾銆�
Hashtable鍜孒ashMap閲囩敤鐨刪ash/rehash绠楁硶閮藉ぇ姒備竴鏍凤紝鎵�浠ユ�ц兘涓嶄細鏈夊緢澶х殑宸紓銆�
灏盚ashMap涓嶩ashTable涓昏浠庝笁鏂归潰鏉ヨ銆�
涓�.鍘嗗彶鍘熷洜:Hashtable鏄熀浜庨檲鏃х殑Dictionary绫荤殑锛孒ashMap鏄疛ava 1.2寮曡繘鐨凪ap鎺ュ彛鐨勪竴涓疄鐜�
浜�.鍚屾鎬�:Hashtable鏄嚎绋嬪畨鍏ㄧ殑锛屼篃灏辨槸璇存槸鍚屾鐨勶紝鑰孒ashMap鏄嚎绋嬪簭涓嶅畨鍏ㄧ殑锛屼笉鏄悓姝ョ殑
涓�.鍊硷細鍙湁HashMap鍙互璁╀綘灏嗙┖鍊间綔涓轰竴涓〃鐨勬潯鐩殑key鎴杤alue
61銆丩ist鍜� Map鍖哄埆?
涓�涓槸瀛樺偍鍗曞垪鏁版嵁鐨勯泦鍚堬紝鍙︿竴涓槸瀛樺偍閿拰鍊艰繖鏍风殑鍙屽垪鏁版嵁鐨勯泦鍚堬紝List涓瓨鍌ㄧ殑鏁版嵁鏄湁椤哄簭锛屽苟涓斿厑璁搁噸澶嶏紱Map涓瓨鍌ㄧ殑鏁版嵁鏄病鏈夐『搴忕殑锛屽叾閿槸涓嶈兘閲嶅鐨勶紝瀹冪殑鍊兼槸鍙互鏈夐噸澶嶇殑銆�
62銆丩ist, Set, Map鏄惁缁ф壙鑷狢ollection鎺ュ彛?
List锛孲et鏄紝Map涓嶆槸
63銆丩ist銆丮ap銆丼et涓変釜鎺ュ彛锛屽瓨鍙栧厓绱犳椂锛屽悇鏈変粈涔堢壒鐐癸紵
杩欐牱鐨勯灞炰簬闅忔剰鍙戞尌棰橈細杩欐牱鐨勯姣旇緝鑰冩按骞筹紝涓や釜鏂归潰鐨勬按骞筹細涓�鏄鐪熸鏄庣櫧杩欎簺鍐呭锛屼簩鏄鏈夎緝寮虹殑鎬荤粨鍜岃〃杩拌兘鍔涖�傚鏋滀綘鏄庣櫧锛屼絾琛ㄨ堪涓嶆竻妤氾紝鍦ㄥ埆浜洪偅閲屽垯绛夊悓浜庝笉鏄庣櫧銆�
棣栧厛锛孡ist涓嶴et鍏锋湁鐩镐技鎬э紝瀹冧滑閮芥槸鍗曞垪鍏冪礌鐨勯泦鍚堬紝鎵�浠ワ紝瀹冧滑鏈変竴涓姛鍏卞悓鐨勭埗鎺ュ彛锛屽彨Collection銆係et閲岄潰涓嶅厑璁告湁閲嶅鐨勫厓绱狅紝鎵�璋撻噸澶嶏紝鍗充笉鑳芥湁涓や釜鐩哥瓑锛堟敞鎰忥紝涓嶆槸浠呬粎鏄浉鍚岋級鐨勫璞★紝鍗冲亣璁維et闆嗗悎涓湁浜嗕竴涓狝瀵硅薄锛岀幇鍦ㄦ垜瑕佸悜Set闆嗗悎鍐嶅瓨鍏ヤ竴涓狟瀵硅薄锛屼絾B瀵硅薄涓嶢瀵硅薄equals鐩哥瓑锛屽垯B瀵硅薄瀛樺偍涓嶈繘鍘伙紝鎵�浠ワ紝Set闆嗗悎鐨刟dd鏂规硶鏈変竴涓猙oolean鐨勮繑鍥炲�硷紝褰撻泦鍚堜腑娌℃湁鏌愪釜鍏冪礌锛屾鏃禷dd鏂规硶鍙垚鍔熷姞鍏ヨ鍏冪礌鏃讹紝鍒欒繑鍥瀟rue锛屽綋闆嗗悎鍚湁涓庢煇涓厓绱爀quals鐩哥瓑鐨勫厓绱犳椂锛屾鏃禷dd鏂规硶鏃犳硶鍔犲叆璇ュ厓绱狅紝杩斿洖缁撴灉涓篺alse銆係et鍙栧厓绱犳椂锛屾病娉曡鍙栫鍑犱釜锛屽彧鑳戒互Iterator鎺ュ彛鍙栧緱鎵�鏈夌殑鍏冪礌锛屽啀閫愪竴閬嶅巻鍚勪釜鍏冪礌銆�
List琛ㄧず鏈夊厛鍚庨『搴忕殑闆嗗悎锛屾敞鎰忥紝涓嶆槸閭g鎸夊勾榫勩�佹寜澶у皬銆佹寜浠锋牸涔嬬被鐨勬帓搴忋�傚綋鎴戜滑澶氭璋冪敤add(Obj e)鏂规硶鏃讹紝姣忔鍔犲叆鐨勫璞″氨鍍忕伀杞︾珯涔扮エ鏈夋帓闃熼『搴忎竴鏍凤紝鎸夊厛鏉ュ悗鍒扮殑椤哄簭鎺掑簭銆傛湁鏃跺�欙紝涔熷彲浠ユ彃闃燂紝鍗宠皟鐢╝dd(int index,Obj e)鏂规硶锛屽氨鍙互鎸囧畾褰撳墠瀵硅薄鍦ㄩ泦鍚堜腑鐨勫瓨鏀句綅缃�備竴涓璞″彲浠ヨ鍙嶅瀛樺偍杩汱ist涓紝姣忚皟鐢ㄤ竴娆dd鏂规硶锛岃繖涓璞″氨琚彃鍏ヨ繘闆嗗悎涓竴娆★紝鍏跺疄锛屽苟涓嶆槸鎶婅繖涓璞℃湰韬瓨鍌ㄨ繘浜嗛泦鍚堜腑锛岃�屾槸鍦ㄩ泦鍚堜腑鐢ㄤ竴涓储寮曞彉閲忔寚鍚戣繖涓璞★紝褰撹繖涓璞¤add澶氭鏃讹紝鍗崇浉褰撲簬闆嗗悎涓湁澶氫釜绱㈠紩鎸囧悜浜嗚繖涓璞★紝濡傚浘x鎵�绀恒�侺ist闄や簡鍙互浠terator鎺ュ彛鍙栧緱鎵�鏈夌殑鍏冪礌锛屽啀閫愪竴閬嶅巻鍚勪釜鍏冪礌涔嬪锛岃繕鍙互璋冪敤get(index i)鏉ユ槑纭鏄庡彇绗嚑涓��
Map涓嶭ist鍜孲et涓嶅悓锛屽畠鏄弻鍒楃殑闆嗗悎锛屽叾涓湁put鏂规硶锛屽畾涔夊涓嬶細put(obj key,objvalue)锛屾瘡娆″瓨鍌ㄦ椂锛岃瀛樺偍涓�瀵筴ey/value锛屼笉鑳藉瓨鍌ㄩ噸澶嶇殑key锛岃繖涓噸澶嶇殑瑙勫垯涔熸槸鎸塭quals姣旇緝鐩哥瓑銆傚彇鍒欏彲浠ユ牴鎹甼ey鑾峰緱鐩稿簲鐨剉alue锛屽嵆get(Object key)杩斿洖鍊间负key鎵�瀵瑰簲鐨剉alue銆傚彟澶栵紝涔熷彲浠ヨ幏寰楁墍鏈夌殑key鐨勭粨鍚堬紝杩樺彲浠ヨ幏寰楁墍鏈夌殑value鐨勭粨鍚堬紝杩樺彲浠ヨ幏寰梜ey鍜寁alue缁勫悎鎴愮殑Map.Entry瀵硅薄鐨勯泦鍚堛��
List 浠ョ壒瀹氭搴忔潵鎸佹湁鍏冪礌锛屽彲鏈夐噸澶嶅厓绱犮�係et鏃犳硶鎷ユ湁閲嶅鍏冪礌,鍐呴儴鎺掑簭銆侻ap淇濆瓨key-value鍊硷紝value鍙鍊笺��
HashSet鎸夌収hashcode鍊肩殑鏌愮杩愮畻鏂瑰紡杩涜瀛樺偍锛岃�屼笉鏄洿鎺ユ寜hashCode鍊肩殑澶у皬杩涜瀛樺偍銆備緥濡傦紝"abc"---> 78锛�"def" ---> 62锛�"xyz" ---> 65鍦╤ashSet涓殑瀛樺偍椤哄簭涓嶆槸62,65,78锛岃繖浜涢棶棰樻劅璋互鍓嶄竴涓彨宕斿仴鐨勫鍛樻彁鍑猴紝鏈�鍚庨�氳繃鏌ョ湅婧愪唬鐮佺粰浠栬В閲婃竻妤氾紝鐪嬫湰娆″煿璁鍛樺綋涓湁澶氬皯鑳界湅鎳傛簮鐮併�侺inkedHashSet鎸夋彃鍏ョ殑椤哄簭瀛樺偍锛岄偅琚瓨鍌ㄥ璞$殑hashcode鏂规硶杩樻湁浠�涔堜綔鐢ㄥ憿锛熷鍛樻兂鎯�!hashset闆嗗悎姣旇緝涓や釜瀵硅薄鏄惁鐩哥瓑锛岄鍏堢湅hashcode鏂规硶鏄惁鐩哥瓑锛岀劧鍚庣湅equals鏂规硶鏄惁鐩哥瓑銆俷ew涓や釜Student鎻掑叆鍒癏ashSet涓紝鐪婬ashSet鐨剆ize锛屽疄鐜癶ashcode鍜宔quals鏂规硶鍚庡啀鐪媠ize銆�
鍚屼竴涓璞″彲浠ュ湪Vector涓姞鍏ュ娆°�傚線闆嗗悎閲岄潰鍔犲厓绱狅紝鐩稿綋浜庨泦鍚堥噷鐢ㄤ竴鏍圭怀瀛愯繛鎺ュ埌浜嗙洰鏍囧璞°�傚線HashSet涓嵈鍔犱笉浜嗗娆$殑銆�
64銆佽鍑篈rrayList,Vector, LinkedList鐨勫瓨鍌ㄦ�ц兘鍜岀壒鎬�
ArrayList鍜孷ector閮芥槸浣跨敤鏁扮粍鏂瑰紡瀛樺偍鏁版嵁锛屾鏁扮粍鍏冪礌鏁板ぇ浜庡疄闄呭瓨鍌ㄧ殑鏁版嵁浠ヤ究澧炲姞鍜屾彃鍏ュ厓绱狅紝瀹冧滑閮藉厑璁哥洿鎺ユ寜搴忓彿绱㈠紩鍏冪礌锛屼絾鏄彃鍏ュ厓绱犺娑夊強鏁扮粍鍏冪礌绉诲姩绛夊唴瀛樻搷浣滐紝鎵�浠ョ储寮曟暟鎹揩鑰屾彃鍏ユ暟鎹參锛孷ector鐢变簬浣跨敤浜唖ynchronized鏂规硶锛堢嚎绋嬪畨鍏級锛岄�氬父鎬ц兘涓婅緝ArrayList宸紝鑰孡inkedList浣跨敤鍙屽悜閾捐〃瀹炵幇瀛樺偍锛屾寜搴忓彿绱㈠紩鏁版嵁闇�瑕佽繘琛屽墠鍚戞垨鍚庡悜閬嶅巻锛屼絾鏄彃鍏ユ暟鎹椂鍙渶瑕佽褰曟湰椤圭殑鍓嶅悗椤瑰嵆鍙紝鎵�浠ユ彃鍏ラ�熷害杈冨揩銆�
LinkedList涔熸槸绾跨▼涓嶅畨鍏ㄧ殑锛孡inkedList鎻愪緵浜嗕竴浜涙柟娉曪紝浣垮緱LinkedList鍙互琚綋浣滃爢鏍堝拰闃熷垪鏉ヤ娇鐢ㄣ��
65銆佸幓鎺変竴涓猇ector闆嗗悎涓噸澶嶇殑鍏冪礌
Vector newVector = new Vector();
For (int i=0;i
{
Object obj = vector.get(i);
if(!newVector.contains(obj);
newVector.add(obj);
}
杩樻湁涓�绉嶇畝鍗曠殑鏂瑰紡锛孒ashSet set = new HashSet(vector);
66銆丆ollection鍜� Collections鐨勫尯鍒��
Collection鏄泦鍚堢被鐨勪笂绾ф帴鍙o紝缁ф壙涓庝粬鐨勬帴鍙d富瑕佹湁Set鍜孡ist.
Collections鏄拡瀵归泦鍚堢被鐨勪竴涓府鍔╃被锛屼粬鎻愪緵涓�绯诲垪闈欐�佹柟娉曞疄鐜板鍚勭闆嗗悎鐨勬悳绱€�佹帓搴忋�佺嚎绋嬪畨鍏ㄥ寲绛夋搷浣溿��
67銆丼et閲岀殑鍏冪礌鏄笉鑳介噸澶嶇殑锛岄偅涔堢敤浠�涔堟柟娉曟潵鍖哄垎閲嶅涓庡惁鍛�?鏄敤==杩樻槸equals()?瀹冧滑鏈変綍鍖哄埆?
Set閲岀殑鍏冪礌鏄笉鑳介噸澶嶇殑锛屽厓绱犻噸澶嶄笌鍚︽槸浣跨敤equals()鏂规硶杩涜鍒ゆ柇鐨勩��
equals()鍜�==鏂规硶鍐冲畾寮曠敤鍊兼槸鍚︽寚鍚戝悓涓�瀵硅薄equals()鍦ㄧ被涓瑕嗙洊锛屼负鐨勬槸褰撲袱涓垎绂荤殑瀵硅薄鐨勫唴瀹瑰拰绫诲瀷鐩搁厤鐨勮瘽锛岃繑鍥炵湡鍊笺��
68銆佷綘鎵�鐭ラ亾鐨勯泦鍚堢被閮芥湁鍝簺锛熶富瑕佹柟娉曪紵
鏈�甯哥敤鐨勯泦鍚堢被鏄� List 鍜� Map銆� List鐨勫叿浣撳疄鐜板寘鎷� ArrayList鍜� Vector锛屽畠浠槸鍙彉澶у皬鐨勫垪琛紝姣旇緝閫傚悎鏋勫缓銆佸瓨鍌ㄥ拰鎿嶄綔浠讳綍绫诲瀷瀵硅薄鐨勫厓绱犲垪琛ㄣ�� List閫傜敤浜庢寜鏁板�肩储寮曡闂厓绱犵殑鎯呭舰銆�
Map 鎻愪緵浜嗕竴涓洿閫氱敤鐨勫厓绱犲瓨鍌ㄦ柟娉曘�� Map闆嗗悎绫荤敤浜庡瓨鍌ㄥ厓绱犲锛堢О浣�"閿�"鍜�"鍊�"锛夛紝鍏朵腑姣忎釜閿槧灏勫埌涓�涓�笺��
ArrayList/Vector脿List
脿Collection
HashSet/TreeSet脿Set
Propeties脿HashTable
脿Map
Treemap/HashMap
鎴戣鐨勪笉鏄柟娉曞悕锛岃�屾槸鎬濇兂锛屾垜鐭ラ亾瀹冧滑閮芥湁澧炲垹鏀规煡鐨勬柟娉曪紝浣嗚繖浜涙柟娉曠殑鍏蜂綋鍚嶇О锛屾垜璁板緱涓嶆槸寰堟竻妤氾紝瀵逛簬set锛屽ぇ姒傜殑鏂规硶鏄痑dd,remove, contains锛涘浜巑ap锛屽ぇ姒傜殑鏂规硶灏辨槸put,remove锛宑ontains绛夛紝鍥犱负锛屾垜鍙鍦╡clispe涓嬫寜鐐规搷浣滅锛屽緢鑷劧鐨勮繖浜涙柟娉曞氨鍑烘潵浜嗐�傛垜璁颁綇鐨勪竴浜涙�濇兂灏辨槸List绫讳細鏈塯et(int index)杩欐牱鐨勬柟娉曪紝鍥犱负瀹冨彲浠ユ寜椤哄簭鍙栧厓绱狅紝鑰宻et绫讳腑娌℃湁get(int index)杩欐牱鐨勬柟娉曘�侺ist鍜宻et閮藉彲浠ヨ凯浠e嚭鎵�鏈夊厓绱狅紝杩唬鏃跺厛瑕佸緱鍒颁竴涓猧terator瀵硅薄锛屾墍浠ワ紝set鍜宭ist绫婚兘鏈変竴涓猧terator鏂规硶锛岀敤浜庤繑鍥為偅涓猧terator瀵硅薄銆俶ap鍙互杩斿洖涓変釜闆嗗悎锛屼竴涓槸杩斿洖鎵�鏈夌殑key鐨勯泦鍚堬紝鍙﹀涓�涓繑鍥炵殑鏄墍鏈塿alue鐨勯泦鍚堬紝鍐嶄竴涓繑鍥炵殑key鍜寁alue缁勫悎鎴愮殑EntrySet瀵硅薄鐨勯泦鍚堬紝map涔熸湁get鏂规硶锛屽弬鏁版槸key锛岃繑鍥炲�兼槸key瀵瑰簲鐨剉alue銆�
69銆佷袱涓璞″�肩浉鍚�(x.equals(y) == true)锛屼絾鍗村彲鏈変笉鍚岀殑hash code锛岃繖鍙ヨ瘽瀵逛笉瀵�?
瀵广��
濡傛灉瀵硅薄瑕佷繚瀛樺湪HashSet鎴朒ashMap涓紝瀹冧滑鐨別quals鐩哥瓑锛岄偅涔堬紝瀹冧滑鐨刪ashcode鍊煎氨蹇呴』鐩哥瓑銆�
濡傛灉涓嶆槸瑕佷繚瀛樺湪HashSet鎴朒ashMap锛屽垯涓巋ashcode娌℃湁浠�涔堝叧绯讳簡锛岃繖鏃跺�檋ashcode涓嶇瓑鏄彲浠ョ殑锛屼緥濡俛rrayList瀛樺偍鐨勫璞″氨涓嶇敤瀹炵幇hashcode锛屽綋鐒讹紝鎴戜滑娌℃湁鐞嗙敱涓嶅疄鐜帮紝閫氬父閮戒細鍘诲疄鐜扮殑銆�
70銆乀reeSet閲岄潰鏀惧璞★紝濡傛灉鍚屾椂鏀惧叆浜嗙埗绫诲拰瀛愮被鐨勫疄渚嬪璞★紝閭f瘮杈冩椂浣跨敤鐨勬槸鐖剁被鐨刢ompareTo鏂规硶锛岃繕鏄娇鐢ㄧ殑瀛愮被鐨刢ompareTo鏂规硶锛岃繕鏄姏寮傚父锛�
锛堝簲璇ユ槸娌℃湁閽堝闂鐨勭‘鍒囩殑绛旀锛屽綋鍓嶇殑add鏂规硶鏀惧叆鐨勬槸鍝釜瀵硅薄锛屽氨璋冪敤鍝釜瀵硅薄鐨刢ompareTo鏂规硶锛岃嚦浜庤繖涓猚ompareTo鏂规硶鎬庝箞鍋氾紝灏辩湅褰撳墠杩欎釜瀵硅薄鐨勭被涓槸濡備綍缂栧啓杩欎釜鏂规硶鐨勶級
瀹為獙浠g爜锛�
publicclassParentimplementsComparable {
private intage = 0;
publicParent(intage){
this.age = age;
}
public intcompareTo(Object o){
//TODOAuto-generated method stub
System.out.println("method ofparent");
Parent o1 = (Parent)o;
returnage>o1.age?1:age
}
}
publicclassChildextendsParent {
publicChild(){
super(3);
}
public intcompareTo(Object o){
//TODOAuto-generated methodstub
System.out.println("methodof child");
//Child o1 = (Child)o;
return1;
}
}
publicclassTreeSetTest {
/**
*@paramargs
*/
public static voidmain(String[] args) {
//TODOAuto-generated method stub
TreeSet set =newTreeSet();
set.add(newParent(3));
set.add(newChild());
set.add(newParent(4));
System.out.println(set.size());
}
}
71銆佽鍑轰竴浜涘父鐢ㄧ殑绫伙紝鍖咃紝鎺ュ彛锛岃鍚勪妇5涓�
瑕佽浜哄鎰熻浣犲java ee寮�鍙戝緢鐔燂紝鎵�浠ワ紝涓嶈兘浠呬粎鍙垪core java涓殑閭d簺涓滆タ锛岃澶氬垪浣犲湪鍋歴sh椤圭洰涓秹鍙婄殑閭d簺涓滆タ銆傚氨鍐欎綘鏈�杩戝啓鐨勯偅浜涚▼搴忎腑娑夊強鐨勯偅浜涚被銆�
甯哥敤鐨勭被锛欱ufferedReaderBufferedWriter 聽FileReader FileWirter 聽String 聽Integer
java.util.Date锛孲ystem锛孋lass锛孡ist,HashMap
甯哥敤鐨勫寘锛歫ava.langjava.io java.util 聽java.sql,javax.servlet,org.apache.strtuts.action,org.hibernate
甯哥敤鐨勬帴鍙o細RemoteList Map 聽Document 聽NodeList,Servlet,HttpServletRequest,HttpServletResponse,Transaction(Hibernate)銆丼ession(Hibernate),HttpSession
72銆乯ava涓湁鍑犵绫诲瀷鐨勬祦锛烰DK涓烘瘡绉嶇被鍨嬬殑娴佹彁渚涗簡涓�浜涙娊璞$被浠ヤ緵缁ф壙锛岃璇村嚭浠栦滑鍒嗗埆鏄摢浜涚被锛�
瀛楄妭娴侊紝瀛楃娴併�傚瓧鑺傛祦缁ф壙浜嶪nputStream OutputStream锛屽瓧绗︽祦缁ф壙浜嶪nputStreamReaderOutputStreamWriter銆傚湪java.io鍖呬腑杩樻湁璁稿鍏朵粬鐨勬祦锛屼富瑕佹槸涓轰簡鎻愰珮鎬ц兘鍜屼娇鐢ㄦ柟渚裤��
73銆佸瓧鑺傛祦涓庡瓧绗︽祦鐨勫尯鍒�
瑕佹妸涓�鐗囦簩杩涘埗鏁版嵁鏁版嵁閫愪竴杈撳嚭鍒版煇涓澶囦腑锛屾垨鑰呬粠鏌愪釜璁惧涓�愪竴璇诲彇涓�鐗囦簩杩涘埗鏁版嵁锛屼笉绠¤緭鍏ヨ緭鍑鸿澶囨槸浠�涔堬紝鎴戜滑瑕佺敤缁熶竴鐨勬柟寮忔潵瀹屾垚杩欎簺鎿嶄綔锛岀敤涓�绉嶆娊璞$殑鏂瑰紡杩涜鎻忚堪锛岃繖涓娊璞℃弿杩版柟寮忚捣鍚嶄负IO娴侊紝瀵瑰簲鐨勬娊璞$被涓篛utputStream鍜孖nputStream锛屼笉鍚岀殑瀹炵幇绫诲氨浠h〃涓嶅悓鐨勮緭鍏ュ拰杈撳嚭璁惧锛屽畠浠兘鏄拡瀵瑰瓧鑺傝繘琛屾搷浣滅殑銆�
鍦ㄥ簲鐢ㄤ腑锛岀粡甯歌瀹屽叏鏄瓧绗︾殑涓�娈垫枃鏈緭鍑哄幓鎴栬杩涙潵锛岀敤瀛楄妭娴佸彲浠ュ悧锛熻绠楁満涓殑涓�鍒囨渶缁堥兘鏄簩杩涘埗鐨勫瓧鑺傚舰寮忓瓨鍦ㄣ�傚浜庘�滀腑鍥解�濊繖浜涘瓧绗︼紝棣栧厛瑕佸緱鍒板叾瀵瑰簲鐨勫瓧鑺傦紝鐒跺悗灏嗗瓧鑺傚啓鍏ュ埌杈撳嚭娴併�傝鍙栨椂锛岄鍏堣鍒扮殑鏄瓧鑺傦紝鍙槸鎴戜滑瑕佹妸瀹冩樉绀轰负瀛楃锛屾垜浠渶瑕佸皢瀛楄妭杞崲鎴愬瓧绗︺�傜敱浜庤繖鏍风殑闇�姹傚緢骞挎硾锛屼汉瀹朵笓闂ㄦ彁渚涗簡瀛楃娴佺殑鍖呰绫汇��
搴曞眰璁惧姘歌繙鍙帴鍙楀瓧鑺傛暟鎹紝鏈夋椂鍊欒鍐欏瓧绗︿覆鍒板簳灞傝澶囷紝闇�瑕佸皢瀛楃涓茶浆鎴愬瓧鑺傚啀杩涜鍐欏叆銆�瀛楃娴佹槸瀛楄妭娴佺殑鍖呰锛�瀛楃娴佸垯鏄洿鎺ユ帴鍙楀瓧绗︿覆锛屽畠鍐呴儴灏嗕覆杞垚瀛楄妭锛屽啀鍐欏叆搴曞眰璁惧锛岃繖涓烘垜浠悜IO璁惧埆鍐欏叆鎴栬鍙栧瓧绗︿覆鎻愪緵浜嗕竴鐐圭偣鏂逛究銆�
瀛楃鍚戝瓧鑺傝浆鎹㈡椂锛岃娉ㄦ剰缂栫爜鐨勯棶棰�锛屽洜涓哄瓧绗︿覆杞垚瀛楄妭鏁扮粍锛�
鍏跺疄鏄浆鎴愯瀛楃鐨勬煇绉嶇紪鐮佺殑瀛楄妭褰㈠紡锛岃鍙栦篃鏄弽涔嬬殑閬撶悊銆�
璁茶В瀛楄妭娴佷笌瀛楃娴佸叧绯荤殑浠g爜妗堜緥锛�
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.io.PrintWriter;
public class IOTest {
public static void main(String[]args) throws Exception {
String str = "涓浗浜�";
/*FileOutputStreamfos 聽= newFileOutputStream("1.txt");
fos.write(str.getBytes("UTF-8"));
fos.close();*/
/*FileWriter fw =new FileWriter("1.txt");
fw.write(str);
fw.close();*/
PrintWriter pw =new PrintWriter("1.txt","utf-8");
pw.write(str);
pw.close();
/*FileReader fr =new FileReader("1.txt");
char[] buf = newchar[1024];
int len =fr.read(buf);
String myStr = newString(buf,0,len);
System.out.println(myStr);*/
/*FileInputStreamfr = new FileInputStream("1.txt");
byte[] buf = newbyte[1024];
int len =fr.read(buf);
String myStr = newString(buf,0,len,"UTF-8");
System.out.println(myStr);*/
BufferedReader br =new BufferedReader(
newInputStreamReader(
newFileInputStream("1.txt"),"UTF-8"
)
);
String myStr =br.readLine();
br.close();
System.out.println(myStr);
}
}
74銆佷粈涔堟槸java搴忓垪鍖栵紝濡備綍瀹炵幇java搴忓垪鍖栵紵鎴栬�呰瑙i噴Serializable鎺ュ彛鐨勪綔鐢ㄣ��
鎴戜滑鏈夋椂鍊欏皢涓�涓猨ava瀵硅薄鍙樻垚瀛楄妭娴佺殑褰㈠紡浼犲嚭鍘绘垨鑰呬粠涓�涓瓧鑺傛祦涓仮澶嶆垚涓�涓猨ava瀵硅薄锛屼緥濡傦紝瑕佸皢java瀵硅薄瀛樺偍鍒扮‖鐩樻垨鑰呬紶閫佺粰缃戠粶涓婄殑鍏朵粬璁$畻鏈猴紝杩欎釜杩囩▼鎴戜滑鍙互鑷繁鍐欎唬鐮佸幓鎶婁竴涓猨ava瀵硅薄鍙樻垚鏌愪釜鏍煎紡鐨勫瓧鑺傛祦鍐嶄紶杈擄紝浣嗘槸锛宩re鏈韩灏辨彁渚涗簡杩欑鏀寔锛屾垜浠彲浠ヨ皟鐢∣utputStream鐨剋riteObject鏂规硶鏉ュ仛锛屽鏋滆璁﹋ava甯垜浠仛锛�瑕佽浼犺緭鐨勫璞″繀椤诲疄鐜皊erializable鎺ュ彛锛岃繖鏍凤紝javac缂栬瘧鏃跺氨浼氳繘琛岀壒娈婂鐞嗭紝缂栬瘧鐨勭被鎵嶅彲浠ヨwriteObject鏂规硶鎿嶄綔锛岃繖灏辨槸鎵�璋撶殑搴忓垪鍖栥�傞渶瑕佽搴忓垪鍖栫殑绫诲繀椤诲疄鐜癝erializable鎺ュ彛锛岃鎺ュ彛鏄竴涓猰ini鎺ュ彛锛屽叾涓病鏈夐渶瑕佸疄鐜扮殑鏂规硶锛宨mplementsSerializable鍙槸涓轰簡鏍囨敞璇ュ璞℃槸鍙搴忓垪鍖栫殑銆�
渚嬪锛屽湪web寮�鍙戜腑锛屽鏋滃璞¤淇濆瓨鍦ㄤ簡Session涓紝tomcat鍦ㄩ噸鍚椂瑕佹妸Session瀵硅薄搴忓垪鍖栧埌纭洏锛岃繖涓璞″氨蹇呴』瀹炵幇Serializable鎺ュ彛銆傚鏋滃璞¤缁忚繃鍒嗗竷寮忕郴缁熻繘琛岀綉缁滀紶杈撴垨閫氳繃rmi绛夎繙绋嬭皟鐢紝杩欏氨闇�瑕佸湪缃戠粶涓婁紶杈撳璞★紝琚紶杈撶殑瀵硅薄灏卞繀椤诲疄鐜癝erializable鎺ュ彛銆�
75銆佹弿杩颁竴涓婮VM鍔犺浇class鏂囦欢鐨勫師鐞嗘満鍒�?
JVM涓被鐨勮杞芥槸鐢盋lassLoader鍜屽畠鐨勫瓙绫绘潵瀹炵幇鐨�,Java ClassLoader鏄竴涓噸瑕佺殑Java杩愯鏃剁郴缁熺粍浠躲�傚畠璐熻矗鍦ㄨ繍琛屾椂鏌ユ壘鍜岃鍏ョ被鏂囦欢鐨勭被銆�
76銆乭eap鍜宻tack鏈変粈涔堝尯鍒��
java鐨勫唴瀛樺垎涓轰袱绫伙紝涓�绫绘槸鏍堝唴瀛橈紝涓�绫绘槸鍫嗗唴瀛樸�傛爤鍐呭瓨鏄寚绋嬪簭杩涘叆涓�涓柟娉曟椂锛屼細涓鸿繖涓柟娉曞崟鐙垎閰嶄竴鍧楃灞炲瓨鍌ㄧ┖闂达紝鐢ㄤ簬瀛樺偍杩欎釜鏂规硶鍐呴儴鐨勫眬閮ㄥ彉閲忥紝褰撹繖涓柟娉曠粨鏉熸椂锛屽垎閰嶇粰杩欎釜鏂规硶鐨勬爤浼氶噴鏀撅紝杩欎釜鏍堜腑鐨勫彉閲忎篃灏嗛殢涔嬮噴鏀俱��
鍫嗘槸涓庢爤浣滅敤涓嶅悓鐨勫唴瀛橈紝涓�鑸敤浜庡瓨鏀句笉鏀惧湪褰撳墠鏂规硶鏍堜腑鐨勯偅浜涙暟鎹紝渚嬪锛屼娇鐢╪ew鍒涘缓鐨勫璞¢兘鏀惧湪鍫嗛噷锛屾墍浠ワ紝瀹冧笉浼氶殢鏂规硶鐨勭粨鏉熻�屾秷澶便��鏂规硶涓殑灞�閮ㄥ彉閲忎娇鐢╢inal淇グ鍚庯紝鏀惧湪鍫嗕腑锛岃�屼笉鏄爤涓��
77銆丟C鏄粈涔�?涓轰粈涔堣鏈塆C?
GC鏄瀮鍦炬敹闆嗙殑鎰忔�濓紙Gabage Collection锛�,鍐呭瓨澶勭悊鏄紪绋嬩汉鍛樺鏄撳嚭鐜伴棶棰樼殑鍦版柟锛屽繕璁版垨鑰呴敊璇殑鍐呭瓨鍥炴敹浼氬鑷寸▼搴忔垨绯荤粺鐨勪笉绋冲畾鐢氳嚦宕╂簝锛孞ava鎻愪緵鐨凣C鍔熻兘鍙互鑷姩鐩戞祴瀵硅薄鏄惁瓒呰繃浣滅敤鍩熶粠鑰岃揪鍒�鑷姩鍥炴敹鍐呭瓨鐨勭洰鐨勶紝Java璇█娌℃湁鎻愪緵閲婃斁宸插垎閰嶅唴瀛樼殑鏄剧ず鎿嶄綔鏂规硶銆�
78銆佸瀮鍦惧洖鏀剁殑浼樼偣鍜屽師鐞嗐�傚苟鑰冭檻2绉嶅洖鏀舵満鍒躲��
Java璇█涓竴涓樉钁楃殑鐗圭偣灏辨槸寮曞叆浜嗗瀮鍦惧洖鏀舵満鍒讹紝浣縞++绋嬪簭鍛樻渶澶寸柤鐨勫唴瀛樼鐞嗙殑闂杩庡垉鑰岃В锛屽畠浣垮緱Java绋嬪簭鍛樺湪缂栧啓绋嬪簭鐨勬椂鍊欎笉鍐嶉渶瑕佽�冭檻鍐呭瓨绠$悊銆傜敱浜庢湁涓瀮鍦惧洖鏀舵満鍒讹紝Java涓殑瀵硅薄涓嶅啀鏈�"浣滅敤鍩�"鐨勬蹇碉紝鍙湁瀵硅薄鐨勫紩鐢ㄦ墠鏈�"浣滅敤鍩�"銆傚瀮鍦惧洖鏀跺彲浠ユ湁鏁堢殑闃叉鍐呭瓨娉勯湶锛屾湁鏁堢殑浣跨敤鍙互浣跨敤鐨勫唴瀛樸�傚瀮鍦惧洖鏀跺櫒閫氬父鏄綔涓轰竴涓崟鐙殑浣庣骇鍒殑绾跨▼杩愯锛屼笉鍙鐭ョ殑鎯呭喌涓嬪鍐呭瓨鍫嗕腑宸茬粡姝讳骸鐨勬垨鑰呴暱鏃堕棿娌℃湁浣跨敤鐨勫璞¤繘琛屾竻妤氬拰鍥炴敹锛岀▼搴忓憳涓嶈兘瀹炴椂鐨勮皟鐢ㄥ瀮鍦惧洖鏀跺櫒瀵规煇涓璞℃垨鎵�鏈夊璞¤繘琛屽瀮鍦惧洖鏀躲�傚洖鏀舵満鍒舵湁鍒嗕唬澶嶅埗鍨冨溇鍥炴敹鍜屾爣璁板瀮鍦惧洖鏀讹紝澧為噺鍨冨溇鍥炴敹銆�
79銆佸瀮鍦惧洖鏀跺櫒鐨勫熀鏈師鐞嗘槸浠�涔堬紵鍨冨溇鍥炴敹鍣ㄥ彲浠ラ┈涓婂洖鏀跺唴瀛樺悧锛熸湁浠�涔堝姙娉曚富鍔ㄩ�氱煡铏氭嫙鏈鸿繘琛屽瀮鍦惧洖鏀讹紵
瀵逛簬GC鏉ヨ锛屽綋绋嬪簭鍛樺垱寤哄璞℃椂锛孏C灏卞紑濮嬬洃鎺ц繖涓璞$殑鍦板潃銆佸ぇ灏忎互鍙婁娇鐢ㄦ儏鍐点�傞�氬父锛孏C閲囩敤鏈夊悜鍥剧殑鏂瑰紡璁板綍鍜岀鐞�鍫�(heap)涓殑鎵�鏈夊璞°�傞�氳繃杩欑鏂瑰紡纭畾鍝簺瀵硅薄鏄�"鍙揪鐨�"锛屽摢浜涘璞℃槸"涓嶅彲杈剧殑"銆傚綋GC纭畾涓�浜涘璞′负"涓嶅彲杈�"鏃讹紝GC灏辨湁璐d换鍥炴敹杩欎簺鍐呭瓨绌洪棿銆傚彲浠ャ�傜▼搴忓憳鍙互鎵嬪姩鎵цSystem.gc()锛岄�氱煡GC杩愯锛屼絾鏄疛ava璇█瑙勮寖骞朵笉淇濊瘉GC涓�瀹氫細鎵ц銆�
80銆佷粈涔堟椂鍊欑敤assert銆�
assertion(鏂█)鍦ㄨ蒋浠跺紑鍙戜腑鏄竴绉嶅父鐢ㄧ殑璋冭瘯鏂瑰紡锛屽緢澶氬紑鍙戣瑷�涓兘鏀寔杩欑鏈哄埗銆傚湪瀹炵幇涓紝assertion灏辨槸鍦ㄧ▼搴忎腑鐨勪竴鏉¤鍙ワ紝瀹冨涓�涓猙oolean琛ㄨ揪寮忚繘琛屾鏌ワ紝涓�涓纭▼搴忓繀椤讳繚璇佽繖涓猙oolean琛ㄨ揪寮忕殑鍊间负true锛涘鏋滆鍊间负false锛岃鏄庣▼搴忓凡缁忓浜庝笉姝g‘鐨勭姸鎬佷笅锛宎ssert灏嗙粰鍑鸿鍛婃垨閫�鍑恒�備竴鑸潵璇达紝assertion鐢ㄤ簬淇濊瘉绋嬪簭鏈�鍩烘湰銆佸叧閿殑姝g‘鎬с��assertion妫�鏌ラ�氬父鍦ㄥ紑鍙戝拰娴嬭瘯鏃跺紑鍚�備负浜嗘彁楂樻�ц兘锛屽湪杞欢鍙戝竷鍚庯紝assertion妫�鏌ラ�氬父鏄叧闂殑銆�
packagecom.huawei.interview;
publicclassAssertTest {
/**
*@paramargs
*/
public static voidmain(String[] args) {
//TODOAuto-generated method stub
inti = 0;
for(i=0;i<5;i++)
{
System.out.println(i);
}
//鍋囪绋嬪簭涓嶅皬蹇冨浜嗕竴鍙�--i;
--i;
asserti==5;
}
}
81銆乯ava涓細瀛樺湪鍐呭瓨娉勬紡鍚楋紝璇风畝鍗曟弿杩般��
鎵�璋撳唴瀛樻硠闇插氨鏄寚涓�涓笉鍐嶈绋嬪簭浣跨敤鐨勫璞℃垨鍙橀噺涓�鐩磋鍗犳嵁鍦ㄥ唴瀛樹腑銆俲ava涓湁鍨冨溇鍥炴敹鏈哄埗锛屽畠鍙互淇濊瘉涓�瀵硅薄涓嶅啀琚紩鐢ㄧ殑鏃跺�欙紝鍗冲璞$紪绋嬩簡瀛ゅ効鐨勬椂鍊欙紝瀵硅薄灏嗚嚜鍔ㄨ鍨冨溇鍥炴敹鍣ㄤ粠鍐呭瓨涓竻闄ゆ帀銆傜敱浜嶫ava浣跨敤鏈夊悜鍥剧殑鏂瑰紡杩涜鍨冨溇鍥炴敹绠$悊锛屽彲浠ユ秷闄ゅ紩鐢ㄥ惊鐜殑闂锛屼緥濡傛湁涓や釜瀵硅薄锛岀浉浜掑紩鐢紝鍙瀹冧滑鍜屾牴杩涚▼涓嶅彲杈剧殑锛岄偅涔圙C涔熸槸鍙互鍥炴敹瀹冧滑鐨勶紝渚嬪涓嬮潰鐨勪唬鐮佸彲浠ョ湅鍒拌繖绉嶆儏鍐电殑鍐呭瓨鍥炴敹锛�
packagecom.huawei.interview;
importjava.io.IOException;
publicclassGarbageTest {
/**
*@paramargs
*@throwsIOException
*/
public static voidmain(String[] args)throwsIOException {
//TODOAuto-generated method stub
try{
gcTest();
}catch(IOException e) {
//TODOAuto-generated catch block
e.printStackTrace();
}
System.out.println("hasexited gcTest!");
System.in.read();
System.in.read();
System.out.println("out begingc!");
for(inti=0;i<100;i++)
{
System.gc();
System.in.read();
System.in.read();
}
}
private static voidgcTest()throwsIOException {
System.in.read();
System.in.read();
Person p1 =newPerson();
System.in.read();
System.in.read();
Person p2 =newPerson();
p1.setMate(p2);
p2.setMate(p1);
System.out.println("beforeexit gctest!");
System.in.read();
System.in.read();
System.gc();
System.out.println("exitgctest!");
}
private static classPerson
{
byte[] data =new byte[20000000];
Person mate =null;
public voidsetMate(Personother)
{
mate = other;
}
}
}
java涓殑鍐呭瓨娉勯湶鐨勬儏鍐碉細闀跨敓鍛藉懆鏈熺殑瀵硅薄鎸佹湁鐭敓鍛藉懆鏈熷璞$殑寮曠敤灏卞緢鍙兘鍙戠敓鍐呭瓨娉勯湶锛屽敖绠$煭鐢熷懡鍛ㄦ湡瀵硅薄宸茬粡涓嶅啀闇�瑕侊紝浣嗘槸鍥犱负闀跨敓鍛藉懆鏈熷璞℃寔鏈夊畠鐨勫紩鐢ㄨ�屽鑷翠笉鑳借鍥炴敹锛岃繖灏辨槸java涓唴瀛樻硠闇茬殑鍙戠敓鍦烘櫙锛岄�氫織鍦拌锛屽氨鏄▼搴忓憳鍙兘鍒涘缓浜嗕竴涓璞★紝浠ュ悗涓�鐩翠笉鍐嶄娇鐢ㄨ繖涓璞★紝杩欎釜瀵硅薄鍗翠竴鐩磋寮曠敤锛屽嵆杩欎釜瀵硅薄鏃犵敤浣嗘槸鍗存棤娉曡鍨冨溇鍥炴敹鍣ㄥ洖鏀剁殑锛岃繖灏辨槸java涓彲鑳藉嚭鐜板唴瀛樻硠闇茬殑鎯呭喌锛屼緥濡傦紝缂撳瓨绯荤粺锛屾垜浠姞杞戒簡涓�涓璞℃斁鍦ㄧ紦瀛樹腑(渚嬪鏀惧湪涓�涓叏灞�map瀵硅薄涓�)锛岀劧鍚庝竴鐩翠笉鍐嶄娇鐢ㄥ畠锛岃繖涓璞′竴鐩磋缂撳瓨寮曠敤锛屼絾鍗翠笉鍐嶈浣跨敤銆�
妫�鏌ava涓殑鍐呭瓨娉勯湶锛屼竴瀹氳璁╃▼搴忓皢鍚勭鍒嗘敮鎯呭喌閮藉畬鏁存墽琛屽埌绋嬪簭缁撴潫锛岀劧鍚庣湅鏌愪釜瀵硅薄鏄惁琚娇鐢ㄨ繃锛屽鏋滄病鏈夛紝鍒欐墠鑳藉垽瀹氳繖涓璞″睘浜庡唴瀛樻硠闇层��
濡傛灉涓�涓閮ㄧ被鐨勫疄渚嬪璞$殑鏂规硶杩斿洖浜嗕竴涓唴閮ㄧ被鐨勫疄渚嬪璞★紝杩欎釜鍐呴儴绫诲璞¤闀挎湡寮曠敤浜嗭紝鍗充娇閭d釜澶栭儴绫诲疄渚嬪璞′笉鍐嶈浣跨敤锛屼絾鐢变簬鍐呴儴绫绘寔涔呭閮ㄧ被鐨勫疄渚嬪璞★紝杩欎釜澶栭儴绫诲璞″皢涓嶄細琚瀮鍦惧洖鏀讹紝杩欎篃浼氶�犳垚鍐呭瓨娉勯湶銆�
涓嬮潰鍐呭鏉ヨ嚜浜庣綉涓婏紙涓昏鐗圭偣灏辨槸娓呯┖鍫嗘爤涓殑鏌愪釜鍏冪礌锛屽苟涓嶆槸褰诲簳鎶婂畠浠庢暟缁勪腑鎷挎帀锛岃�屾槸鎶婂瓨鍌ㄧ殑鎬绘暟鍑忓皯锛屾湰浜哄啓寰楀彲浠ユ瘮杩欎釜濂斤紝鍦ㄦ嬁鎺夋煇涓厓绱犳椂锛岄『渚夸篃璁╁畠浠庢暟缁勪腑娑堝け锛屽皢閭d釜鍏冪礌鎵�鍦ㄧ殑浣嶇疆鐨勫�艰缃负null鍗冲彲锛夛細
鎴戝疄鍦ㄦ兂涓嶅埌姣旈偅涓爢鏍堟洿缁忓吀鐨勪緥瀛愪簡,浠ヨ嚧浜庢垜杩樿寮曠敤鍒汉鐨勪緥瀛愶紝涓嬮潰鐨勪緥瀛愪笉鏄垜鎯冲埌鐨勶紝鏄功涓婄湅鍒扮殑锛屽綋鐒跺鏋滄病鏈夊湪涔︿笂鐪嬪埌锛屽彲鑳借繃涓�娈垫椂闂存垜鑷繁涔熸兂鐨勫埌锛屽彲鏄偅鏃舵垜璇存槸鎴戣嚜宸辨兂鍒扮殑涔熸病鏈変汉鐩镐俊鐨勩��
public class Stack {
private Object[] elements=new Object[10];
private int size = 0;
public void push(Object e){
ensureCapacity();
elements[size++] = e;
}
public Object pop(){
if( size == 0)
throw new EmptyStackException();
return elements[--size];
}
private void ensureCapacity(){
if(elements.length == size){
Object[] oldElements = elements;
elements = new Object[2 * elements.length+1];
System.arraycopy(oldElements,0, elements, 0, size);
}
}
}
涓婇潰鐨勫師鐞嗗簲璇ュ緢绠�鍗曪紝鍋囧鍫嗘爤鍔犱簡10涓厓绱狅紝鐒跺悗鍏ㄩ儴寮瑰嚭鏉ワ紝铏界劧鍫嗘爤鏄┖鐨勶紝娌℃湁鎴戜滑瑕佺殑涓滆タ锛屼絾鏄繖鏄釜瀵硅薄鏄棤娉曞洖鏀剁殑锛岃繖涓墠绗﹀悎浜嗗唴瀛樻硠闇茬殑涓や釜鏉′欢锛氭棤鐢紝鏃犳硶鍥炴敹銆�
浣嗘槸灏辨槸瀛樺湪杩欐牱鐨勪笢瑗夸篃涓嶄竴瀹氫細瀵艰嚧浠�涔堟牱鐨勫悗鏋滐紝濡傛灉杩欎釜鍫嗘爤鐢ㄧ殑姣旇緝灏戯紝涔熷氨娴垂浜嗗嚑涓狵鍐呭瓨鑰屽凡锛屽弽姝f垜浠殑鍐呭瓨閮戒笂G浜嗭紝鍝噷浼氭湁浠�涔堝奖鍝嶏紝鍐嶈杩欎釜涓滆タ寰堝揩灏变細琚洖鏀剁殑锛屾湁浠�涔堝叧绯汇�備笅闈㈢湅涓や釜渚嬪瓙銆�
渚嬪瓙1
public class Bad{
public static Stack s=Stack();
static{
s.push(new Object());
s.pop(); //杩欓噷鏈変竴涓璞″彂鐢熷唴瀛樻硠闇�
s.push(new Object()); //涓婇潰鐨勫璞″彲浠ヨ鍥炴敹浜嗭紝绛変簬鏄嚜鎰堜簡
}
}
鍥犱负鏄痵tatic锛屽氨涓�鐩村瓨鍦ㄥ埌绋嬪簭閫�鍑猴紝浣嗘槸鎴戜滑涔熷彲浠ョ湅鍒板畠鏈夎嚜鎰堝姛鑳斤紝灏辨槸璇村鏋滀綘鐨凷tack鏈�澶氭湁100涓璞★紝閭d箞鏈�澶氫篃灏卞彧鏈�100涓璞℃棤娉曡鍥炴敹鍏跺疄杩欎釜搴旇寰堝鏄撶悊瑙o紝Stack鍐呴儴鎸佹湁100涓紩鐢紝鏈�鍧忕殑鎯呭喌灏辨槸浠栦滑閮芥槸鏃犵敤鐨勶紝鍥犱负鎴戜滑涓�鏃︽斁鏂扮殑杩涘彇锛屼互鍓嶇殑寮曠敤鑷劧娑堝け锛�
鍐呭瓨娉勯湶鐨勫彟澶栦竴绉嶆儏鍐碉細褰撲竴涓璞¤瀛樺偍杩汬ashSet闆嗗悎涓互鍚庯紝灏变笉鑳戒慨鏀硅繖涓璞′腑鐨勯偅浜涘弬涓庤绠楀搱甯屽�肩殑瀛楁浜嗭紝鍚﹀垯锛屽璞′慨鏀瑰悗鐨勫搱甯屽�间笌鏈�鍒濆瓨鍌ㄨ繘HashSet闆嗗悎涓椂鐨勫搱甯屽�煎氨涓嶅悓浜嗭紝鍦ㄨ繖绉嶆儏鍐典笅锛屽嵆浣垮湪contains鏂规硶浣跨敤璇ュ璞$殑褰撳墠寮曠敤浣滀负鐨勫弬鏁板幓HashSet闆嗗悎涓绱㈠璞★紝涔熷皢杩斿洖鎵句笉鍒板璞$殑缁撴灉锛岃繖涔熶細瀵艰嚧鏃犳硶浠嶩ashSet闆嗗悎涓崟鐙垹闄ゅ綋鍓嶅璞★紝閫犳垚鍐呭瓨娉勯湶銆�
82銆佽兘涓嶈兘鑷繁鍐欎釜绫伙紝涔熷彨java.lang.String锛�
鍙互锛屼絾鍦ㄥ簲鐢ㄧ殑鏃跺�欙紝闇�瑕佺敤鑷繁鐨勭被鍔犺浇鍣ㄥ幓鍔犺浇锛屽惁鍒欙紝绯荤粺鐨勭被鍔犺浇鍣ㄦ案杩滃彧鏄幓鍔犺浇jre.jar鍖呬腑鐨勯偅涓猨ava.lang.String銆傜敱浜庡湪tomcat鐨剋eb搴旂敤绋嬪簭涓紝閮芥槸鐢眞ebapp鑷繁鐨勭被鍔犺浇鍣ㄥ厛鑷繁鍔犺浇WEB-INF/classess鐩綍涓殑绫伙紝鐒跺悗鎵嶅鎵樹笂绾х殑绫诲姞杞藉櫒鍔犺浇锛屽鏋滄垜浠湪tomcat鐨剋eb搴旂敤绋嬪簭涓啓涓�涓猨ava.lang.String锛岃繖鏃跺�橲ervlet绋嬪簭鍔犺浇鐨勫氨鏄垜浠嚜宸卞啓鐨刯ava.lang.String锛屼絾鏄繖涔堝共灏变細鍑哄緢澶氭綔鍦ㄧ殑闂锛屽師鏉ユ墍鏈夌敤浜唈ava.lang.String绫荤殑閮藉皢鍑虹幇闂銆�
铏界劧java鎻愪緵浜�endorsed鎶�鏈紝鍙互瑕嗙洊jdk涓殑鏌愪簺绫伙紝鍏蜂綋鍋氭硶鏄��.銆備絾鏄紝鑳藉琚鐩栫殑绫绘槸鏈夐檺鍒惰寖鍥达紝鍙嶆涓嶅寘鎷琷ava.lang杩欐牱鐨勫寘涓殑绫汇��
锛堜笅闈㈢殑渚嬪涓昏鏄究浜庡ぇ瀹跺涔犵悊瑙e彧鐢紝涓嶈浣滀负绛旀鐨勪竴閮ㄥ垎锛屽惁鍒欙紝浜哄鎬�鐤戞槸棰樼洰娉勯湶浜嗭級渚嬪锛岃繍琛屼笅闈㈢殑绋嬪簭锛�
packagejava.lang;
publicclassString {
/**
*@paramargs
*/
public static voidmain(String[] args) {
//TODOAuto-generated method stub
System.out.println("string");
}
}
鎶ュ憡鐨勯敊璇涓嬶細
java.lang.NoSuchMethodError:main
Exception inthread "main"
杩欐槸鍥犱负鍔犺浇浜唈re鑷甫鐨刯ava.lang.String锛岃�岃绫讳腑娌℃湁main鏂规硶銆�
83. Java浠g爜鏌ラ敊
1.
abstract class Name {
private String name;
public abstract boolean isStupidName(String name) {}
}
澶т緺浠紝杩欐湁浣曢敊璇�?
绛旀: 閿欍�俛bstract method蹇呴』浠ュ垎鍙风粨灏撅紝涓斾笉甯﹁姳鎷彿銆�
2.
public class Something {
void doSomething () {
private String s = "";
int l = s.length();
}
}
鏈夐敊鍚�?
绛旀: 閿欍�傚眬閮ㄥ彉閲忓墠涓嶈兘鏀剧疆浠讳綍璁块棶淇グ绗� (private锛宲ublic锛屽拰protected)銆俧inal鍙互鐢ㄦ潵淇グ灞�閮ㄥ彉閲�
(final濡傚悓abstract鍜宻trictfp锛岄兘鏄潪璁块棶淇グ绗︼紝strictfp鍙兘淇グclass鍜宮ethod鑰岄潪variable)銆�
3.
abstract class Something {
private abstract String doSomething ();
}
杩欏ソ鍍忔病浠�涔堥敊鍚�?
绛旀: 閿欍�俛bstract鐨刴ethods涓嶈兘浠rivate淇グ銆俛bstract鐨刴ethods灏辨槸璁╁瓙绫籭mplement(瀹炵幇)鍏蜂綋缁嗚妭鐨勶紝鎬庝箞鍙互鐢╬rivate鎶奱bstract
method灏侀攣璧锋潵鍛�? (鍚岀悊锛宎bstract method鍓嶄笉鑳藉姞final)銆�
4.
public class Something {
public int addOne(final int x) {
return ++x;
}
}
杩欎釜姣旇緝鏄庢樉銆�
绛旀: 閿欍�俰nt x琚慨楗版垚final锛屾剰鍛崇潃x涓嶈兘鍦╝ddOne method涓淇敼銆�
5.
public class Something {
public static void main(String[] args) {
Other o = new Other();
new Something().addOne(o);
}
public void addOne(final Other o) {
o.i++;
}
}
class Other {
public int i;
}
鍜屼笂闈㈢殑寰堢浉浼硷紝閮芥槸鍏充簬final鐨勯棶棰橈紝杩欐湁閿欏悧?
绛旀: 姝g‘銆傚湪addOne method涓紝鍙傛暟o琚慨楗版垚final銆傚鏋滃湪addOne method閲屾垜浠慨鏀逛簡o鐨剅eference
(姣斿: o = new Other();)锛岄偅涔堝鍚屼笂渚嬭繖棰樹篃鏄敊鐨勩�備絾杩欓噷淇敼鐨勬槸o鐨刴ember vairable
(鎴愬憳鍙橀噺)锛岃�宱鐨剅eference骞舵病鏈夋敼鍙樸��
6.
class Something {
int i;
public void doSomething() {
System.out.println("i = "+ i);
}
}
鏈変粈涔堥敊鍛�? 鐪嬩笉鍑烘潵鍟娿��
绛旀: 姝g‘銆傝緭鍑虹殑鏄�"i = 0"銆俰nt i灞炴柤instant variable (瀹炰緥鍙橀噺锛屾垨鍙垚鍛樺彉閲�)銆俰nstant variable鏈塪efault value銆俰nt鐨刣efault value鏄�0銆�
7.
class Something {
final int i;
public void doSomething() {
System.out.println("i = "+ i);
}
}
鍜屼笂闈竴棰樺彧鏈変竴涓湴鏂逛笉鍚岋紝灏辨槸澶氫簡涓�涓猣inal銆傝繖闅鹃亾灏遍敊浜嗗悧?
绛旀: 閿欍�俧inal int i鏄釜final鐨刬nstant variable (瀹炰緥鍙橀噺锛屾垨鍙垚鍛樺彉閲�)銆俧inal鐨刬nstant variable娌℃湁default value锛屽繀椤诲湪constructor (鏋勯�犲櫒)缁撴潫涔嬪墠琚祴浜堜竴涓槑纭殑鍊笺�傚彲浠ヤ慨鏀逛负"final int i =0;"銆�
8.
public class Something {
public static void main(String[] args) {
Something s = new Something();
System.out.println("s.doSomething() returns " + doSomething());
}
public String doSomething() {
return "Do something ...";
}
}
鐪嬩笂鍘诲緢瀹岀編銆�
绛旀: 閿欍�傜湅涓婂幓鍦╩ain閲宑all doSomething娌℃湁浠�涔堥棶棰橈紝姣曠珶涓や釜methods閮藉湪鍚屼竴涓猚lass閲屻�備絾浠旂粏鐪嬶紝main鏄痵tatic鐨勩�俿tatic method涓嶈兘鐩存帴call non-staticmethods銆傚彲鏀规垚"System.out.println("s.doSomething()returns " + s.doSomething());"銆傚悓鐞嗭紝static method涓嶈兘璁块棶non-static instant variable銆�
9.
姝ゅ锛孲omething绫荤殑鏂囦欢鍚嶅彨OtherThing.java
class Something {
private static void main(String[] something_to_do){
System.out.println("Dosomething ...");
}
}
杩欎釜濂藉儚寰堟槑鏄俱��
绛旀: 姝g‘銆備粠鏉ユ病鏈変汉璇磋繃Java鐨凜lass鍚嶅瓧蹇呴』鍜屽叾鏂囦欢鍚嶇浉鍚屻�備絾public class鐨勫悕瀛楀繀椤诲拰鏂囦欢鍚嶇浉鍚屻��
10锛�
interface 聽A{
int x = 0;
}
class B{
int x =1;
}
class C extends B implements A {
public void pX(){
System.out.println(x);
}
public static void main(String[] args) {
new C().pX();
}
}
绛旀锛氶敊璇�傚湪缂栬瘧鏃朵細鍙戠敓閿欒(閿欒鎻忚堪涓嶅悓鐨凧VM鏈変笉鍚岀殑淇℃伅锛屾剰鎬濆氨鏄湭鏄庣‘鐨剎璋冪敤锛屼袱涓獂閮藉尮閰嶏紙灏辫薄鍦ㄥ悓鏃秈mport java.util鍜宩ava.sql涓や釜鍖呮椂鐩存帴澹版槑Date涓�鏍凤級銆傚浜庣埗绫荤殑鍙橀噺,鍙互鐢╯uper.x鏉ユ槑纭紝鑰屾帴鍙g殑灞炴�ч粯璁ら殣鍚负 public staticfinal.鎵�浠ュ彲浠ラ�氳繃A.x鏉ユ槑纭��
11.
interface Playable {
void play();
}
interface Bounceable {
void play();
}
interface Rollable extends Playable, Bounceable {
Ball ball = new Ball("PingPang");
}
class Ball implements Rollable {
private String name;
public String getName() {
return name;
}
public Ball(String name) {
this.name =name;
}
public void play() {
ball = newBall("Football");
System.out.println(ball.getName());
}
}
杩欎釜閿欒涓嶅鏄撳彂鐜般��
绛旀: 閿欍��"interfaceRollable extends Playable, Bounceable"娌℃湁闂銆俰nterface鍙户鎵垮涓猧nterfaces锛屾墍浠ヨ繖閲屾病閿欍�傞棶棰樺嚭鍦╥nterface Rollable閲岀殑"Ball ball =new Ball("PingPang");"銆備换浣曞湪interface閲屽0鏄庣殑interface variable (鎺ュ彛鍙橀噺锛屼篃鍙О鎴愬憳鍙橀噺)锛岄粯璁や负public static final銆備篃灏辨槸璇�"Ball ball = new Ball("PingPang");"瀹為檯涓婃槸"public staticfinal Ball ball = new Ball("PingPang");"銆傚湪Ball绫荤殑Play()鏂规硶涓紝"ball = newBall("Football");"鏀瑰彉浜哹all鐨剅eference锛岃�岃繖閲岀殑ball鏉ヨ嚜Rollable interface锛孯ollable interface閲岀殑ball鏄痯ublic static final鐨勶紝final鐨刼bject鏄笉鑳借鏀瑰彉reference鐨勩�傚洜姝ょ紪璇戝櫒灏嗗湪"ball = newBall("Football");"杩欓噷鏄剧ず鏈夐敊銆�
浜�.绠楁硶涓庣紪绋�
1銆佺紪鍐欎竴涓▼搴忥紝灏哸.txt鏂囦欢涓殑鍗曡瘝涓巄.txt鏂囦欢涓殑鍗曡瘝浜ゆ浛鍚堝苟鍒癱.txt鏂囦欢涓紝a.txt鏂囦欢涓殑鍗曡瘝鐢ㄥ洖杞︾鍒嗛殧锛宐.txt鏂囦欢涓敤鍥炶溅鎴栫┖鏍艰繘琛屽垎闅斻��
绛旓細
packagecn.itcast;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
public class MainClass{
public static voidmain(String[] args) throws Exception{
FileManager a= new FileManager("a.txt",new char[]{'\n'});
FileManager b= new FileManager("b.txt",new char[]{'\n',' '});
FileWriter c= new FileWriter("c.txt");
String aWord= null;
String bWord= null;
while((aWord= a.nextWord()) !=null ){
c.write(aWord+ "\n");
bWord= b.nextWord();
if(bWord!= null)
c.write(bWord+ "\n");
}
while((bWord= b.nextWord()) != null){
c.write(bWord+ "\n");
}
c.close();
}
}
class FileManager{
String[] words =null;
int pos = 0;
Public FileManager(String filename,char[] seperators) throws Exception{
File f = new File(filename);
FileReader reader = new FileReader(f);
char[] buf =new char[(int)f.length()];
int len =reader.read(buf);
Stringresults = new String(buf,0,len);
String regex= null;
if(seperators.length>1 ){
regex= "" + seperators[0] + "|" + seperators[1];
}else{
regex= "" + seperators[0];
}
words =results.split(regex);
}
public StringnextWord(){
if(pos ==words.length)
returnnull;
returnwords[pos++];
}
}
2銆佺紪鍐欎竴涓▼搴忥紝灏哾:\java鐩綍涓嬬殑鎵�鏈�.java鏂囦欢澶嶅埗鍒癲:\jad鐩綍涓嬶紝骞跺皢鍘熸潵鏂囦欢鐨勬墿灞曞悕浠�.java鏀逛负.jad銆�
锛堝ぇ瀹舵鍦ㄥ仛涓婇潰杩欓亾棰橈紝缃戜笂杩熷埌鐨勬湅鍙嬩篃璇峰仛鍋氳繖閬撻锛屾壘宸ヤ綔蹇呴』鑳界紪鍐欒繖浜涚畝鍗曢棶棰樼殑浠g爜锛侊級
绛旓細listFiles鏂规硶鎺ュ彈涓�涓狥ileFilter瀵硅薄锛�杩欎釜FileFilter瀵硅薄灏辨槸杩囪檻鐨勭瓥鐣ュ璞�锛屼笉鍚岀殑浜烘彁渚涗笉鍚岀殑FileFilter瀹炵幇锛屽嵆鎻愪緵浜嗕笉鍚岀殑杩囨护绛栫暐銆�
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class Jad2Java {
public static voidmain(String[] args) throws Exception {
File srcDir =new File("java");
if(!(srcDir.exists()&& srcDir.isDirectory()))
thrownew Exception("鐩綍涓嶅瓨鍦�");
File[] files= srcDir.listFiles(
newFilenameFilter(){
publicboolean accept(File dir, String name) {
returnname.endsWith(".java");
}
}
);
System.out.println(files.length);
File destDir= new File("jad");
if(!destDir.exists())destDir.mkdir();
for(File f:files){
FileInputStream 聽fis = new FileInputStream(f);
StringdestFileName = f.getName().replaceAll("\\.java$", ".jad");
FileOutputStreamfos = new FileOutputStream(new File(destDir,destFileName));
copy(fis,fos);
fis.close();
fos.close();
}
}
private static voidcopy(InputStream ips,OutputStream ops) throws Exception{
int len = 0;
byte[] buf =new byte[1024];
while((len =ips.read(buf)) != -1){
ops.write(buf,0,len);
}
}
}
鐢辨湰棰樻�荤粨鐨勬�濇兂鍙婄瓥鐣ユā寮忕殑瑙f瀽锛�
1.
class jad2java{
1.寰楀埌鏌愪釜鐩綍涓嬬殑鎵�鏈夌殑java鏂囦欢闆嗗悎
1.1寰楀埌鐩綍 File srcDir = newFile("d:\\java");
1.2寰楀埌鐩綍涓嬬殑鎵�鏈塲ava鏂囦欢锛欶ile[] files =srcDir.listFiles(new MyFileFilter());
1.3鍙兂寰楀埌.java鐨勬枃浠讹細 class MyFileFilterimplememyts FileFilter{
publicboolean accept(File pathname){
returnpathname.getName().endsWith(".java")
}
}
2.灏嗘瘡涓枃浠跺鍒跺埌鍙﹀涓�涓洰褰曪紝骞舵敼鎵╁睍鍚�
2.1寰楀埌鐩爣鐩綍锛屽鏋滅洰鏍囩洰褰曚笉瀛樺湪锛屽垯鍒涘缓涔�
2.2鏍规嵁婧愭枃浠跺悕寰楀埌鐩爣鏂囦欢鍚嶏紝娉ㄦ剰瑕佺敤姝e垯琛ㄨ揪寮忥紝娉ㄦ剰.鐨勮浆涔夈��
2.3鏍规嵁琛ㄧず鐩綍鐨凢ile鍜岀洰鏍囨枃浠跺悕鐨勫瓧绗︿覆锛屽緱鍒拌〃绀虹洰鏍囨枃浠剁殑File銆�
//瑕佸湪纭洏涓噯纭湴鍒涘缓鍑轰竴涓枃浠讹紝闇�瑕佺煡閬撴枃浠跺悕鍜屾枃浠剁殑鐩綍銆�
2.4灏嗘簮鏂囦欢鐨勬祦鎷疯礉鎴愮洰鏍囨枃浠舵祦锛屾嫹璐濇柟娉曠嫭绔嬫垚涓轰竴涓柟娉曪紝鏂规硶鐨勫弬鏁伴噰鐢ㄦ娊璞℃祦鐨勫舰寮忋��
//鏂规硶鎺ュ彈鐨勫弬鏁扮被鍨嬪敖閲忛潰鍚戠埗绫伙紝瓒婃娊璞¤秺濂斤紝杩欐牱閫傚簲闈㈡洿瀹藉箍銆�
}
鍒嗘瀽listFiles鏂规硶鍐呴儴鐨勭瓥鐣ユā寮忓疄鐜板師鐞�
File[] listFiles(FileFilter filter){
File[] files =listFiles();
//ArraylistacceptedFilesList = new ArrayList();
File[] acceptedFiles= new File[files.length];
int pos = 0;
for(File file:files){
booleanaccepted = filter.accept(file);
if(accepted){
//acceptedFilesList.add(file);
acceptedFiles[pos++]= file;
}
}
Arrays.copyOf(acceptedFiles,pos);
//return(File[])accpetedFilesList.toArray();
}
3銆佺紪鍐欎竴涓埅鍙栧瓧绗︿覆鐨勫嚱鏁帮紝杈撳叆涓轰竴涓瓧绗︿覆鍜屽瓧鑺傛暟锛岃緭鍑轰负鎸夊瓧鑺傛埅鍙栫殑瀛楃涓诧紝浣嗚淇濊瘉姹夊瓧涓嶈鎴彇鍗婁釜锛屽鈥滄垜ABC鈥濓紝4锛屽簲璇ユ埅鍙栤�滄垜AB鈥濓紝杈撳叆鈥滄垜ABC姹塂EF鈥濓紝6锛屽簲璇ヨ緭鍑衡�滄垜ABC鈥濓紝鑰屼笉鏄�滄垜ABC+姹夌殑鍗婁釜鈥濄��
绛旓細
棣栧厛瑕佷簡瑙d腑鏂囧瓧绗︽湁澶氱缂栫爜鍙婂悇绉嶇紪鐮佺殑鐗瑰緛銆�
鍋囪n涓鸿鎴彇鐨勫瓧鑺傛暟銆�
public static voidmain(String[] args) throws Exception{
String str ="鎴慳鐖变腑鍗巃bc鎴戠埍浼犳櫤def';
String str ="鎴慉BC姹�";
int num =trimGBK(str.getBytes("GBK"),5);
System.out.println(str.substring(0,num));
}
public staticint 聽trimGBK(byte[] buf,int n){
int num = 0;
booleanbChineseFirstHalf = false;
for(inti=0;i
{
if(buf[i]<0&& !bChineseFirstHalf){
bChineseFirstHalf= true;
}else{
num++;
bChineseFirstHalf= false;
}
}
return num;
}
4銆佹湁涓�涓瓧绗︿覆锛屽叾涓寘鍚腑鏂囧瓧绗︺�佽嫳鏂囧瓧绗﹀拰鏁板瓧瀛楃锛岃缁熻鍜屾墦鍗板嚭鍚勪釜瀛楃鐨勪釜鏁般��
绛旓細鍝堝搱锛屽叾瀹炲寘鍚腑鏂囧瓧绗︺�佽嫳鏂囧瓧绗︺�佹暟瀛楀瓧绗﹀師鏉ユ槸鍑洪鑰呮斁鐨勭儫闆惧脊銆�
String content = 鈥滀腑鍥絘adf鐨�111钀╞bb鑿茬殑zz钀ㄨ彶鈥�;
HashMap map = new HashMap();
for(int i=0;i
{
char c =content.charAt(i);
Integer num =map.get(c);
if(num == null)
num = 1;
else
num = num +1;
map.put(c,num);
}
for(Map.EntrySet entry : map)
{
system.out.println(entry.getkey()+ 鈥�:鈥� + entry.getValue());
}
浼拌鏄綋鍒濋潰璇曠殑閭d釜瀛﹀憳琛ㄨ堪涓嶆竻妤氾紝闂寰堝彲鑳芥槸锛�
濡傛灉涓�涓插瓧绗﹀"aaaabbc涓浗1512"瑕佸垎鍒粺璁¤嫳鏂囧瓧绗︾殑鏁伴噺锛屼腑鏂囧瓧绗︾殑鏁伴噺锛屽拰鏁板瓧瀛楃鐨勬暟閲忥紝鍋囪瀛楃涓病鏈変腑鏂囧瓧绗︺�佽嫳鏂囧瓧绗︺�佹暟瀛楀瓧绗︿箣澶栫殑鍏朵粬鐗规畩瀛楃銆�
int engishCount;
int chineseCount;
int digitCount;
for(int i=0;i
{
charch = str.charAt(i);
if(ch>=鈥�0鈥�&& ch<=鈥�9鈥�)
{
digitCount++
}
elseif((ch>=鈥檃鈥�&& ch<=鈥檢鈥�) || (ch>=鈥橝鈥� && ch<=鈥橺鈥�))
{
engishCount++;
}
else
{
chineseCount++;
}
}
System.out.println(鈥︹�︹�︹�︹��);
5銆佽鏄庣敓娲讳腑閬囧埌鐨勪簩鍙夋爲锛岀敤java瀹炵幇浜屽弶鏍�
杩欐槸缁勫悎璁捐妯″紡銆�
鎴戞湁寰堝涓�(鍋囪10涓囦釜)鏁版嵁瑕佷繚瀛樿捣鏉ワ紝浠ュ悗杩橀渶瑕佷粠淇濆瓨鐨勮繖浜涙暟鎹腑妫�绱㈡槸鍚﹀瓨鍦ㄦ煇涓暟鎹紝锛堟垜鎯宠鍑轰簩鍙夋爲鐨勫ソ澶勶紝璇ユ�庝箞璇村憿锛熼偅灏辨槸璇村埆浜虹殑缂虹偣锛夛紝鍋囧瀛樺湪鏁扮粍涓紝閭d箞锛岀宸ц鎵剧殑鏁板瓧浣嶄簬99999閭d釜鍦版柟锛岄偅鏌ユ壘鐨勯�熷害灏嗗緢鎱紝鍥犱负瑕佷粠绗�1涓緷娆″線鍚庡彇锛屽彇鍑烘潵鍚庤繘琛屾瘮杈冦�傚钩琛′簩鍙夋爲锛堟瀯寤哄钩琛′簩鍙夋爲闇�瑕佸厛鎺掑簭锛屾垜浠繖閲屽氨涓嶄綔鑰冭檻浜嗭級鍙互寰堝ソ鍦拌В鍐宠繖涓棶棰橈紝浣嗕簩鍙夋爲鐨勯亶鍘嗭紙鍓嶅簭锛屼腑搴忥紝鍚庡簭锛夋晥鐜囪姣旀暟缁勪綆寰堝锛屽師鐞嗗涓嬪浘锛�
浠g爜濡備笅锛�
packagecom.huawei.interview;
publicclassNode {
public intvalue;
publicNode left;
publicNode right;
public voidstore(intvalue)
{
if(value<this.value)
{
if(left ==null)
{
left =newNode();
left.value=value;
}
else
{
left.store(value);
}
}
else if(value>this.value)
{
if(right ==null)
{
right =newNode();
right.value=value;
}
else
{
right.store(value);
}
}
}
public booleanfind(intvalue)
{
System.out.println("happen" +this.value);
if(value ==this.value)
{
return true;
}
else if(value>this.value)
{
if(right ==null)returnfalse;
returnright.find(value);
}else
{
if(left ==null)returnfalse;
returnleft.find(value);
}
}
public 聽voidpreList()
{
System.out.print(this.value+ ",");
if(left!=null)left.preList();
if(right!=null) right.preList();
}
public voidmiddleList()
{
if(left!=null)left.preList();
System.out.print(this.value+ ",");
if(right!=null)right.preList();
}
public voidafterList()
{
if(left!=null)left.preList();
if(right!=null)right.preList();
System.out.print(this.value+ ",");
}
public static voidmain(String [] args)
{
int[] data =new int[20];
for(inti=0;i
{
data[i] = (int)(Math.random()*100)+ 1;
System.out.print(data[i] +",");
}
System.out.println();
Node root =newNode();
root.value = data[0];
for(inti=1;i
{
root.store(data[i]);
}
root.find(data[19]);
root.preList();
System.out.println();
root.middleList();
System.out.println();
root.afterList();
}
}
-----------------鍙堜竴娆′复鍦哄啓鐨勪唬鐮�---------------------------
importjava.util.Arrays;
importjava.util.Iterator;
public class Node{
private Node left;
private Node right;
private int value;
//private int num;
public Node(int value){
this.value = value;
}
public void add(int value){
if(value > this.value)
{
if(right != null)
right.add(value);
else
{
Node node = new Node(value);
right = node;
}
}
else{
if(left != null)
left.add(value);
else
{
Node node = new Node(value);
left = node;
}
}
}
public boolean find(int value){
if(value == this.value) return true;
else if(value > this.value){
if(right == null) return false;
else return right.find(value);
}else{
if(left == null) return false;
else return left.find(value);
}
}
public void display(){
System.out.println(value);
if(left != null) left.display();
if(right != null) right.display();
}
/*public Iterator iterator(){
}*/
public static void main(String[] args){
int[] values = new int[8];
for(int i=0;i<8;i++){
int num = (int)(Math.random() * 15);
//System.out.println(num);
//if(Arrays.binarySearch(values,num)<0)
if(!contains(values,num))
values[i] = num;
else
i--;
}
System.out.println(Arrays.toString(values));
Node root = new Node(values[0]);
for(int i=1;i
root.add(values[i]);
}
System.out.println(root.find(13));
root.display();
}
public static boolean contains(int [] arr,int value){
int i = 0;
for(;i
if(arr[i] == value) return true;
}
return false;
}
}
6銆佷粠绫讳技濡備笅鐨勬枃鏈枃浠朵腑璇诲彇鍑烘墍鏈夌殑濮撳悕锛屽苟鎵撳嵃鍑洪噸澶嶇殑濮撳悕鍜岄噸澶嶇殑娆℃暟锛屽苟鎸夐噸澶嶆鏁版帓搴忥細
1,寮犱笁,28
2,鏉庡洓,35
3,寮犱笁,28
4,鐜嬩簲,35
5,寮犱笁,28
6,鏉庡洓,35
7,璧靛叚,28
8,鐢颁竷,35
绋嬪簭浠g爜濡備笅锛堢瓟棰樿鍗氬緱鐢ㄤ汉鍗曚綅鐨勫枩娆紝鍖呭悕鐢ㄨ鍏徃锛岄潰璇曞墠灏辨彁鍓嶆煡濂借鍏徃鐨勭綉鍧�锛屽鏋滄煡涓嶅埌锛岀幇鍦洪棶涔熸槸鍙互鐨勩�傝繕瑕佸姞涓婂疄鐜版�濊矾鐨勬敞閲婏級锛�
packagecom.huawei.interview;
importjava.io.BufferedReader;
importjava.io.IOException;
importjava.io.InputStream;
importjava.io.InputStreamReader;
importjava.util.Comparator;
importjava.util.HashMap;
importjava.util.Iterator;
importjava.util.Map;
importjava.util.TreeSet;
publicclassGetNameTest {
/**
*@paramargs
*/
public static voidmain(String[] args) {
//TODOAuto-generated method stub
//InputStream ips =GetNameTest.class.getResourceAsStream("/com/huawei/interview/info.txt");
//鐢ㄤ笂涓�琛屾敞閲婄殑浠g爜鍜屼笅涓�琛岀殑浠g爜閮藉彲浠ワ紝鍥犱负info.txt涓嶨etNameTest绫诲湪鍚屼竴鍖呬笅闈紝鎵�浠ワ紝鍙互鐢ㄤ笅闈㈢殑鐩稿璺緞褰㈠紡
Map results =newHashMap();
InputStream ips = GetNameTest.class.getResourceAsStream("info.txt");
BufferedReader in =newBufferedReader(newInputStreamReader(ips));
String line =null;
try{
while((line=in.readLine())!=null)
{
dealLine(line,results);
}
sortResults(results);
}catch(IOException e) {
//TODOAuto-generated catchblock
e.printStackTrace();
}
}
static classUser
{
publicString name;
publicInteger value;
publicUser(String name,Integervalue)
{
this.name = name;
this.value = value;
}
@Override
public booleanequals(Object obj) {
//TODOAuto-generated methodstub
//涓嬮潰鐨勪唬鐮佹病鏈夋墽琛岋紝璇存槑寰�treeset涓鍔犳暟鎹椂锛屼笉浼氫娇鐢ㄥ埌equals鏂规硶銆�
booleanresult =super.equals(obj);
System.out.println(result);
returnresult;
}
}
private static voidsortResults(Map results) {
//TODOAuto-generated method stub
TreeSet sortedResults =newTreeSet(
newComparator(){
public intcompare(Object o1, Object o2) {
//TODOAuto-generated method stub
User user1 = (User)o1;
User user2 = (User)o2;
/*濡傛灉compareTo杩斿洖缁撴灉0锛屽垯璁や负涓や釜瀵硅薄鐩哥瓑锛屾柊鐨勫璞′笉浼氬鍔犲埌闆嗗悎涓幓
*鎵�浠ワ紝涓嶈兘鐩存帴鐢ㄤ笅闈㈢殑浠g爜锛屽惁鍒欙紝閭d簺涓暟鐩稿悓鐨勫叾浠栧鍚嶅氨鎵撳嵃涓嶅嚭鏉ャ��
* */
//returnuser1.value-user2.value;
//returnuser1.value
if(user1.value
{
return-1;
}else if(user1.value>user2.value)
{
return1;
}else
{
returnuser1.name.compareTo(user2.name);
}
}
}
);
Iterator iterator =results.keySet().iterator();
while(iterator.hasNext())
{
String name = (String)iterator.next();
Integer value =(Integer)results.get(name);
if(value > 1)
{
sortedResults.add(newUser(name,value));
}
}
printResults(sortedResults);
}
private static voidprintResults(TreeSet sortedResults)
{
Iterator iterator 聽= sortedResults.iterator();
while(iterator.hasNext())
{
User user = (User)iterator.next();
System.out.println(user.name +":" + user.value);
}
}
public static voiddealLine(String line,Map map)
{
if(!"".equals(line.trim()))
{
String [] results =line.split(",");
if(results.length == 3)
{
String name = results[1];
Integer value =(Integer)map.get(name);
if(value ==null)value = 0;
map.put(name,value + 1);
}
}
}
}
7銆佸啓涓�涓猄ingleton鍑烘潵銆�
绗竴绉嶏細楗辨眽妯″紡
public classSingleTon {
private SingleTon(){
}
//瀹炰緥鍖栨斁鍦ㄩ潤鎬佷唬鐮佸潡閲屽彲鎻愰珮绋嬪簭鐨勬墽琛屾晥鐜囷紝浣嗕篃鍙兘鏇村崰鐢ㄧ┖闂�
private final static SingleTon instance =new SingleTon();
public static SingleTon getInstance(){
return instance;
}
}
绗簩绉嶏細楗ユ眽妯″紡
public classSingleTon {
private SingleTon(){}
private static instance = null;//newSingleTon();
public static synchronized SingleTongetInstance(){
if(instance == null)
instance = new SingleTon();
return instance;
}
}
绗笁绉嶏細鐢ㄦ灇涓�
public enum SingleTon{
ONE;
}
绗笁锛氭洿瀹為檯鐨勫簲鐢紙鍦ㄤ粈涔堟儏鍐电敤鍗曚緥锛�
public classSequenceGenerator{
//涓嬮潰鏄绫昏嚜韬殑涓氬姟鍔熻兘浠g爜
private int count = 0;
public synchronized int getSequence(){
++count;
}
//涓嬮潰鏄妸璇ョ被鍙樻垚鍗曚緥鐨勪唬鐮�
private SequenceGenerator(){}
private final static instance = newSequenceGenerator();
public static SingleTon getInstance(){
return instance;
}
}
绗洓锛�
public class MemoryDao
{
private HashMap map = new HashMap();
publicvoid add(Student stu1){
map.put(SequenceGenerator.getInstance().getSequence(),stu1);
}
//鎶奙emoryDao鍙樻垚鍗曚緥
}
Singleton妯″紡涓昏浣滅敤鏄繚璇佸湪Java搴旂敤绋嬪簭涓紝涓�涓被Class鍙湁涓�涓疄渚嬪瓨鍦ㄣ��
涓�鑸琒ingleton妯″紡閫氬父鏈夊嚑绉嶇褰㈠紡:
绗竴绉嶅舰寮�: 瀹氫箟涓�涓被锛屽畠鐨勬瀯閫犲嚱鏁颁负private鐨勶紝瀹冩湁涓�涓猻tatic鐨刾rivate鐨勮绫诲彉閲忥紝鍦ㄧ被鍒濆鍖栨椂瀹炰緥璇濓紝閫氳繃涓�涓猵ublic鐨刧etInstance鏂规硶鑾峰彇瀵瑰畠鐨勫紩鐢�,缁ц�岃皟鐢ㄥ叾涓殑鏂规硶銆�
public class Singleton {
private Singleton(){}
//鍦ㄨ嚜宸卞唴閮ㄥ畾涔夎嚜宸变竴涓疄渚嬶紝鏄笉鏄緢濂囨�紵
//娉ㄦ剰杩欐槸private鍙緵鍐呴儴璋冪敤
private staticSingleton instance = new Singleton();
//杩欓噷鎻愪緵浜嗕竴涓緵澶栭儴璁块棶鏈琧lass鐨勯潤鎬佹柟娉曪紝鍙互鐩存帴璁块棶
public staticSingleton getInstance() {
return instance;
}
}
绗簩绉嶅舰寮�:
public class Singleton {
private static Singleton instance = null;
public static synchronized Singleton getInstance() {
//杩欎釜鏂规硶姣斾笂闈㈡湁鎵�鏀硅繘锛屼笉鐢ㄦ瘡娆¢兘杩涜鐢熸垚瀵硅薄锛屽彧鏄涓�娆�
//浣跨敤鏃剁敓鎴愬疄渚嬶紝鎻愰珮浜嗘晥鐜囷紒
if (instance==null)
instance锛漬ew Singleton();
return instance;
}
}
鍏朵粬褰㈠紡:
瀹氫箟涓�涓被锛屽畠鐨勬瀯閫犲嚱鏁颁负private鐨勶紝鎵�鏈夋柟娉曚负static鐨勩��
涓�鑸涓虹涓�绉嶅舰寮忚鏇村姞瀹夊叏浜�
8銆侀�掑綊绠楁硶棰�1
涓�涓暣鏁帮紝澶т簬0锛屼笉鐢ㄥ惊鐜拰鏈湴鍙橀噺锛屾寜鐓锛�2n锛�4n锛�8n鐨勯『搴忛�掑锛屽綋鍊煎ぇ浜�5000鏃讹紝鎶婂�兼寜鐓ф寚瀹氶『搴忚緭鍑烘潵銆�
渚嬶細n=1237
鍒欒緭鍑轰负锛�
1237锛�
2474锛�
4948锛�
9896锛�
9896锛�
4948锛�
2474锛�
1237锛�
鎻愮ず锛氬啓绋嬪簭鏃讹紝鍏堣嚧璋㈡寜閫掑鏂瑰紡鐨勪唬鐮侊紝鍐欏ソ閫掑鐨勪互鍚庯紝鍐嶅鍔犺�冭檻閫掑噺閮ㄥ垎銆�
public static void doubleNum(int n)
{
System.out.println(n);
if(n<=5000)
doubleNum(n*2);
System.out.println(n);
}
Gaibaota(N) = Gaibaota(N-1) + n
9銆侀�掑綊绠楁硶棰�2
绗�1涓汉10锛岀2涓瘮绗�1涓汉澶�2宀侊紝渚濇閫掓帹锛岃鐢ㄩ�掑綊鏂瑰紡璁$畻鍑虹8涓汉澶氬ぇ锛�
packagecn.itcast;
importjava.util.Date;
publicclassA1 {
public static voidmain(String [] args)
{
System.out.println(computeAge(8));
}
public static intcomputeAge(intn)
{
if(n==1)return10;
returncomputeAge(n-1) + 2;
}
}
public static voidtoBinary(intn,StringBuffer result)
{
if(n/2 != 0)
toBinary(n/2,result);
result.append(n%2);
}
10銆佹帓搴忛兘鏈夊摢鍑犵鏂规硶锛熻鍒椾妇銆傜敤JAVA瀹炵幇涓�涓揩閫熸帓搴忋��
鏈汉鍙爺绌惰繃鍐掓场鎺掑簭銆侀�夋嫨鎺掑簭鍜屽揩閫熸帓搴忥紝涓嬮潰鏄揩閫熸帓搴忕殑浠g爜锛�
public class QuickSort {
/**
* 蹇�熸帓搴�
* @param strDate
* @param left
* @param right
*/
public void quickSort(String[] strDate,int left,int right){
String middle,tempDate;
int i,j;
i=left;
j=right;
middle=strDate[(i+j)/2];
do{
while(strDate[i].compareTo(middle)<0&& i
i++; //鎵惧嚭宸﹁竟姣斾腑闂村�煎ぇ鐨勬暟
while(strDate[j].compareTo(middle)>0&& j>left)
j--; //鎵惧嚭鍙宠竟姣斾腑闂村�煎皬鐨勬暟
if(i<=j){ //灏嗗乏杈瑰ぇ鐨勬暟鍜屽彸杈瑰皬鐨勬暟杩涜鏇挎崲
tempDate=strDate[i];
strDate[i]=strDate[j];
strDate[j]=tempDate;
i++;
j--;
}
}while(i<=j); //褰撲袱鑰呬氦閿欐椂鍋滄
if(i
quickSort(strDate,i,right);//浠�
}
if(j>left){
quickSort(strDate,left,j);
}
}
/**
* @param args
*/
public static void main(String[] args){
String[] strVoid=newString[]{"11","66","22","0","55","22","0","32"};
QuickSort sort=new QuickSort();
sort.quickSort(strVoid,0,strVoid.length-1);
for(int i=0;i
System.out.println(strVoid[i]+" ");
}
}
}
11銆佹湁鏁扮粍a[n]锛岀敤java浠g爜灏嗘暟缁勫厓绱犻『搴忛鍊�
//鐢ㄤ笅闈㈢殑涔熷彲浠�
//for(inti=0,int j=a.length-1;i
importjava.util.Arrays;
public classSwapDemo{
public static void main(String[] args){
int [] a = new int[]{
(int)(Math.random() *1000),
(int)(Math.random() * 1000),
(int)(Math.random() *1000),
(int)(Math.random() *1000),
(int)(Math.random() * 1000)
};
System.out.println(a);
System.out.println(Arrays.toString(a));
swap(a);
System.out.println(Arrays.toString(a));
}
public static void swap(int a[]){
int len = a.length;
for(int i=0;i
int tmp = a[i];
a[i] = a[len-1-i];
a[len-1-i] = tmp;
}
}
}
12锛庨噾棰濊浆鎹紝闃挎媺浼暟瀛楃殑閲戦杞崲鎴愪腑鍥戒紶缁熺殑褰㈠紡濡傦細锛堬骏1011锛夛紞>锛堜竴鍗冮浂涓�鎷句竴鍏冩暣锛夎緭鍑恒��
鍘婚浂鐨勪唬鐮侊細
returnsb.reverse().toString().replaceAll("闆禰鎷句桨浠焆","闆�").replaceAll("闆�+涓�","涓�").replaceAll("闆�+鍏�","鍏�").replaceAll("闆�+","闆�");
public class RenMingBi {
/**
* @param args add by zxx ,Nov 29, 2008
*/
private static finalchar[] data = new char[]{
'闆�','澹�','璐�','鍙�','鑲�','浼�','闄�','鏌�','鎹�','鐜�'
};
private static finalchar[] units = new char[]{
'鍏�','鎷�','浣�','浠�','涓�','鎷�','浣�','浠�','浜�'
};
public static voidmain(String[] args) {
// TODOAuto-generated method stub
System.out.println(
convert(135689123));
}
public static Stringconvert(int money)
{
StringBuffersbf = new StringBuffer();
int unit = 0;
while(money!=0)
{
sbf.insert(0,units[unit++]);
intnumber = money%10;
sbf.insert(0,data[number]);
money/= 10;
}
returnsbf.toString();
}
}
涓�. html&JavaScript&ajax閮ㄥ垎
1. 鍒ゆ柇绗簩涓棩鏈熸瘮绗竴涓棩鏈熷ぇ
濡備綍鐢ㄨ剼鏈垽鏂敤鎴疯緭鍏ョ殑鐨勫瓧绗︿覆鏄笅闈㈢殑鏃堕棿鏍煎紡2004-11-21蹇呴』瑕佷繚璇佺敤鎴风殑杈撳叆鏄鏍煎紡锛屽苟涓旀槸鏃堕棿锛屾瘮濡傝鏈堜唤涓嶅ぇ浜�12绛夌瓑锛屽彟澶栨垜闇�瑕佺敤鎴疯緭鍏ヤ袱涓紝骞朵笖鍚庝竴涓姣斿墠涓�涓櫄锛屽彧鍏佽鐢↗AVASCRIPT锛岃璇︾粏甯姪浣滅瓟锛�,
//杩欓噷鍙敤姝e垯琛ㄨ揪寮忓垽鏂彁鍓嶅垽鏂竴涓嬫牸寮忥紝鐒跺悗鎸変笅鎻愬彇鍚勬椂闂村瓧娈靛唴瀹�
window.onload =function()
{
//杩欎箞鍐欐槸涓轰簡瀹炵幇js浠g爜涓巋tml浠g爜鐨勫垎绂伙紝褰撴垜淇敼js鏃讹紝涓嶈兘褰卞搷html浠g爜銆�
document.getElementById("frm1").onsubmit=
function(){
vard1 = this.d1.value;
vard2 = this.d2.value;
if(!verifyDate(d1)) {alert("绗竴涓棩鏈熸牸寮忎笉瀵�");return false;}
if(!verifyDate(d2)) {alert("绗簩涓棩鏈熸牸寮忎笉瀵�");return false;}
if(!compareDate(d1,d2)){alert("绗簩涓棩鏈熸瘮绗竴鏃ユ湡灏�");return false;}
};
}
functioncompareDate(d1,d2)
{
var arrayD1= d1.split("-");
var date1 =new Date(arrayD1[0],arrayD1[1],arrayD1[2]);
var arrayD2= d2.split("-");
var date2 =new Date(arrayD2[0],arrayD2[1],arrayD2[2]);
if(date1> date2) return false;
return true;
}
functionverifyDate(d)
{
vardatePattern = /^\d{4}-(0?[1-9]|1[0-2])-(0?[1-9]|[1-2]\d|3[0-1])$/;
returndatePattern.test(d);
}
2. 鐢╰able鏄剧ずn鏉¤褰曪紝姣�3琛屾崲涓�娆¢鑹诧紝鍗�1锛�2锛�3鐢ㄧ孩鑹插瓧浣擄紝4锛�5锛�6鐢ㄧ豢鑹插瓧浣擄紝7锛�8锛�9鐢ㄧ孩棰滆壊瀛椾綋銆�
1
2
3
4
5
6
7
8
9
10
window.onload=function()
{
var tbl =document.getElementById("tbl");
rows =tbl.getElementsByTagName("tr");
for(i=0;i
{
var j= parseInt(i/3);
if(j%2==0)rows[i].style.backgroundColor="#f00";
else 聽rows[i].style.backgroundColor="#0f0";
}
}
3銆丠TML鐨� form鎻愪氦涔嬪墠濡備綍楠岃瘉鏁板�兼枃鏈鐨勫唴瀹瑰叏閮ㄤ负鏁板瓧?鍚﹀垯鐨勮瘽鎻愮ず鐢ㄦ埛骞剁粓姝㈡彁浜�?
function chkForm(this)
{
var value = thist.d1.value;
var len =value.length;
for(vari=0;i
{
if(value.charAt(i)>"9"|| value.charAt(i)<"0")
{
alert("鍚湁闈炴暟瀛楀瓧绗�");
returnfalse;
}
}
return true;
}
4銆佽鍐欏嚭鐢ㄤ簬鏍¢獙HTML鏂囨湰妗嗕腑杈撳叆鐨勫唴瀹瑰叏閮ㄤ负鏁板瓧鐨刯avascript浠g爜
function chkNumber(eleText)
{
var value =eleText.value;
var len =value.length;
for(vari=0;i
{
if(value.charAt(i)>"9"|| value.charAt(i)<"0")
{
alert("鍚湁闈炴暟瀛楀瓧绗�");
eleText.focus();
break;
}
}
}
闄や簡鍐欏畬浠g爜锛岃繕搴旇鍦ㄧ綉椤典笂鍐欏嚭瀹為獙姝ラ鍜屽湪浠g爜涓姞鍏ュ疄鐜版�濊矾锛岃闈㈣瘯瀹樹竴鐪嬪氨鏄庣櫧浣犵殑鎰忓浘鍜屾鏌ヤ綘鐨勭粨鏋溿��
5銆佽璇翠綘鐢ㄨ繃閭d簺ajax鎶�鏈拰妗嗘灦锛岃璇村畠浠殑鍖哄埆
鍥�. Java web閮ㄥ垎
1銆乀omcat鐨勪紭鍖栫粡楠�
绛�:鍘绘帀瀵箇eb.xml鐨勭洃瑙嗭紝鎶妀sp鎻愬墠缂栬緫鎴怱ervlet銆�
鏈夊瘜浣欑墿鐞嗗唴瀛樼殑鎯呭喌锛屽姞澶omcat浣跨敤鐨刯vm鐨勫唴瀛�
2銆丠TTP璇锋眰鐨凣ET涓嶱OST鏂瑰紡鐨勫尯鍒�
绛�:servlet鏈夎壇濂界殑鐢熷瓨鏈熺殑瀹氫箟锛屽寘鎷姞杞藉拰瀹炰緥鍖栥�佸垵濮嬪寲銆佸鐞嗚姹備互鍙婃湇鍔$粨鏉熴�傝繖涓敓瀛樻湡鐢眏avax.servlet.Servlet鎺ュ彛鐨刬nit,service鍜宒estroy鏂规硶琛ㄨ揪銆�
3銆佽В閲婁竴涓嬩粈涔堟槸servlet;
绛�:servlet鏈夎壇濂界殑鐢熷瓨鏈熺殑瀹氫箟锛屽寘鎷姞杞藉拰瀹炰緥鍖栥�佸垵濮嬪寲銆佸鐞嗚姹備互鍙婃湇鍔$粨鏉熴�傝繖涓敓瀛樻湡鐢眏avax.servlet.Servlet鎺ュ彛鐨刬nit,service鍜宒estroy鏂规硶琛ㄨ揪銆�
4銆佽涓�璇碨ervlet鐨勭敓鍛藉懆鏈�?
绛�:servlet鏈夎壇濂界殑鐢熷瓨鏈熺殑瀹氫箟锛屽寘鎷姞杞藉拰瀹炰緥鍖栥�佸垵濮嬪寲銆佸鐞嗚姹備互鍙婃湇鍔$粨鏉熴�傝繖涓敓瀛樻湡鐢眏avax.servlet.Servlet鎺ュ彛鐨刬nit,service鍜宒estroy鏂规硶琛ㄨ揪銆�
Servlet琚湇鍔″櫒瀹炰緥鍖栧悗锛屽鍣ㄨ繍琛屽叾init鏂规硶锛岃姹傚埌杈炬椂杩愯鍏秙ervice鏂规硶锛宻ervice鏂规硶鑷姩娲鹃仯杩愯涓庤姹傚搴旂殑doXXX鏂规硶锛坉oGet锛宒oPost锛夌瓑锛屽綋鏈嶅姟鍣ㄥ喅瀹氬皢瀹炰緥閿�姣佺殑鏃跺�欒皟鐢ㄥ叾destroy鏂规硶銆�
web瀹瑰櫒鍔犺浇servlet锛岀敓鍛藉懆鏈熷紑濮嬨�傞�氳繃璋冪敤servlet鐨刬nit()鏂规硶杩涜servlet鐨勫垵濮嬪寲銆傞�氳繃璋冪敤service()鏂规硶瀹炵幇锛屾牴鎹姹傜殑涓嶅悓璋冪敤涓嶅悓鐨刣o***()鏂规硶銆傜粨鏉熸湇鍔★紝web瀹瑰櫒璋冪敤servlet鐨刣estroy()鏂规硶銆�
5銆丼ervlet鐨勫熀鏈灦鏋�
public class ServletName extends HttpServlet {
public void doPost(HttpServletRequest request,HttpServletResponse response) throws
ServletException, IOException {
}
public void doGet(HttpServletRequest request,HttpServletResponse response) throws
ServletException, IOException {
}
}
6銆丼ERVLET API涓璮orward()涓巖edirect()鐨勫尯鍒紵
绛�:鍓嶈�呬粎鏄鍣ㄤ腑鎺у埗鏉冪殑杞悜锛屽湪瀹㈡埛绔祻瑙堝櫒鍦板潃鏍忎腑涓嶄細鏄剧ず鍑鸿浆鍚戝悗鐨勫湴鍧�锛涘悗鑰呭垯鏄畬鍏ㄧ殑璺宠浆锛屾祻瑙堝櫒灏嗕細寰楀埌璺宠浆鐨勫湴鍧�锛屽苟閲嶆柊鍙戦�佽姹傞摼鎺ャ�傝繖鏍凤紝浠庢祻瑙堝櫒鐨勫湴鍧�鏍忎腑鍙互鐪嬪埌璺宠浆鍚庣殑閾炬帴鍦板潃銆傛墍浠ワ紝鍓嶈�呮洿鍔犻珮鏁堬紝鍦ㄥ墠鑰呭彲浠ユ弧瓒抽渶瑕佹椂锛屽敖閲忎娇鐢╢orward()鏂规硶锛屽苟涓旓紝杩欐牱涔熸湁鍔╀簬闅愯棌瀹為檯鐨勯摼鎺ャ�傚湪鏈変簺鎯呭喌涓嬶紝姣斿锛岄渶瑕佽烦杞埌涓�涓叾瀹冩湇鍔″櫒涓婄殑璧勬簮锛屽垯蹇呴』浣跨敤
sendRedirect()鏂规硶銆�
7銆佷粈涔堟儏鍐典笅璋冪敤doGet()鍜宒oPost()锛�
Jsp椤甸潰涓殑FORM鏍囩閲岀殑method灞炴�т负get鏃惰皟鐢╠oGet()锛屼负post鏃惰皟鐢╠oPost()銆�
8銆丷equest瀵硅薄鐨勪富瑕佹柟娉曪細
setAttribute(String name,Object)锛氳缃悕瀛椾负name鐨剅equest鐨勫弬鏁板��
getAttribute(String name)锛氳繑鍥炵敱name鎸囧畾鐨勫睘鎬у��
getAttributeNames()锛氳繑鍥瀝equest瀵硅薄鎵�鏈夊睘鎬х殑鍚嶅瓧闆嗗悎锛岀粨鏋滄槸涓�涓灇涓剧殑瀹炰緥
getCookies()锛氳繑鍥炲鎴风鐨勬墍鏈塁ookie瀵硅薄锛岀粨鏋滄槸涓�涓狢ookie鏁扮粍
getCharacterEncoding()锛氳繑鍥炶姹備腑鐨勫瓧绗︾紪鐮佹柟寮�
getContentLength()锛氳繑鍥炶姹傜殑Body鐨勯暱搴�
getHeader(String name)锛氳幏寰桯TTP鍗忚瀹氫箟鐨勬枃浠跺ご淇℃伅
getHeaders(String name)锛氳繑鍥炴寚瀹氬悕瀛楃殑request Header鐨勬墍鏈夊�硷紝缁撴灉鏄竴涓灇涓剧殑瀹炰緥
getHeaderNames()锛氳繑鍥炴墍浠equest Header鐨勫悕瀛楋紝缁撴灉鏄竴涓灇涓剧殑瀹炰緥
getInputStream()锛氳繑鍥炶姹傜殑杈撳叆娴侊紝鐢ㄤ簬鑾峰緱璇锋眰涓殑鏁版嵁
getMethod()锛氳幏寰楀鎴风鍚戞湇鍔″櫒绔紶閫佹暟鎹殑鏂规硶
getParameter(String name)锛氳幏寰楀鎴风浼犻�佺粰鏈嶅姟鍣ㄧ鐨勬湁name鎸囧畾鐨勫弬鏁板��
getParameterNames()锛氳幏寰楀鎴风浼犻�佺粰鏈嶅姟鍣ㄧ鐨勬墍鏈夊弬鏁扮殑鍚嶅瓧锛岀粨鏋滄槸涓�涓灇涓剧殑瀹炰緥
getParametervalues(String name)锛氳幏寰楁湁name鎸囧畾鐨勫弬鏁扮殑鎵�鏈夊��
getProtocol()锛氳幏鍙栧鎴风鍚戞湇鍔″櫒绔紶閫佹暟鎹墍渚濇嵁鐨勫崗璁悕绉�
getQueryString()锛氳幏寰楁煡璇㈠瓧绗︿覆
getRequestURI()锛氳幏鍙栧彂鍑鸿姹傚瓧绗︿覆鐨勫鎴风鍦板潃
getRemoteAddr()锛氳幏鍙栧鎴风鐨処P鍦板潃
getRemoteHost()锛氳幏鍙栧鎴风鐨勫悕瀛�
getSession([Boolean create])锛氳繑鍥炲拰璇锋眰鐩稿叧Session
getServerName()锛氳幏鍙栨湇鍔″櫒鐨勫悕瀛�
getServletPath()锛氳幏鍙栧鎴风鎵�璇锋眰鐨勮剼鏈枃浠剁殑璺緞
getServerPort()锛氳幏鍙栨湇鍔″櫒鐨勭鍙e彿
removeAttribute(String name)锛氬垹闄よ姹備腑鐨勪竴涓睘鎬�
9銆乫orward鍜宺edirect鐨勫尯鍒�
forward鏄湇鍔″櫒璇锋眰璧勬簮锛屾湇鍔″櫒鐩存帴璁块棶鐩爣鍦板潃鐨刄RL锛屾妸閭d釜URL鐨勫搷搴斿唴瀹硅鍙栬繃鏉ワ紝鐒跺悗鎶婅繖浜涘唴瀹瑰啀鍙戠粰娴忚鍣紝娴忚鍣ㄦ牴鏈笉鐭ラ亾鏈嶅姟鍣ㄥ彂閫佺殑鍐呭鏄粠鍝効鏉ョ殑锛屾墍浠ュ畠鐨勫湴鍧�鏍忎腑杩樻槸鍘熸潵鐨勫湴鍧�銆�
redirect灏辨槸鏈嶅姟绔牴鎹�昏緫,鍙戦�佷竴涓姸鎬佺爜,鍛婅瘔娴忚鍣ㄩ噸鏂板幓璇锋眰閭d釜鍦板潃锛屼竴鑸潵璇存祻瑙堝櫒浼氱敤鍒氭墠璇锋眰鐨勬墍鏈夊弬鏁伴噸鏂拌姹傦紝鎵�浠ession,request鍙傛暟閮藉彲浠ヨ幏鍙栥��
10銆乺equest.getAttribute()鍜� request.getParameter()鏈変綍鍖哄埆?
11. jsp鏈夊摢浜涘唴缃璞�?浣滅敤鍒嗗埆鏄粈涔�?鍒嗗埆鏈変粈涔堟柟娉曪紵
绛�:JSP鍏辨湁浠ヤ笅9涓唴缃殑瀵硅薄锛�
request 鐢ㄦ埛绔姹傦紝姝よ姹備細鍖呭惈鏉ヨ嚜GET/POST璇锋眰鐨勫弬鏁�
response 缃戦〉浼犲洖鐢ㄦ埛绔殑鍥炲簲
pageContext 缃戦〉鐨勫睘鎬ф槸鍦ㄨ繖閲岀鐞�
session 涓庤姹傛湁鍏崇殑浼氳瘽鏈�
application servlet 姝e湪鎵ц鐨勫唴瀹�
out 鐢ㄦ潵浼犻�佸洖搴旂殑杈撳嚭
configservlet鐨勬瀯鏋堕儴浠�
page JSP缃戦〉鏈韩
exception 閽堝閿欒缃戦〉锛屾湭鎹曟崏鐨勪緥澶�
request琛ㄧずHttpServletRequest瀵硅薄銆傚畠鍖呭惈浜嗘湁鍏虫祻瑙堝櫒璇锋眰鐨勪俊鎭紝骞朵笖鎻愪緵浜嗗嚑涓敤浜庤幏鍙朿ookie, header,鍜宻ession鏁版嵁鐨勬湁鐢ㄧ殑鏂规硶銆�
response琛ㄧずHttpServletResponse瀵硅薄锛屽苟鎻愪緵浜嗗嚑涓敤浜庤缃�佸洖娴忚鍣ㄧ殑鍝嶅簲鐨勬柟娉曪紙濡俢ookies,澶翠俊鎭瓑锛�
out瀵硅薄鏄痡avax.jsp.JspWriter鐨勪竴涓疄渚嬶紝骞舵彁渚涗簡鍑犱釜鏂规硶浣夸綘鑳界敤浜庡悜娴忚鍣ㄥ洖閫佽緭鍑虹粨鏋溿��
pageContext琛ㄧず涓�涓猨avax.servlet.jsp.PageContext瀵硅薄銆傚畠鏄敤浜庢柟渚垮瓨鍙栧悇绉嶈寖鍥寸殑鍚嶅瓧绌洪棿銆乻ervlet鐩稿叧鐨勫璞$殑API锛屽苟涓斿寘瑁呬簡閫氱敤鐨剆ervlet鐩稿叧鍔熻兘鐨勬柟娉曘��
session琛ㄧず涓�涓姹傜殑javax.servlet.http.HttpSession瀵硅薄銆係ession鍙互瀛樿串鐢ㄦ埛鐨勭姸鎬佷俊鎭�
applicaton琛ㄧず涓�涓猨avax.servle.ServletContext瀵硅薄銆傝繖鏈夊姪浜庢煡鎵炬湁鍏硈ervlet寮曟搸鍜宻ervlet鐜鐨勪俊鎭�
config琛ㄧず涓�涓猨avax.servlet.ServletConfig瀵硅薄銆傝瀵硅薄鐢ㄤ簬瀛樺彇servlet瀹炰緥鐨勫垵濮嬪寲鍙傛暟銆�
page琛ㄧず浠庤椤甸潰浜х敓鐨勪竴涓猻ervlet瀹炰緥
12. jsp鏈夊摢浜涘姩浣�?浣滅敤鍒嗗埆鏄粈涔�?
锛堣繖涓棶棰樹技涔庝笉閲嶈锛屼笉鏄庣櫧涓轰綍鏈夋棰橈級
绛�:JSP鍏辨湁浠ヤ笅6绉嶅熀鏈姩浣�
jsp:include锛氬湪椤甸潰琚姹傜殑鏃跺�欏紩鍏ヤ竴涓枃浠躲��
jsp:useBean锛氬鎵炬垨鑰呭疄渚嬪寲涓�涓狫avaBean銆�
jsp:setProperty锛氳缃甁avaBean鐨勫睘鎬с��
jsp:getProperty锛氳緭鍑烘煇涓狫avaBean鐨勫睘鎬с��
jsp:forward锛氭妸璇锋眰杞埌涓�涓柊鐨勯〉闈€��
jsp:plugin锛氭牴鎹祻瑙堝櫒绫诲瀷涓篔ava鎻掍欢鐢熸垚OBJECT鎴朎MBED鏍囪
13銆丣SP鐨勫父鐢ㄦ寚浠�
isErrorPage(鏄惁鑳戒娇鐢‥xception瀵硅薄)锛宨sELIgnored(鏄惁蹇界暐琛ㄨ揪寮�)
14. JSP涓姩鎬両NCLUDE涓庨潤鎬両NCLUDE鐨勫尯鍒紵
绛旓細鍔ㄦ�両NCLUDE鐢╦sp:include鍔ㄤ綔瀹炵幇
瀹冩�绘槸浼氭鏌ユ墍鍚枃浠朵腑鐨勫彉鍖栵紝閫傚悎鐢ㄤ簬鍖呭惈鍔ㄦ�侀〉闈紝骞朵笖鍙互甯﹀弬鏁� 闈欐�両NCLUDE鐢╥nclude浼爜瀹炵幇,瀹氫笉浼氭鏌ユ墍鍚枃浠剁殑鍙樺寲锛岄�傜敤浜庡寘鍚潤鎬侀〉闈� <%@include file=included.htm %>
15銆佷袱绉嶈烦杞柟寮忓垎鍒槸浠�涔�?鏈変粈涔堝尯鍒�?
锛堜笅闈㈢殑鍥炵瓟涓ラ噸閿欒锛屽簲璇ユ槸鎯抽棶forward鍜宻endRedirect鐨勫尯鍒紝姣曠珶鍑洪鐨勪汉涓嶆槸涓撲笟鎼炴枃瀛楄壓鏈殑浜猴紝鍙兘琛ㄨ揪鑳藉姏骞朵笉瑙佸緱寰堝己锛岀敤璇嶄笉涓�瀹氱簿鍑嗭紝鍔犱箣鍏惰嚜韬殑鎶�鏈潰涔熷彲鑳藉瓨鍦ㄤ竴浜涢棶棰橈紝涓嶄竴瀹氱湡姝e皢浠栫殑鎰忔�濊〃杈炬竻妤氫簡锛屼弗鏍兼剰鎬濅笂鏉ヨ锛屼竴浜涢鐩彲鑳芥牴鏈氨鏃犱汉鑳界瓟锛屾墍浠ワ紝绛旈鏃惰鎺屾彙涓诲姩锛屽彧瑕佹妸鑷繁鐭ラ亾鐨勮〃杈炬竻妤氬氨澶熶簡锛岃�屼笉瑕佸幓鎺ㄦ暡鍘熷棰樼洰鐨勫叿浣撳惈涔夋槸浠�涔堬紝涓嶈涓�鍛虫兂鐫�鏄湪绛旈锛�
绛旓細鏈変袱绉嶏紝鍒嗗埆涓猴細
鍓嶈�呴〉闈笉浼氳浆鍚慽nclude鎵�鎸囩殑椤甸潰锛屽彧鏄樉绀鸿椤电殑缁撴灉锛屼富椤甸潰杩樻槸鍘熸潵鐨勯〉闈€�傛墽琛屽畬鍚庤繕浼氬洖鏉ワ紝鐩稿綋浜庡嚱鏁拌皟鐢ㄣ�傚苟涓斿彲浠ュ甫鍙傛暟.鍚庤�呭畬鍏ㄨ浆鍚戞柊椤甸潰锛屼笉浼氬啀鍥炴潵銆傜浉褰撲簬go to 璇彞銆�
16銆侀〉闈㈤棿瀵硅薄浼犻�掔殑鏂规硶
request锛宻ession锛宎pplication锛宑ookie绛�
17銆丣SP鍜孲ervlet鏈夊摢浜涚浉鍚岀偣鍜屼笉鍚岀偣锛屼粬浠箣闂寸殑鑱旂郴鏄粈涔堬紵
JSP鏄疭ervlet鎶�鏈殑鎵╁睍锛屾湰璐ㄤ笂鏄疭ervlet鐨勭畝鏄撴柟寮忥紝鏇村己璋冨簲鐢ㄧ殑澶栬〃琛ㄨ揪銆侸SP缂栬瘧鍚庢槸"绫籹ervlet"銆係ervlet鍜孞SP鏈�涓昏鐨勪笉鍚岀偣鍦ㄤ簬锛孲ervlet鐨勫簲鐢ㄩ�昏緫鏄湪Java鏂囦欢涓紝骞朵笖瀹屽叏浠庤〃绀哄眰涓殑HTML閲屽垎绂诲紑鏉ャ�傝�孞SP鐨勬儏鍐垫槸Java鍜孒TML鍙互缁勫悎鎴愪竴涓墿灞曞悕涓�.jsp鐨勬枃浠躲�侸SP渚ч噸浜庤鍥撅紝Servlet涓昏鐢ㄤ簬鎺у埗閫昏緫銆�
18銆丮VC鐨勫悇涓儴鍒嗛兘鏈夐偅浜涙妧鏈潵瀹炵幇?濡備綍瀹炵幇?
绛�:MVC鏄疢odel锛峍iew锛岰ontroller鐨勭畝鍐欍�侻odel浠h〃鐨勬槸搴旂敤鐨勪笟鍔¢�昏緫锛堥�氳繃JavaBean锛孍JB缁勪欢瀹炵幇锛夛紝View鏄簲鐢ㄧ殑琛ㄧず闈紙鐢盝SP椤甸潰浜х敓锛夛紝Controller鏄彁渚涘簲鐢ㄧ殑澶勭悊杩囩▼鎺у埗锛堜竴鑸槸涓�涓猄ervlet锛夛紝閫氳繃杩欑璁捐妯″瀷鎶婂簲鐢ㄩ�昏緫锛屽鐞嗚繃绋嬪拰鏄剧ず閫昏緫鍒嗘垚涓嶅悓鐨勭粍浠跺疄鐜般�傝繖浜涚粍浠跺彲浠ヨ繘琛屼氦浜掑拰閲嶇敤銆�
19銆佹垜浠湪web搴旂敤寮�鍙戣繃绋嬩腑缁忓父閬囧埌杈撳嚭鏌愮缂栫爜鐨勫瓧绗︼紝濡俰so8859-1绛夛紝濡備綍杈撳嚭涓�涓煇绉嶇紪鐮佺殑瀛楃涓诧紵
Public String translate(String str) {
String tempStr ="";
try {
tempStr = newString(str.getBytes("ISO-8859-1"), "GBK");
tempStr =tempStr.trim();
}
catch (Exception e) {
System.err.println(e.getMessage());
}
return tempStr;
}
20锛�鐜板湪杈撳叆n涓暟瀛楋紝浠ラ�楀彿锛屽垎寮�锛涚劧鍚庡彲閫夋嫨鍗囨垨鑰呴檷搴忔帓搴忥紱鎸夋彁浜ら敭灏卞湪鍙︿竴椤甸潰鏄剧ず鎸変粈涔堟帓搴忥紝缁撴灉涓猴紝鎻愪緵reset
浜�.鏁版嵁搴撻儴鍒�
1銆佺敤涓ょ鏂瑰紡鏍规嵁閮ㄩ棬鍙蜂粠楂樺埌浣庯紝宸ヨ祫浠庝綆鍒伴珮鍒楀嚭姣忎釜鍛樺伐鐨勪俊鎭��
employee:
eid,ename,salary,deptid;
select * from employeeorder by deptid desc,salary
2銆佸垪鍑哄悇涓儴闂ㄤ腑宸ヨ祫楂樹簬鏈儴闂ㄧ殑骞冲潎宸ヨ祫鐨勫憳宸ユ暟鍜岄儴闂ㄥ彿锛屽苟鎸夐儴闂ㄥ彿鎺掑簭
鍒涘缓琛細
mysql> createtable employee921(id int primary key auto_increment,name varchar(5
0),salary bigint,deptid int);
鎻掑叆瀹為獙鏁版嵁锛�
mysql> insert into employee921values(null,'zs',1000,1),(null,'ls',1100,1),(null
,'ww',1100,1),(null,'zl',900,1) ,(null,'zl',1000,2), (null,'zl',900,2),(null,'z
l',1000,2) , (null,'zl',1100,2);
缂栧啓sql璇彞锛�
锛堬級select avg(salary) from employee921 group by deptid;
锛堬級mysql> selectemployee921.id,employee921.name,employee921.salary,employee921.dep
tid tid fromemployee921where salary > (select avg(salary) from employee921 where 聽deptid = tid);
鏁堢巼浣庣殑涓�涓鍙ワ紝浠呬緵瀛︿範鍙傝�冧娇鐢紙鍦╣roup by涔嬪悗涓嶈兘浣跨敤where锛屽彧鑳戒娇鐢╤aving锛屽湪group by涔嬪墠鍙互浣跨敤where锛屽嵆琛ㄧず瀵硅繃婊ゅ悗鐨勭粨鏋滃垎缁勶級锛�
mysql> selectemployee921.id,employee921.name,employee921.salary,employee921.dep
tid tid fromemployee921where salary > (select avg(salary) from employee921 group by deptid havingdeptid = tid);
锛堬級select count(*) ,tid
from (
selectemployee921.id,employee921.name,employee921.salary,employee921.deptid tid
from 聽 聽 聽employee921
where salary>
(selectavg(salary) from employee921 where deptid = tid)
) as t
group by tid ;
鍙﹀涓�绉嶆柟寮忥細鍏宠仈鏌ヨ
select a.ename,a.salary,a.deptid
from emp a,
(selectdeptd,avg(salary) avgsal from emp group by deptid ) b
where a.deptid=b.deptidand a.salary>b.avgsal;
3銆佸瓨鍌ㄨ繃绋嬩笌瑙﹀彂鍣ㄥ繀椤昏锛岀粡甯歌闈㈣瘯鍒�?
create procedure insert_Student (_name varchar(50),_age int ,out_id int)
begin
insert into studentvalue(null,_name,_age);
select max(stuId)into _id from student;
end;
call insert_Student('wfz',23,@id);
select @id;
mysql> create trigger update_Student BEFORE update on studentFOR EACH ROW
-> select * from student;
瑙﹀彂鍣ㄤ笉鍏佽杩斿洖缁撴灉
create trigger update_StudentBEFORE update on student FOR EACH ROW
insert intostudent value(null,'zxx',28);
mysql鐨勮Е鍙戝櫒鐩墠涓嶈兘瀵瑰綋鍓嶈〃杩涜鎿嶄綔
create trigger update_StudentBEFORE update on student FOR EACH ROW
delete from articleswhere id=8;
杩欎釜渚嬪瓙涓嶆槸寰堝ソ锛屾渶濂芥槸鐢ㄥ垹闄や竴涓敤鎴锋椂锛岄『甯﹀垹闄よ鐢ㄦ埛鐨勬墍鏈夊笘瀛�
杩欓噷瑕佹敞鎰忎娇鐢∣LD.id
瑙﹀彂鍣ㄧ敤澶勮繕鏄緢澶氱殑锛屾瘮濡傛牎鍐呯綉銆佸紑蹇冪綉銆丗acebook锛屼綘鍙戜竴涓棩蹇楋紝鑷姩閫氱煡濂藉弸锛屽叾瀹炲氨鏄湪澧炲姞鏃ュ織鏃跺仛涓�涓悗瑙﹀彂锛屽啀鍚戦�氱煡琛ㄤ腑鍐欏叆鏉$洰銆傚洜涓鸿Е鍙戝櫒鏁堢巼楂樸�傝�孶CH娌℃湁鐢ㄨЕ鍙戝櫒锛屾晥鐜囧拰鏁版嵁澶勭悊鑳藉姏閮藉緢浣庛��
瀛樺偍杩囩▼鐨勫疄楠屾楠わ細
mysql> delimiter |
mysql> create procedure insertArticle_Procedure (pTitle varchar(50),pBid int,out
pId int)
-> begin
-> insert into article1value(null,pTitle,pBid);
-> select max(id) into pId fromarticle1;
-> end;
-> |
Query OK, 0 rows affected (0.05sec)
mysql> callinsertArticle_Procedure('浼犳櫤鎾',1,@pid);
-> |
Query OK, 0 rows affected (0.00sec)
mysql> delimiter ;
mysql> select @pid;
+------+
| @pid |
+------+
| 3|
+------+
1 row in set (0.00 sec)
mysql> select * fromarticle1;
+----+--------------+------+
| id | title| bid |
+----+--------------+------+
| 1| test 聽 聽 聽 聽| 1 聽 聽|
| 2| chuanzhiboke | 1 聽 聽|
| 3|浼犳櫤鎾| 1 聽 聽|
+----+--------------+------+
3 rows in set (0.00 sec)
瑙﹀彂鍣ㄧ殑瀹為獙姝ラ锛�
create table board1(id intprimary key auto_increment,name varchar(50),ar
ticleCount int);
create table article1(id intprimary key auto_increment,title varchar(50)
,bid int referencesboard1(id));
delimiter |
create triggerinsertArticle_Trigger after insert on article1 for each ro
w begin
-> update board1 setarticleCount=articleCount+1 where id= NEW.bid;
-> end;
-> |
delimiter ;
insert into board1 value(null,'test',0);
insert into article1value(null,'test',1);
杩樻湁锛屾瘡鎻掑叆涓�涓笘瀛愶紝閮藉笇鏈涘皢鐗堥潰琛ㄤ腑鐨勬渶鍚庡彂甯栨椂闂达紝甯栧瓙鎬绘暟瀛楁杩涜鍚屾鏇存柊锛岀敤瑙﹀彂鍣ㄥ仛鏁堢巼灏卞緢楂樸�備笅娆¤璁捐杩欐牱涓�涓渚嬶紝鍐欒Е鍙戝櫒鏃讹紝瀵逛簬鏈�鍚庡彂甯栨椂闂村彲鑳介渶瑕佺敤declare鏂瑰紡澹版槑涓�涓彉閲忥紝鎴栬�呮槸鐢∟EW.posttime鏉ョ敓鎴愩��
4銆佹暟鎹簱涓夎寖寮忔槸浠�涔�?
绗竴鑼冨紡锛�1NF锛夛細瀛楁鍏锋湁鍘熷瓙鎬�,涓嶅彲鍐嶅垎銆傛墍鏈夊叧绯诲瀷鏁版嵁搴撶郴缁熼兘婊¤冻绗竴鑼冨紡锛�
鏁版嵁搴撹〃涓殑瀛楁閮芥槸鍗曚竴灞炴�х殑锛屼笉鍙啀鍒嗐�備緥濡傦紝濮撳悕瀛楁锛屽叾涓殑濮撳拰鍚嶅繀椤讳綔涓轰竴涓暣浣擄紝鏃犳硶鍖哄垎鍝儴鍒嗘槸濮擄紝鍝儴鍒嗘槸鍚嶏紝濡傛灉瑕佸尯鍒嗗嚭濮撳拰鍚嶏紝蹇呴』璁捐鎴愪袱涓嫭绔嬬殑瀛楁銆�
绗簩鑼冨紡锛�2NF锛夛細
绗簩鑼冨紡锛�2NF锛夋槸鍦ㄧ涓�鑼冨紡锛�1NF锛夌殑鍩虹涓婂缓绔嬭捣鏉ョ殑锛屽嵆婊¤冻绗簩鑼冨紡锛�2NF锛夊繀椤诲厛婊¤冻绗竴鑼冨紡锛�1NF锛夈��
瑕佹眰鏁版嵁搴撹〃涓殑姣忎釜瀹炰緥鎴栬蹇呴』鍙互琚儫涓�鍦板尯鍒嗐�傞�氬父闇�瑕佷负琛ㄥ姞涓婁竴涓垪锛屼互瀛樺偍鍚勪釜瀹炰緥鐨勬儫涓�鏍囪瘑銆傝繖涓儫涓�灞炴�у垪琚О涓轰富鍏抽敭瀛楁垨涓婚敭銆�
绗簩鑼冨紡锛�2NF锛夎姹傚疄浣撶殑灞炴�у畬鍏ㄤ緷璧栦簬涓诲叧閿瓧銆傛墍璋撳畬鍏ㄤ緷璧栨槸鎸囦笉鑳藉瓨鍦ㄤ粎渚濊禆涓诲叧閿瓧涓�閮ㄥ垎鐨勫睘鎬э紝濡傛灉瀛樺湪锛岄偅涔堣繖涓睘鎬у拰涓诲叧閿瓧鐨勮繖涓�閮ㄥ垎搴旇鍒嗙鍑烘潵褰㈡垚涓�涓柊鐨勫疄浣擄紝鏂板疄浣撲笌鍘熷疄浣撲箣闂存槸涓�瀵瑰鐨勫叧绯汇�備负瀹炵幇鍖哄垎閫氬父闇�瑕佷负琛ㄥ姞涓婁竴涓垪锛屼互瀛樺偍鍚勪釜瀹炰緥鐨勬儫涓�鏍囪瘑銆傜畝鑰岃█涔嬶紝绗簩鑼冨紡灏辨槸闈炰富灞炴�ч潪閮ㄥ垎渚濊禆浜庝富鍏抽敭瀛椼��
绗笁鑼冨紡鐨勮姹傚涓嬶細
婊¤冻绗笁鑼冨紡锛�3NF锛夊繀椤诲厛婊¤冻绗簩鑼冨紡锛�2NF锛夈�傜畝鑰岃█涔嬶紝绗笁鑼冨紡锛�3NF锛夎姹備竴涓暟鎹簱琛ㄤ腑涓嶅寘鍚凡鍦ㄥ叾瀹冭〃涓凡鍖呭惈鐨勯潪涓诲叧閿瓧淇℃伅銆�
鎵�浠ョ涓夎寖寮忓叿鏈夊涓嬬壒寰侊細
1锛屾瘡涓�鍒楀彧鏈変竴涓��
2锛屾瘡涓�琛岄兘鑳藉尯鍒嗐��
3锛屾瘡涓�涓〃閮戒笉鍖呭惈鍏朵粬琛ㄥ凡缁忓寘鍚殑闈炰富鍏抽敭瀛椾俊鎭��
渚嬪锛屽笘瀛愯〃涓彧鑳藉嚭鐜板彂甯栦汉鐨刬d锛岃�屼笉鑳藉嚭鐜板彂甯栦汉鐨刬d锛岃繕鍚屾椂鍑虹幇鍙戝笘浜哄鍚嶏紝鍚﹀垯锛屽彧瑕佸嚭鐜板悓涓�鍙戝笘浜篿d鐨勬墍鏈夎褰曪紝瀹冧滑涓殑濮撳悕閮ㄥ垎閮藉繀椤讳弗鏍间繚鎸佷竴鑷达紝杩欏氨鏄暟鎹啑浣欍��
5銆佽鍑轰竴浜涙暟鎹簱浼樺寲鏂归潰鐨勭粡楠�?
鐢≒reparedStatement涓�鑸潵璇存瘮Statement鎬ц兘楂橈細涓�涓猻ql鍙戠粰鏈嶅姟鍣ㄥ幓鎵ц锛屾秹鍙婃楠わ細璇硶妫�鏌ャ�佽涔夊垎鏋愶紝缂栬瘧锛岀紦瀛�
鈥渋nert into user values(1,1,1)鈥�-脿浜岃繘鍒�
鈥渋nert into user values(2,2,2)鈥�-脿浜岃繘鍒�
鈥渋nert into user values(?,?,?)鈥�-脿浜岃繘鍒�
鏈夊閿害鏉熶細褰卞搷鎻掑叆鍜屽垹闄ゆ�ц兘锛屽鏋滅▼搴忚兘澶熶繚璇佹暟鎹殑瀹屾暣鎬э紝閭e湪璁捐鏁版嵁搴撴椂灏卞幓鎺夊閿�傦紙姣斿柣锛氬氨濂芥瘮鍏嶆浜у搧锛屽氨鏄负浜嗘彁楂樻晥鐜囷紝鍏呭垎鐩镐俊浜у搧鐨勫埗閫犲晢锛�
锛堝浜巋ibernate鏉ヨ锛屽氨搴旇鏈変竴涓彉鍖栵細empleyee->Deptment瀵硅薄锛岀幇鍦ㄨ璁℃椂灏辨垚浜唀mployee脿deptid锛�
鐪媘ysql甯姪鏂囨。瀛愭煡璇㈢珷鑺傜殑鏈�鍚庨儴鍒嗭紝渚嬪锛屾牴鎹壂鎻忕殑鍘熺悊锛屼笅闈㈢殑瀛愭煡璇㈣鍙ヨ姣旂浜屾潯鍏宠仈鏌ヨ鐨勬晥鐜囬珮锛�
1.select e.name,e.salarywhere e.managerid=(select id from employee where name='zxx');
2.select e.name,e.salary,m.name,m.salary fromemployees e,employees m where
e.managerid = m.id andm.name='zxx';
琛ㄤ腑鍏佽閫傚綋鍐椾綑锛岃濡傦紝涓婚甯栫殑鍥炲鏁伴噺鍜屾渶鍚庡洖澶嶆椂闂寸瓑
灏嗗鍚嶅拰瀵嗙爜鍗曠嫭浠庣敤鎴疯〃涓嫭绔嬪嚭鏉ャ�傝繖鍙互鏄潪甯稿ソ鐨勪竴瀵逛竴鐨勬渚嬪摕锛�
sql璇彞鍏ㄩ儴澶у啓锛岀壒鍒槸鍒楀悕鍜岃〃鍚嶉兘澶у啓銆傜壒鍒槸sql鍛戒护鐨勭紦瀛樺姛鑳斤紝鏇村姞闇�瑕佺粺涓�澶у皬鍐欙紝sql璇彞脿鍙戠粰oracle鏈嶅姟鍣犺娉曟鏌ュ拰缂栬瘧鎴愪负鍐呴儴鎸囦护脿缂撳瓨鍜屾墽琛屾寚浠ゃ�傛牴鎹紦瀛樼殑鐗圭偣锛屼笉瑕佹嫾鍑戞潯浠讹紝鑰屾槸鐢�?鍜孭reparedStatment
杩樻湁绱㈠紩瀵规煡璇㈡�ц兘鐨勬敼杩涗篃鏄�煎緱鍏虫敞鐨勩��
澶囨敞锛氫笅闈㈡槸鍏充簬鎬ц兘鐨勮璁轰妇渚�
4鑸彮 3涓煄甯�
m*n
select * from flight,city where flight.startcityid=city.cityidand city.name='beijing';
m + n
select * from flight where startcityid = (select cityid fromcity where cityname='beijing');
select flight.id,'beijing',flight.flightTime from flight wherestartcityid = (select cityid from city where cityname='beijing')
6銆乽nion鍜寀nion all鏈変粈涔堜笉鍚�?
鍋囪鎴戜滑鏈変竴涓〃Student锛屽寘鎷互涓嬪瓧娈典笌鏁版嵁锛�
drop table student;
create table student
(
id int primary key,
name nvarchar2(50) not null,
score number not null
);
insert into student values(1,'Aaron',78);
insert into student values(2,'Bill',76);
insert into student values(3,'Cindy',89);
insert into student values(4,'Damon',90);
insert into student values(5,'Ella',73);
insert into student values(6,'Frado',61);
insert into student values(7,'Gill',99);
insert into student values(8,'Hellen',56);
insert into student values(9,'Ivan',93);
insert into student values(10,'Jay',90);
commit;
Union鍜孶nion All鐨勫尯鍒��
select *
from student
where id < 4
union
select *
from student
where id > 2 and id < 6
缁撴灉灏嗘槸
1 聽 聽Aaron 聽 聽78
2 聽 聽Bill 聽 聽76
3 聽 聽Cindy 聽 聽89
4 聽 聽Damon 聽 聽90
5 聽 聽Ella 聽 聽73
濡傛灉鎹㈡垚Union All杩炴帴涓や釜缁撴灉闆嗭紝鍒欒繑鍥炵粨鏋滄槸锛�
1 聽 聽Aaron 聽 聽78
2 聽 聽Bill 聽 聽76
3 聽 聽Cindy 聽 聽89
3 聽 聽Cindy 聽 聽89
4 聽 聽Damon 聽 聽90
5 聽 聽Ella 聽 聽73
鍙互鐪嬪埌锛孶nion鍜孶nion All鐨勫尯鍒箣涓�鍦ㄤ簬瀵归噸澶嶇粨鏋滅殑澶勭悊銆�
UNION鍦ㄨ繘琛岃〃閾炬帴鍚庝細绛涢�夋帀閲嶅鐨勮褰曪紝鎵�浠ュ湪琛ㄩ摼鎺ュ悗浼氬鎵�浜х敓鐨勭粨鏋滈泦杩涜鎺掑簭杩愮畻锛屽垹闄ら噸澶嶇殑璁板綍鍐嶈繑鍥炵粨鏋溿�傚疄闄呭ぇ閮ㄥ垎搴旂敤涓槸涓嶄細浜х敓閲嶅鐨勮褰曪紝鏈�甯歌鐨勬槸杩囩▼琛ㄤ笌鍘嗗彶琛║NION銆傚锛�
select * from gc_dfys
union
select * from ls_jg_dfys
杩欎釜SQL鍦ㄨ繍琛屾椂鍏堝彇鍑轰袱涓〃鐨勭粨鏋滐紝鍐嶇敤鎺掑簭绌洪棿杩涜鎺掑簭鍒犻櫎閲嶅鐨勮褰曪紝鏈�鍚庤繑鍥炵粨鏋滈泦锛屽鏋滆〃鏁版嵁閲忓ぇ鐨勮瘽鍙兘浼氬鑷寸敤纾佺洏杩涜鎺掑簭銆�
鑰孶NION ALL鍙槸绠�鍗曠殑灏嗕袱涓粨鏋滃悎骞跺悗灏辫繑鍥炪�傝繖鏍凤紝濡傛灉杩斿洖鐨勪袱涓粨鏋滈泦涓湁閲嶅鐨勬暟鎹紝閭d箞杩斿洖鐨勭粨鏋滈泦灏变細鍖呭惈閲嶅鐨勬暟鎹簡銆�
浠庢晥鐜囦笂璇达紝UNION ALL瑕佹瘮UNION蹇緢澶氾紝鎵�浠ワ紝濡傛灉鍙互纭鍚堝苟鐨勪袱涓粨鏋滈泦涓笉鍖呭惈閲嶅鐨勬暟鎹殑璇濓紝閭d箞灏变娇鐢║NION ALL锛�
7.鍒嗛〉璇彞
鍙栧嚭sql琛ㄤ腑绗�31鍒�40鐨勮褰曪紙浠ヨ嚜鍔ㄥ闀縄D涓轰富閿級
sql server鏂规1锛�
selecttop 10 * from t where id not in (select top 30 id from t order by id ) orde byid
sql server鏂规2锛�
selecttop 10 * from t where id in (select top 40 id from t order by id) order by iddesc
mysql鏂规锛歴elect * from t order by idlimit 30,10
oracle鏂规锛歴elect * from (select rownum r,* from t where r<=40) wherer>30
--------------------寰呮暣鐞嗚繘鍘荤殑鍐呭-------------------------------------
pageSize=20;
pageNo = 5;
1.鍒嗛〉鎶�鏈�1锛堢洿鎺ュ埄鐢╯ql璇彞杩涜鍒嗛〉锛屾晥鐜囨渶楂樺拰鏈�鎺ㄨ崘鐨勶級
mysql:sql = "select * from articles limit " +(pageNo-1)*pageSize + "," + pageSize;
oracle: sql = "select * from " +
"(selectrownum r,* from " +
"(select* from articles order by postime desc)" +
"whererownum<= " + pageNo*pageSize +") tmp " +
"wherer>" + (pageNo-1)*pageSize;
娉ㄩ噴锛氱7琛屼繚璇乺ownum鐨勯『搴忔槸纭畾鐨勶紝鍥犱负oracle鐨勭储寮曚細閫犳垚rownum杩斿洖涓嶅悓鐨勫��
绠�娲嬫彁绀猴細娌℃湁order by鏃讹紝rownum鎸夐『搴忚緭鍑猴紝涓�鏃︽湁浜唎rder by锛宺ownum涓嶆寜椤哄簭杈撳嚭浜嗭紝杩欒鏄巖ownum鏄帓搴忓墠鐨勭紪鍙枫�傚鏋滃order by浠庡彞涓殑瀛楁寤虹珛浜嗙储寮曪紝閭d箞锛宺ownum涔熸槸鎸夐『搴忚緭鍑虹殑锛屽洜涓鸿繖鏃跺�欑敓鎴愬師濮嬬殑鏌ヨ缁撴灉闆嗘椂浼氬弬鐓х储寮曡〃鐨勯『搴忔潵鏋勫缓銆�
sqlserver:sql = "select top 10 * from id not id(select top" + (pageNo-1)*pageSize + "id from articles)"
DataSource ds = new InitialContext().lookup(jndiurl);
Connection cn = ds.getConnection();
//"select * from user where id=?"--->binary directive
PreparedStatement pstmt = cn.prepareSatement(sql);
ResultSet rs = pstmt.executeQuery()
while(rs.next())
{
out.println(rs.getString(1));
}
2.涓嶅彲婊氬姩鐨勬父鏍�
pageSize=20;
pageNo = 5;
cn = null
stmt = null;
rs = null;
try
{
sqlserver:sql = "select* from articles";
DataSource ds = new InitialContext().lookup(jndiurl);
Connection cn = ds.getConnection();
//"select * from user where id=?"--->binary directive
PreparedStatement pstmt = cn.prepareSatement(sql);
ResultSet rs = pstmt.executeQuery()
for(int j=0;j<(pageNo-1)*pageSize;j++)
{
rs.next();
}
int i=0;
while(rs.next() && i<10)
{
i++;
out.println(rs.getString(1));
}
}
cacth(){}
finnaly
{
if(rs!=null)try{rs.close();}catch(Exceptione){}
if(stm.........
if(cn............
}
3.鍙粴鍔ㄧ殑娓告爣
pageSize=20;
pageNo = 5;
cn = null
stmt = null;
rs = null;
try
{
sqlserver:sql = "select* from articles";
DataSource ds = new InitialContext().lookup(jndiurl);
Connection cn = ds.getConnection();
//"select * from user where id=?"--->binary directive
PreparedStatement pstmt = cn.prepareSatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE,...);
//鏍规嵁涓婇潰杩欒浠g爜鐨勫紓甯窼QLFeatureNotSupportedException锛屽氨鍙垽鏂┍鍔ㄦ槸鍚︽敮鎸佸彲婊氬姩娓告爣
ResultSet rs = pstmt.executeQuery()
rs.absolute((pageNo-1)*pageSize)
int i=0;
while(rs.next() && i<10)
{
i++;
out.println(rs.getString(1));
}
}
cacth(){}
finnaly
{
if(rs!=null)try{rs.close();}catch(Exceptione){}
if(stm.........
if(cn............
}
8.鐢ㄤ竴鏉QL璇彞鏌ヨ鍑烘瘡闂ㄨ閮藉ぇ浜�80鍒嗙殑瀛︾敓濮撳悕
name 聽 kecheng 聽 fenshu
寮犱笁 聽 聽璇枃 聽 聽 聽81
寮犱笁 聽 聽鏁板 聽 聽 聽75
鏉庡洓 聽 聽璇枃 聽 聽 聽76
鏉庡洓 聽 聽鏁板 聽 聽 聽90
鐜嬩簲 聽 聽璇枃 聽 聽 聽81
鐜嬩簲 聽 聽鏁板 聽 聽 聽100
鐜嬩簲 聽 聽鑻辫 聽 聽 聽90
鍑嗗鏁版嵁鐨剆ql浠g爜锛�
create table score(id int primary key auto_increment,namevarchar(20),subject varchar(20),score int);
insert into score values
(null,'寮犱笁','璇枃',81),
(null,'寮犱笁','鏁板',75),
(null,'鏉庡洓','璇枃',76),
(null,'鏉庡洓','鏁板',90),
(null,'鐜嬩簲','璇枃',81),
(null,'鐜嬩簲','鏁板',100),
(null,'鐜嬩簲 ','鑻辫',90);
鎻愮ず锛氬綋鐧炬�濅笉寰楀叾瑙f椂锛岃鐞嗘兂鎬濈淮锛屾妸灏忓彉鎴愬ぇ鍋氾紝鎶婂ぇ鍙樻垚灏忓仛锛�
绛旀锛�
A: select distinct name from score 聽where 聽name not in (selectdistinct name from score where score<=80)
B:select distince name t1 from score where 80< all (selectscore from score where name=t1);
9.鎵�鏈夐儴闂ㄤ箣闂寸殑姣旇禌缁勫悎
涓�涓彨department鐨勮〃锛岄噷闈㈠彧鏈変竴涓瓧娈祅ame,涓�鍏辨湁4鏉$邯褰曪紝鍒嗗埆鏄痑,b,c,d,瀵瑰簲鍥涗釜鐞冨锛岀幇鍦ㄥ洓涓悆瀵硅繘琛屾瘮璧涳紝鐢ㄤ竴鏉ql璇彞鏄剧ず鎵�鏈夊彲鑳界殑姣旇禌缁勫悎.
绛旓細select a.name,b.name
from team a, team b
where a.name < b.name
10.姣忎釜鏈堜唤鐨勫彂鐢熼閮芥瘮101绉戠洰澶氱殑绉戠洰
璇风敤SQL璇彞瀹炵幇锛氫粠TestDB鏁版嵁琛ㄤ腑鏌ヨ鍑烘墍鏈夋湀浠界殑鍙戠敓棰濋兘姣�101绉戠洰鐩稿簲鏈堜唤鐨勫彂鐢熼楂樼殑绉戠洰銆傝娉ㄦ剰锛歍estDB涓湁寰堝绉戠洰锛岄兘鏈�1锛�12鏈堜唤鐨勫彂鐢熼銆�
AccID锛氱鐩唬鐮侊紝Occmonth锛氬彂鐢熼鏈堜唤锛孌ebitOccur锛氬彂鐢熼銆�
鏁版嵁搴撳悕锛欽cyAudit锛屾暟鎹泦锛歋elect * from TestDB
鍑嗗鏁版嵁鐨剆ql浠g爜锛�
drop table if exists TestDB;
create table TestDB(id int primary key auto_increment,AccIDvarchar(20), Occmonth date, DebitOccur bigint);
insert into TestDB values
(null,'101','1988-1-1',100),
(null,'101','1988-2-1',110),
(null,'101','1988-3-1',120),
(null,'101','1988-4-1',100),
(null,'101','1988-5-1',100),
(null,'101','1988-6-1',100),
(null,'101','1988-7-1',100),
(null,'101','1988-8-1',100);
--澶嶅埗涓婇潰鐨勬暟鎹紝鏁呮剰鎶婄涓�涓湀浠界殑鍙戠敓棰濇暟瀛楁敼灏忎竴鐐�
insert into TestDB values
(null,'102','1988-1-1',90),
(null,'102','1988-2-1',110),
(null,'102','1988-3-1',120),
(null,'102','1988-4-1',100),
(null,'102','1988-5-1',100),
(null,'102','1988-6-1',100),
(null,'102','1988-7-1',100),
(null,'102','1988-8-1',100);
--澶嶅埗鏈�涓婇潰鐨勬暟鎹紝鏁呮剰鎶婃墍鏈夊彂鐢熼鏁板瓧鏀瑰ぇ涓�鐐�
insert into TestDB values
(null,'103','1988-1-1',150),
(null,'103','1988-2-1',160),
(null,'103','1988-3-1',180),
(null,'103','1988-4-1',120),
(null,'103','1988-5-1',120),
(null,'103','1988-6-1',120),
(null,'103','1988-7-1',120),
(null,'103','1988-8-1',120);
--澶嶅埗鏈�涓婇潰鐨勬暟鎹紝鏁呮剰鎶婃墍鏈夊彂鐢熼鏁板瓧鏀瑰ぇ涓�鐐�
insert into TestDB values
(null,'104','1988-1-1',130),
(null,'104','1988-2-1',130),
(null,'104','1988-3-1',140),
(null,'104','1988-4-1',150),
(null,'104','1988-5-1',160),
(null,'104','1988-6-1',170),
(null,'104','1988-7-1',180),
(null,'104','1988-8-1',140);
--澶嶅埗鏈�涓婇潰鐨勬暟鎹紝鏁呮剰鎶婄浜屼釜鏈堜唤鐨勫彂鐢熼鏁板瓧鏀瑰皬涓�鐐�
insert into TestDB values
(null,'105','1988-1-1',100),
(null,'105','1988-2-1',80),
(null,'105','1988-3-1',120),
(null,'105','1988-4-1',100),
(null,'105','1988-5-1',100),
(null,'105','1988-6-1',100),
(null,'105','1988-7-1',100),
(null,'105','1988-8-1',100);
绛旀锛�
select distinct AccID from TestDB
where AccID not in
(selectTestDB.AccIDfrom TestDB,
(select * from TestDB where AccID='101') asdb101
whereTestDB.Occmonth=db101.Occmonth and TestDB.DebitOccur<=db101.DebitOccur
);
11.缁熻姣忓勾姣忔湀鐨勪俊鎭�
year 聽monthamount
1991 聽 1 聽 聽 1.1
1991 聽 2 聽 聽 1.2
1991 聽 3 聽 聽 1.3
1991 聽 4 聽 聽 1.4
1992 聽 1 聽 聽 2.1
1992 聽 2 聽 聽 2.2
1992 聽 3 聽 聽 2.3
1992 聽 4 聽 聽 2.4
鏌ユ垚杩欐牱涓�涓粨鏋�
year m1 聽m2 聽m3 聽m4
1991 1.1 1.2 1.3 1.4
1992 2.1 2.2 2.3 2.4
鎻愮ず锛氳繖涓笌宸ヨ祫鏉¢潪甯哥被浼硷紝涓庡鐢熺殑绉戠洰鎴愮哗涔熷緢鐩镐技銆�
鍑嗗sql璇彞锛�
drop table if existssales;
create table sales(idint auto_increment primary key,year varchar(10), month varchar(10), amountfloat(2,1));
insert into salesvalues
(null,'1991','1',1.1),
(null,'1991','2',1.2),
(null,'1991','3',1.3),
(null,'1991','4',1.4),
(null,'1992','1',2.1),
(null,'1992','2',2.2),
(null,'1992','3',2.3),
(null,'1992','4',2.4);
绛旀涓�銆�
select sales.year ,
(select t.amount fromsales t where t.month='1' and t.year= sales.year) '1',
(select t.amount fromsales t where t.month='1' and t.year= sales.year) '2',
(select t.amount fromsales t where t.month='1' and t.year= sales.year) '3',
(select t.amount fromsales t where t.month='1' and t.year= sales.year) as '4'
from salesgroup by year;
12.鏄剧ず鏂囩珷鏍囬锛屽彂甯栦汉銆佹渶鍚庡洖澶嶆椂闂�
琛細id,title,postuser,postdate,parentid
鍑嗗sql璇彞锛�
drop table if exists articles;
create table articles(id int auto_increment primary key,titlevarchar(50), postuser varchar(10), postdate datetime,parentid int referencesarticles(id));
insert into articles values
(null,'绗竴鏉�','寮犱笁','1998-10-10 12:32:32',null),
(null,'绗簩鏉�','寮犱笁','1998-10-10 12:34:32',null),
(null,'绗竴鏉″洖澶�1','鏉庡洓','1998-10-10 12:35:32',1),
(null,'绗簩鏉″洖澶�1','鏉庡洓','1998-10-10 12:36:32',2),
(null,'绗竴鏉″洖澶�2','鐜嬩簲','1998-10-10 12:37:32',1),
(null,'绗竴鏉″洖澶�3','鏉庡洓','1998-10-10 12:38:32',1),
(null,'绗簩鏉″洖澶�2','鏉庡洓','1998-10-10 12:39:32',2),
(null,'绗竴鏉″洖澶�4','鐜嬩簲','1998-10-10 12:39:40',1);
绛旀锛�
select a.title,a.postuser,
(selectmax(postdate) from articles where parentid=a.id) reply
from articles a where a.parentid is null;
娉ㄩ噴锛氬瓙鏌ヨ鍙互鐢ㄥ湪閫夋嫨鍒椾腑锛屼篃鍙敤浜巜here鐨勬瘮杈冩潯浠朵腑锛岃繕鍙互鐢ㄤ簬from浠庡彞涓��
13.鍒犻櫎闄や簡id鍙蜂笉鍚�,鍏朵粬閮界浉鍚岀殑瀛︾敓鍐椾綑淇℃伅
2.瀛︾敓琛ㄥ涓�:
id鍙� 聽 瀛﹀彿 聽 濮撳悕璇剧▼缂栧彿璇剧▼鍚嶇О鍒嗘暟
1 聽 聽 聽 聽2005001 聽寮犱笁 0001 聽 聽 鏁板 聽 聽69
2 聽 聽 聽 聽2005002 聽鏉庡洓 0001 聽 聽 鏁板 聽 聽89
3 聽 聽 聽 聽2005001 聽寮犱笁 0001 聽 聽 鏁板 聽 聽69
A: delete from tablename where id鍙� not in(select min(id鍙�) from tablename group by瀛﹀彿,濮撳悕,璇剧▼缂栧彿,璇剧▼鍚嶇О,鍒嗘暟)
瀹為獙锛�
create table student2(id int auto_increment primary key,codevarchar(20),name varchar(20));
insert into student2 values(null,'2005001','寮犱笁'),(null,'2005002','鏉庡洓'),(null,'2005001','寮犱笁');
//濡備笅璇彞锛宮ysql鎶ュ憡閿欒锛屽彲鑳藉垹闄や緷璧栧悗闈㈢粺璁¤鍙ワ紝鑰屽垹闄ゅ張瀵艰嚧缁熻璇彞缁撴灉涓嶄竴鑷淬��
delete from student2 where id not in(select min(id) fromstudent2 group by name);
//浣嗘槸锛屽涓嬭鍙ユ病鏈夐棶棰橈細
select *from student2where id not in(select min(id) from student2 group by name);
//浜庢槸锛屾垜鎯冲厛鎶婂垎缁勭殑缁撴灉鍋氭垚铏氳〃锛岀劧鍚庝粠铏氳〃涓�夊嚭缁撴灉锛屾渶鍚庡啀灏嗙粨鏋滀綔涓哄垹闄ょ殑鏉′欢鏁版嵁銆�
delete from student2 where id not in(select mid from (selectmin(id) mid
from student2 group by name) as t);
鎴栬�咃細
delete from student2 where id not in(select min(id) from (select* from s
tudent2) as t group by t.name);
14.鑸┖缃戠殑鍑犱釜鑸彮鏌ヨ棰橈細
琛ㄧ粨鏋勫涓嬶細
flight{flightID,StartCityID ,endCityID,StartTime}
city{cityID, CityName)
瀹為獙鐜锛�
create table city(cityID int auto_increment primary key,cityNamevarchar(20));
create table flight (flightID int auto_increment primary key,
StartCityID intreferences city(cityID),
endCityID 聽int references city(cityID),
StartTimetimestamp);
//鑸彮鏈潵搴旇娌℃湁鏃ユ湡閮ㄥ垎鎵嶅ソ锛屼絾鏄笅闈㈢殑棰樼洰褰撲腑娑夊強鍒颁簡鏃ユ湡
insert into city values(null,'鍖椾含'),(null,'涓婃捣'),(null,'骞垮窞');
insert into flight values
(null,1,2,'9:37:23'),(null,1,3,'9:37:23'),(null,1,2,'10:37:23'),(null,2,3,'10:37:23');
1銆佹煡璇㈣捣椋炲煄甯傛槸鍖椾含鐨勬墍鏈夎埅鐝紝鎸夊埌杈惧煄甯傜殑鍚嶅瓧鎺掑簭
鍙備笌杩愮畻鐨勫垪鏄垜璧风爜鑳藉鏄剧ず鍑烘潵鐨勯偅浜涘垪锛屼絾鏈�缁堟垜涓嶄竴瀹氭妸瀹冧滑鏄剧ず鍑烘潵銆傚悇涓〃缁勫悎鍑烘潵鐨勪腑闂寸粨鏋滃瓧娈典腑蹇呴』鍖呭惈鎵�鏈夎繍绠楃殑瀛楁銆�
select 聽* from flight f,city c
where f.endcityid =c.cityid and startcityid =
(select c1.cityidfrom city c1 where c1.cityname = "鍖椾含")
order by c.citynameasc;
mysql> select flight.flightid,'鍖椾含' startcity, e.cityname from flight,city e wh
ere flight.endcityid=e.cityid and flight.startcityid=(selectcityid from city wh
ere cityname='鍖椾含');
mysql> select flight.flightid,s.cityname,e.cityname fromflight,city s,city e wh
ere flight.startcityid=s.cityid and s.cityname='鍖椾含' andflight.endCityId=e.cit
yID order by e.cityName desc;
2銆佹煡璇㈠寳浜埌涓婃捣鐨勬墍鏈夎埅鐝邯褰曪紙璧烽鍩庡競锛屽埌杈惧煄甯傦紝璧烽鏃堕棿锛岃埅鐝彿锛�
select c1.CityName,c2.CityName,f.StartTime,f.flightID
from city c1,city c2,flight f
where f.StartCityID=c1.cityID
and f.endCityID=c2.cityID
and c1.cityName='鍖椾含'
and c2.cityName='涓婃捣'
3銆佹煡璇㈠叿浣撴煇涓�澶╋紙2005-5-8锛夌殑鍖椾含鍒颁笂娴风殑鐨勮埅鐝鏁�
select count(*) from
(select c1.CityName,c2.CityName,f.StartTime,f.flightID
from city c1,city c2,flight f
where f.StartCityID=c1.cityID
and f.endCityID=c2.cityID
and c1.cityName='鍖椾含'
and c2.cityName='涓婃捣'
and 鏌ュ府鍔╄幏寰楃殑鏌愪釜鏃ユ湡澶勭悊鍑芥暟(startTime) like '2005-5-8%'
mysql涓彁鍙栨棩鏈熼儴鍒嗚繘琛屾瘮杈冪殑绀轰緥浠g爜濡備笅锛�
select * from flight wheredate_format(starttime,'%Y-%m-%d')='1998-01-02'
15.鏌ュ嚭姣旂粡鐞嗚柂姘磋繕楂樼殑鍛樺伐淇℃伅锛�
Drop table if not exists employees;
create table employees(id int primary key auto_increment,namevarchar(50)
,salary int,managerid intreferences employees(id));
insert into employees values (null,' lhm',10000,null), (null,'zxx',15000,1
),(null,'flx',9000,1),(null,'tg',10000,2),(null,'wzg',10000,3);
Wzg澶т簬flx,lhm澶т簬zxx
瑙i鎬濊矾锛�
鏍规嵁sql璇彞鐨勬煡璇㈢壒鐐癸紝鏄�愯杩涜杩愮畻锛屼笉鍙兘涓よ鍚屾椂鍙備笌杩愮畻銆�
娑夊強浜嗗憳宸ヨ柂姘村拰缁忕悊钖按锛屾墍鏈夛紝涓�琛岃褰曡鍚屾椂鍖呭惈涓や釜钖按锛屾墍鏈夋兂鍒拌鎶婅繖涓〃鑷叧鑱旂粍鍚堜竴涓嬨��
棣栧厛瑕佺粍鍚堝嚭涓�涓寘鍚湁鍚勪釜鍛樺伐鍙婅鍛樺伐鐨勭粡鐞嗕俊鎭殑闀胯褰曪紝璀锛屽乏鍗婇儴鍒嗘槸鍛樺伐锛屽彸鍗婇儴鍒嗘槸缁忕悊銆傝�岃开鍗″皵绉細缁勫悎鍑哄緢澶氬瀮鍦句俊鎭紝鍏堝幓闄よ繖浜涘瀮鍦句俊鎭��
select e.* from employees e,employees m where e.managerid=m.idand e.sala
ry>m.salary;
16銆佹眰鍑哄皬浜�45宀佺殑鍚勪釜鑰佸笀鎵�甯︾殑澶т簬12宀佺殑瀛︾敓浜烘暟
鏁版嵁搴撲腑鏈�3涓〃 teacher琛紝student琛紝tea_stu鍏崇郴琛ㄣ��
teacher 琛� teaID name age
student 琛� stuID name age
teacher_student琛� teaID stuID
瑕佹眰鐢ㄤ竴鏉ql鏌ヨ鍑鸿繖鏍风殑缁撴灉
1.鏄剧ず鐨勫瓧娈佃鏈夎�佸笀name, age姣忎釜鑰佸笀鎵�甯︾殑瀛︾敓浜烘暟
2 鍙垪鍑鸿�佸笀age涓�40浠ヤ笅锛屽鐢焌ge涓�12浠ヤ笂鐨勮褰�
棰勫鐭ヨ瘑锛�
1.sql璇彞鏄姣忎竴鏉¤褰曚緷娆″鐞嗭紝鏉′欢涓虹湡鍒欐墽琛屽姩浣滐紙select,insert,delete,update锛�
2.鍙鏄开鍗″皵绉紝灏变細浜х敓鈥滃瀮鍦锯�濅俊鎭紝鎵�浠ワ紝鍙杩崱灏旂Н浜嗭紝鎴戜滑棣栧厛灏辫鎯冲埌娓呴櫎鈥滃瀮鍦锯�濅俊鎭�
瀹為獙鍑嗗锛�
drop table if exists tea_stu;
drop table if exists teacher;
drop table if exists student;
create table teacher(teaID int primarykey,name varchar(50),age int);
create table student(stuID int primarykey,name varchar(50),age int);
create table tea_stu(teaID int referencesteacher(teaID),stuID int references student(stuID));
insertinto teacher values(1,'zxx',45), (2,'lhm',25) , (3,'wzg',26) , (4,'tg',27);
insertinto student values(1,'wy',11), (2,'dh',25) , (3,'ysq',26) , (4,'mxc',27);
insertinto tea_stu values(1,1), (1,2), (1,3);
insertinto tea_stu values(2,2), (2,3), (2,4);
insert into tea_stu values(3,3), (3,4), (3,1);
insertinto tea_stu values(4,4), (4,1), (4,2) , (4,3);
缁撴灉锛�2脿3,3脿2,4脿3
瑙i鎬濊矾锛氾紙鐪熷疄闈㈣瘯绛旈鏃讹紝涔熻鍐欏嚭姣忎釜鍒嗘瀽姝ラ锛屽鏋滅焊寮犱笉澶燂紝灏辨壘鍒汉瑕侊級
1瑕佷細缁熻鍒嗙粍淇℃伅锛岀粺璁′俊鎭斁鍦ㄤ腑闂磋〃涓細
selectteaid,count(*) from tea_stu group by teaid;
2鎺ョ潃鍏跺疄搴旇鏄瓫闄ゆ帀灏忎簬12宀佺殑瀛︾敓锛岀劧鍚庡啀杩涜缁熻锛屼腑闂磋〃蹇呴』涓巗tudent鍏宠仈鎵嶈兘寰楀埌12宀佷互涓嬪鐢熷拰鎶婅瀛︾敓璁板綍浠庝腑闂磋〃涓墧闄わ紝浠g爜鏄細
selecttea_stu.teaid,count(*) total from student,tea_stu
wherestudent.stuid=tea_stu.stuid and student.age>12 group by tea_stu.teaid
3.鎺ョ潃鎶婁笂闈㈢殑缁撴灉鍋氭垚铏氳〃涓巘eacher杩涜鍏宠仈锛屽苟绛涢櫎澶т簬45鐨勮�佸笀
selectteacher.teaid,teacher.name,total from teacher ,(select tea_stu.tea
id,count(*)total from student,tea_stu where student.stuid=tea_stu.stuid and stu
dent.age>12group by tea_stu.teaid) as tea_stu2where teacher.teaid=tea_stu2.tea
idand teacher.age<45;
17.姹傚嚭鍙戝笘鏈�澶氱殑浜猴細
select authorid,count(*) total from articles
group by authorid
having total=
(select max(total2) from(select count(*) total2 from articles group by authorid) as t);
select t.authorid,max(t.total) from
锛坰elect authorid,count(*) total from articles锛塧s t
杩欐潯璇彞涓嶈锛屽洜涓簃ax鍙湁涓�鍒楋紝涓嶈兘涓庡叾浠栧垪娣锋穯銆�
select authorid,count(*) total from articles
group by authorid having total=max(total)涔熶笉琛屻��
18銆佷竴涓敤鎴疯〃涓湁涓�涓Н鍒嗗瓧娈碉紝鍋囧鏁版嵁搴撲腑鏈�100澶氫竾涓敤鎴凤紝鑻ヨ鍦ㄦ瘡骞寸涓�澶╁噷鏅ㄥ皢绉垎娓呴浂锛屼綘灏嗚�冭檻浠�涔堬紝浣犲皢鎯充粈涔堝姙娉曡В鍐�?
alter table drop column score;
alter table add colunm score int;
鍙兘浼氬緢蹇紝浣嗘槸闇�瑕佽瘯楠岋紝璇曢獙涓嶈兘鎷跨湡瀹炵殑鐜鏉ユ搷鍒�锛屽苟涓旇娉ㄦ剰锛�
杩欐牱鐨勬搷浣滄椂鏃犳硶鍥炴粴鐨勶紝鍦ㄦ垜鐨勫嵃璞′腑锛屽彧鏈塱nert update delete绛塂ML璇彞鎵嶈兘鍥炴粴锛�
瀵逛簬create table,drop table ,alter table绛塂DL璇彞鏄笉鑳藉洖婊氥��
瑙e喅鏂规涓�锛寀pdate user set score=0;
瑙e喅鏂规浜岋紝鍋囪涓婇潰鐨勪唬鐮佽鎵ц濂介暱鏃堕棿锛岃秴鍑烘垜浠殑瀹瑰繊鑼冨洿锛岄偅鎴戝氨alter table user drop columnscore;alter table user add column score int銆�
涓嬮潰浠g爜瀹炵幇姣忓勾鐨勯偅涓噷鏅ㄦ椂鍒昏繘琛屾竻闆躲��
Runnable runnable =
new Runnable(){
public voidrun(){
clearDb();
schedule(this,newDate(new Date().getYear()+1,0,0));
}
};
schedule(runnable,
new Date(newDate().getYear()+1,0,1));
19銆佷竴涓敤鎴峰叿鏈夊涓鑹诧紝璇锋煡璇㈠嚭璇ヨ〃涓叿鏈夎鐢ㄦ埛鐨勬墍鏈夎鑹茬殑鍏朵粬鐢ㄦ埛銆�
select count(*) as num,tb.id
from
tb,
(select role from tbwhere id=xxx) as t1
where
tb.role = t1.role andtb.id != t1.id
group by tb.id
having
num = select count(role)from tb where id=xxx;
20. xxx鍏徃鐨剆ql闈㈣瘯
TableEMPLOYEESStructure:
EMPLOYEE_IDNUMBER 聽 聽 聽 聽Primary Key,
FIRST_NAMEVARCHAR2(25),
LAST_NAMEVARCHAR2(25),
Salary number(8,2),
HiredDate DATE,
Departmentid number(2)
TableDepartmentsStructure:
Departmentid number(2)Primary Key,
DepartmentNameVARCHAR2(25).
(2锛夊熀浜庝笂杩癊MPLOYEES琛ㄥ啓鍑烘煡璇細鍐欏嚭闆囩敤鏃ユ湡鍦ㄤ粖骞寸殑锛屾垨鑰呭伐璧勫湪[1000,2000]涔嬮棿鐨勶紝鎴栬�呭憳宸ュ鍚嶏紙last_name锛変互鈥橭bama鈥欐墦澶寸殑鎵�鏈夊憳宸ワ紝鍒楀嚭杩欎簺鍛樺伐鐨勫叏閮ㄤ釜浜轰俊鎭�傦紙4鍒嗭級
select* from employees
whereYear(hiredDate) = Year(date())
or (salary between 1000 and 200)
or left(last_name,3)='abc';
(3) 鍩轰簬涓婅堪EMPLOYEES琛ㄥ啓鍑烘煡璇細鏌ュ嚭閮ㄩ棬骞冲潎宸ヨ祫澶т簬1800鍏冪殑閮ㄩ棬鐨勬墍鏈夊憳宸ワ紝鍒楀嚭杩欎簺鍛樺伐鐨勫叏閮ㄤ釜浜轰俊鎭�傦紙4鍒嗭級
mysql>select id,name,salary,deptid did from employee1 where (select avg(salary)
from employee1 where deptid = did) > 1800;
(4) 鍩轰簬涓婅堪EMPLOYEES琛ㄥ啓鍑烘煡璇細鏌ュ嚭涓汉宸ヨ祫楂樹簬鍏舵墍鍦ㄩ儴闂ㄥ钩鍧囧伐璧勭殑鍛樺伐锛屽垪鍑鸿繖浜涘憳宸ョ殑鍏ㄩ儴涓汉淇℃伅鍙婅鍛樺伐宸ヨ祫楂樺嚭閮ㄩ棬骞冲潎宸ヨ祫鐧惧垎姣斻�傦紙5鍒嗭級
selectemployee1.*,(employee1.salary-t.avgSalary)*100/employee1.salary
fromemployee1,
(select deptid,avg(salary) avgSalary fromemployee1 group by deptid) as t
whereemployee1.deptid = t.deptid and employee1.salary>t.avgSalary;
21銆佹敞鍐孞dbc椹卞姩绋嬪簭鐨勪笁绉嶆柟寮�
22銆佺敤JDBC濡備綍璋冪敤瀛樺偍杩囩▼
浠g爜濡備笅锛�
packagecom.huawei.interview.lym;
importjava.sql.CallableStatement;
importjava.sql.Connection;
importjava.sql.DriverManager;
importjava.sql.SQLException;
importjava.sql.Types;
publicclassJdbcTest {
/**
*@paramargs
*/
public static voidmain(String[] args) {
//TODOAuto-generated method stub
Connection cn =null;
CallableStatement cstmt =null;
try{
//杩欓噷鏈�濂戒笉瑕佽繖涔堝共锛屽洜涓洪┍鍔ㄥ悕鍐欐鍦ㄧ▼搴忎腑浜�
Class.forName("com.mysql.jdbc.Driver");
//瀹為檯椤圭洰涓紝杩欓噷搴旂敤DataSource鏁版嵁锛屽鏋滅敤妗嗘灦锛�
//杩欎釜鏁版嵁婧愪笉闇�瑕佹垜浠紪鐮佸垱寤猴紝鎴戜滑鍙渶Datasource ds =context.lookup()
//cn = ds.getConnection();
cn = DriverManager.getConnection("jdbc:mysql:///test","root","root");
cstmt = cn.prepareCall("{callinsert_Student(?,?,?)}");
cstmt.registerOutParameter(3,Types.INTEGER);
cstmt.setString(1,"wangwu");
cstmt.setInt(2, 25);
cstmt.execute();
//get绗嚑涓紝涓嶅悓鐨勬暟鎹簱涓嶄竴鏍凤紝寤鸿涓嶅啓
System.out.println(cstmt.getString(3));
}catch(Exception e) {
//TODOAuto-generated catchblock
e.printStackTrace();
}
finally
{
/*try{cstmt.close();}catch(Exceptione){}
try{cn.close();}catch(Exceptione){}*/
try{
if(cstmt !=null)
cstmt.close();
if(cn !=null)
cn.close();
}catch(SQLException e) {
//TODOAuto-generatedcatch block
e.printStackTrace();
}
}
}
23銆丣DBC涓殑PreparedStatement鐩告瘮Statement鐨勫ソ澶�
绛旓細涓�涓猻ql鍛戒护鍙戠粰鏈嶅姟鍣ㄥ幓鎵ц鐨勬楠や负锛氳娉曟鏌ワ紝璇箟鍒嗘瀽锛岀紪璇戞垚鍐呴儴鎸囦护锛岀紦瀛樻寚浠わ紝鎵ц鎸囦护绛夎繃绋嬨��
select * from student where id =3----缂撳瓨--脿xxxxx浜岃繘鍒跺懡浠�
select * from student where id =3----鐩存帴鍙�-脿xxxxx浜岃繘鍒跺懡浠�
select * from student where id =4--- -脿浼氭�庝箞骞诧紵
濡傛灉褰撳垵鏄痵elect * from student where id =?--- -脿鍙堜細鎬庝箞骞诧紵
涓婇潰璇寸殑鏄�ц兘鎻愰珮
鍙互闃叉sql娉ㄥ叆銆�
24. 鍐欎竴涓敤jdbc杩炴帴骞惰闂畂racle鏁版嵁鐨勭▼搴忎唬鐮�
25銆丆lass.forName鐨勪綔鐢�?涓轰粈涔堣鐢�?
绛旓細鎸夊弬鏁颁腑鎸囧畾鐨勫瓧绗︿覆褰㈠紡鐨勭被鍚嶅幓鎼滅储骞跺姞杞界浉搴旂殑绫伙紝濡傛灉璇ョ被瀛楄妭鐮佸凡缁忚鍔犺浇杩囷紝鍒欒繑鍥炰唬琛ㄨ瀛楄妭鐮佺殑Class瀹炰緥瀵硅薄锛屽惁鍒欙紝鎸夌被鍔犺浇鍣ㄧ殑濮旀墭鏈哄埗鍘绘悳绱㈠拰鍔犺浇璇ョ被锛屽鏋滄墍鏈夌殑绫诲姞杞藉櫒閮芥棤娉曞姞杞藉埌璇ョ被锛屽垯鎶涘嚭ClassNotFoundException銆傚姞杞藉畬杩欎釜Class瀛楄妭鐮佸悗锛屾帴鐫�灏卞彲浠ヤ娇鐢–lass瀛楄妭鐮佺殑newInstance鏂规硶鍘诲垱寤鸿绫荤殑瀹炰緥瀵硅薄浜嗐��
鏈夋椂鍊欙紝鎴戜滑绋嬪簭涓墍鏈変娇鐢ㄧ殑鍏蜂綋绫诲悕鍦ㄨ璁℃椂锛堝嵆寮�鍙戞椂锛夋棤娉曠‘瀹氾紝鍙湁绋嬪簭杩愯鏃舵墠鑳界‘瀹氾紝杩欐椂鍊欏氨闇�瑕佷娇鐢–lass.forName鍘诲姩鎬佸姞杞借绫伙紝杩欎釜绫诲悕閫氬父鏄湪閰嶇疆鏂囦欢涓厤缃殑锛屼緥濡傦紝spring鐨刬oc涓瘡娆′緷璧栨敞鍏ョ殑鍏蜂綋绫诲氨鏄繖鏍烽厤缃殑锛宩dbc鐨勯┍鍔ㄧ被鍚嶉�氬父涔熸槸閫氳繃閰嶇疆鏂囦欢鏉ラ厤缃殑锛屼互渚垮湪浜у搧浜や粯浣跨敤鍚庝笉鐢ㄤ慨鏀规簮绋嬪簭灏卞彲浠ユ洿鎹㈤┍鍔ㄧ被鍚嶃��
26銆佸ぇ鏁版嵁閲忎笅鐨勫垎椤佃В鍐虫柟娉曘��
绛旓細鏈�濂界殑鍔炴硶鏄埄鐢╯ql璇彞杩涜鍒嗛〉锛岃繖鏍锋瘡娆℃煡璇㈠嚭鐨勭粨鏋滈泦涓氨鍙寘鍚煇椤电殑鏁版嵁鍐呭銆傚啀sql璇彞鏃犳硶瀹炵幇鍒嗛〉鐨勬儏鍐典笅锛屽彲浠ヨ�冭檻瀵瑰ぇ鐨勭粨鏋滈泦閫氳繃娓告爣瀹氫綅鏂瑰紡鏉ヨ幏鍙栨煇椤电殑鏁版嵁銆�
sql璇彞鍒嗛〉锛屼笉鍚岀殑鏁版嵁搴撲笅鐨勫垎椤垫柟妗堝悇涓嶄竴鏍凤紝涓嬮潰鏄富娴佺殑涓夌鏁版嵁搴撶殑鍒嗛〉sql锛�
sql server:
String sql =
"select top" + pageSize + " * from students where id not in" +
"(select top "+ pageSize * (pageNumber-1) + " id from students order by id)" +
"order by id";
mysql:
String sql =
"select * fromstudents order by id limit " + pageSize*(pageNumber-1) + "," +pageSize;
oracle:
String sql =
"select * from " +
(select *,rownum rid from (select * fromstudents order by postime desc) where rid<=" + pagesize*pagenumber +") as t" +
"where t>" +pageSize*(pageNumber-1);
27銆佺敤 JDBC鏌ヨ瀛︾敓鎴愮哗鍗�,鎶婁富瑕佷唬鐮佸啓鍑烘潵锛堣�冭瘯姒傜巼鏋佸ぇ锛�.
Connection cn = null;
PreparedStatement pstmt =null;
Resultset rs = null;
try
{
Class.forname(driveClassName);
cn = 聽DriverManager.getConnection(url,username,password);
pstmt =cn.prepareStatement(鈥渟elect 聽score.* fromscore ,student 鈥� +
鈥渨herescore.stuId = student.id and student.name = ?鈥�);
pstmt.setString(1,studentName);
Resultset rs =pstmt.executeQuery();
while(rs.next())
{
system.out.println(rs.getInt(鈥渟ubject鈥�) 聽+ 聽鈥� 聽 聽鈥� + rs.getFloat(鈥渟core鈥�) );
}
}catch(Exception e){e.printStackTrace();}
finally
{
if(rs != null) try{rs.close() }catch(exception e){}
if(pstmt != null)try{pstmt.close()}catch(exception e){}
if(cn != null) try{cn.close() }catch(exception e){}
}
28銆佽繖娈典唬鐮佹湁浠�涔堜笉瓒充箣澶�?
try {
Connection conn = ...;
Statement stmt = ...;
ResultSet rs =stmt.executeQuery("select * from table1");
while(rs.next()) {
}
} catch(Exception ex) {
}
绛旓細娌℃湁finally璇彞鏉ュ叧闂悇涓璞★紝鍙﹀锛屼娇鐢╢inally涔嬪悗锛岃鎶婂彉閲忕殑瀹氫箟鏀惧湪try璇彞鍧楃殑澶栭潰锛屼互渚垮湪try璇彞鍧椾箣澶栫殑finally鍧椾腑浠嶅彲浠ヨ闂繖浜涘彉閲忋��
29銆佽鍑烘暟鎹繛鎺ユ睜鐨勫伐浣滄満鍒舵槸浠�涔�?
J2EE鏈嶅姟鍣ㄥ惎鍔ㄦ椂浼氬缓绔嬩竴瀹氭暟閲忕殑姹犺繛鎺ワ紝骞朵竴鐩寸淮鎸佷笉灏戜簬姝ゆ暟鐩殑姹犺繛鎺ャ�傚鎴风绋嬪簭闇�瑕佽繛鎺ユ椂锛屾睜椹卞姩绋嬪簭浼氳繑鍥炰竴涓湭浣跨敤鐨勬睜杩炴帴骞跺皢鍏惰〃璁颁负蹇欍�傚鏋滃綋鍓嶆病鏈夌┖闂茶繛鎺ワ紝姹犻┍鍔ㄧ▼搴忓氨鏂板缓涓�瀹氭暟閲忕殑杩炴帴锛屾柊寤鸿繛鎺ョ殑鏁伴噺鏈夐厤缃弬鏁板喅瀹氥�傚綋浣跨敤鐨勬睜杩炴帴璋冪敤瀹屾垚鍚庯紝姹犻┍鍔ㄧ▼搴忓皢姝よ繛鎺ヨ〃璁颁负绌洪棽锛屽叾浠栬皟鐢ㄥ氨鍙互浣跨敤杩欎釜杩炴帴銆�
瀹炵幇鏂瑰紡锛岃繑鍥炵殑Connection鏄師濮婥onnection鐨勪唬鐞嗭紝浠g悊Connection鐨刢lose鏂规硶涓嶆槸鐪熸鍏宠繛鎺ワ紝鑰屾槸鎶婂畠浠g悊鐨凜onnection瀵硅薄杩樺洖鍒拌繛鎺ユ睜涓��
30銆佷负浠�涔堣鐢� ORM? 鍜� JDBC鏈変綍涓嶄竴鏍�?
orm鏄竴绉嶆�濇兂锛屽氨鏄妸object杞彉鎴愭暟鎹簱涓殑璁板綍锛屾垨鑰呮妸鏁版嵁搴撲腑鐨勮褰曡浆鍙樻垚objecdt锛屾垜浠彲浠ョ敤jdbc鏉ュ疄鐜拌繖绉嶆�濇兂锛屽叾瀹烇紝濡傛灉鎴戜滑鐨勯」鐩槸涓ユ牸鎸夌収oop鏂瑰紡缂栧啓鐨勮瘽锛屾垜浠殑jdbc绋嬪簭涓嶇鏄湁鎰忚繕鏄棤鎰忥紝灏卞凡缁忓湪瀹炵幇orm鐨勫伐浣滀簡銆�
鐜板湪鏈夎澶歰rm宸ュ叿锛屽畠浠簳灞傝皟鐢╦dbc鏉ュ疄鐜颁簡orm宸ヤ綔锛屾垜浠洿鎺ヤ娇鐢ㄨ繖浜涘伐鍏凤紝灏辩渷鍘讳簡鐩存帴浣跨敤jdbc鐨勭箒鐞愮粏鑺傦紝鎻愰珮浜嗗紑鍙戞晥鐜囷紝鐜板湪鐢ㄧ殑杈冨鐨刼rm宸ュ叿鏄痟ibernate銆備篃鍚涓�浜涘叾浠杘rm宸ュ叿锛屽toplink,ojb绛夈��
鍏�. XML閮ㄥ垎
1銆亁ml鏈夊摢浜涜В鏋愭妧鏈�?鍖哄埆鏄粈涔�?
绛�:鏈塂OM,SAX,STAX绛�
DOM:澶勭悊澶у瀷鏂囦欢鏃跺叾鎬ц兘涓嬮檷鐨勯潪甯稿帀瀹炽�傝繖涓棶棰樻槸鐢盌OM鐨勬爲缁撴瀯鎵�閫犳垚鐨勶紝杩欑缁撴瀯鍗犵敤鐨勫唴瀛樿緝澶氾紝鑰屼笖DOM蹇呴』鍦ㄨВ鏋愭枃浠朵箣鍓嶆妸鏁翠釜鏂囨。瑁呭叆鍐呭瓨,閫傚悎瀵筙ML鐨勯殢鏈鸿闂甋AX:涓嶇幇浜嶥OM,SAX鏄簨浠堕┍鍔ㄥ瀷鐨刋ML瑙f瀽鏂瑰紡銆傚畠椤哄簭璇诲彇XML鏂囦欢锛屼笉闇�瑕佷竴娆″叏閮ㄨ杞芥暣涓枃浠躲�傚綋閬囧埌鍍忔枃浠跺紑澶达紝鏂囨。缁撴潫锛屾垨鑰呮爣绛惧紑澶翠笌鏍囩缁撴潫鏃讹紝瀹冧細瑙﹀彂涓�涓簨浠讹紝鐢ㄦ埛閫氳繃鍦ㄥ叾鍥炶皟浜嬩欢涓啓鍏ュ鐞嗕唬鐮佹潵澶勭悊XML鏂囦欢锛岄�傚悎瀵筙ML鐨勯『搴忚闂�
STAX:Streaming API for XML (StAX)
璁茶В杩欎簺鍖哄埆鏄笉闇�瑕佺壒鍒幓姣旇緝锛屽氨鍍忚浼犳櫤鎾涓庡叾浠栧煿璁満鏋勭殑鍖哄埆鏃讹紝鎴戜滑鍙渶璇存竻妤氫紶鏅烘挱瀹㈡湁浠�涔堢壒鐐瑰拰浼樼偣灏辫浜嗭紝杩欏氨宸茬粡闂存帴鍥炵瓟浜嗗郊姝ょ殑鍖哄埆銆�
2銆佷綘鍦ㄩ」鐩腑鐢ㄥ埌浜唜ml鎶�鏈殑鍝簺鏂归潰?濡備綍瀹炵幇鐨�?
绛�:鐢ㄥ埌浜嗘暟鎹瓨璐紝淇℃伅閰嶇疆涓ゆ柟闈€�傚湪鍋氭暟鎹氦鎹㈠钩鍙版椂锛屽皢涓嶈兘鏁版嵁婧愮殑鏁版嵁缁勮鎴怷ML鏂囦欢锛岀劧鍚庡皢XML鏂囦欢鍘嬬缉鎵撳寘鍔犲瘑鍚庨�氳繃缃戠粶浼犻�佺粰鎺ユ敹鑰咃紝鎺ユ敹瑙e瘑涓庤В鍘嬬缉鍚庡啀鍚孹ML鏂囦欢涓繕鍘熺浉鍏充俊鎭繘琛屽鐞嗐�傚湪鍋氳蒋浠堕厤缃椂锛屽埄鐢╔ML鍙互寰堟柟渚跨殑杩涜锛岃蒋浠剁殑鍚勭閰嶇疆鍙傛暟閮藉瓨璐湪XML鏂囦欢涓��
3銆佺敤jdom瑙f瀽xml鏂囦欢鏃跺浣曡В鍐充腑鏂囬棶棰�?濡備綍瑙f瀽?
绛�:鐪嬪涓嬩唬鐮�,鐢ㄧ紪鐮佹柟寮忓姞浠ヨВ鍐�
package test;
import java.io.*;
public class DOMTest
{
private String inFile = "c:\\people.xml"
private String outFile = "c:\\people.xml"
public static void main(String args[])
{
new DOMTest();
}
public DOMTest()
{
try
{
javax.xml.parsers.DocumentBuilder builder =
javax.xml.parsers.DocumentBuilderFactory.newInstance().newDocumentBuilder();
org.w3c.dom.Document doc = builder.newDocument();
org.w3c.dom.Element root = doc.createElement("鑰佸笀");
org.w3c.dom.Element wang = doc.createElement("鐜�");
org.w3c.dom.Element liu = doc.createElement("鍒�");
wang.appendChild(doc.createTextNode("鎴戞槸鐜嬭�佸笀"));
root.appendChild(wang);
doc.appendChild(root);
javax.xml.transform.Transformer transformer =
javax.xml.transform.TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(javax.xml.transform.OutputKeys.ENCODING,"gb2312");
transformer.setOutputProperty(javax.xml.transform.OutputKeys.INDENT,"yes");
transformer.transform(newjavax.xml.transform.dom.DOMSource(doc),
new
javax.xml.transform.stream.StreamResult(outFile));
}
catch (Exception e)
{
System.out.println (e.getMessage());
}
}
}
4銆佺紪绋嬬敤JAVA瑙f瀽XML鐨勬柟寮�.
绛�:鐢⊿AX鏂瑰紡瑙f瀽XML锛孹ML鏂囦欢濡備笅锛�
鐜嬪皬鏄�
淇℃伅瀛﹂櫌
6258113
鐢�,1955骞寸敓,鍗氬+锛�95骞磋皟鍏ユ捣鍗楀ぇ瀛�
浜嬩欢鍥炶皟绫籗AXHandler.java
import java.io.*;
import java.util.Hashtable;
import org.xml.sax.*;
public class SAXHandler extends HandlerBase
{
private Hashtable table = new Hashtable();
private String currentElement = null;
private String currentValue = null;
public void setTable(Hashtable table)
{
this.table = table;
}
public Hashtable getTable()
{
return table;
}
public void startElement(String tag, AttributeList attrs)
throws SAXException
{
currentElement = tag;
}
public void characters(char[] ch, int start, int length)
throws SAXException
{
currentValue = new String(ch, start, length);
}
public void endElement(String name) throws SAXException
{
if (currentElement.equals(name))
table.put(currentElement, currentValue);
}
}
JSP鍐呭鏄剧ず婧愮爜,SaxXml.jsp:
鍓栨瀽XML鏂囦欢people.xml
<%@ page errorPage=ErrPage.jsp
contentType=text/html;charset=GB2312 %>
<%@ page import=java.io.* %>
<%@ page import=java.util.Hashtable %>
<%@ page import=org.w3c.dom.* %>
<%@ page import=org.xml.sax.* %>
<%@ page import=javax.xml.parsers.SAXParserFactory %>
<%@ page import=javax.xml.parsers.SAXParser %>
<%@ page import=SAXHandler %>
<%
File file = new File(c:\people.xml);
FileReader reader = new FileReader(file);
Parser parser;
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
SAXHandler handler = new SAXHandler();
sp.parse(new InputSource(reader), handler);
Hashtable hashTable = handler.getTable();
out.println();鏁欏笀淇℃伅琛�
濮撳悕 +
(String)hashTable.get(new String(name)) +);
瀛﹂櫌 +
(String)hashTable.get(newString(college))+);
鐢佃瘽 +
(String)hashTable.get(new String(telephone)) +);
澶囨敞 +
(String)hashTable.get(new String(notes)) +);
out.println();
%>
5銆乆ML鏂囨。瀹氫箟鏈夊嚑绉嶅舰寮忥紵瀹冧滑涔嬮棿鏈変綍鏈川鍖哄埆锛熻В鏋怷ML鏂囨。鏈夊摢鍑犵鏂瑰紡锛�
a: 涓ょ褰㈠紡 dtdschema锛宐:鏈川鍖哄埆:schema鏈韩鏄痻ml鐨勶紝鍙互琚玐ML瑙f瀽鍣ㄨВ鏋�(杩欎篃鏄粠DTD涓婂彂灞晄chema鐨勬牴鏈洰鐨�)锛宑:鏈塂OM,SAX,STAX绛�
DOM:澶勭悊澶у瀷鏂囦欢鏃跺叾鎬ц兘涓嬮檷鐨勯潪甯稿帀瀹炽�傝繖涓棶棰樻槸鐢盌OM鐨勬爲缁撴瀯鎵�閫犳垚鐨勶紝杩欑缁撴瀯鍗犵敤鐨勫唴瀛樿緝澶氾紝鑰屼笖DOM蹇呴』鍦ㄨВ鏋愭枃浠朵箣鍓嶆妸鏁翠釜鏂囨。瑁呭叆鍐呭瓨,閫傚悎瀵筙ML鐨勯殢鏈鸿闂�
SAX:涓嶇幇浜嶥OM,SAX鏄簨浠堕┍鍔ㄥ瀷鐨刋ML瑙f瀽鏂瑰紡銆傚畠椤哄簭璇诲彇XML鏂囦欢锛屼笉闇�瑕佷竴娆″叏閮ㄨ杞芥暣涓枃浠躲�傚綋閬囧埌鍍忔枃浠跺紑澶达紝鏂囨。缁撴潫锛屾垨鑰呮爣绛惧紑澶翠笌鏍囩缁撴潫鏃讹紝瀹冧細瑙﹀彂涓�涓簨浠讹紝鐢ㄦ埛閫氳繃鍦ㄥ叾鍥炶皟浜嬩欢涓啓鍏ュ鐞嗕唬鐮佹潵澶勭悊XML鏂囦欢锛岄�傚悎瀵筙ML鐨勯『搴忚闂�
STAX:Streaming API forXML (StAX)
涓�.娴佽鐨勬鏋朵笌鏂版妧鏈�
1銆佽皥璋堜綘瀵筍truts鐨勭悊瑙c��
绛�:
1. struts鏄竴涓寜MVC妯″紡璁捐鐨刉eb灞傛鏋讹紝鍏跺疄瀹冨氨鏄竴涓ぇ澶х殑servlet锛岃繖涓猄ervlet鍚嶄负ActionServlet锛屾垨鏄疉ctionServlet鐨勫瓙绫汇�傛垜浠彲浠ュ湪web.xml鏂囦欢涓皢绗﹀悎鏌愮鐗瑰緛鐨勬墍鏈夎姹備氦缁欒繖涓猄ervlet澶勭悊锛岃繖涓猄ervlet鍐嶅弬鐓т竴涓厤缃枃浠讹紙閫氬父涓�/WEB-INF/struts-config.xml锛夊皢鍚勪釜璇锋眰鍒嗗埆鍒嗛厤缁欎笉鍚岀殑action鍘诲鐞嗐��
涓�涓墿灞曠煡璇嗙偣锛歴truts鐨勯厤缃枃浠跺彲浠ユ湁澶氫釜锛屽彲浠ユ寜妯″潡閰嶇疆鍚勮嚜鐨勯厤缃枃浠讹紝杩欐牱鍙互闃叉閰嶇疆鏂囦欢鐨勮繃搴﹁啫鑳�锛�
2.ActionServlet鎶婅姹備氦缁檃ction鍘诲鐞嗕箣鍓嶏紝浼氬皢璇锋眰鍙傛暟灏佽鎴愪竴涓猣ormbean瀵硅薄锛堝氨鏄竴涓猨ava绫伙紝杩欎釜绫讳腑鐨勬瘡涓睘鎬у搴斾竴涓姹傚弬鏁帮級锛屽皝瑁呮垚涓�涓粈涔堟牱鐨刦ormbean瀵硅薄鍛紵鐪嬮厤缃枃浠躲��
3.瑕佽鏄庣殑鏄紝 ActionServlet鎶奻ormbean瀵硅薄浼犻�掔粰action鐨別xecute鏂规硶涔嬪墠锛屽彲鑳戒細璋冪敤formbean鐨剉alidate鏂规硶杩涜鏍¢獙锛屽彧鏈夋牎楠岄�氳繃鍚庢墠灏嗚繖涓猣ormbean瀵硅薄浼犻�掔粰action鐨別xecute鏂规硶锛屽惁鍒欙紝瀹冨皢杩斿洖涓�涓敊璇〉闈紝杩欎釜閿欒椤甸潰鐢眎nput灞炴�ф寚瀹氾紝锛堢湅閰嶇疆鏂囦欢锛変綔鑰呬负浠�涔堝皢杩欓噷鍛藉悕涓篿nput灞炴�э紝鑰屼笉鏄痚rror灞炴�э紝鎴戜滑鍚庨潰缁撳悎瀹為檯鐨勮繍琛屾晥鏋滆繘琛屽垎鏋愩��
4.action鎵ц瀹屽悗瑕佽繑鍥炴樉绀虹殑缁撴灉瑙嗗浘锛岃繖涓粨鏋滆鍥炬槸鐢ㄤ竴涓狝ctionForward瀵硅薄鏉ヨ〃绀虹殑锛宎ctionforward瀵硅薄閫氳繃struts-config.xml閰嶇疆鏂囦欢涓殑閰嶇疆鍏宠仈鍒版煇涓猨sp椤甸潰锛屽洜涓虹▼搴忎腑浣跨敤鐨勬槸鍦╯truts-config.xml閰嶇疆鏂囦欢涓簀sp椤甸潰璁剧疆鐨勯�昏緫鍚嶏紝杩欐牱鍙互瀹炵幇action绋嬪簭浠g爜涓庤繑鍥炵殑jsp椤甸潰鍚嶇О鐨勮В鑰︺��
浣犲struts鍙兘杩樻湁鑷繁鐨勫簲鐢ㄦ柟闈㈢殑缁忛獙锛岄偅涔熻涓�骞惰鍑烘潵銆�
2銆佽皥璋堜綘瀵笻ibernate鐨勭悊瑙c��
绛�:
1. 闈㈠悜瀵硅薄璁捐鐨勮蒋浠跺唴閮ㄨ繍琛岃繃绋嬪彲浠ョ悊瑙f垚灏辨槸鍦ㄤ笉鏂垱寤哄悇绉嶆柊瀵硅薄銆佸缓绔嬪璞′箣闂寸殑鍏崇郴锛岃皟鐢ㄥ璞$殑鏂规硶鏉ユ敼鍙樺悇涓璞$殑鐘舵�佸拰瀵硅薄娑堜骸鐨勮繃绋嬶紝涓嶇绋嬪簭杩愯鐨勮繃绋嬪拰鎿嶄綔鎬庝箞鏍凤紝鏈川涓婇兘鏄寰楀埌涓�涓粨鏋滐紝绋嬪簭涓婁竴涓椂鍒诲拰涓嬩竴涓椂鍒荤殑杩愯缁撴灉鐨勫樊寮傚氨琛ㄧ幇鍦ㄥ唴瀛樹腑鐨勫璞$姸鎬佸彂鐢熶簡鍙樺寲銆�
2.涓轰簡鍦ㄥ叧鏈哄拰鍐呭瓨绌洪棿涓嶅鐨勭姸鍐典笅锛屼繚鎸佺▼搴忕殑杩愯鐘舵�侊紝闇�瑕佸皢鍐呭瓨涓殑瀵硅薄鐘舵�佷繚瀛樺埌鎸佷箙鍖栬澶囧拰浠庢寔涔呭寲璁惧涓仮澶嶅嚭瀵硅薄鐨勭姸鎬侊紝閫氬父閮芥槸淇濆瓨鍒板叧绯绘暟鎹簱鏉ヤ繚瀛樺ぇ閲忓璞′俊鎭�備粠Java绋嬪簭鐨勮繍琛屽姛鑳戒笂鏉ヨ锛屼繚瀛樺璞$姸鎬佺殑鍔熻兘鐩告瘮绯荤粺杩愯鐨勫叾浠栧姛鑳芥潵璇达紝搴旇鏄竴涓緢涓嶈捣鐪肩殑闄勫睘鍔熻兘锛宩ava閲囩敤jdbc鏉ュ疄鐜拌繖涓姛鑳斤紝杩欎釜涓嶈捣鐪肩殑鍔熻兘鍗磋缂栧啓澶ч噺鐨勪唬鐮侊紝鑰屽仛鐨勪簨鎯呬粎浠呮槸淇濆瓨瀵硅薄鍜屾仮澶嶅璞★紝骞朵笖閭d簺澶ч噺鐨刯dbc浠g爜骞舵病鏈変粈涔堟妧鏈惈閲忥紝鍩烘湰涓婃槸閲囩敤涓�濂椾緥琛屽叕浜嬬殑鏍囧噯浠g爜妯℃澘鏉ョ紪鍐欙紝鏄竴绉嶈嫤娲诲拰閲嶅鎬х殑宸ヤ綔銆�
3.閫氳繃鏁版嵁搴撲繚瀛榡ava绋嬪簭杩愯鏃朵骇鐢熺殑瀵硅薄鍜屾仮澶嶅璞★紝鍏跺疄灏辨槸瀹炵幇浜唈ava瀵硅薄涓庡叧绯绘暟鎹簱璁板綍鐨勬槧灏勫叧绯伙紝绉颁负ORM锛堝嵆Object RelationMapping锛夛紝浜轰滑鍙互閫氳繃灏佽JDBC浠g爜鏉ュ疄鐜颁簡杩欑鍔熻兘锛屽皝瑁呭嚭鏉ョ殑浜у搧绉颁箣涓篛RM妗嗘灦锛孒ibernate灏辨槸鍏朵腑鐨勪竴绉嶆祦琛孫RM妗嗘灦銆備娇鐢℉ibernate妗嗘灦锛屼笉鐢ㄥ啓JDBC浠g爜锛屼粎浠呮槸璋冪敤涓�涓猻ave鏂规硶锛屽氨鍙互灏嗗璞′繚瀛樺埌鍏崇郴鏁版嵁搴撲腑锛屼粎浠呮槸璋冪敤涓�涓猤et鏂规硶锛屽氨鍙互浠庢暟鎹簱涓姞杞藉嚭涓�涓璞°��
4.浣跨敤Hibernate鐨勫熀鏈祦绋嬫槸锛氶厤缃瓹onfiguration瀵硅薄銆佷骇鐢烻essionFactory銆佸垱寤簊ession瀵硅薄锛屽惎鍔ㄤ簨鍔★紝瀹屾垚CRUD鎿嶄綔锛屾彁浜や簨鍔★紝鍏抽棴session銆�
5.浣跨敤Hibernate鏃讹紝鍏堣閰嶇疆hibernate.cfg.xml鏂囦欢锛屽叾涓厤缃暟鎹簱杩炴帴淇℃伅鍜屾柟瑷�绛夛紝杩樿涓烘瘡涓疄浣撻厤缃浉搴旂殑hbm.xml鏂囦欢锛宧ibernate.cfg.xml鏂囦欢涓渶瑕佺櫥璁版瘡涓猦bm.xml鏂囦欢銆�
6.鍦ㄥ簲鐢℉ibernate鏃讹紝閲嶇偣瑕佷簡瑙ession鐨勭紦瀛樺師鐞嗭紝绾ц仈锛屽欢杩熷姞杞藉拰hql鏌ヨ銆�
3銆丄OP鐨勪綔鐢ㄣ��
4銆佷綘瀵筍pring鐨勭悊瑙c��
1.Spring瀹炵幇浜嗗伐鍘傛ā寮忕殑宸ュ巶绫伙紙鍦ㄨ繖閲屾湁蹇呰瑙i噴娓呮浠�涔堟槸宸ュ巶妯″紡锛夛紝杩欎釜绫诲悕涓築eanFactory锛堝疄闄呬笂鏄竴涓帴鍙o級锛屽湪绋嬪簭涓�氬父BeanFactory鐨勫瓙绫籄pplicationContext銆係pring鐩稿綋浜庝竴涓ぇ鐨勫伐鍘傜被锛屽湪鍏堕厤缃枃浠朵腑閫氳繃鍏冪礌閰嶇疆鐢ㄤ簬鍒涘缓瀹炰緥瀵硅薄鐨勭被鍚嶅拰瀹炰緥瀵硅薄鐨勫睘鎬с��
2. Spring鎻愪緵浜嗗IOC鑹ソ鏀寔锛孖OC鏄竴绉嶇紪绋嬫�濇兂锛屾槸涓�绉嶆灦鏋勮壓鏈紝鍒╃敤杩欑鎬濇兂鍙互寰堝ソ鍦板疄鐜版ā鍧椾箣闂寸殑瑙h�︺�侷OC涔熺О涓篋I锛圖epency Injection锛夛紝浠�涔堝彨渚濊禆娉ㄥ叆鍛紵
璀锛孋lass Programmer
{
Computer computer =null;
public void code()
{
//Computercomputer = new IBMComputer();
//Computercomputer = beanfacotry.getComputer();
computer.write();
}
public voidsetComputer(Computer computer)
{
this.computer= computer;
}
}
鍙﹀涓ょ鏂瑰紡閮界敱渚濊禆锛岀涓�涓洿鎺ヤ緷璧栦簬鐩爣绫伙紝绗簩涓妸渚濊禆杞Щ鍒板伐鍘備笂锛岀涓変釜褰诲簳涓庣洰鏍囧拰宸ュ巶瑙h�︿簡銆傚湪spring鐨勯厤缃枃浠朵腑閰嶇疆鐗囨濡備笅锛�
3. Spring鎻愪緵浜嗗AOP鎶�鏈殑鑹ソ灏佽锛� AOP绉颁负闈㈠悜鍒囬潰缂栫▼锛屽氨鏄郴缁熶腑鏈夊緢澶氬悇涓嶇浉骞茬殑绫荤殑鏂规硶锛屽湪杩欎簺浼楀鏂规硶涓鍔犲叆鏌愮绯荤粺鍔熻兘鐨勪唬鐮侊紝渚嬪锛屽姞鍏ユ棩蹇楋紝鍔犲叆鏉冮檺鍒ゆ柇锛屽姞鍏ュ紓甯稿鐞嗭紝杩欑搴旂敤绉颁负AOP銆傚疄鐜癆OP鍔熻兘閲囩敤鐨勬槸浠g悊鎶�鏈紝瀹㈡埛绔▼搴忎笉鍐嶈皟鐢ㄧ洰鏍囷紝鑰岃皟鐢ㄤ唬鐞嗙被锛屼唬鐞嗙被涓庣洰鏍囩被瀵瑰鍏锋湁鐩稿悓鐨勬柟娉曞0鏄庯紝鏈変袱绉嶆柟寮忓彲浠ュ疄鐜扮浉鍚岀殑鏂规硶澹版槑锛屼竴鏄疄鐜扮浉鍚岀殑鎺ュ彛锛屼簩鏄綔涓虹洰鏍囩殑瀛愮被鍦紝JDK涓噰鐢≒roxy绫讳骇鐢熷姩鎬佷唬鐞嗙殑鏂瑰紡涓烘煇涓帴鍙g敓鎴愬疄鐜扮被锛屽鏋滆涓烘煇涓被鐢熸垚瀛愮被锛屽垯鍙互鐢–GLI B銆傚湪鐢熸垚鐨勪唬鐞嗙被鐨勬柟娉曚腑鍔犲叆绯荤粺鍔熻兘鍜岃皟鐢ㄧ洰鏍囩被鐨勭浉搴旀柟娉曪紝绯荤粺鍔熻兘鐨勪唬鐞嗕互Advice瀵硅薄杩涜鎻愪緵锛屾樉鐒惰鍒涘缓鍑轰唬鐞嗗璞★紝鑷冲皯闇�瑕佺洰鏍囩被鍜孉dvice绫汇�俿pring鎻愪緵浜嗚繖绉嶆敮鎸侊紝鍙渶瑕佸湪spring閰嶇疆鏂囦欢涓厤缃繖涓や釜鍏冪礌鍗冲彲瀹炵幇浠g悊鍜宎op鍔熻兘锛屼緥濡傦紝
5銆佽皥璋圫truts涓殑Action servlet銆�
6銆丼truts浼樼己鐐�
浼樼偣锛�
1.瀹炵幇MVC妯″紡锛岀粨鏋勬竻鏅�,浣垮紑鍙戣�呭彧鍏虫敞涓氬姟閫昏緫鐨勫疄鐜�.
2锛庢湁涓板瘜鐨則ag鍙互鐢� ,Struts鐨勬爣璁板簱(Taglib)锛屽鑳界伒娲诲姩鐢紝鍒欒兘澶уぇ鎻愰珮寮�鍙戞晥鐜�
3. 椤甸潰瀵艰埅
浣跨郴缁熺殑鑴夌粶鏇村姞娓呮櫚銆傞�氳繃涓�涓厤缃枃浠讹紝鍗冲彲鎶婃彙鏁翠釜绯荤粺鍚勯儴鍒嗕箣闂寸殑鑱旂郴锛岃繖瀵逛簬鍚庢湡鐨勭淮鎶ゆ湁鐫�鑾ぇ鐨勫ソ澶勩�傚挨鍏舵槸褰撳彟涓�鎵瑰紑鍙戣�呮帴鎵嬭繖涓」鐩椂锛岃繖绉嶄紭鍔夸綋鐜板緱鏇村姞鏄庢樉銆�
4. 鎻愪緵Exception澶勭悊鏈哄埗 .
5. 鏁版嵁搴撻摼鎺ユ睜绠$悊
6. 鏀寔I18N
缂虹偣
涓�銆� 聽 杞埌灞曠ず灞傛椂锛岄渶瑕侀厤缃甪orward锛屽鏋滄湁鍗佷釜灞曠ず灞傜殑jsp锛岄渶瑕侀厤缃崄娆truts锛岃�屼笖杩樹笉鍖呮嫭鏈夋椂鍊欑洰褰曘�佹枃浠跺彉鏇达紝闇�瑕侀噸鏂颁慨鏀筬orward锛屾敞鎰忥紝姣忔淇敼閰嶇疆涔嬪悗锛岃姹傞噸鏂伴儴缃叉暣涓」鐩紝鑰宼omcate杩欐牱鐨勬湇鍔″櫒锛岃繕蹇呴』閲嶆柊鍚姩鏈嶅姟鍣�
浜屻�� 聽 浜屻�� Struts鐨凙ction蹇呴渶鏄痶hread锛峴afe鏂瑰紡锛屽畠浠呬粎鍏佽涓�涓疄渚嬪幓澶勭悊鎵�鏈夌殑璇锋眰銆傛墍浠ction鐢ㄥ埌鐨勬墍鏈夌殑璧勬簮閮藉繀闇�缁熶竴鍚屾锛岃繖涓氨寮曡捣浜嗙嚎绋嬪畨鍏ㄧ殑闂銆�
涓夈�� 聽 聽娴嬭瘯涓嶆柟渚�. Struts鐨勬瘡涓狝ction閮藉悓Web灞傝�﹀悎鍦ㄤ竴璧凤紝杩欐牱瀹冪殑娴嬭瘯渚濊禆浜嶹eb瀹瑰櫒锛屽崟鍏冩祴璇曚篃寰堥毦瀹炵幇銆備笉杩囨湁涓�涓狫unit鐨勬墿灞曞伐鍏稴truts TestCase鍙互瀹炵幇瀹冪殑鍗曞厓娴嬭瘯銆�
鍥涖�� 聽 聽绫诲瀷鐨勮浆鎹�. Struts鐨凢ormBean鎶婃墍鏈夌殑鏁版嵁閮戒綔涓篠tring绫诲瀷锛屽畠鍙互浣跨敤宸ュ叿Commons-Beanutils杩涜绫诲瀷杞寲銆備絾瀹冪殑杞寲閮芥槸鍦–lass绾у埆锛岃�屼笖杞寲鐨勭被鍨嬫槸涓嶅彲閰嶇疆鐨勩�傜被鍨嬭浆鍖栨椂鐨勯敊璇俊鎭繑鍥炵粰鐢ㄦ埛涔熸槸闈炲父鍥伴毦鐨勩��
浜斻�� 聽 瀵筍ervlet鐨勪緷璧栨�ц繃寮�. Struts澶勭悊Action鏃跺繀闇�瑕佷緷璧朣ervletRequest鍜孲ervletResponse锛屾墍鏈夊畠鎽嗚劚涓嶄簡Servlet瀹瑰櫒銆�
鍏�� 聽 聽鍓嶇琛ㄨ揪寮忚瑷�鏂归潰.Struts闆嗘垚浜咼STL锛屾墍浠ュ畠涓昏浣跨敤JSTL鐨勮〃杈惧紡璇█鏉ヨ幏鍙栨暟鎹�傚彲鏄疛STL鐨勮〃杈惧紡璇█鍦–ollection鍜岀储寮曞睘鎬ф柟闈㈠鐞嗘樉寰楀緢寮便��
涓冦�� 聽 聽瀵笰ction鎵ц鐨勬帶鍒跺洶闅�. Struts鍒涘缓涓�涓狝ction锛屽鏋滄兂鎺у埗瀹冪殑鎵ц椤哄簭灏嗕細闈炲父鍥伴毦銆傜敋鑷充綘瑕侀噸鏂板幓鍐橲ervlet鏉ュ疄鐜颁綘鐨勮繖涓姛鑳介渶姹傘��
鍏�� 聽 聽瀵笰ction鎵ц鍓嶅拰鍚庣殑澶勭悊. Struts澶勭悊Action鐨勬椂鍊欐槸鍩轰簬class鐨刪ierarchies锛屽緢闅惧湪action澶勭悊鍓嶅拰鍚庤繘琛屾搷浣溿��
涔濄�� 聽 聽瀵逛簨浠舵敮鎸佷笉澶�.鍦╯truts涓紝瀹為檯鏄竴涓〃鍗旻orm瀵瑰簲涓�涓狝ction绫�(鎴朌ispatchAction)锛屾崲涓�鍙ヨ瘽璇达細鍦⊿truts涓疄闄呮槸涓�涓〃鍗曞彧鑳藉搴斾竴涓簨浠讹紝struts杩欑浜嬩欢鏂瑰紡绉颁负application event锛宎pplication event鍜宑omponent event鐩告瘮鏄竴绉嶇矖绮掑害鐨勪簨浠�
7銆丼TRUTS鐨勫簲鐢�(濡係TRUTS鏋舵瀯)
Struts鏄噰鐢↗ava Servlet/JavaServer Pages鎶�鏈紝寮�鍙慦eb搴旂敤绋嬪簭鐨勫紑鏀炬簮鐮佺殑framework銆傞噰鐢⊿truts鑳藉紑鍙戝嚭鍩轰簬MVC(Model-View-Controller)璁捐妯″紡鐨勫簲鐢ㄦ瀯鏋躲�� Struts鏈夊涓嬬殑涓昏鍔熻兘锛氫竴.鍖呭惈涓�涓猚ontroller servlet锛岃兘灏嗙敤鎴风殑璇锋眰鍙戦�佸埌鐩稿簲鐨凙ction瀵硅薄銆備簩.JSP鑷敱tag搴擄紝骞朵笖鍦╟ontroller servlet涓彁渚涘叧鑱旀敮鎸侊紝甯姪寮�鍙戝憳鍒涘缓浜や簰寮忚〃鍗曞簲鐢ㄣ�備笁.鎻愪緵浜嗕竴绯诲垪瀹炵敤瀵硅薄锛歑ML澶勭悊銆侀�氳繃Java reflection APIs鑷姩澶勭悊JavaBeans灞炴�с�佸浗闄呭寲鐨勬彁绀哄拰娑堟伅銆�
8銆佽璇磗truts1涓巗truts2鐨勫尯鍒��
1.閮芥槸MVC鐨刉EB妗嗘灦,
2 struts1鐨勮�佺墝妗嗘灦锛屽簲鐢ㄥ緢骞挎硾锛屾湁寰堝ソ鐨勭兢浼楀熀纭�锛屼娇鐢ㄥ畠寮�鍙戦闄╁緢灏忥紝鎴愭湰鏇翠綆锛乻truts2铏界劧鍩轰簬杩欎釜妗嗘灦锛屼絾鏄簲鐢ㄧ兢浼楀苟澶氾紝鐩稿涓嶆垚鐔燂紝鏈煡鐨勯闄╁拰鍙樺寲寰堝锛屽紑鍙戜汉鍛樼浉瀵逛笉濂芥嫑锛屼娇鐢ㄥ畠寮�鍙戦」鐩殑椋庨櫓绯绘暟鏇村ぇ锛岀敤浜烘垚鏈洿楂橈紒
3.struts2姣曠珶鏄珯鍦ㄥ墠杈堢殑鍩虹璁捐鍑烘潵锛屽畠浼氭敼鍠勫拰瀹屽杽struts1涓殑涓�浜涚己闄凤紝struts1涓竴浜涙偓鑰屾湭鍐抽棶棰樺湪struts2寰楀埌浜嗚В鍐炽��
4.struts1鐨勫墠绔帶鍒跺櫒鏄竴涓猄ervlet锛屽悕绉颁负ActionServlet锛宻truts2鐨勫墠绔帶鍒跺櫒鏄竴涓猣ilter锛屽湪struts2.0涓彨FilterDispatcher锛屽湪struts2.1涓彨StrutsPrepareAndExecuteFilter銆�
5.struts1鐨刟ction闇�瑕佺户鎵緼ction绫伙紝struts2鐨刟ction鍙互涓嶇户鎵夸换浣曠被锛泂truts1瀵瑰悓涓�涓矾寰勭殑鎵�鏈夎姹傚叡浜竴涓狝ction瀹炰緥锛宻truts2瀵瑰悓涓�涓矾寰勭殑姣忎釜璇锋眰鍒嗗埆浣跨敤涓�涓嫭绔婣ction瀹炰緥瀵硅薄锛屾墍鏈夊浜巗truts2鐨凙ction涓嶇敤鑰冭檻绾跨▼瀹夊叏闂銆�
6.鍦╯truts1涓娇鐢╢ormbean灏佽璇锋眰鍙傛暟锛屽湪struts2涓洿鎺ヤ娇鐢╝ction鐨勫睘鎬ф潵灏佽璇锋眰鍙傛暟銆�
7.struts1涓殑澶氫釜涓氬姟鏂规硶鏀惧湪涓�涓狝ction涓椂锛堝嵆缁ф壙DispatchAction鏃讹級锛岃涔堥兘鏍¢獙锛岃涔堥兘涓嶆牎楠岋紱瀵逛簬struts2锛屽彲浠ユ寚瀹氬彧瀵规煇涓柟娉曡繘琛屾牎楠岋紝褰撲竴涓狝ction缁ф壙浜咥ctionSupport涓斿湪杩欎釜绫讳腑鍙紪鍐欎簡validateXxx()鏂规硶锛岄偅涔堝垯鍙Xxx()鏂规硶杩涜鏍¢獙銆�
锛堜竴涓姹傛潵浜嗙殑鎵ц娴佺▼杩涜鍒嗘瀽锛宻truts2鏄嚜鍔ㄦ敮鎸佸垎妯″潡寮�鍙戯紝骞跺彲浠ヤ笉鍚屾ā鍧楄缃笉鍚岀殑url鍓嶇紑锛岃繖鏄�氳繃package鐨刵amespace鏉ュ疄鐜扮殑锛泂truts2鏄敮鎸佸绉嶇被鍨嬬殑瑙嗗浘锛泂truts2鐨勮鍥惧湴鍧�鍙互鏄姩鎬佺殑锛屽嵆瑙嗗浘鐨勫悕绉版槸鏀寔鍙橀噺鏂瑰紡鐨勶紝涓句緥锛岃鍧涘彂甯栧け璐ュ悗鍥炴潵杩樿浼犻�抌oardid銆傝鍥惧唴瀹规樉绀烘柟闈細瀹冪殑鏍囩鐢╫gnl锛岃el寮哄ぇ寰堝锛屽湪鍥介檯鍖栨柟闈㈡敮鎸佸垎妯″潡绠$悊锛屼袱涓ā鍧楃敤鍒板悓鏍风殑key锛屽搴斾笉鍚岀殑娑堟伅锛涳級
涓嶴truts1涓嶅悓锛孲truts2瀵圭敤鎴风殑姣忎竴娆¤姹傞兘浼氬垱寤轰竴涓狝ction锛屾墍浠truts2涓殑Action鏄嚎绋嬪畨鍏ㄧ殑銆�
缁欐垜鍗拌薄鏈�娣卞埢鐨勬槸锛歴truts閰嶇疆鏂囦欢涓殑redirect瑙嗗浘鐨剈rl涓嶈兘鎺ュ彈鍙傛暟锛岃�宻truts2閰嶇疆鏂囦欢涓殑redirect瑙嗗浘鍙互鎺ュ彈鍙傛暟銆�
9銆乭ibernate涓殑update()鍜宻aveOrUpdate()鐨勫尯鍒紝session鐨刲oad()鍜実et()鐨勫尯鍒��
10銆佺畝杩� Hibernate鍜� JDBC鐨勪紭缂虹偣?濡備綍涔﹀啓涓�涓� one to many閰嶇疆鏂囦欢.
11銆乮Batis涓嶩ibernate鏈変粈涔堜笉鍚�?
鐩稿悓鐐癸細灞忚斀jdbc api鐨勫簳灞傝闂粏鑺傦紝浣跨敤鎴戜滑涓嶇敤涓巎dbc api鎵撲氦閬擄紝灏卞彲浠ヨ闂暟鎹��
jdbc api缂栫▼娴佺▼鍥哄畾锛岃繕灏唖ql璇彞涓巎ava浠g爜娣锋潅鍦ㄤ簡涓�璧凤紝缁忓父闇�瑕佹嫾鍑憇ql璇彞锛岀粏鑺傚緢绻佺悙銆�
ibatis鐨勫ソ澶勶細灞忚斀jdbc api鐨勫簳灞傝闂粏鑺傦紱灏唖ql璇彞涓巎ava浠g爜杩涜鍒嗙;鎻愪緵浜嗗皢缁撴灉闆嗚嚜鍔ㄥ皝瑁呯О涓哄疄浣撳璞″拰瀵硅薄鐨勯泦鍚堢殑鍔熻兘锛宷ueryForList杩斿洖瀵硅薄闆嗗悎锛岀敤queryForObject杩斿洖鍗曚釜瀵硅薄锛涙彁渚涗簡鑷姩灏嗗疄浣撳璞$殑灞炴�т紶閫掔粰sql璇彞鐨勫弬鏁般��
Hibernate鏄竴涓叏鑷姩鐨刼rm鏄犲皠宸ュ叿锛屽畠鍙互鑷姩鐢熸垚sql璇彞,ibatis闇�瑕佹垜浠嚜宸卞湪xml閰嶇疆鏂囦欢涓啓sql璇彞锛宧ibernate瑕佹瘮ibatis鍔熻兘璐熻矗鍜屽己澶у緢澶氥�傚洜涓篽ibernate鑷姩鐢熸垚sql璇彞锛屾垜浠棤娉曟帶鍒惰璇彞锛屾垜浠氨鏃犳硶鍘诲啓鐗瑰畾鐨勯珮鏁堢巼鐨剆ql銆傚浜庝竴浜涗笉澶鏉傜殑sql鏌ヨ锛宧ibernate鍙互寰堝ソ甯垜浠畬鎴愶紝浣嗘槸锛屽浜庣壒鍒鏉傜殑鏌ヨ锛宧ibernate灏卞緢闅鹃�傚簲浜嗭紝杩欐椂鍊欑敤ibatis灏辨槸涓嶉敊鐨勯�夋嫨锛屽洜涓篿batis杩樻槸鐢辨垜浠嚜宸卞啓sql璇彞銆�
12銆佸啓Hibernate鐨勪竴瀵瑰鍜屽瀵逛竴鍙屽悜鍏宠仈鐨刼rm閰嶇疆?
9銆乭ibernate鐨刬nverse灞炴�х殑浣滅敤?
瑙e喅鏂规涓�锛屾寜鐓bject[]鏁版嵁鍙栧嚭鏁版嵁锛岀劧鍚庤嚜宸辩粍bean
瑙e喅鏂规浜岋紝瀵规瘡涓〃鐨刡ean鍐欐瀯閫犲嚱鏁帮紝姣斿琛ㄤ竴瑕佹煡鍑篺ield1,field2涓や釜瀛楁锛岄偅涔堟湁涓�涓瀯閫犲嚱鏁板氨鏄疊ean(type1filed1,type2
field2) 锛岀劧鍚庡湪hql閲岄潰灏卞彲浠ョ洿鎺ョ敓鎴愯繖涓猙ean浜嗐��
13銆佸湪DAO涓浣曚綋鐜癉AO璁捐妯″紡?
瑙e喅鏂规涓�锛屾寜鐓bject[]鏁版嵁鍙栧嚭鏁版嵁锛岀劧鍚庤嚜宸辩粍bean
瑙e喅鏂规浜岋紝瀵规瘡涓〃鐨刡ean鍐欐瀯閫犲嚱鏁帮紝姣斿琛ㄤ竴瑕佹煡鍑篺ield1,field2涓や釜瀛楁锛岄偅涔堟湁涓�涓瀯閫犲嚱鏁板氨鏄疊ean(type1filed1,type2
field2) 锛岀劧鍚庡湪hql閲岄潰灏卞彲浠ョ洿鎺ョ敓鎴愯繖涓猙ean浜嗐��
14銆乻pring+Hibernate涓鎵樻柟妗堟�庝箞閰嶇疆?
瑙e喅鏂规涓�锛屾寜鐓bject[]鏁版嵁鍙栧嚭鏁版嵁锛岀劧鍚庤嚜宸辩粍bean
瑙e喅鏂规浜岋紝瀵规瘡涓〃鐨刡ean鍐欐瀯閫犲嚱鏁帮紝姣斿琛ㄤ竴瑕佹煡鍑篺ield1,field2涓や釜瀛楁锛岄偅涔堟湁涓�涓瀯閫犲嚱鏁板氨鏄疊ean(type1filed1,type2
field2) 锛岀劧鍚庡湪hql閲岄潰灏卞彲浠ョ洿鎺ョ敓鎴愯繖涓猙ean浜嗐��
15銆乻pring+Hibernate涓鎵樻柟妗堟�庝箞閰嶇疆?
瑙e喅鏂规涓�锛屾寜鐓bject[]鏁版嵁鍙栧嚭鏁版嵁锛岀劧鍚庤嚜宸辩粍bean
瑙e喅鏂规浜岋紝瀵规瘡涓〃鐨刡ean鍐欐瀯閫犲嚱鏁帮紝姣斿琛ㄤ竴瑕佹煡鍑篺ield1,field2涓や釜瀛楁锛岄偅涔堟湁涓�涓瀯閫犲嚱鏁板氨鏄疊ean(type1filed1,type2
field2) 锛岀劧鍚庡湪hql閲岄潰灏卞彲浠ョ洿鎺ョ敓鎴愯繖涓猙ean浜嗐��
16. hibernate杩涜澶氳〃鏌ヨ姣忎釜琛ㄤ腑鍚勫彇鍑犱釜瀛楁锛屼篃灏辨槸璇存煡璇㈠嚭鏉ョ殑缁撴灉闆嗘病鏈変竴涓疄浣撶被涓庝箣瀵瑰簲濡備綍瑙e喅锛�
瑙e喅鏂规涓�锛屾寜鐓bject[]鏁版嵁鍙栧嚭鏁版嵁锛岀劧鍚庤嚜宸辩粍bean
瑙e喅鏂规浜岋紝瀵规瘡涓〃鐨刡ean鍐欐瀯閫犲嚱鏁帮紝姣斿琛ㄤ竴瑕佹煡鍑篺ield1,field2涓や釜瀛楁锛岄偅涔堟湁涓�涓瀯閫犲嚱鏁板氨鏄疊ean(type1filed1,type2
field2) 锛岀劧鍚庡湪hql閲岄潰灏卞彲浠ョ洿鎺ョ敓鎴愯繖涓猙ean浜嗐��
17.浠嬬粛涓�涓婬ibernate鐨勪簩绾х紦瀛�
鎸夌収浠ヤ笅鎬濊矾鏉ュ洖绛旓細锛�1锛夐鍏堣娓呮浠�涔堟槸缂撳瓨锛岋紙2锛夊啀璇存湁浜唄ibernate鐨凷ession灏辨槸涓�绾х紦瀛橈紝鍗虫湁浜嗕竴绾х紦瀛橈紝涓轰粈涔堣繕瑕佹湁浜岀骇缂撳瓨锛岋紙3锛夋渶鍚庡啀璇村浣曢厤缃瓾ibernate鐨勪簩绾х紦瀛樸��
锛�1锛夌紦瀛樺氨鏄妸浠ュ墠浠庢暟鎹簱涓煡璇㈠嚭鏉ュ拰浣跨敤杩囩殑瀵硅薄淇濆瓨鍦ㄥ唴瀛樹腑锛堜竴涓暟鎹粨鏋勪腑锛夛紝杩欎釜鏁版嵁缁撴瀯閫氬父鏄垨绫讳技Hashmap锛屽綋浠ュ悗瑕佷娇鐢ㄦ煇涓璞℃椂锛屽厛鏌ヨ缂撳瓨涓槸鍚︽湁杩欎釜瀵硅薄锛屽鏋滄湁鍒欎娇鐢ㄧ紦瀛樹腑鐨勫璞★紝濡傛灉娌℃湁鍒欏幓鏌ヨ鏁版嵁搴擄紝骞跺皢鏌ヨ鍑烘潵鐨勫璞′繚瀛樺湪缂撳瓨涓紝浠ヤ究涓嬫浣跨敤銆備笅闈㈡槸缂撳瓨鐨勪吉浠g爜锛�
寮曞嚭hibernate鐨勭浜岀骇缂撳瓨锛岀敤涓嬮潰鐨勪吉浠g爜鍒嗘瀽浜咰ache鐨勫疄鐜板師鐞�
Dao
{
hashmap map = newmap();
User getUser(integerid)
{
User user =map.get(id)
if(user ==null)
{
user =session.get(id);
map.put(id,user);
}
return user;
}
}
Dao
{
Cache cache = null
setCache(Cachecache)
{
this.cache =cache
}
User getUser(int id)
{
if(cache!=null)
{
Useruser = cache.get(id);
if(user==null)
{
user= session.get(id);
cache.put(id,user);
}
returnuser;
}
returnsession.get(id);
}
}
锛�2锛塇ibernate鐨凷ession灏辨槸涓�绉嶇紦瀛橈紝鎴戜滑閫氬父灏嗕箣绉颁负Hibernate鐨勪竴绾х紦瀛橈紝褰撴兂浣跨敤session浠庢暟鎹簱涓煡璇㈠嚭涓�涓璞℃椂锛孲ession涔熸槸鍏堜粠鑷繁鍐呴儴鏌ョ湅鏄惁瀛樺湪杩欎釜瀵硅薄锛屽瓨鍦ㄥ垯鐩存帴杩斿洖锛屼笉瀛樺湪鎵嶅幓璁块棶鏁版嵁搴擄紝骞跺皢鏌ヨ鐨勭粨鏋滀繚瀛樺湪鑷繁鍐呴儴銆傜敱浜嶴ession浠h〃涓�娆′細璇濊繃绋嬶紝涓�涓猄ession涓庝竴涓暟鎹簱杩炴帴鐩稿叧杩烇紝鎵�浠ession鏈�濂戒笉瑕侀暱鏃堕棿淇濇寔鎵撳紑锛岄�氬父浠呯敤浜庝竴涓簨鍔″綋涓紝鍦ㄤ簨鍔$粨鏉熸椂灏卞簲鍏抽棴銆傚苟涓擲ession鏄嚎绋嬩笉瀹夊叏鐨勶紝琚涓嚎绋嬪叡浜椂瀹规槗鍑虹幇闂銆傞�氬父鍙湁閭g鍏ㄥ眬鎰忎箟涓婄殑缂撳瓨鎵嶆槸鐪熸鐨勭紦瀛樺簲鐢紝鎵嶆湁杈冨ぇ鐨勭紦瀛樹环鍊硷紝鍥犳锛孒ibernate鐨凷ession杩欎竴绾х紦瀛樼殑缂撳瓨浣滅敤骞朵笉鏄庢樉锛屽簲鐢ㄤ环鍊间笉澶с�侶ibernate鐨勪簩绾х紦瀛樺氨鏄涓篐ibernate閰嶇疆涓�绉嶅叏灞�缂撳瓨锛岃澶氫釜绾跨▼鍜屽涓簨鍔¢兘鍙互鍏变韩杩欎釜缂撳瓨銆傛垜浠笇鏈涚殑鏄竴涓汉浣跨敤杩囷紝鍏朵粬浜轰篃鍙互浣跨敤锛宻ession娌℃湁杩欑鏁堟灉銆�
锛�3锛変簩绾х紦瀛樻槸鐙珛浜嶩ibernate鐨勮蒋浠堕儴浠讹紝灞炰簬绗笁鏂圭殑浜у搧锛屽涓巶鍟嗗拰缁勭粐閮芥彁渚涙湁缂撳瓨浜у搧锛屼緥濡傦紝EHCache鍜孫SCache绛夌瓑銆傚湪Hibernate涓娇鐢ㄤ簩绾х紦瀛橈紝棣栧厛灏辫鍦╤ibernate.cfg.xml閰嶇疆鏂囦欢涓厤缃娇鐢ㄥ摢涓巶瀹剁殑缂撳瓨浜у搧锛屾帴鐫�闇�瑕侀厤缃缂撳瓨浜у搧鑷繁鐨勯厤缃枃浠讹紝鏈�鍚庤閰嶇疆Hibernate涓殑鍝簺瀹炰綋瀵硅薄瑕佺撼鍏ュ埌浜岀骇缂撳瓨鐨勭鐞嗕腑銆傛槑鐧戒簡浜岀骇缂撳瓨鍘熺悊鍜屾湁浜嗚繖涓�濊矾鍚庯紝寰堝鏄撻厤缃捣Hibernate鐨勪簩绾х紦瀛樸�傛墿灞曠煡璇嗭細涓�涓猄essionFactory鍙互鍏宠仈涓�涓簩绾х紦瀛橈紝涔熷嵆涓�涓簩绾х紦瀛樺彧鑳借礋璐g紦瀛樹竴涓暟鎹簱涓殑鏁版嵁锛屽綋浣跨敤Hibernate鐨勪簩绾х紦瀛樺悗锛屾敞鎰忎笉瑕佹湁鍏朵粬鐨勫簲鐢ㄦ垨SessionFactory鏉ユ洿鏀瑰綋鍓嶆暟鎹簱涓殑鏁版嵁锛岃繖鏍风紦瀛樼殑鏁版嵁灏变細涓庢暟鎹簱涓殑瀹為檯鏁版嵁涓嶄竴鑷淬��
18銆丼pring鐨勪緷璧栨敞鍏ユ槸浠�涔堟剰鎬�?缁欎竴涓� Bean 鐨� message灞炴��,瀛楃涓茬被鍨�,娉ㄥ叆鍊间负"Hello"鐨� XML閰嶇疆鏂囦欢璇ユ�庝箞鍐�?
19銆丣do鏄粈涔�?
JDO鏄疛ava瀵硅薄鎸佷箙鍖栫殑鏂扮殑瑙勮寖锛屼负java data object鐨勭畝绉�,涔熸槸涓�涓敤浜庡瓨鍙栨煇绉嶆暟鎹粨搴撲腑鐨勫璞$殑鏍囧噯鍖朅PI銆侸DO鎻愪緵浜嗛�忔槑鐨勫璞″瓨鍌紝鍥犳瀵瑰紑鍙戜汉鍛樻潵璇达紝瀛樺偍鏁版嵁瀵硅薄瀹屽叏涓嶉渶瑕侀澶栫殑浠g爜锛堝JDBC API鐨勪娇鐢級銆傝繖浜涚箒鐞愮殑渚嬭宸ヤ綔宸茬粡杞Щ鍒癑DO浜у搧鎻愪緵鍟嗚韩涓婏紝浣垮紑鍙戜汉鍛樿В鑴卞嚭鏉ワ紝浠庤�岄泦涓椂闂村拰绮惧姏鍦ㄤ笟鍔¢�昏緫涓娿�傚彟澶栵紝JDO寰堢伒娲伙紝鍥犱负瀹冨彲浠ュ湪浠讳綍鏁版嵁搴曞眰涓婅繍琛屻�侸DBC鍙槸闈㈠悜鍏崇郴鏁版嵁搴擄紙RDBMS锛塉DO鏇撮�氱敤锛屾彁渚涘埌浠讳綍鏁版嵁搴曞眰鐨勫瓨鍌ㄥ姛鑳斤紝姣斿鍏崇郴鏁版嵁搴撱�佹枃浠躲�乆ML浠ュ強瀵硅薄鏁版嵁搴擄紙ODBMS锛夌瓑绛夛紝浣垮緱搴旂敤鍙Щ妞嶆�ф洿寮恒��
20銆佷粈涔堟槸spring鐨処OCAOP
21銆丼TRUTS鐨勫伐浣滄祦绋嬶紒
22銆乻pring涓嶦JB鐨勫尯鍒紒锛�
鍏�.杞欢宸ョ▼涓庤璁℃ā寮�
1銆乁ML鏂归潰
鏍囧噯寤烘ā璇█UML銆傜敤渚嬪浘,闈欐�佸浘(鍖呮嫭绫诲浘銆佸璞″浘鍜屽寘鍥�),琛屼负鍥�,浜や簰鍥�(椤哄簭鍥�,鍚堜綔鍥�),瀹炵幇鍥俱��
2銆乯2ee甯哥敤鐨勮璁℃ā寮忥紵璇存槑宸ュ巶妯″紡銆�
鎬诲叡23绉嶏紝鍒嗕负涓夊ぇ绫伙細鍒涘缓鍨嬶紝缁撴瀯鍨嬶紝琛屼负鍨�
鎴戝彧璁板緱鍏朵腑甯哥敤鐨�6銆�7绉嶏紝鍒嗗埆鏄細
鍒涘缓鍨嬶紙宸ュ巶銆佸伐鍘傛柟娉曘�佹娊璞″伐鍘傘�佸崟渚嬶級
缁撴瀯鍨嬶紙鍖呰銆侀�傞厤鍣紝缁勫悎锛屼唬鐞嗭級
琛屼负锛堣瀵熻�咃紝妯$増锛岀瓥鐣ワ級
鐒跺悗鍐嶉拡瀵逛綘鐔熸倝鐨勬ā寮忚皥璋堜綘鐨勭悊瑙e嵆鍙��
Java涓殑23绉嶈璁℃ā寮忥細
Factory锛堝伐鍘傛ā寮忥級锛孊uilder锛堝缓閫犳ā寮忥級锛孎actory Method锛堝伐鍘傛柟娉曟ā寮忥級锛�
Prototype锛堝師濮嬫ā鍨嬫ā寮忥級锛孲ingleton锛堝崟渚嬫ā寮忥級锛孎acade锛堥棬闈㈡ā寮忥級锛�
Adapter锛堥�傞厤鍣ㄦā寮忥級锛孊ridge锛堟ˉ姊佹ā寮忥級锛孋omposite锛堝悎鎴愭ā寮忥級锛�
Decorator锛堣楗版ā寮忥級锛孎lyweight锛堜韩鍏冩ā寮忥級锛孭roxy锛堜唬鐞嗘ā寮忥級锛�
Command锛堝懡浠ゆā寮忥級锛孖nterpreter锛堣В閲婂櫒妯″紡锛夛紝 Visitor锛堣闂�呮ā寮忥級锛�
Iterator锛堣凯浠e瓙妯″紡锛夛紝Mediator锛堣皟鍋滆�呮ā寮忥級锛孧emento锛堝蹇樺綍妯″紡锛夛紝
Observer锛堣瀵熻�呮ā寮忥級锛孲tate锛堢姸鎬佹ā寮忥級锛孲trategy锛堢瓥鐣ユā寮忥級锛�
Template Method锛堟ā鏉挎柟娉曟ā寮忥級锛� Chain Of Responsibleity锛堣矗浠婚摼妯″紡锛�
宸ュ巶妯″紡锛氬伐鍘傛ā寮忔槸涓�绉嶇粡甯歌浣跨敤鍒扮殑妯″紡锛屾牴鎹伐鍘傛ā寮忓疄鐜扮殑绫诲彲浠ユ牴鎹彁渚涚殑鏁版嵁鐢熸垚涓�缁勭被涓煇涓�涓被鐨勫疄渚嬶紝閫氬父杩欎竴缁勭被鏈変竴涓叕鍏辩殑鎶借薄鐖剁被骞朵笖瀹炵幇浜嗙浉鍚岀殑鏂规硶锛屼絾鏄繖浜涙柟娉曢拡瀵逛笉鍚岀殑鏁版嵁杩涜浜嗕笉鍚岀殑鎿嶄綔銆傞鍏堥渶瑕佸畾涔変竴涓熀绫伙紝璇ョ被鐨勫瓙绫婚�氳繃涓嶅悓鐨勬柟娉曞疄鐜颁簡鍩虹被涓殑鏂规硶銆傜劧鍚庨渶瑕佸畾涔変竴涓伐鍘傜被锛屽伐鍘傜被鍙互鏍规嵁鏉′欢鐢熸垚涓嶅悓鐨勫瓙绫诲疄渚嬨�傚綋寰楀埌瀛愮被鐨勫疄渚嬪悗锛屽紑鍙戜汉鍛樺彲浠ヨ皟鐢ㄥ熀绫讳腑鐨勬柟娉曡�屼笉蹇呰�冭檻鍒板簳杩斿洖鐨勬槸鍝竴涓瓙绫荤殑瀹炰緥銆�
3銆佸紑鍙戜腑閮界敤鍒颁簡閭d簺璁捐妯″紡?鐢ㄥ湪浠�涔堝満鍚�?
姣忎釜妯″紡閮芥弿杩颁簡涓�涓湪鎴戜滑鐨勭幆澧冧腑涓嶆柇鍑虹幇鐨勯棶棰橈紝鐒跺悗鎻忚堪浜嗚闂鐨勮В鍐虫柟妗堢殑鏍稿績銆傞�氳繃杩欑鏂瑰紡锛屼綘鍙互鏃犳暟娆″湴浣跨敤閭d簺宸叉湁鐨勮В鍐虫柟妗堬紝鏃犻渶鍦ㄩ噸澶嶇浉鍚岀殑宸ヤ綔銆備富瑕佺敤鍒颁簡MVC鐨勮璁℃ā寮忋�傜敤鏉ュ紑鍙慗SP/Servlet鎴栬�匤2EE鐨勭浉鍏冲簲鐢ㄣ�傜畝鍗曞伐鍘傛ā寮忕瓑銆�
涔�. j2ee閮ㄥ垎
1銆丅S涓嶤S鐨勮仈绯讳笌鍖哄埆銆�
C/S鏄疌lient/Server鐨勭缉鍐欍�傛湇鍔″櫒閫氬父閲囩敤楂樻�ц兘鐨凱C銆佸伐浣滅珯鎴栧皬鍨嬫満锛屽苟閲囩敤澶у瀷鏁版嵁搴撶郴缁燂紝濡侽racle銆丼ybase銆両nFORMix鎴� SQL Server銆傚鎴风闇�瑕佸畨瑁呬笓鐢ㄧ殑瀹㈡埛绔蒋浠躲��
B/锛虫槸Brower/Server鐨勭缉鍐欙紝瀹㈡埛鏈轰笂鍙瀹夎涓�涓祻瑙堝櫒锛圔rowser锛夛紝濡侼etscape Navigator鎴朓nternet Explorer锛屾湇鍔″櫒瀹夎Oracle銆丼ybase銆両nFORMix鎴� SQL Server绛夋暟鎹簱銆傚湪杩欑缁撴瀯涓嬶紝鐢ㄦ埛鐣岄潰瀹屽叏閫氳繃WWW娴忚鍣ㄥ疄鐜帮紝涓�閮ㄥ垎浜嬪姟閫昏緫鍦ㄥ墠绔疄鐜帮紝浣嗘槸涓昏浜嬪姟閫昏緫鍦ㄦ湇鍔″櫒绔疄鐜般�傛祻瑙堝櫒閫氳繃锛積b Server鍚屾暟鎹簱杩涜鏁版嵁浜や簰銆�
C/S 涓� B/S鍖哄埆锛�
锛戯紟纭欢鐜涓嶅悓:
C/S 涓�鑸缓绔嬪湪涓撶敤鐨勭綉缁滀笂,灏忚寖鍥撮噷鐨勭綉缁滅幆澧�,灞�鍩熺綉涔嬮棿鍐嶉�氳繃涓撻棬鏈嶅姟鍣ㄦ彁渚涜繛鎺ュ拰鏁版嵁浜ゆ崲鏈嶅姟.
B/S 寤虹珛鍦ㄥ箍鍩熺綉涔嬩笂鐨�,涓嶅繀鏄笓闂ㄧ殑缃戠粶纭欢鐜,渚嬩笌鐢佃瘽涓婄綉,绉熺敤璁惧.淇℃伅鑷繁绠$悊.鏈夋瘮C/S鏇村己鐨勯�傚簲鑼冨洿,涓�鑸彧瑕佹湁鎿嶄綔绯荤粺鍜屾祻瑙堝櫒灏辫
锛掞紟瀵瑰畨鍏ㄨ姹備笉鍚�
C/S 涓�鑸潰鍚戠浉瀵瑰浐瀹氱殑鐢ㄦ埛缇�,瀵逛俊鎭畨鍏ㄧ殑鎺у埗鑳藉姏寰堝己.涓�鑸珮搴︽満瀵嗙殑淇℃伅绯荤粺閲囩敤C/S缁撴瀯閫傚疁.鍙互閫氳繃B/S鍙戝竷閮ㄥ垎鍙叕寮�淇℃伅.
B/S 寤虹珛鍦ㄥ箍鍩熺綉涔嬩笂,瀵瑰畨鍏ㄧ殑鎺у埗鑳藉姏鐩稿寮�,鍙兘闈㈠悜涓嶅彲鐭ョ殑鐢ㄦ埛銆�
锛擄紟瀵圭▼搴忔灦鏋勪笉鍚�
C/S 绋嬪簭鍙互鏇村姞娉ㄩ噸娴佺▼,鍙互瀵规潈闄愬灞傛鏍¢獙,瀵圭郴缁熻繍琛岄�熷害鍙互杈冨皯鑰冭檻.
B/S 瀵瑰畨鍏ㄤ互鍙婅闂�熷害鐨勫閲嶇殑鑰冭檻,寤虹珛鍦ㄩ渶瑕佹洿鍔犱紭鍖栫殑鍩虹涔嬩笂.姣擟/S鏈夋洿楂樼殑瑕佹眰 B/S缁撴瀯鐨勭▼搴忔灦鏋勬槸鍙戝睍鐨勮秼鍔�,浠嶮S鐨�.Net绯诲垪鐨凚izTalk 2000Exchange 2000绛�,鍏ㄩ潰鏀寔缃戠粶鐨勬瀯浠舵惌寤虹殑绯荤粺. SUN鍜孖BM鎺ㄧ殑JavaBean鏋勪欢鎶�鏈瓑,浣� B/S鏇村姞鎴愮啛.
锛旓紟杞欢閲嶇敤涓嶅悓
C/S 绋嬪簭鍙互涓嶅彲閬垮厤鐨勬暣浣撴�ц�冭檻,鏋勪欢鐨勯噸鐢ㄦ�т笉濡傚湪B/S瑕佹眰涓嬬殑鏋勪欢鐨勯噸鐢ㄦ�уソ.
B/S 瀵圭殑澶氶噸缁撴瀯,瑕佹眰鏋勪欢鐩稿鐙珛鐨勫姛鑳�.鑳藉鐩稿杈冨ソ鐨勯噸鐢�.灏卞叆涔版潵鐨勯妗屽彲浠ュ啀鍒╃敤,鑰屼笉鏄仛鍦ㄥ涓婄殑鐭冲ご妗屽瓙
锛曪紟绯荤粺缁存姢涓嶅悓
C/S 绋嬪簭鐢变簬鏁翠綋鎬�,蹇呴』鏁翠綋鑰冨療,澶勭悊鍑虹幇鐨勯棶棰樹互鍙婄郴缁熷崌绾�.鍗囩骇闅�.鍙兘鏄啀鍋氫竴涓叏鏂扮殑绯荤粺
B/S 鏋勪欢缁勬垚,鏂归潰鏋勪欢涓埆鐨勬洿鎹�,瀹炵幇绯荤粺鐨勬棤缂濆崌绾�.绯荤粺缁存姢寮�閿�鍑忓埌鏈�灏�.鐢ㄦ埛浠庣綉涓婅嚜宸变笅杞藉畨瑁呭氨鍙互瀹炵幇鍗囩骇.
锛栵紟澶勭悊闂涓嶅悓
C/S 绋嬪簭鍙互澶勭悊鐢ㄦ埛闈㈠浐瀹�,骞朵笖鍦ㄧ浉鍚屽尯鍩�,瀹夊叏瑕佹眰楂橀渶姹�,涓庢搷浣滅郴缁熺浉鍏�.搴旇閮芥槸鐩稿悓鐨勭郴缁�
B/S 寤虹珛鍦ㄥ箍鍩熺綉涓�,闈㈠悜涓嶅悓鐨勭敤鎴风兢,鍒嗘暎鍦板煙,杩欐槸C/S鏃犳硶浣滃埌鐨�.涓庢搷浣滅郴缁熷钩鍙板叧绯绘渶灏�.
锛楋紟鐢ㄦ埛鎺ュ彛涓嶅悓
C/S 澶氭槸寤虹珛鐨刉indow骞冲彴涓�,琛ㄧ幇鏂规硶鏈夐檺,瀵圭▼搴忓憳鏅亶瑕佹眰杈冮珮
B/S 寤虹珛鍦ㄦ祻瑙堝櫒涓�,鏈夋洿鍔犱赴瀵屽拰鐢熷姩鐨勮〃鐜版柟寮忎笌鐢ㄦ埛浜ゆ祦.骞朵笖澶ч儴鍒嗛毦搴﹀噺浣�,鍑忎綆寮�鍙戞垚鏈�.
锛橈紟淇℃伅娴佷笉鍚�
C/S 绋嬪簭涓�鑸槸鍏稿瀷鐨勪腑澶泦鏉冪殑鏈烘寮忓鐞�,浜や簰鎬х浉瀵逛綆
B/S 淇℃伅娴佸悜鍙彉鍖�, B-B B-C B-G绛変俊鎭�佹祦鍚戠殑鍙樺寲,鏇村儚浜ゆ槗涓績銆�
2銆佸簲鐢ㄦ湇鍔″櫒涓嶹EB SERVER鐨勫尯鍒紵
搴旂敤鏈嶅姟鍣細Weblogic銆乀omcat銆丣boss
WEB SERVER锛欼IS銆� Apache
3銆佸簲鐢ㄦ湇鍔″櫒鏈夐偅浜涳紵
BEA WebLogic Server锛孖BM WebSphere Application Server锛孫racle9i ApplicationServer锛宩Boss锛孴omcat
4銆丣2EE鏄粈涔堬紵
绛�:Je22鏄疭un鍏徃鎻愬嚭鐨勫灞�(multi-diered),鍒嗗竷寮�(distributed),鍩轰簬缁勪欢(component-base)鐨勪紒涓氱骇搴旂敤妯″瀷(enterpriese applicationmodel).鍦ㄨ繖鏍风殑涓�涓簲鐢ㄧ郴缁熶腑锛屽彲鎸夌収鍔熻兘鍒掑垎涓轰笉鍚岀殑缁勪欢锛岃繖浜涚粍浠跺張鍙湪涓嶅悓璁$畻鏈轰笂锛屽苟涓斿浜庣浉搴旂殑灞傛(tier)涓�傛墍灞炲眰娆″寘鎷鎴峰眰(clietn tier)缁勪欢,web灞傚拰缁勪欢,Business灞傚拰缁勪欢,浼佷笟淇℃伅绯荤粺(EIS)灞傘��
涓�涓彟绫荤殑鍥炵瓟锛歫2ee灏辨槸澧炲垹鏀规煡銆�
5銆丣2EE鏄妧鏈繕鏄钩鍙拌繕鏄鏋讹紵浠�涔堟槸J2EE
J2EE鏈韩鏄竴涓爣鍑嗭紝涓�涓负浼佷笟鍒嗗竷寮忓簲鐢ㄧ殑寮�鍙戞彁渚涚殑鏍囧噯骞冲彴銆�
J2EE涔熸槸涓�涓鏋讹紝鍖呮嫭JDBC銆丣NDI銆丷MI銆丣MS銆丒JB銆丣TA绛夋妧鏈��
6銆佽瀵逛互涓嬪湪J2EE涓父鐢ㄧ殑鍚嶈瘝杩涜瑙i噴(鎴栫畝鍗曟弿杩�)
web瀹瑰櫒锛氱粰澶勪簬鍏朵腑鐨勫簲鐢ㄧ▼搴忕粍浠讹紙JSP锛孲ERVLET锛夋彁渚涗竴涓幆澧冿紝浣縅SP,SERVLET鐩存帴鏇村鍣ㄤ腑鐨勭幆澧冨彉閲忔帴鍙d氦浜掞紝涓嶅繀鍏虫敞鍏跺畠绯荤粺闂銆備富瑕佹湁WEB鏈嶅姟鍣ㄦ潵瀹炵幇銆備緥濡傦細TOMCAT,WEBLOGIC,WEBSPHERE绛夈�傝瀹瑰櫒鎻愪緵鐨勬帴鍙d弗鏍奸伒瀹圝2EE瑙勮寖涓殑WEB APPLICATION 鏍囧噯銆傛垜浠妸閬靛畧浠ヤ笂鏍囧噯鐨刉EB鏈嶅姟鍣ㄥ氨鍙仛J2EE涓殑WEB瀹瑰櫒銆�
EJB瀹瑰櫒锛欵nterprise java bean瀹瑰櫒銆傛洿鍏锋湁琛屼笟棰嗗煙鐗硅壊銆備粬鎻愪緵缁欒繍琛屽湪鍏朵腑鐨勭粍浠禘JB鍚勭绠$悊鍔熻兘銆傚彧瑕佹弧瓒矹2EE瑙勮寖鐨凟JB鏀惧叆璇ュ鍣紝椹笂灏变細琚鍣ㄨ繘琛岄珮鏁堢巼鐨勭鐞嗐�傚苟涓斿彲浠ラ�氳繃鐜版垚鐨勬帴鍙f潵鑾峰緱绯荤粺绾у埆鐨勬湇鍔°�備緥濡傞偖浠舵湇鍔°�佷簨鍔$鐞嗐��
JNDI锛氾紙Java Naming & Directory Interface锛塉AVA鍛藉悕鐩綍鏈嶅姟銆備富瑕佹彁渚涚殑鍔熻兘鏄細鎻愪緵涓�涓洰褰曠郴缁燂紝璁╁叾瀹冨悇鍦扮殑搴旂敤绋嬪簭鍦ㄥ叾涓婇潰鐣欎笅鑷繁鐨勭储寮曪紝浠庤�屾弧瓒冲揩閫熸煡鎵惧拰瀹氫綅鍒嗗竷寮忓簲鐢ㄧ▼搴忕殑鍔熻兘銆�
JMS锛氾紙Java Message Service锛塉AVA娑堟伅鏈嶅姟銆備富瑕佸疄鐜板悇涓簲鐢ㄧ▼搴忎箣闂寸殑閫氳銆傚寘鎷偣瀵圭偣鍜屽箍鎾��
JTA锛氾紙Java Transaction API锛塉AVA浜嬪姟鏈嶅姟銆傛彁渚涘悇绉嶅垎甯冨紡浜嬪姟鏈嶅姟銆傚簲鐢ㄧ▼搴忓彧闇�璋冪敤鍏舵彁渚涚殑鎺ュ彛鍗冲彲銆�
JAF锛氾紙Java Action FrameWork锛塉AVA瀹夊叏璁よ瘉妗嗘灦銆傛彁渚涗竴浜涘畨鍏ㄦ帶鍒舵柟闈㈢殑妗嗘灦銆傝寮�鍙戣�呴�氳繃鍚勭閮ㄧ讲鍜岃嚜瀹氫箟瀹炵幇鑷繁鐨勪釜鎬у畨鍏ㄦ帶鍒剁瓥鐣ャ��
RMI/IIOP:锛圧emote Method Invocation /internet瀵硅薄璇锋眰涓粙鍗忚锛変粬浠富瑕佺敤浜庨�氳繃杩滅▼璋冪敤鏈嶅姟銆備緥濡傦紝杩滅▼鏈変竴鍙拌绠楁満涓婅繍琛屼竴涓▼搴忥紝瀹冩彁渚涜偂绁ㄥ垎鏋愭湇鍔★紝鎴戜滑鍙互鍦ㄦ湰鍦拌绠楁満涓婂疄鐜板鍏剁洿鎺ヨ皟鐢ㄣ�傚綋鐒惰繖鏄閫氳繃涓�瀹氱殑瑙勮寖鎵嶈兘鍦ㄥ紓鏋勭殑绯荤粺涔嬮棿杩涜閫氫俊銆俁MI鏄疛AVA鐗规湁鐨勩��
7銆佸浣曠粰weblogic鎸囧畾澶у皬鐨勫唴瀛�?
锛堣繖涓棶棰樹笉浣滃叿浣撳洖绛旓紝鍒楀嚭鏉ュ彧鏄憡璇夎鑰呭彲鑳戒細閬囧埌浠�涔堥棶棰橈紝浣犱笉闇�瑕侀潰闈勘鍒帮紝浠�涔堥兘绮鹃�氥�傦級
鍦ㄥ惎鍔╓eblogic鐨勮剼鏈腑锛堜綅浜庢墍鍦―omian瀵瑰簲鏈嶅姟鍣ㄧ洰褰曚笅鐨剆tartServerName锛夛紝澧炲姞set MEM_ARGS=-Xms32m-Xmx200m锛屽彲浠ヨ皟鏁存渶灏忓唴瀛樹负32M锛屾渶澶�200M
8銆佸浣曡瀹氱殑weblogic鐨勭儹鍚姩妯″紡(寮�鍙戞ā寮�)涓庝骇鍝佸彂甯冩ā寮�?
鍙互鍦ㄧ鐞嗘帶鍒跺彴涓慨鏀瑰搴旀湇鍔″櫒鐨勫惎鍔ㄦā寮忎负寮�鍙戞垨浜у搧妯″紡涔嬩竴銆傛垨鑰呬慨鏀规湇鍔$殑鍚姩鏂囦欢鎴栬�卌ommenv鏂囦欢锛屽鍔爏etPRODUCTION_MODE=true銆�
9銆佸浣曞惎鍔ㄦ椂涓嶉渶杈撳叆鐢ㄦ埛鍚嶄笌瀵嗙爜?
淇敼鏈嶅姟鍚姩鏂囦欢锛屽鍔� WLS_USER鍜學LS_PW椤广�備篃鍙互鍦╞oot.properties鏂囦欢涓鍔犲姞瀵嗚繃鐨勭敤鎴峰悕鍜屽瘑鐮�.
10銆佸湪weblogic绠$悊鍒跺彴涓涓�涓簲鐢ㄥ煙(鎴栬�呰鏄竴涓綉绔�,Domain)杩涜jms鍙奺jb鎴栬繛鎺ユ睜绛夌浉鍏充俊鎭繘琛岄厤缃悗,瀹為檯淇濆瓨鍦ㄤ粈涔堟枃浠朵腑?
淇濆瓨鍦ㄦDomain鐨刢onfig.xml鏂囦欢涓紝瀹冩槸鏈嶅姟鍣ㄧ殑鏍稿績閰嶇疆鏂囦欢銆�
11銆佽璇磜eblogic涓竴涓狣omain鐨勭己鐪佺洰褰曠粨鏋�?姣斿瑕佸皢涓�涓畝鍗曠殑helloWorld.jsp鏀惧叆浣曠洰褰曚笅,鐒剁殑鍦ㄦ祻瑙堝櫒涓婂氨鍙墦鍏ttp://涓绘満:绔彛鍙�//helloword.jsp灏卞彲浠ョ湅鍒拌繍琛岀粨鏋滀簡?鍙堟瘮濡傝繖鍏朵腑鐢ㄥ埌浜嗕竴涓嚜宸卞啓鐨刯avaBean璇ュ浣曞姙?
Domain鐩綍鏈嶅姟鍣ㄧ洰褰昦pplications锛屽皢搴旂敤鐩綍鏀惧湪姝ょ洰褰曚笅灏嗗彲浠ヤ綔涓哄簲鐢ㄨ闂紝濡傛灉鏄疻eb搴旂敤锛屽簲鐢ㄧ洰褰曢渶瑕佹弧瓒砏eb搴旂敤鐩綍瑕佹眰锛宩sp鏂囦欢鍙互鐩存帴鏀惧湪搴旂敤鐩綍涓紝Javabean闇�瑕佹斁鍦ㄥ簲鐢ㄧ洰褰曠殑WEB-INF鐩綍鐨刢lasses鐩綍涓紝璁剧疆鏈嶅姟鍣ㄧ殑缂虹渷搴旂敤灏嗗彲浠ュ疄鐜板湪娴忚鍣ㄤ笂鏃犻渶杈撳叆搴旂敤鍚嶃��
12銆佸湪weblogic涓彂甯僥jb闇�娑夊強鍒板摢浜涢厤缃枃浠�
涓嶅悓绫诲瀷鐨凟JB娑夊強鐨勯厤缃枃浠朵笉鍚岋紝閮芥秹鍙婂埌鐨勯厤缃枃浠跺寘鎷琫jb-jar.xml,weblogic-ejb-jar.xmlCMP瀹炰綋Bean涓�鑸繕闇�瑕亀eblogic-cmp-rdbms-jar.xml
13銆佸浣曞湪weblogic涓繘琛宻sl閰嶇疆涓庡鎴风鐨勮璇侀厤缃垨璇磋j2ee(鏍囧噯)杩涜ssl鐨勯厤缃�?
缂虹渷瀹夎涓娇鐢―emoIdentity.jks鍜孌emoTrust.jksKeyStore瀹炵幇SSL锛岄渶瑕侀厤缃湇鍔″櫒浣跨敤Enable SSL锛岄厤缃叾绔彛锛屽湪浜у搧妯″紡涓嬮渶瑕佷粠CA鑾峰彇绉佹湁瀵嗛挜鍜屾暟瀛楄瘉涔︼紝鍒涘缓identity鍜宼rust keystore锛岃杞借幏寰楃殑瀵嗛挜鍜屾暟瀛楄瘉涔︺�傚彲浠ラ厤缃SSL杩炴帴鏄崟鍚戣繕鏄弻鍚戠殑銆�
14銆佸浣曟煡鐪嬪湪weblogic涓凡缁忓彂甯冪殑EJB?
鍙互浣跨敤绠$悊鎺у埗鍙帮紝鍦ㄥ畠鐨凞eployment涓彲浠ユ煡鐪嬫墍鏈夊凡鍙戝竷鐨凟JB
鍗�. EBJ閮ㄥ垎
1銆丒JB鏄熀浜庡摢浜涙妧鏈疄鐜扮殑锛熷苟璇村嚭SessionBean鍜孍ntityBean鐨勫尯鍒紝StatefulBean鍜孲tatelessBean鐨勫尯鍒��
EJB鍖呮嫭Session Bean銆丒ntity Bean銆丮essage Driven Bean锛屽熀浜嶫NDI銆丷MI銆丣AT绛夋妧鏈疄鐜般��
SessionBean鍦↗2EE搴旂敤绋嬪簭涓鐢ㄦ潵瀹屾垚涓�浜涙湇鍔″櫒绔殑涓氬姟鎿嶄綔锛屼緥濡傝闂暟鎹簱銆佽皟鐢ㄥ叾浠朎JB缁勪欢銆侲ntityBean琚敤鏉ヤ唬琛ㄥ簲鐢ㄧ郴缁熶腑鐢ㄥ埌鐨勬暟鎹��
瀵逛簬瀹㈡埛鏈猴紝SessionBean鏄竴绉嶉潪鎸佷箙鎬у璞★紝瀹冨疄鐜版煇浜涘湪鏈嶅姟鍣ㄤ笂杩愯鐨勪笟鍔¢�昏緫銆�
瀵逛簬瀹㈡埛鏈猴紝EntityBean鏄竴绉嶆寔涔呮�у璞★紝瀹冧唬琛ㄤ竴涓瓨鍌ㄥ湪鎸佷箙鎬у瓨鍌ㄥ櫒涓殑瀹炰綋鐨勫璞¤鍥撅紝鎴栨槸涓�涓敱鐜版湁浼佷笟搴旂敤绋嬪簭瀹炵幇鐨勫疄浣撱��
Session Bean 杩樺彲浠ュ啀缁嗗垎涓� Stateful Session Bean涓� Stateless SessionBean锛岃繖涓ょ鐨� Session Bean閮藉彲浠ュ皢绯荤粺閫昏緫鏀惧湪 method涔嬩腑鎵ц锛屼笉鍚岀殑鏄� Stateful SessionBean鍙互璁板綍鍛煎彨鑰呯殑鐘舵�侊紝鍥犳閫氬父鏉ヨ锛屼竴涓娇鐢ㄨ�呬細鏈変竴涓浉瀵瑰簲鐨� Stateful SessionBean鐨勫疄浣撱�係tateless SessionBean铏界劧涔熸槸閫昏緫缁勪欢锛屼絾鏄粬鍗翠笉璐熻矗璁板綍浣跨敤鑰呯姸鎬侊紝涔熷氨鏄褰撲娇鐢ㄨ�呭懠鍙� Stateless SessionBean鐨勬椂鍊欙紝EJB Container骞朵笉浼氭壘瀵荤壒瀹氱殑 Stateless SessionBean鐨勫疄浣撴潵鎵ц杩欎釜 method銆傛崲瑷�涔嬶紝寰堝彲鑳芥暟涓娇鐢ㄨ�呭湪鎵ц鏌愪釜 Stateless SessionBean鐨� methods鏃讹紝浼氭槸鍚屼竴涓� Bean鐨� Instance鍦ㄦ墽琛屻�備粠鍐呭瓨鏂归潰鏉ョ湅锛� Stateful SessionBean涓嶴tateless SessionBean姣旇緝锛� Stateful SessionBean浼氭秷鑰� J2EE Server杈冨鐨勫唴瀛橈紝鐒惰�� Stateful SessionBean鐨勪紭鍔垮嵈鍦ㄤ簬浠栧彲浠ョ淮鎸佷娇鐢ㄨ�呯殑鐘舵�併��
2銆佺畝瑕佽涓�涓� EJB鐨� 7涓� Transaction Level?
3銆丒JB涓嶫AVA BEAN鐨勫尯鍒紵
Java Bean 鏄彲澶嶇敤鐨勭粍浠讹紝瀵笿ava Bean骞舵病鏈変弗鏍肩殑瑙勮寖锛岀悊璁轰笂璁诧紝浠讳綍涓�涓狫ava绫婚兘鍙互鏄竴涓狟ean銆備絾閫氬父鎯呭喌涓嬶紝鐢变簬Java Bean鏄瀹瑰櫒鎵�鍒涘缓锛堝Tomcat锛夌殑锛屾墍浠ava Bean搴斿叿鏈変竴涓棤鍙傜殑鏋勯�犲櫒锛屽彟澶栵紝閫氬父Java Bean杩樿瀹炵幇Serializable鎺ュ彛鐢ㄤ簬瀹炵幇Bean鐨勬寔涔呮�с�侸ava Bean瀹為檯涓婄浉褰撲簬寰蒋COM妯″瀷涓殑鏈湴杩涚▼鍐匔OM缁勪欢锛屽畠鏄笉鑳借璺ㄨ繘绋嬭闂殑銆侲nterprise Java Bean鐩稿綋浜嶥COM锛屽嵆鍒嗗竷寮忕粍浠躲�傚畠鏄熀浜嶫ava鐨勮繙绋嬫柟娉曡皟鐢紙RMI锛夋妧鏈殑锛屾墍浠JB鍙互琚繙绋嬭闂紙璺ㄨ繘绋嬨�佽法璁$畻鏈猴級銆備絾EJB蹇呴』琚竷缃插湪璇稿Webspere銆乄ebLogic杩欐牱鐨勫鍣ㄤ腑锛孍JB瀹㈡埛浠庝笉鐩存帴璁块棶鐪熸鐨凟JB缁勪欢锛岃�屾槸閫氳繃鍏跺鍣ㄨ闂�侲JB瀹瑰櫒鏄疎JB缁勪欢鐨勪唬鐞嗭紝EJB缁勪欢鐢卞鍣ㄦ墍鍒涘缓鍜岀鐞嗐�傚鎴烽�氳繃瀹瑰櫒鏉ヨ闂湡姝g殑EJB缁勪欢銆�
4銆丒JB鍖呮嫭锛圫essionBean,EntityBean锛夎鍑轰粬浠殑鐢熷懡鍛ㄦ湡锛屽強濡備綍绠$悊浜嬪姟鐨勶紵
SessionBean锛歋tateless Session Bean鐨勭敓鍛藉懆鏈熸槸鐢卞鍣ㄥ喅瀹氱殑锛屽綋瀹㈡埛鏈哄彂鍑鸿姹傝寤虹珛涓�涓狟ean鐨勫疄渚嬫椂锛孍JB瀹瑰櫒涓嶄竴瀹氳鍒涘缓涓�涓柊鐨凚ean鐨勫疄渚嬩緵瀹㈡埛鏈鸿皟鐢紝鑰屾槸闅忎究鎵句竴涓幇鏈夌殑瀹炰緥鎻愪緵缁欏鎴锋満銆傚綋瀹㈡埛鏈虹涓�娆¤皟鐢ㄤ竴涓猄tateful SessionBean 鏃讹紝瀹瑰櫒蹇呴』绔嬪嵆鍦ㄦ湇鍔″櫒涓垱寤轰竴涓柊鐨凚ean瀹炰緥锛屽苟鍏宠仈鍒板鎴锋満涓婏紝浠ュ悗姝ゅ鎴锋満璋冪敤Stateful SessionBean鐨勬柟娉曟椂瀹瑰櫒浼氭妸璋冪敤鍒嗘淳鍒颁笌姝ゅ鎴锋満鐩稿叧鑱旂殑Bean瀹炰緥銆�
EntityBean锛欵ntity Beans鑳藉瓨娲荤浉瀵硅緝闀跨殑鏃堕棿锛屽苟涓旂姸鎬佹槸鎸佺画鐨勩�傚彧瑕佹暟鎹簱涓殑鏁版嵁瀛樺湪锛孍ntity beans灏变竴鐩村瓨娲汇�傝�屼笉鏄寜鐓у簲鐢ㄧ▼搴忔垨鑰呮湇鍔¤繘绋嬫潵璇寸殑銆傚嵆浣縀JB瀹瑰櫒宕╂簝浜嗭紝Entity beans涔熸槸瀛樻椿鐨勩�侲ntity Beans鐢熷懡鍛ㄦ湡鑳藉琚鍣ㄦ垨鑰� Beans鑷繁绠$悊銆�
EJB閫氳繃浠ヤ笅鎶�鏈鐞嗗疄鍔★細瀵硅薄绠$悊缁勭粐锛圤MG锛夌殑瀵硅薄瀹炲姟鏈嶅姟锛圤TS锛夛紝Sun Microsystems鐨凾ransaction Service锛圝TS锛夈�丣ava Transaction API锛圝TA锛夛紝寮�鍙戠粍锛圶/Open锛夌殑XA鎺ュ彛銆�
5銆丒JB瀹瑰櫒鎻愪緵鐨勬湇鍔�
涓昏鎻愪緵澹版槑鍛ㄦ湡绠$悊銆佷唬鐮佷骇鐢熴�佹寔缁�х鐞嗐�佸畨鍏ㄣ�佷簨鍔$鐞嗐�侀攣鍜屽苟鍙戣绠$悊绛夋湇鍔°��
6銆丒JB鐨勬縺娲绘満鍒�
浠tateful Session Bean涓轰緥锛氬叾Cache澶у皬鍐冲畾浜嗗唴瀛樹腑鍙互鍚屾椂瀛樺湪鐨凚ean瀹炰緥鐨勬暟閲忥紝鏍规嵁MRU鎴朜RU绠楁硶锛屽疄渚嬪湪婵�娲诲拰鍘绘縺娲荤姸鎬佷箣闂磋縼绉伙紝婵�娲绘満鍒舵槸褰撳鎴风璋冪敤鏌愪釜EJB瀹炰緥涓氬姟鏂规硶鏃讹紝濡傛灉瀵瑰簲EJB Object鍙戠幇鑷繁娌℃湁缁戝畾瀵瑰簲鐨凚ean瀹炰緥鍒欎粠鍏跺幓婵�娲籅ean瀛樺偍涓紙閫氳繃搴忓垪鍖栨満鍒跺瓨鍌ㄥ疄渚嬶級鍥炲锛堟縺娲伙級姝ゅ疄渚嬨�傜姸鎬佸彉杩佸墠浼氳皟鐢ㄥ搴旂殑ejbActive鍜宔jbPassivate鏂规硶銆�
7銆丒JB鐨勫嚑绉嶇被鍨�
浼氳瘽锛圫ession锛塀ean锛屽疄浣擄紙Entity锛塀ean娑堟伅椹卞姩鐨勶紙Message Driven锛塀ean
浼氳瘽Bean鍙堝彲鍒嗕负鏈夌姸鎬侊紙Stateful锛夊拰鏃犵姸鎬侊紙Stateless锛変袱绉�
瀹炰綋Bean鍙垎涓築ean绠$悊鐨勬寔缁�э紙BMP锛夊拰瀹瑰櫒绠$悊鐨勬寔缁�э紙CMP锛変袱绉�
8銆佸鏈嶇璋冪敤EJB瀵硅薄鐨勫嚑涓熀鏈楠�
璁剧疆JNDI鏈嶅姟宸ュ巶浠ュ強JNDI鏈嶅姟鍦板潃绯荤粺灞炴�э紝鏌ユ壘Home鎺ュ彛锛屼粠Home鎺ュ彛璋冪敤Create鏂规硶鍒涘缓Remote鎺ュ彛锛岄�氳繃Remote鎺ュ彛璋冪敤鍏朵笟鍔℃柟娉曘��
鍗佷竴. webservice閮ㄥ垎
1銆乄EB SERVICE鍚嶈瘝瑙i噴銆侸SWDL寮�鍙戝寘鐨勪粙缁嶃�侸AXP銆丣AXM鐨勮В閲娿�係OAP銆乁DDI,WSDL瑙i噴銆�
Web ServiceWeb Service鏄熀浜庣綉缁滅殑銆佸垎甯冨紡鐨勬ā鍧楀寲缁勪欢锛屽畠鎵ц鐗瑰畾鐨勪换鍔★紝閬靛畧鍏蜂綋鐨勬妧鏈鑼冿紝杩欎簺瑙勮寖浣垮緱Web Service鑳戒笌鍏朵粬鍏煎鐨勭粍浠惰繘琛屼簰鎿嶄綔銆�
JAXP(Java API for XML Parsing) 瀹氫箟浜嗗湪Java涓娇鐢―OM, SAX, XSLT鐨勯�氱敤鐨勬帴鍙c�傝繖鏍峰湪浣犵殑绋嬪簭涓綘鍙浣跨敤杩欎簺閫氱敤鐨勬帴鍙o紝褰撲綘闇�瑕佹敼鍙樺叿浣撶殑瀹炵幇鏃跺�欎篃涓嶉渶瑕佷慨鏀逛唬鐮併��
JAXM(Java API for XML Messaging) 鏄负SOAP閫氫俊鎻愪緵璁块棶鏂规硶鍜屼紶杈撴満鍒剁殑API銆�
WSDL鏄竴绉� XML鏍煎紡锛岀敤浜庡皢缃戠粶鏈嶅姟鎻忚堪涓轰竴缁勭鐐癸紝杩欎簺绔偣瀵瑰寘鍚潰鍚戞枃妗d俊鎭垨闈㈠悜杩囩▼淇℃伅鐨勬秷鎭繘琛屾搷浣溿�傝繖绉嶆牸寮忛鍏堝鎿嶄綔鍜屾秷鎭繘琛屾娊璞℃弿杩帮紝鐒跺悗灏嗗叾缁戝畾鍒板叿浣撶殑缃戠粶鍗忚鍜屾秷鎭牸寮忎笂浠ュ畾涔夌鐐广�傜浉鍏崇殑鍏蜂綋绔偣鍗崇粍鍚堟垚涓烘娊璞$鐐癸紙鏈嶅姟锛夈��
SOAP鍗崇畝鍗曞璞¤闂崗璁�(Simple Object Access Protocol)锛屽畠鏄敤浜庝氦鎹ML缂栫爜淇℃伅鐨勮交閲忕骇鍗忚銆�
UDDI 鐨勭洰鐨勬槸涓虹數瀛愬晢鍔″缓绔嬫爣鍑嗭紱UDDI鏄竴濂楀熀浜嶹eb鐨勩�佸垎甯冨紡鐨勩�佷负Web Service鎻愪緵鐨勩�佷俊鎭敞鍐屼腑蹇冪殑瀹炵幇鏍囧噯瑙勮寖锛屽悓鏃朵篃鍖呭惈涓�缁勪娇浼佷笟鑳藉皢鑷韩鎻愪緵鐨刉eb Service娉ㄥ唽锛屼互浣垮埆鐨勪紒涓氳兘澶熷彂鐜扮殑璁块棶鍗忚鐨勫疄鐜版爣鍑嗐��
2銆丆ORBA鏄粈涔�?鐢ㄩ�旀槸浠�涔�?
CORBA 鏍囧噯鏄叕鍏卞璞¤姹備唬鐞嗙粨鏋�(Common Object Request Broker Architecture)锛岀敱瀵硅薄绠$悊缁勭粐 (Object ManagementGroup锛岀缉鍐欎负 OMG)鏍囧噯鍖栥�傚畠鐨勭粍鎴愭槸鎺ュ彛瀹氫箟璇█(IDL),璇█缁戝畾(binding:涔熻瘧涓鸿仈缂�)鍜屽厑璁稿簲鐢ㄧ▼搴忛棿浜掓搷浣滅殑鍗忚銆傚叾鐩殑涓猴細鐢ㄤ笉鍚岀殑绋嬪簭璁捐璇█涔﹀啓鍦ㄤ笉鍚岀殑杩涚▼涓繍琛岋紝涓轰笉鍚岀殑鎿嶄綔绯荤粺寮�鍙戙��
3. Linux
4銆丩INUX涓嬬嚎绋嬶紝GDI绫荤殑瑙i噴銆�
LINUX瀹炵幇鐨勫氨鏄熀浜庢牳蹇冭交閲忕骇杩涚▼鐨�"涓�瀵逛竴"绾跨▼妯″瀷锛屼竴涓嚎绋嬪疄浣撳搴斾竴涓牳蹇冭交閲忕骇杩涚▼锛岃�岀嚎绋嬩箣闂寸殑绠$悊鍦ㄦ牳澶栧嚱鏁板簱涓疄鐜般��
GDI绫讳负鍥惧儚璁惧缂栫▼鎺ュ彛绫诲簱銆�
5. 闂緱绋�閲岀硦娑傜殑棰�
6銆佸洓绉嶄細璇濊窡韪妧鏈�
浼氳瘽浣滅敤鍩烻ervletsJSP椤甸潰鎻忚堪
page鍚︽槸浠h〃涓庝竴涓〉闈㈢浉鍏崇殑瀵硅薄鍜屽睘鎬с�備竴涓〉闈㈢敱涓�涓紪璇戝ソ鐨� Java servlet绫伙紙鍙互甯︽湁浠讳綍鐨刬nclude鎸囦护锛屼絾鏄病鏈� include鍔ㄤ綔锛夎〃绀恒�傝繖鏃㈠寘鎷� servlet鍙堝寘鎷缂栬瘧鎴� servlet鐨� JSP椤甸潰
request鏄槸浠h〃涓� Web瀹㈡埛鏈哄彂鍑虹殑涓�涓姹傜浉鍏崇殑瀵硅薄鍜屽睘鎬с�備竴涓姹傚彲鑳借法瓒婂涓〉闈紝娑夊強澶氫釜Web缁勪欢锛堢敱浜� forward鎸囦护鍜� include鍔ㄤ綔鐨勫叧绯伙級
session鏄槸浠h〃涓庣敤浜庢煇涓� Web瀹㈡埛鏈虹殑涓�涓敤鎴蜂綋楠岀浉鍏崇殑瀵硅薄鍜屽睘鎬с�備竴涓� Web浼氳瘽鍙互涔熺粡甯镐細璺ㄨ秺澶氫釜瀹㈡埛鏈鸿姹�
application鏄槸浠h〃涓庢暣涓� Web搴旂敤绋嬪簭鐩稿叧鐨勫璞″拰灞炴�с�傝繖瀹炶川涓婃槸璺ㄨ秺鏁翠釜 Web搴旂敤绋嬪簭锛屽寘鎷涓〉闈€�佽姹傚拰浼氳瘽鐨勪竴涓叏灞�浣滅敤鍩�
7銆佺畝杩伴�昏緫鎿嶄綔(&,|,^)涓庢潯浠舵搷浣�(&&,||)鐨勫尯鍒��
鍖哄埆涓昏绛斾袱鐐癸細a.鏉′欢鎿嶄綔鍙兘鎿嶄綔甯冨皵鍨嬬殑,鑰岄�昏緫鎿嶄綔涓嶄粎鍙互鎿嶄綔甯冨皵鍨�,鑰屼笖鍙互鎿嶄綔鏁板�煎瀷
b.閫昏緫鎿嶄綔涓嶄細浜х敓鐭矾
鍗佷簩.鍏朵粬
1銆佽鐢ㄨ嫳鏂囩畝鍗曚粙缁嶄竴涓嬭嚜宸�.
4銆乄EB SERVICE鍚嶈瘝瑙i噴銆侸SWDL寮�鍙戝寘鐨勪粙缁嶃�侸AXP銆丣AXM鐨勮В閲娿�係OAP銆乁DDI,WSDL瑙i噴銆�
2銆佽鎶�http://tomcat.apache.org/棣栭〉鐨勮繖涓�娈佃瘽鐢ㄤ腑鏂囩炕璇戜竴涓�?
Apache Tomcat is the servlet container that is used in the officialReference Implementation for theJava ServletandJavaServerPagestechnologies. The Java Servlet and JavaServer Pagesspecifications are developed by Sun under theJavaCommunity Process.
Apache Tomcat is developed in an open andparticipatory environment and released under theApacheSoftware License. Apache Tomcat is intended to be a collaboration ofthe best-of-breed developers from around the world. We invite you toparticipate in this open development project. To learn more about gettinginvolved,click here.
ApacheTomcat powers numerous large-scale, mission-critical web applications across adiverse range of industries and organizations. Some of these users and theirstories are listed on thePoweredBywiki page.
3銆�缇庤祫杞欢鍏徃JAVA宸ョ▼甯堢數璇濋潰璇曢鐩�
1. Talk about overriding, overloading.
2. Talk about JAVA design patterns you known.
3. Talk about the difference between LinkList, ArrayList and Victor.
4. Talk about the difference between an Abstract class and an Interface.
5. Class a = new Class(); Class b = new Class();
if(a == b) returns true or false, why?
6. Why we use StringBuffer when concatenating strings?
7. Try to explain Singleton to us? Is it thread safe? If no, how to make itthread safe?
8. Try to explain Ioc?
9. How to set many-to-many relationship in Hibernate?
10. Talk about the difference between INNER JOIN and LFET JOIN.
11. Why we use index in database? How many indexes is the maximum in one tableas your suggestion?
12. When 鈥楩inal鈥� is used in class, method and property, what dose it mean?
13. Do you have any experience on XML? Talk about any XML tool youused ,e.g. JAXB, JAXG.
14. Do you have any experience on Linux?
15. In OOD what is the reason when you create a Sequence diagram?
Administrator 10:34:20
1锛屽爢鍜屾爤鐨勫尯鍒紝鏈変竴涓�64k鐨勫瓧绗︿覆锛屾槸鏀惧埌鍫嗕笂锛岃繕鏄斁鍒版爤涓婏紝涓轰粈涔堬紵
2锛屼粈涔堟椂鍊欑敤鍒版帴鍙o紝浠�涔堟椂鍊欑敤鍒版娊璞$被锛屼簩鑰呭尯鍒�
3锛屾湁涓�涓�100涓囩殑鏁扮粍锛岄噷杈规湁涓や釜甯傞噸澶嶇殑锛屽浣曡璁$畻娉曟壘鍒般��
4锛岃璁℃暟鎹簱鏃讹紝n缁达紝濡備綍璁捐銆�
渚嬪[鐪佷唤][鍩庡競][缃戝惂]锛岃繖鏄笁缁村叧绯伙紝瀹冪殑琛ㄤ篃搴旇鏈変笁涓紝缃戝惂鏈夊閿紩鐢ㄥ煄甯傦紝鍩庡競鏈夊閿簲鐢ㄧ渷浠斤紝杩欎釜瑙勫緥灏辨槸涓嬪眰鐨勮鏈変竴澶栭敭鍘诲紩鐢ㄤ笂灞傘��