1. 程式人生 > 其它 >Java面向物件(類、方法過載、this關鍵字)

Java面向物件(類、方法過載、this關鍵字)

一、什麼是物件

程式設計師眼裡的面向物件思想

  • 一切客觀存在的食物都是物件,即:“萬物皆物件”
  • 任何物件一定具有自己的特徵和行為(即:屬性和方法)
  • 特徵: 稱為屬性,一般為名詞,代表物件有什麼
  • 行為: 稱為方法,一般為動詞,代表物件能做什麼
  • 現實中的物件多數來自於“模板”,而程式中的物件也不例外也具有相應的“模板”

物件的建立:

public class classDemo1 {
    public static void main(String[] args) {
        Dog myDog = new Dog();                               //基於Dog類建立物件

        myDog.breed = "薩摩";                              //訪問屬性:物件名.屬性名 =  值; //賦值
        myDog.age = 2;
        myDog.sex = "公";
        myDog.furColor = "白色";

        System.out.println(myDog.breed + "\t" + myDog.age + "\t" + myDog.sex + "\t" + myDog.furColor);
//      訪問屬性:物件名.屬性名; //取值
        myDog.eat( );
        myDog.sleep( );
//        呼叫方法:物件名.方法名( );
    }
}

物件建立過程:

public class demo {
    public static void main(String[] args) {
        Student1 s = new Student1();                 // new Student1(); 觸發物件建立
    }
}
class  Student1{                                     //1.記憶體中開闢空間
    String name;                                     //2.為各個屬性賦予初始值
    int age;                                         //3.執行構造方法中的程式碼
    String sex;                                      //4.[將物件的地址賦值給變數]
    double score;

    public  Student1(){
        System.out.println("Student() Executed");
    }
}

物件中的記憶體分佈:

儲存物件的變數中儲存物件的地址,通過變數中的地址訪問物件的屬性和方法

二、什麼是類

什麼是類?

類(Class)是面向物件程式設計實現資訊封裝的基礎

類的定義:

public class Dog{
    String breed; //品種                      屬性:通過變量表示,又稱例項方法,又稱例項變數。
    int age; //年齡                           語法:資料型別 屬性名;
    String Sex; //性別                        位置:類的內部,方法的外部。
    String furColor; //毛色

    public void eat(){                        //方法:通過函式表示,又稱例項方法
        System.out.println("eating...");      //語法:
    }                                         //     public 返回值型別 方法名(形參){
    public  void sleep(){                     //     //方法的主體
        System.out.println("sleeping...");    //}
    }
}

三、類的組成

  • 屬性: 比如:學生的屬性有姓名、性別、出生年月、家庭住址、電話等
  • 比如: 學生的行為(也就是方法)有學習、打遊戲、看電影等

類與物件的關係:

  • 類: 定義了物件具有的特徵和行為,類是物件的模板
  • 物件: 擁有多個特徵和行為的實體,物件是類的例項

例項變數:

  • 思考:之前學習區域性變數時,要求先賦值再使用,否則編譯錯誤。對於例項變數而言,未賦值並不會編譯錯誤,能否直接訪問?
public class classDemo1 {
    public static void main(String[] args) {
        Dog myDog = new Dog();                               //基於Dog類建立物件
//		例項變數的預設值: 整數:0	小數:0.0	字元:\u0000 (空格)	布林:False	其他:null
//      省略賦值語句
        
        System.out.println(myDog.breed + "\t" + myDog.age + "\t" + myDog.sex + "\t" + myDog.furColor);
//		執行結果: null 0 null null
    }
}

例項變數與區域性變數的區別:

種類 區域性變數 成員變數
定義位置 方法或方法內的構造當中 類的內部,方法的外部
預設值 無預設值 字面值與陣列相同
適用範圍 從定義行到包含其構造結束 本類有效
命名衝突 不允許重名 可與區域性變數重名,區域性變數優先

例項方法:

例項方法包含兩部分: 方法的宣告和方法的實現

  • 方法的宣告:
    • 代表物件能做什麼
    • 組成: 修飾符、返回值型別和方法名(形參列表)
  • 方法的實現:
    • 代表物件怎麼做: 即如何實現對應的功能
    • 組成:{}

四、方法過載

什麼是方法的過載?

  • 一個類中定義多個相同名稱的方法
  • 要求:
    • 方法名稱相同
    • 引數列表不同(型別、個數、順序)
    • 與訪問修飾符、返回值無關
  • 好處: 遮蔽使用差異、靈活、方便

注意:只是引數不同並不能構成方法的過載。

  • 有些情況下,物件的同一種行為可能存在多種實現過程。
  • 例如:人物件的eat行為,吃飯,吃藥和嚼口香糖的過程就存在差異。
public class Person{
    public void eat(食物 a){
        //食物放入口中
        //咀嚼
        //嚥下
    }
    public void eat(藥物 b){
        //藥物放入口中
        //喝水
        //嚥下
    }
    public void eat(口香糖 c){
        //口香糖放入口中
        //咀嚼
        //吐出
    }
}

五、構造方法

什麼是構造方法呢?

  • 類中的特殊方法,主要用於建立物件
  • 特點:
    • 名稱與類名完全相同
    • 沒有返回值型別
    • 建立物件時,觸發構造方法的呼叫,不可通過句點手動呼叫

注意:如果沒有在類中顯示定義構造方法,則編譯器預設提供無參構造方法

構造方法的過載:

構造方法也可以過載,遵循過載規則

public class demo {
    public static void main(String[] args) {
//        Student1 s = new Student1();
        new Student1();
        new Student1("tom");
        new Student1("jack", 20);
//        建立物件時,根據傳入引數,匹配對應的構造方法。
    }
}
class  Student1{
    String name;
    int age;
    String sex;
    double score;

    public Student1(){
        System.out.println("Student1() Executed");
    }
    public Student1(String name){
    	System.out.println("Student1(String name) Executed");
    }
    public Student1(String name,int age){
        System.out.println("Student1(String name,int age) Executed");
    }
}
/*
輸出結果:
Student1() Executed
Student1(String name) Executed
Student1(String name,int age) Executed
*/

預設構造方法:

public class demo {
    public static void main(String[] args) {
        Student1 s = new Student1(); //編譯錯誤:無參構造方法未定義
    }
}
class  Student1{
    String name;                    //在類中,如果沒有顯示定義構造方法,則編譯器預設提供無參構造方法。
    int age;
    String sex;
    double score;

    public Student1(String n, int a, String s, double sc){
     //如果已經手動新增有參構造方法,則無參構造方法不在預設提供,可更具需求自行新增。
    }
}

構造方法為屬性賦值:

public class demo {
    public static void main(String[] args) {
        Student1 s = new Student1("tom", 20,"male", 98.5);    //建立物件的同時,將值傳入構造方法
        System.out.println(s.name + "\t" + s.age + "\t" + s.sex + "\t" + s.score);
    }
}
class  Student1{
    String name;                    //tom 20 male 98.5
    int age;
    String sex;
    double score;

    public Student1(String n, int a, String s, double sc){
        name = n;
        age = a;
        sex = s;
        score = sc;                 //由構造方法為各個屬性賦值
    }
}

六、this關鍵字

public class demo {
    public static void main(String[] args) {
        Student1 s1 = new Student1();  //0x0000A0001
        s1.sayHi();           //建立物件s1時,this指向0x0000A0001,訪問的name屬性即是0x0000A0001地址中的name空間。
        Student1 s2 = new Student1();  //0x0000A0002
        s2.sayHi();           //建立物件s2時,this指向0x0000A0002,訪問的name屬性即是0x0000A0002地址中的name空間。
    }
}
class  Student1{
    String name;                    
    int age;
    String sex;
    double score;

    public void sayHi(){
//      類是模板,可服務於此類的所有物件,當類服務於某個物件時,this則指向這個物件。
        System.out.println(this.name); // this是類中的預設引用,代表當前例項。
    }
}

this關鍵字的用法:

  1. 呼叫例項屬性、例項方法,如:this.name、this.sayHi();
public class demo {
    public static void main(String[] args) {
        Student1 s1 = new Student1();  //0x0000A0001
        s1.sayHi();
    }
}
class Student1{
    String name = "tom";

    public void sayHi(){
        String name = "jack"; //當例項變數與區域性變數重名時,優先訪問區域性變數;此時如需訪問例項變數則需要this.字首。
        System.out.println(name); //不存在重名時,則可以省略this
        System.out.println(this.name);
    }
}
/*
jack
tom
*/
  1. 呼叫本類中其他構造方法,如:this(); this(實參);
class student1{
    String name;
    int age;
    String sex;
    Double score;

    public student1(String name, int age, String sex){
        this.name = name;        //在構造方法中,呼叫本類其他構造方法,即可複用構造方法中的邏輯程式碼。
        this.age = age;
        this.sex = sex;
    }

    public student1(String name, int age, String sex,Double score){
        this(name, age, sex);    //this(實參):呼叫有參構造 必須在構造方法的首行
//        四參構造方法將接收到的實參直接傳遞給三參構造進行屬性賦值。
        this.score = score;      // this():呼叫無參構造
    }
}

本筆記僅限參考使用

參考部落格:https://blog.csdn.net/weixin_44170221/article/details/104310533