当前位置: 首页>后端>正文

Java核心-面向对象(上)

闈㈠悜瀵硅薄鏄?Java鐨勪富瑕佺壒鎬т箣涓€锛屾槸涓€绉嶉噸瑕佺殑缂栫▼鎬濇兂銆傛垜浠兘鐭ラ亾闈㈠悜瀵硅薄鏈変笁澶х壒鎬э紙灏佽銆佺户鎵垮拰澶氭€侊級锛屼絾瀛︿範瀹冧滑涔嬪墠锛岄鍏堣鍋氱殑鍩烘湰鍔熷氨鏄簡瑙d粈涔堟槸绫汇€佸璞¤繕鏈夋柟娉曪紝浠ュ強瀹冧滑涔嬮棿鐨勭浉鍏虫搷浣滃拰鍒嗙被锛屽唴瀹瑰ぇ姒傚涓嬨€?div class="image-package">
Java核心-面向对象(上),第1张

涓€銆佸璞?绫?/h2>

1銆佹蹇?/h3>

1.1 瀵硅薄(object)

Java涓竴璧风殕瀵硅薄銆傞偅涔堜粈涔堟槸瀵硅薄锛熺畝鍗曠悊瑙o紝瀵硅薄鏄竴涓?strong>瀹炰緥(instance)锛屾湁鐘舵€?灞炴€?/strong>)鍜岃涓?鏂规硶)銆俥g锛?/p>

瀛︾敓鏄竴涓璞★紝鏈夊鍚嶃€佹€у埆銆佸勾榫勩€佸鍙枫€佹墍灞炵彮绾с€佹垚缁╃瓑灞炴€с€傚鐢熷彲浠ユ湁鍚冮キ銆佺潯瑙夈€佸涔犵瓑琛屼负锛岃繖浜涘嵆鏂规硶銆?/p>

鍛樺伐鏄竴涓璞★紝鏈夊鍚嶃€佹€у埆銆佸勾榫勩€佸伐鍙枫€佹墍灞為儴闂ㄣ€佺哗鏁堢瓑灞炴€с€傚憳宸ュ彲浠ユ湁鍚冮キ銆佺潯瑙夈€佹懜楸肩瓑琛屼负锛岃繖浜涘嵆鏂规硶銆?/p>

涓嶅彧鏄汉锛屽姩鐗╋紙馃悥锛岎煇憋紝馃悤...锛夛紝妞嶇墿锛堣姳銆佽崏銆佹爲...锛夐兘鏄璞°€傜畝瑷€涔嬶紝涓栭棿涓€鍒囩殕瀵硅薄銆?/p>

1.2 绫?class)

璇村畬瀵硅薄锛屽啀鏉ヨ绫汇€傝鍒扮被锛屽緢澶氫汉鍙兘浼氭兂鍒?鐗╀互绫昏仛锛屼汉浠ョ兢鍒?銆?strong>绫绘槸涓€涓ā鏉匡紝鎻忚堪浜嗕竴绫诲璞$殑琛屼负鍜岀姸鎬?/strong>銆傚緢濂界悊瑙o紝eg锛?/p>

浜烘槸涓€涓被锛屽叿浣撴瘡涓€涓汉濡傚皬鏄庛€佽€佺帇灏辨槸涓€涓璞°€傦紙杩欓噷浜哄氨鏄竴涓娊璞$殑姒傚康锛屽洜涓哄彲浠ュ啀缁嗗垎濡傜敺浜?濂充汉锛?/p>

鐙楁槸涓€涓被锛屽叿浣撳娌欑毊鐙椼€佸搱宸寸嫍灏辨槸涓€涓璞★紝铏界劧灞炰簬涓嶅悓鐨勫搧绉嶏紝浣嗕粬浠兘鏈夌嫍鐨勭壒寰佸拰琛屼负锛岄兘鏄嫍銆傦紙杩欓噷鐙楀氨鏄竴涓娊璞$殑姒傚康锛屽洜涓哄彲浠ュ啀缁嗗垎锛?/p>

鍙互鐪嬪埌锛岀被鍙互鍒掔殑姣旇緝瀹芥硾锛屼篃鍙互鍒掔殑缁嗕竴鐐癸紝鍏蜂綋杩樿鐪嬪疄闄呯殑涓氬姟闇€姹傘€傜畝瑷€涔嬶紝瀵硅薄鏄被鐨勫疄渚嬶紝绫绘槸瀵硅薄鐨勬娊璞?/strong>銆?/p>

2銆佹繁鍏?/h3>

2.1 绫荤殑瀹氫箟

1锛夎娉?/p>

[淇グ绗 class 绫诲悕 {
    0-N 鎴愬憳鍙橀噺锛堝瓧娈碉級 // 鎻忚堪绫诲叿鏈夌殑鐗规€э紝瀵硅薄鐨勭姸鎬?
    0-N 鏋勯€犲櫒
    0-N 鏂规硶            // 鎻忚堪绫诲叿鏈夌殑鍔熻兘锛屽璞$殑琛屼负
}

pass锛氱被鍙湅鍋氭槸涓€绉嶈嚜瀹氫箟鐨勬暟鎹被鍨嬶紝鍙互浣跨敤绫绘潵瀹氫箟鍙橀噺銆?/p>

鍏充簬淇グ绗︿箣鍓嶄粙缁嶈繃锛屽畾涔夌被 public銆乸rotected銆乤bstract鍜?static鐢ㄧ殑杈冨锛屽悇鏈変笉鍚岀殑鍚箟鍜屽姛鑳姐€?/p>

鎴愬憳鍙橀噺(闈為潤鎬佸彉閲?/ 瀹炰緥鍙橀噺 / 鍏ㄥ眬鍙橀噺)锛氬疄渚嬪彉閲忓0鏄庡湪涓€涓被涓紝浣嗗湪鏂规硶銆佹瀯閫犳柟娉曞拰璇彞鍧椾箣澶栵紙绫讳腑鏂规硶澶栵級銆?/p>

鑷充簬鏋勯€犲櫒鍜屾柟娉曪紝鎺ヤ笅鏉ヤ細璁插埌銆?/p>

2.2 瀵硅薄鍒涘缓銆佽闂?淇敼灞炴€?/h4>

1锛夊垱寤哄璞?br> 鍒涘缓瀵硅薄鍗?strong>瀹炰緥鍖栦竴涓璞?/strong>銆備竴鑸€氳繃new鍏抽敭瀛楁潵鍒涘缓瀵硅薄銆傝繕鍙互閫氳繃鍙嶅皠鍜屽簭鍒楀寲鍒涘缓锛屽綋鐒讹紝杩欓兘鏄悗璇濅簡銆?/p>

绫诲悕 瀵硅薄鍚?= new 绫诲悕();

2锛夎闂?淇敼
绫?瀵硅薄鐨勮闂垚鍛樺彉閲忔垨鏂规硶濡備笅

閫氳繃 瀵硅薄.灞炴€?鐩存帴璧嬪€肩殑鏂瑰紡淇敼灞炴€?/p>

绫?绫绘柟娉?
绫?绫诲彉閲?
瀵硅薄鍚?鎴愬憳鏂规硶
瀵硅薄鍚?鎴愬憳鍙橀噺

3锛塭g

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
鍚冮キ
鐫¤

pass锛欽ava涓殑娴嬭瘯绫?/strong>鏄敤鏉ユ槸鐢ㄦ潵瀵瑰凡缁忓啓濂界殑鍔熻兘缁勪欢杩涜娴嬭瘯銆備笂闈㈣繖绉嶉€氳繃鏂板缓涓€涓甫鏈塵ain鏂规硶鐨勭被锛屽埄鐢ㄨ绫绘潵璋冪敤闇€瑕佹祴璇曠殑绫伙紝骞舵妸闇€瑕佹祴璇曠殑鏁版嵁浼犲叆杩涘幓鐨勬祴璇曟柟娉曞湪鍓嶆湡瀛︿範鏄潪甯稿父鐢ㄧ殑锛屽悗鏈熷紑鍙戝垯閲囩敤鏇村姞楂樻晥渚挎嵎鐨?strong>JUnit鍗曞厓娴嬭瘯銆?/p>

2.3 甯哥敤鍏抽敭瀛?/h4>

浠ヤ笅閮ㄥ垎鍙兘鍒氬紑濮嬩笉鏄偅涔堝ソ鐞嗚В锛屾參鎱㈠氨鐔熸倝浜嗐€?/p>

1锛夐潤鎬?static
static 淇グ鐨勬垚鍛樼О涓?strong>绫绘垚鍛?/strong>锛堢被鍙橀噺銆佺被鏂规硶銆侀潤鎬佸垵濮嬪寲鍧椼€侀潤鎬佸唴閮ㄧ被锛夛紝鍏?strong>闅忕潃鎵€鍦ㄧ被鐨勫姞杞借€屽姞杞斤紝鍏堜簬璇ョ被瀵硅薄瀛樺湪
锛堝綋瀛楄妭鐮佽鍔犺浇杩?JVM 鏃剁被鎴愬憳灏卞瓨鍦ㄤ簡锛岃€屽璞℃槸鍚庨潰 new 鍑烘潵鐨勶級锛岃璇ョ被鎵€鏈夊璞℃墍鍏变韩锛屽彲浠ョ洿鎺ョ敤绫诲悕璋冪敤銆?/p>

娉細琚?static淇グ鐨勬垚鍛樻柟娉曞拰鎴愬憳鍙橀噺绉颁负绫绘垚鍛橈紝鍏朵腑锛岃 static淇グ鐨勬垚鍛樺彉閲忕О涓虹被鍙橀噺锛岃 static淇グ鐨勬垚鍛樻柟娉曠О涓虹被鏂规硶銆?/p>

2锛夋湭琚?static淇グ
鐩稿弽锛屾湭琚?static淇グ鐨勬垚鍛樼О涓轰笌瀹炰緥鎴愬憳锛堝疄渚嬪彉閲忋€佸疄渚嬫柟娉曘€佹櫘閫氬垵濮嬪寲鍧椼€佸疄渚嬪唴閮ㄧ被锛?/p>

娉細鏈static淇グ鐨勬垚鍛樻柟娉曞拰鎴愬憳鍙橀噺绉颁负瀹炰緥鎴愬憳锛屽叾涓紝鏈static淇グ鐨勬垚鍛樺彉閲忓彨鍋氬疄渚嬪彉閲忥紝鏈static淇グ鐨勬垚鍛樻柟娉曞彨鍋氬疄渚嬫柟娉曘€?/p>

3锛夊叾浠栨敞鎰?/p>

灞€閮ㄥ彉閲忥紙绫荤殑鏂规硶涓€佷唬鐮佸潡涓殑鍙橀噺锛変笉灞炰簬浠讳綍绫绘垨瀹炰緥锛屼笉鑳戒娇鐢?static 淇グ锛涗笉鑳戒慨楗版瀯閫犲櫒銆?/p>

public鍙兘鐢卞璞¤闂?瀵硅薄.灞炴€鏂规硶)锛岃€?static 闈欐€佹柟娉曞彲浠ュ湪涓嶅垱寤虹被鐨勫璞$殑鎯呭喌涓嬭闂鏂规硶銆?/p>

static 淇グ鐨勬垚鍛橈紙灞炰簬绫伙級鍙婃瀯閫犲櫒涓嶈兘鐩存帴璁块棶娌℃湁 static 淇グ鐨勬垚鍛橈紙灞炰簬瀵硅薄锛?/p>

鐢变簬static 寮鸿皟鐨勬槸绫伙紝this 鍜?super 寮鸿皟鐨勬槸瀵硅薄锛屾晠 static 淇グ鐨勬柟娉曚腑涓嶈兘浣跨敤 this 鎴?super 寮曠敤

4锛?strong>浠g爜绀轰緥锛堥噸鐐癸級
鎼炴竻妤氬涓嬩唬鐮佺ず渚嬶紙缁撳悎娉ㄩ噴锛夊氨鍩烘湰涓婅兘鐞嗚В涓婇潰鐨勮〃杈炬剰鎬濄€傝嚦浜庣 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>

甯﹀弬鏈夎繑鍥炲€兼柟娉曠殑瀹氫箟鍜岃皟鐢ㄣ€備竴鑸儏鍐垫渶甯哥敤璧嬪€艰皟鐢?/strong>銆?/p>

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>

鏋勯€犳柟娉曟槸涓€绉嶇壒娈婄殑鏂规硶锛岀敤浜庡垱寤哄璞℃椂鎵ц蹇呰鐨?strong>鍒濆鍖栨搷浣?/strong>銆?/p>

娉細鏋勯€犳柟娉曠殑鍚嶇О蹇呴』涓庣被鍚嶇浉鍚岋紝鏃犺繑鍥炲€硷紝涓斿繀椤婚€氳繃 new鎿嶄綔绗﹁皟鐢ㄦ瀯閫犳柟娉曘€?/p>

2.1 鍒涘缓瀹炰緥锛屽垵濮嬪寲瀹炰緥瀛楁

鍦ㄥ垱寤哄疄渚嬶紙瀵硅薄锛夋椂锛岀粡甯搁渶瑕佸悓鏃跺垵濮嬪寲杩欎釜瀹炰緥鐨勫瓧娈碉紙灞炴€э級銆?br> 娉?锛氶€氳繃setter()鏂规硶锛屾瘡娆¢渶瑕佽皟鐢ㄨ鏂规硶锛屾瘮杈冮夯鐑︺€?/p>

Person p = new Person();
p.setName("鑰佺帇");
p.setAge(11);
System.out.println(p.getName()+" "+p.getAge());

娉?锛?strong>閫氳繃鏋勯€犳柟娉曪紝鍦ㄥ垱寤哄疄渚嬫椂灏辨妸鎵€鏈夊睘鎬ч兘鍒濆鍖?/strong>銆?鎺ㄨ崘)

Person p=new Person("鑰佺帇",11);
System.out.println(p.getName()+" "+p.getAge());

2.2 榛樿鏋勯€犳柟娉?閲嶇偣)

鑻ユ湭瀹氫箟鏋勯€犳柟娉曪紝缂栬瘧鍣ㄤ細鑷姩鐢熸垚涓€涓粯璁ゆ瀯閫犳柟娉曪紙鏃犲弬鏋勯€狅級銆傚畾涔変簡鏋勯€犳柟娉曪紝缂栬瘧鍣ㄤ笉鍐嶈嚜鍔ㄥ垱寤洪粯璁ゆ瀯閫犳柟娉?/strong>銆?br> 涓€鑸€岃█锛屾渶濂藉氨鏄妸涓や釜鏋勯€犳柟娉曢兘瀹氫箟鍑烘潵锛屽涓嬨€?/p>

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
    }
}

pass锛?strong>閫氳繃 new 鏂瑰紡璋冪敤鏋勯€犳柟娉?/strong>銆?/p>

2.3 鍒涘缓瀹炰緥鍒濆鍖栭『搴?/h4>

鍏堝垵濮嬪寲瀛楁(灞炴€?锛屽啀鍒濆鍖栨瀯閫犳柟娉?/strong>銆?/p>

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>

1锛夊涓瀯閫犳柟娉曠殑閲嶈浇锛岀紪璇戝櫒浼氭牴鎹弬鏁拌嚜鍔ㄥ垽鏂€傚涓?/p>

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() {
    }
}

2锛夊疄鐜版瀯閫犳柟娉曢棿鐨?strong>鐩镐簰璋冪敤锛堣皟鐢ㄨ娉曪細this(, , ...)锛夛紝浠ユ彁楂樹唬鐮佸鐢ㄦ€с€傚涓?/p>

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

鍒嗘瀽锛氾紙鐞嗚В浜嗕笂杩颁唬鐮佺殑鍙拷鐣ワ級

绗竴涓緭鍑哄ぇ瀹跺簲璇ラ兘娌¢棶棰橈紝鍥犱负 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>

2.5 鎬荤粨

1锛夊疄渚嬪湪鍒涘缓鏃堕€氳繃new鎿嶄綔绗︿細璋冪敤鍏跺搴旂殑鏋勯€犳柟娉曪紙鐢ㄤ簬鍒濆鍖栧疄渚嬶級銆?br> 2锛夋病鏈夊畾涔夋瀯閫犳柟娉曟椂锛岀紪璇戝櫒浼?strong>鑷姩鍒涘缓涓€涓粯璁ゆ瀯閫犳柟娉?/strong>锛堟棤鍙傛瀯閫狅級銆?br> 3锛夋樉寮忓畾涔変簡鏋勯€犲櫒锛堟瀯閫犳柟娉曪級涔嬪悗锛岀紪璇戝櫒灏变笉浼氳嚜鍔ㄧ敓鎴愰粯璁ゆ瀯閫犲櫒浜嗭紝浣嗗彲浠ヨ嚜宸辨墜鍔ㄥ姞涓娿€?br> 4锛夊彲瀹氫箟澶氫釜鏋勯€犳柟娉曪紝缂栬瘧鍣ㄤ細鏍规嵁鍙傛暟鑷姩鍒ゆ柇銆?br> 5锛夊彲鍦ㄤ竴涓瀯閫犳柟娉?strong>鍐呴儴璋冪敤鍙︿竴涓瀯閫犳柟娉曪紝渚夸簬浠g爜澶嶇敤銆?/p>

3銆佹柟娉曢噸杞斤紙閲嶇偣锛?/h3>

涔嬪悗鍦ㄨ鍒?strong>缁ф壙鏃惰繕浼氭湁涓€涓?strong>閲嶅啓锛岃涓?strong>閲嶈浇鍖哄埆寮€锛屽厛鎵撻闃查拡銆?/p>

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>


https://www.xamrdz.com/backend/3q21937425.html

相关文章: