1. 程式人生 > >java中的關鍵字--this、static

java中的關鍵字--this、static

this關鍵字

this關鍵字的主要用途:
1.this呼叫本類屬性
2.this呼叫本類方法
3.this表示當前物件

this呼叫本類屬性

程式碼:

class Person{
    private String name;
    private int age;

    public Person(String name,int age){
        this.name = name; //使用this呼叫本類的屬性name
        this.age = age;   //使用this呼叫本類的屬性age
        /*
        有人可能覺得this不一定要使用,但是在這裡形參和屬性重名,為了區分
        使用this呼叫就表示是本類的屬性,可以進行賦值。否則形參與類中的屬性同名時
        類中屬性無法被正確賦值。
         */
    }

    public String getPersonInfo(){
        return "姓名:" + name + ",年齡:" + age;
    }
}


public class Test{
    public static void main(String[] args) {
        Person person  = new Person("abaka",21);
        System.out.println(person.getPersonInfo());
    }
}
this呼叫本類方法

在這裡this 呼叫本類方法有兩種情況:
1.呼叫普通方法:this.方法名稱(引數)
2.呼叫構造方法:this(引數)

this呼叫普通方法:

class Person{
    private String name;
    private int age;

    public Person(String name,int age){
        this.name = name;
        this.age = age;
        this.getPersonInfo(); 
        /*
        this呼叫普通方法,這裡可以不使用this進行呼叫,但是推薦加上,
        加上可以區分方法的定義來源,在繼承中會使用到。
         */
    }

    public void getPersonInfo(){
        System.out.println("姓名:" + name + ",年齡:" + age);
    }
}


public class Test{
    public static void main(String[] args) {
        Person person  = new Person("abaka",21);
    }
}

this呼叫構造方法:

class Person{
    private String name;
    private int age;

    public Person(){
        System.out.println("1.**********************");
    }
    public Person(String name){
        this();
        /*
        this()呼叫本類的Person()方法。
         */
        System.out.println("2.**********************");
        this.name = name;
    }

    public Person(String name,int age){
        this(name);
        /*
        this(name)呼叫本類的Person(String name)方法
         */
        System.out.println("3.**********************");
        this.age = age;
        this.getPersonInfo();
    }

    public void getPersonInfo(){
        System.out.println("姓名:" + name + ",年齡:" + age);
    }
}


public class Test{
    public static void main(String[] args) {
        Person person  = new Person("abaka",21);
    }
}

執行結果:

1.**********************
2.**********************
3.**********************
姓名:abaka,年齡:21

在java中支援構造方法的相互呼叫,
使用this呼叫構造方法時請注意:
1.this呼叫構造方法的語句必須在方法的首行,否則會報錯(受查異常)
2.使用this呼叫構造方法時,要留出口。不能在呼叫構造中成環,這樣就是死迴圈。

this表示當前物件
class Person{
    private String name;
    private int age;

    public void print(){
        System.out.println("print方法:" + this);//this呼叫當前類的物件
    }
}

public class Test{
    public static void main(String[] args) {
        Person person1 = new Person();
        System.out.println("main方法:"+ person1);
        person1.print();
        System.out.println("----------------------");
        Person person2 = new Person();
        System.out.println("main方法:"+ person2);
        person2.print();
    }
}

執行結果:

main方法:[email protected]
print方法:[email protected]
----------------------
main方法:[email protected]
print方法:[email protected]

結果中print方法和main方法顯示的地址是一樣的,那是因為print方法中呼叫了this,列印的是當前類的物件,而main是直接列印除了當前物件,所以兩個是相同的。至於分割線上下打印出的地址不一樣那是因為原始碼中使用Person()構造new了兩次,每new一次其實就是在堆上開闢了一塊新的空間,所以相當於在堆上開闢了兩個不同地址的空間,而person1,person2分別為這兩個空間的地址,所以才會不一樣。

static關鍵字

在java中static用於修飾屬性和方法。
static宣告的屬性和方法的生命週期和類相同,在整個應用程式執行期間都有效。
注:
static修飾的屬性和方法屬於類
普通屬性和方法屬於物件

Static修飾屬性(類中屬性)

在一個類中定義普通屬性,每new一個物件就會在堆上分配一片空間,空間中是各屬性值的儲存,但是static修飾的屬性不是儲存在堆中,而是在全域性資料區,在整個生命週期中只有一個存在(與new的物件無關),對所有物件共享。

如圖所示:
在這裡插入圖片描述
圖中new了兩個物件,普通屬性拷貝了兩份分別存放在不同的堆空間,物件間不能共享同一屬性,但是static屬性只有一份,在全域性資料區,兩個物件共享一個屬性。

描述共享屬性,只需在屬性前加static關鍵字就可以

static屬性又稱為類屬性,儲存在全域性資料區(所有物件共享區域)的記憶體之中,所有物件都可以進行該資料區的訪問

注:
1.static屬性可以通過類名呼叫,與物件例項化無關
2.所有非static屬性必須在物件例項化後使用,而static屬性不受物件例項化限制
3.在定義類中屬性時一般不使用static,只有需要共享屬性,或者不受物件例項化約束的屬性時使用static.
4.區域性變數不能用static修飾。

static方法

static方法和static屬性特質基本相同,可以通過類名直接呼叫,與物件例項化無關
注:
1.所有的static方法不允許訪問非static定義的屬性或方法。因為static與物件例項化無關,而非static定義的屬性或方法都與物件例項化有關,兩個的建立時間不同,許可權不同。static建立在非static建立之前,static存在而非static不一定存在所以不能訪問。
2.所有非static方法允許訪問static方法或屬性。非static建立在static建立之後,所以非static存在則static必然存在,所以可以訪問。