Java核心-面向对象(上)
涓€銆佸璞?绫?/h2>
1銆佹蹇?/h3>
1.1 瀵硅薄(object)
1.1 瀵硅薄(object)
Java涓竴璧风殕瀵硅薄銆傞偅涔堜粈涔堟槸瀵硅薄锛熺畝鍗曠悊瑙o紝瀵硅薄鏄竴涓?strong>瀹炰緥(instance)锛屾湁鐘舵€?灞炴€?/strong>)鍜岃涓?鏂规硶)銆俥g锛?/p>
瀛︾敓鏄竴涓璞★紝鏈夊鍚嶃€佹€у埆銆佸勾榫勩€佸鍙枫€佹墍灞炵彮绾с€佹垚缁╃瓑灞炴€с€傚鐢熷彲浠ユ湁鍚冮キ銆佺潯瑙夈€佸涔犵瓑琛屼负锛岃繖浜涘嵆鏂规硶銆?/p>
鍛樺伐鏄竴涓璞★紝鏈夊鍚嶃€佹€у埆銆佸勾榫勩€佸伐鍙枫€佹墍灞為儴闂ㄣ€佺哗鏁堢瓑灞炴€с€傚憳宸ュ彲浠ユ湁鍚冮キ銆佺潯瑙夈€佹懜楸肩瓑琛屼负锛岃繖浜涘嵆鏂规硶銆?/p>
涓嶅彧鏄汉锛屽姩鐗╋紙馃悥锛岎煇憋紝馃悤...锛夛紝妞嶇墿锛堣姳銆佽崏銆佹爲...锛夐兘鏄璞°€傜畝瑷€涔嬶紝涓栭棿涓€鍒囩殕瀵硅薄銆?/p>
璇村畬瀵硅薄锛屽啀鏉ヨ绫汇€傝鍒扮被锛屽緢澶氫汉鍙兘浼氭兂鍒?鐗╀互绫昏仛锛屼汉浠ョ兢鍒?銆?strong>绫绘槸涓€涓ā鏉匡紝鎻忚堪浜嗕竴绫诲璞$殑琛屼负鍜岀姸鎬?/strong>銆傚緢濂界悊瑙o紝eg锛?/p>
浜烘槸涓€涓被锛屽叿浣撴瘡涓€涓汉濡傚皬鏄庛€佽€佺帇灏辨槸涓€涓璞°€傦紙杩欓噷浜哄氨鏄竴涓娊璞$殑姒傚康锛屽洜涓哄彲浠ュ啀缁嗗垎濡傜敺浜?濂充汉锛?/p>
鐙楁槸涓€涓被锛屽叿浣撳娌欑毊鐙椼€佸搱宸寸嫍灏辨槸涓€涓璞★紝铏界劧灞炰簬涓嶅悓鐨勫搧绉嶏紝浣嗕粬浠兘鏈夌嫍鐨勭壒寰佸拰琛屼负锛岄兘鏄嫍銆傦紙杩欓噷鐙楀氨鏄竴涓娊璞$殑姒傚康锛屽洜涓哄彲浠ュ啀缁嗗垎锛?/p>
鍙互鐪嬪埌锛岀被鍙互鍒掔殑姣旇緝瀹芥硾锛屼篃鍙互鍒掔殑缁嗕竴鐐癸紝鍏蜂綋杩樿鐪嬪疄闄呯殑涓氬姟闇€姹傘€傜畝瑷€涔嬶紝瀵硅薄鏄被鐨勫疄渚嬶紝绫绘槸瀵硅薄鐨勬娊璞?/strong>銆?/p>
1锛夎娉?/p>
pass锛氱被鍙湅鍋氭槸涓€绉嶈嚜瀹氫箟鐨勬暟鎹被鍨嬶紝鍙互浣跨敤绫绘潵瀹氫箟鍙橀噺銆?/p>
鍏充簬淇グ绗︿箣鍓嶄粙缁嶈繃锛屽畾涔夌被 public銆乸rotected銆乤bstract鍜?static鐢ㄧ殑杈冨锛屽悇鏈変笉鍚岀殑鍚箟鍜屽姛鑳姐€?/p>
鎴愬憳鍙橀噺(闈為潤鎬佸彉閲?/ 瀹炰緥鍙橀噺 / 鍏ㄥ眬鍙橀噺)锛氬疄渚嬪彉閲忓0鏄庡湪涓€涓被涓紝浣嗗湪鏂规硶銆佹瀯閫犳柟娉曞拰璇彞鍧椾箣澶栵紙绫讳腑鏂规硶澶栵級銆?/p>
鑷充簬鏋勯€犲櫒鍜屾柟娉曪紝鎺ヤ笅鏉ヤ細璁插埌銆?/p>
1锛夊垱寤哄璞?br>
鍒涘缓瀵硅薄鍗?strong>瀹炰緥鍖栦竴涓璞?/strong>銆備竴鑸€氳繃new鍏抽敭瀛楁潵鍒涘缓瀵硅薄銆傝繕鍙互閫氳繃鍙嶅皠鍜屽簭鍒楀寲鍒涘缓锛屽綋鐒讹紝杩欓兘鏄悗璇濅簡銆?/p>
2锛夎闂?淇敼 閫氳繃 瀵硅薄.灞炴€?鐩存帴璧嬪€肩殑鏂瑰紡淇敼灞炴€?/p>
3锛塭g 娴嬭瘯缁撴灉 pass锛欽ava涓殑娴嬭瘯绫?/strong>鏄敤鏉ユ槸鐢ㄦ潵瀵瑰凡缁忓啓濂界殑鍔熻兘缁勪欢杩涜娴嬭瘯銆備笂闈㈣繖绉嶉€氳繃鏂板缓涓€涓甫鏈塵ain鏂规硶鐨勭被锛屽埄鐢ㄨ绫绘潵璋冪敤闇€瑕佹祴璇曠殑绫伙紝骞舵妸闇€瑕佹祴璇曠殑鏁版嵁浼犲叆杩涘幓鐨勬祴璇曟柟娉曞湪鍓嶆湡瀛︿範鏄潪甯稿父鐢ㄧ殑锛屽悗鏈熷紑鍙戝垯閲囩敤鏇村姞楂樻晥渚挎嵎鐨?strong>JUnit鍗曞厓娴嬭瘯銆?/p>
浠ヤ笅閮ㄥ垎鍙兘鍒氬紑濮嬩笉鏄偅涔堝ソ鐞嗚В锛屾參鎱㈠氨鐔熸倝浜嗐€?/p>
1锛夐潤鎬?static 娉細琚?static淇グ鐨勬垚鍛樻柟娉曞拰鎴愬憳鍙橀噺绉颁负绫绘垚鍛橈紝鍏朵腑锛岃 static淇グ鐨勬垚鍛樺彉閲忕О涓虹被鍙橀噺锛岃 static淇グ鐨勬垚鍛樻柟娉曠О涓虹被鏂规硶銆?/p>
2锛夋湭琚?static淇グ 娉細鏈static淇グ鐨勬垚鍛樻柟娉曞拰鎴愬憳鍙橀噺绉颁负瀹炰緥鎴愬憳锛屽叾涓紝鏈static淇グ鐨勬垚鍛樺彉閲忓彨鍋氬疄渚嬪彉閲忥紝鏈static淇グ鐨勬垚鍛樻柟娉曞彨鍋氬疄渚嬫柟娉曘€?/p>
3锛夊叾浠栨敞鎰?/p>
灞€閮ㄥ彉閲忥紙绫荤殑鏂规硶涓€佷唬鐮佸潡涓殑鍙橀噺锛変笉灞炰簬浠讳綍绫绘垨瀹炰緥锛屼笉鑳戒娇鐢?static 淇グ锛涗笉鑳戒慨楗版瀯閫犲櫒銆?/p>
public鍙兘鐢卞璞¤闂?瀵硅薄.灞炴€鏂规硶)锛岃€?static 闈欐€佹柟娉曞彲浠ュ湪涓嶅垱寤虹被鐨勫璞$殑鎯呭喌涓嬭闂鏂规硶銆?/p>
static 淇グ鐨勬垚鍛橈紙灞炰簬绫伙級鍙婃瀯閫犲櫒涓嶈兘鐩存帴璁块棶娌℃湁 static 淇グ鐨勬垚鍛橈紙灞炰簬瀵硅薄锛?/p>
鐢变簬static 寮鸿皟鐨勬槸绫伙紝this 鍜?super 寮鸿皟鐨勬槸瀵硅薄锛屾晠 static 淇グ鐨勬柟娉曚腑涓嶈兘浣跨敤 this 鎴?super 寮曠敤 4锛?strong>浠g爜绀轰緥锛堥噸鐐癸級
1.2 绫?class)
2銆佹繁鍏?/h3>
2.1 绫荤殑瀹氫箟
[淇グ绗 class 绫诲悕 {
0-N 鎴愬憳鍙橀噺锛堝瓧娈碉級 // 鎻忚堪绫诲叿鏈夌殑鐗规€э紝瀵硅薄鐨勭姸鎬?
0-N 鏋勯€犲櫒
0-N 鏂规硶 // 鎻忚堪绫诲叿鏈夌殑鍔熻兘锛屽璞$殑琛屼负
}
2.2 瀵硅薄鍒涘缓銆佽闂?淇敼灞炴€?/h4>
绫诲悕 瀵硅薄鍚?= new 绫诲悕();
绫?瀵硅薄鐨勮闂垚鍛樺彉閲忔垨鏂规硶濡備笅
绫?绫绘柟娉?
绫?绫诲彉閲?
瀵硅薄鍚?鎴愬憳鏂规硶
瀵硅薄鍚?鎴愬憳鍙橀噺
public class Person { // 瀹氫箟涓€涓?Person 绫伙紝鏈夊鍚嶅拰骞撮緞涓や釜灞炴€?
String name; // 濮撳悕
int age; // 骞撮緞
public void eat(){
System.out.println("鍚冮キ");
}
public void sleep(){
System.out.println("鐫¤");
}
}
class PersonTest{ // 娴嬭瘯绫?
public static void main(String[] args) {
Person p=new Person(); // 鍒涘缓涓€涓璞?p
p.name="zhangsan"; // 璧嬪€?
p.age=20;
System.out.println(p.name+" "+p.age);
p.name="lishi"; // 閫氳繃瀵硅薄.灞炴€х洿鎺ヨ祴鍊肩殑鏂瑰紡淇敼灞炴€?
System.out.println(p.name+" "+p.age);
p.eat(); // 璋冪敤鏂规硶
p.sleep();
}
}
zhangsan 20
lishi 20
鍚冮キ
鐫¤
2.3 甯哥敤鍏抽敭瀛?/h4>
static 淇グ鐨勬垚鍛樼О涓?strong>绫绘垚鍛?/strong>锛堢被鍙橀噺銆佺被鏂规硶銆侀潤鎬佸垵濮嬪寲鍧椼€侀潤鎬佸唴閮ㄧ被锛夛紝鍏?strong>闅忕潃鎵€鍦ㄧ被鐨勫姞杞借€屽姞杞斤紝鍏堜簬璇ョ被瀵硅薄瀛樺湪
鐩稿弽锛屾湭琚?static淇グ鐨勬垚鍛樼О涓轰笌瀹炰緥鎴愬憳锛堝疄渚嬪彉閲忋€佸疄渚嬫柟娉曘€佹櫘閫氬垵濮嬪寲鍧椼€佸疄渚嬪唴閮ㄧ被锛?/p>
鎼炴竻妤氬涓嬩唬鐮佺ず渚嬶紙缁撳悎娉ㄩ噴锛夊氨鍩烘湰涓婅兘鐞嗚В涓婇潰鐨勮〃杈炬剰鎬濄€傝嚦浜庣 3锛夌偣娑夊強鐨勬瀯閫犲櫒銆乼his浼氬湪鎺ヤ笅鏉ヨ锛岃繖閲屾垜浠厛璁颁綇銆?/p>
public class Person { // 瀹氫箟涓€涓?Person 绫?
String name;
int age; // 瀹炰緥鍙橀噺锛堟垚鍛樺彉閲忥級
static String gender; // 绫诲彉閲?
public static void eat(){ // 绫绘柟娉?
System.out.println("鍚冮キ");
}
public void sleep(){ // 瀹炰緥鏂规硶锛堟垚鍛樻柟娉曪級
// static int i; // 閿欒锛屽眬閮ㄥ彉閲忎笉鑳戒娇鐢?static 淇グ
System.out.println("鐫¤");
}
}
class PersonTest{ // 娴嬭瘯绫?
public static void main(String[] args) {
// static淇グ鐨勬垚鍛樺彲浠ョ洿鎺ョ敤绫诲悕璋冪敤銆?
Person.gender="鐢?;
Person.eat(); // static 闈欐€佹柟娉曞彲浠ュ湪涓嶅垱寤虹被鐨勫璞℃儏鍐典笅璁块棶璇ユ柟娉?
Person p=new Person(); // 鍒涘缓涓€涓璞?p
p.name="zhangsan"; // public鍙兘鐢卞璞¤闂?瀵硅薄.灞炴€鏂规硶)
p.age=20;
p.sleep();
// static淇グ鐨勬垚鍛樿璇ョ被鎵€鏈夊璞℃墍鍏变韩锛屾晠涔熻兘鐢卞璞¤闂?瀵硅薄.灞炴€鏂规硶)
p.gender="濂?; // 閫氳繃瀵硅薄.灞炴€х洿鎺ヨ祴鍊肩殑鏂瑰紡淇敼灞炴€?
p.eat();
System.out.println(p.name+" "+Person.gender+" "+p.age);
}
}
娴嬭瘯缁撴灉锛堝缓璁嚜宸辨妸浠g爜璺戜竴閬嶏紝鍔犳繁鐞嗚В锛?/p>
鍚冮キ
鐫¤
鍚冮キ
zhangsan 濂?20
5锛夋湰绫?this
this 鏄?Java 甯哥敤鐨勫叧閿瓧锛屽彲鐢ㄤ簬浠讳綍瀹炰緥鏂规硶鍐呮寚鍚戝綋鍓嶅璞?/strong>銆傚湪鏂规硶鍐呴儴锛屽彲浠ヤ娇鐢ㄩ殣鍚殑鍙橀噺 this锛屽畠濮嬬粓鎸囧悜褰撳墠瀹炰緥銆傚苟閫氳繃 this.field 灏卞彲浠ヨ闂綋鍓嶅疄渚嬬殑瀛楁锛堝嵆灞炴€э級銆傝嫢鏃犲懡鍚嶅啿绐侊紝鍙互鐪佺暐 this銆俥g
public class Person { // 瀹氫箟涓€涓?Person 绫?
private String name;
public String getName() { // 瀹炰緥鏂规硶
return name; // 鐩稿綋浜巘his.name
}
}
鑻ユ湁灞€閮ㄥ彉閲忓拰瀛楁閲嶅悕锛屽垯灞€閮ㄥ彉閲忎紭鍏堢骇鏇撮珮锛屽氨蹇呴』鍔犱笂 this銆俥g
public class Person {
private String name;
public void setName(String name) {
this.name = name; // 鍓嶉潰鐨則his涓嶅彲灏戯紝灏戜簡灏卞彉鎴愬眬閮ㄥ彉閲弉ame
}
}
2.4 璁剧疆鍜岃幏鍙栧璞℃暟鎹?/h4>
1锛夎缃細
- setter()锛堝睘鎬ф敞鍏ワ級
- 鏋勯€犳敞鍏?/strong>锛?strong>鐩存帴璋冪敤甯﹀弬鏁扮殑鏋勯€犲櫒锛屽垱寤哄璞$殑鍚屾椂瀹屾垚瀵硅薄瀛楁鐨勫垵濮嬪寲锛堟帹鑽愶級銆?/li>
2锛夎幏鍙栵細
- getter()鏂规硶
3锛塭g锛氱湅鎳備互涓嬬ず渚嬪嵆鍙悊瑙h閮ㄥ垎鍐呭
public class Person {
private String name;
private int age;
public Person() { //鏃犲弬鏋勯€?
}
public Person(String name, int age) { //甯﹀弬鏋勯€?
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
class TestPreson{ // 娴嬭瘯绫?
public static void main(String[] args) {
Person p1=new Person();
// 1銆乻etter锛堝睘鎬ф敞鍏ワ級鍒濆鍖?
p1.setName("zhangsan");
p1.setAge(13);
// 2銆佹瀯閫犳敞鍏ワ紝璋冪敤鏋勯€犳柟娉曠殑鍚屾椂瀹屾垚鍒濆鍖栵紙鎺ㄨ崘锛?
Person p2=new Person("lishi",14);
System.out.println(p1.getName()+" "+p1.getAge()); // zhangsan 13
System.out.println(p2.getName()+" "+p2.getAge()); // lishi 14
}
}
浜屻€佹柟娉?/h2>
鏂规硶鍙堢О鍑芥暟锛屾槸涓€涓唬鐮佸潡锛岀敤浜庢墽琛屾煇浜涙搷浣滐紝鍙湪杩愯鏃惰皟鐢ㄣ€?/p>
1銆佹寜鍙傛暟鍜岃繑鍥炲€?/h3>
鍏跺疄鍙互涓嶇敤鍒嗚繖涔堢粏锛屽父鐢ㄧ殑灏遍偅鍑犵锛屽叾浠栦簡瑙e嵆鍙€?/p>
1.1 鏃犲弬鏃犺繑鍥炲€?/h4>
涓€鑸柟娉曞唴涓鸿緭鍑烘墦鍗版煇涓唴瀹广€傜敤鐩存帴璋冪敤鍗冲彲銆?/p>
void锛氭棤杩斿洖鍊硷紝鏈夎繑鍥炲€硷紙return锛変笉鐢?void淇グ绗?/strong>銆?/p>
public class Person {
public static void main(String[] args) {
eat(); // 鐩存帴璋冪敤
}
public static void eat(){ // 鏃犲弬鏃犺繑鍥炲€?
System.out.println("鍚冮キ");
}
}
1.2 甯﹀弬鏃犺繑鍥炲€?/h4>
鍚屼笂锛岀洿鎺ヨ皟鐢ㄣ€?/p>
public class Person {
public static void main(String[] args) {
getSum(10,20); //鐩存帴璋冪敤锛堣寰椾紶鍙傦級銆?
}
public static void getSum(int a, int b){ //甯﹀弬鏃犺繑鍥炲€?
System.out.println(a+b);
}
}
1.3 鏃犲弬鏈夎繑鍥炲€?/h4>
public class Person {
public static void main(String[] args) {
String massage=getMassage(); //璧嬪€艰皟鐢?
System.out.println(massage);
System.out.println(getMassage()); //杈撳嚭璋冪敤
}
public static String getMassage(){ //鏃犲弬鏈夎繑鍥炲€?
return "Hello!";
}
}
1.4 甯﹀弬鏈夎繑鍥炲€硷紙甯哥敤锛?/h4>
public class Person {
public static void main(String[] args) {
String massage=getMassage(); //璧嬪€艰皟鐢?
System.out.println(massage);
System.out.println(getMassage()); //杈撳嚭璋冪敤
}
public static String getMassage(){ //鏃犲弬鏈夎繑鍥炲€?
return "Hello!";
}
}
甯﹀弬鏈夎繑鍥炲€兼柟娉曠殑瀹氫箟鍜岃皟鐢ㄣ€備竴鑸儏鍐垫渶甯哥敤璧嬪€艰皟鐢?/strong>銆?/p>
鏋勯€犳柟娉曟槸涓€绉嶇壒娈婄殑鏂规硶锛岀敤浜庡垱寤哄璞℃椂鎵ц蹇呰鐨?strong>鍒濆鍖栨搷浣?/strong>銆?/p>
娉細鏋勯€犳柟娉曠殑鍚嶇О蹇呴』涓庣被鍚嶇浉鍚岋紝鏃犺繑鍥炲€硷紝涓斿繀椤婚€氳繃 new鎿嶄綔绗﹁皟鐢ㄦ瀯閫犳柟娉曘€?/p>
鍦ㄥ垱寤哄疄渚嬶紙瀵硅薄锛夋椂锛岀粡甯搁渶瑕佸悓鏃跺垵濮嬪寲杩欎釜瀹炰緥鐨勫瓧娈碉紙灞炴€э級銆?br>
娉?锛氶€氳繃setter()鏂规硶锛屾瘡娆¢渶瑕佽皟鐢ㄨ鏂规硶锛屾瘮杈冮夯鐑︺€?/p>
娉?锛?strong>閫氳繃鏋勯€犳柟娉曪紝鍦ㄥ垱寤哄疄渚嬫椂灏辨妸鎵€鏈夊睘鎬ч兘鍒濆鍖?/strong>銆?鎺ㄨ崘) 鑻ユ湭瀹氫箟鏋勯€犳柟娉曪紝缂栬瘧鍣ㄤ細鑷姩鐢熸垚涓€涓粯璁ゆ瀯閫犳柟娉曪紙鏃犲弬鏋勯€狅級銆傚畾涔変簡鏋勯€犳柟娉曪紝缂栬瘧鍣ㄤ笉鍐嶈嚜鍔ㄥ垱寤洪粯璁ゆ瀯閫犳柟娉?/strong>銆?br>
涓€鑸€岃█锛屾渶濂藉氨鏄妸涓や釜鏋勯€犳柟娉曢兘瀹氫箟鍑烘潵锛屽涓嬨€?/p>
pass锛?strong>閫氳繃 new 鏂瑰紡璋冪敤鏋勯€犳柟娉?/strong>銆?/p>
鍏堝垵濮嬪寲瀛楁(灞炴€?锛屽啀鍒濆鍖栨瀯閫犳柟娉?/strong>銆?/p>
1锛夊涓瀯閫犳柟娉曠殑閲嶈浇锛岀紪璇戝櫒浼氭牴鎹弬鏁拌嚜鍔ㄥ垽鏂€傚涓?/p>
2锛夊疄鐜版瀯閫犳柟娉曢棿鐨?strong>鐩镐簰璋冪敤 娴嬭瘯缁撴灉 鍒嗘瀽锛氾紙鐞嗚В浜嗕笂杩颁唬鐮佺殑鍙拷鐣ワ級 绗竴涓緭鍑哄ぇ瀹跺簲璇ラ兘娌¢棶棰橈紝鍥犱负 p璋冪敤涓や釜鍙傛暟鐨勬瀯閫犳柟娉曪紝骞堕€氳繃鏋勯€犳柟娉曪紝鍦ㄥ垱寤哄疄渚嬫椂灏辨妸鎵€鏈夊睘鎬ч兘鍒濆鍖栵紝鎵€浠ヨ緭鍑?zhangsan 19銆?/p>
鏉ョ湅绗簩涓緭鍑猴紝p1璋冪敤鐨勬槸鏃犲弬鏋勯€犳柟娉曪紝浣嗙敱浜嶱erson()鏃犲弬鏋勯€犳柟娉曞張閫氳繃 this("lishi")璋冪敤浜?Person(String name)鏋勯€犳柟娉曪紝涓斿垵濮嬪寲 name涓?lishi锛屾墍浠?p1.getName()涓?lishi锛岃€?Person(String name)鏋勯€犳柟娉曞張閫氳繃 this(name, 18)璋冪敤浜?Person(String name, int age)鏋勯€犳柟娉曪紝涓斿垵濮嬪寲 age涓?8锛屾墍浠?p1.getAge()涓?18銆?/p>
1锛夊疄渚嬪湪鍒涘缓鏃堕€氳繃new鎿嶄綔绗︿細璋冪敤鍏跺搴旂殑鏋勯€犳柟娉曪紙鐢ㄤ簬鍒濆鍖栧疄渚嬶級銆?br>
2锛夋病鏈夊畾涔夋瀯閫犳柟娉曟椂锛岀紪璇戝櫒浼?strong>鑷姩鍒涘缓涓€涓粯璁ゆ瀯閫犳柟娉?/strong>锛堟棤鍙傛瀯閫狅級銆?br>
3锛夋樉寮忓畾涔変簡鏋勯€犲櫒锛堟瀯閫犳柟娉曪級涔嬪悗锛岀紪璇戝櫒灏变笉浼氳嚜鍔ㄧ敓鎴愰粯璁ゆ瀯閫犲櫒浜嗭紝浣嗗彲浠ヨ嚜宸辨墜鍔ㄥ姞涓娿€?br>
4锛夊彲瀹氫箟澶氫釜鏋勯€犳柟娉曪紝缂栬瘧鍣ㄤ細鏍规嵁鍙傛暟鑷姩鍒ゆ柇銆?br>
5锛夊彲鍦ㄤ竴涓瀯閫犳柟娉?strong>鍐呴儴璋冪敤 涔嬪悗鍦ㄨ鍒?strong>缁ф壙public class Person {
public static void main(String[] args) {
getSum(10,20,30); // 1.鐩存帴璋冪敤锛堣繍琛屼笉杈撳嚭缁撴灉锛?
int sum=getSum(10,20,30); // 2.璧嬪€艰皟鐢?
System.out.println(sum);
System.out.println(getSum(10,20,30)); // 3.杈撳嚭璋冪敤
}
public static int getSum(int num1,int num2,int num3){ // 甯﹀弬鏈夎繑鍥炲€?
return num1+num2+num3;
}
}
2銆佹瀯閫犳柟娉?/h3>
2.1 鍒涘缓瀹炰緥锛屽垵濮嬪寲瀹炰緥瀛楁
Person p = new Person();
p.setName("鑰佺帇");
p.setAge(11);
System.out.println(p.getName()+" "+p.getAge());
Person p=new Person("鑰佺帇",11);
System.out.println(p.getName()+" "+p.getAge());
2.2 榛樿鏋勯€犳柟娉?閲嶇偣)
public class Person {
private String name;
private int age;
public Person() { //榛樿鏋勯€狅紙鏃犲弬鏋勯€狅級
}
public Person(String name, int age) { //甯﹀弬鏋勯€?
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
class TestPreson{ // 娴嬭瘯绫?
public static void main(String[] args) {
Person p1=new Person("zhangsan",20); //鏃㈠彲璋冪敤甯﹀弬鏋勯€犳柟娉?
Person p2=new Person(); //涔熷彲璋冪敤鏃犲弬鏋勯€犳柟娉?
System.out.println(p1.getName()+" "+p1.getAge()); // zhangsan 20
System.out.println(p2.getName()+" "+p2.getAge()); // null 0
}
}
2.3 鍒涘缓瀹炰緥鍒濆鍖栭『搴?/h4>
public class Person {
private String name = "zhangsan"; // 鍏堝瓧娈靛垵濮嬪寲
private int age = 20;
public Person(String name, int age) {
this.name = name; //鍐嶆瀯閫犳柟娉曞垵濮嬪寲
this.age = age;
}
}
2.4 澶氭瀯閫犳柟娉?/h4>
public class Person {
private String name;
private int age;
// 鍒涘缓瀵硅薄锛屽垵濮嬪寲 name 鍜?age
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public Person(String name) {
this.name = name;
this.age = 12;
}
// 榛樿鏋勯€犲櫒锛堢紪璇戝櫒鑷姩浜х敓锛?
public Person() {
}
}
public class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public Person(String name) {
this(name, 18); // 璋冪敤鍙︿竴涓瀯閫犳柟娉昉erson(String, int)
}
public Person() { //鏃犲弬鏋勯€?
this("lishi"); // 璋冪敤鍙︿竴涓瀯閫犳柟娉昉erson(String)
}
// 鎻愪緵 getter()鍜宻etter()鏂规硶锛岃繖閲屽彧鐢ㄥ埌getter()
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
class TestPreson{ // 娴嬭瘯绫?
public static void main(String[] args) {
Person p=new Person("zhangsan",19); // 璋冪敤涓や釜鍙傛暟鐨勬瀯閫犳柟娉?
//璋冪敤鏃犲弬鏋勯€狅紝灞傚眰浼犻€掞紝Person()-->Person(String)-->Person(String, int)
Person p1=new Person();
System.out.println(p.getName()+" "+p.getAge());
System.out.println(p1.getName()+" "+p1.getAge());
}
}
zhangsan 19
lishi 18
2.5 鎬荤粨
3銆佹柟娉曢噸杞斤紙閲嶇偣锛?/h3>
3.1 浠嬬粛
鏂规硶閲嶈浇锛圤verload锛夛細鏂规硶鍚嶇浉鍚岋紝鍙傛暟涓嶅悓銆?/p>
浣跨敤閲嶈浇鐨勭洰鐨勶細鍔熻兘绫讳技鐨勬柟娉曚娇鐢ㄥ悓涓€鍚嶅瓧锛屾洿瀹规槗璁颁綇锛岃皟鐢ㄨ捣鏉ユ洿绠€鍗曪紙鍦ㄥ弬鏁颁笂鍋氫慨鏀瑰嵆鍙級銆?/p>
鍙傛暟涓嶅悓锛堜綋鐜板湪锛?/p>
- 鍙傛暟鐨勪釜鏁颁笉鍚?/li>
- 鍙傛暟鐨勭被鍨嬩笉鍚?/li>
- 鍙傛暟鐨勪釜鏁板拰绫诲瀷閮戒笉鍚?/li>
3.2 涓句緥
1锛夊 String绫绘彁渚涗簡澶氫釜閲嶈浇鏂规硶 indexOf()锛岀敤浜庢煡鎵惧瓙涓?br>
int indexOf(int ch)锛氭牴鎹瓧绗︾殑Unicode鐮佹煡鎵撅紱
int indexOf(String str)锛氭牴鎹瓧绗︿覆鏌ユ壘锛?br>
int indexOf(int ch, int fromIndex)锛氭牴鎹瓧绗︽煡鎵撅紝浣嗘寚瀹氳捣濮嬩綅缃紱
int indexOf(String str, int fromIndex)鏍规嵁瀛楃涓叉煡鎵撅紝浣嗘寚瀹氳捣濮嬩綅缃€?br>
2锛夊啀涓句竴涓畝鍗曚緥瀛愶紝濡備笅锛屽疄鐜?hello鏂规硶鐨勯噸杞姐€?/p>
public class Person {
public static void main(String[] args) {
String h = hello();
String h1 = hello("zhangsan");
String h2 = hello("zhangsan", 16);
System.out.println(h);
System.out.println(h1);
System.out.println(h2);
}
public static String hello() {
return "hello,world";
}
public static String hello(String name) {
return "Hello," + name;
}
public static String hello(String name, int age) {
return "Hello," + name+" "+age;
}
}
杩愯缁撴灉
hello,world
Hello, zhangsan
Hello, zhangsan 16
pass锛氳繖娆$殑鍐呭鐪熺殑鏈夌偣澶氾紝甯屾湜鑳藉府鍔╁埌澶у锛岃兘鐪嬪埌杩欓噷浣犲氨鏄渶妫掔殑锛?/p>