天天看點

教妹學Java(二十 七):this 關鍵字的用法(1)

三妹開學了,學的計算機軟體程式設計。她學校離我家很近,坐公共汽車也就 10 站路的距離, 每逢周末她都會來找我,讓我輔導她學習 Java。作為一名擁有十餘年程式設計經驗的程式員,再加上父母給我們的這份血緣關系,我覺得義不容辭。

“二哥,今天我們要學習的内容是‘this 關鍵字’,對吧?”看來三妹已經提前預習了我上次留給她的作業。

“是的,三妹。this 關鍵字有很多用法。 ”我面帶着樸實無華的微笑回答着她,“最常用的一個是,它可以作為一引用變量,指向目前對象。”

----正兒八經的分割線,正文開始------------

01、this 關鍵字有哪些用法

下面給出了 this 關鍵字的六種用法:

指向目前對象;

調用目前類的方法;

this() 可以調用目前類的構造方法;

this 可以作為參數在方法中傳遞;

this 可以作為參數在構造方法中傳遞;

this 可以作為方法的傳回值,傳回目前類的對象。

02、 指向目前對象

如果參數名和執行個體變量名産生了沖突,this 關鍵字可以解決這個問題。先來看一個沒有 this 關鍵字的例子:

/**
 * @author 微信搜「沉默王二」,回複關鍵字 PDF
 */
public class WithoutThisStudent {
    String name;
    int age;
    WithoutThisStudent(String name, int age) {
        name = name;
        age = age;
    }
    void out() {
        System.out.println(name+" " + age);
    }
    public static void main(String[] args) {
        WithoutThisStudent s1 = new WithoutThisStudent("沉默王二", 18);
        WithoutThisStudent s2 = new WithoutThisStudent("沉默王三", 16);
        s1.out();
        s2.out();
    }
}      

在上面的例子中,構造方法的參數名和執行個體變量名相同,由于沒有使用 this 關鍵字,是以無法為執行個體變量指派。程式輸出的結果如下所示:

null 0

從結果中可以看得出來,盡管建立對象的時候傳遞了參數,但執行個體變量并沒有指派。這是因為如果構造方法中沒有使用 this 關鍵字的話,name 和 age 指向的并不是執行個體變量而是參數。

怎麼解決這個問題呢?加上 this 關鍵字就好了。

/**
 * @author 微信搜「沉默王二」,回複關鍵字 PDF
 */
public class WithThisStudent {
    String name;
    int age;
    WithThisStudent(String name, int age) {
        this.name = name;
        this.age = age;
    }
    void out() {
        System.out.println(name+" " + age);
    }
    public static void main(String[] args) {
        WithThisStudent s1 = new WithThisStudent("沉默王二", 18);
        WithThisStudent s2 = new WithThisStudent("沉默王三", 16);
        s1.out();
        s2.out();
    }
}      

再來看一下程式的輸出結果:

沉默王二 18

沉默王三 16

1

2

當然了,如果參數名和執行個體變量名不同的話,就不必使用 this 關鍵字,但我建議使用 this 關鍵字,這樣的代碼更有意義。

03、調用目前類的方法

我們可以在一個類中使用 this 關鍵字來調用另外一個方法,如果沒有使用的話,編譯器會自動幫我們加上。

/**
 * @author 微信搜「沉默王二」,回複關鍵字 PDF
 */
public class InvokeCurrentClassMethod {
    void method1() {}
    void method2() {
        method1();
    }
    public static void main(String[] args) {
        new InvokeCurrentClassMethod().method1();
    }
}      

在源代碼中,method2() 在調用 method1() 的時候并沒有使用 this 關鍵字,但通過反編譯後的位元組碼可以看得到。

public class InvokeCurrentClassMethod {
    public InvokeCurrentClassMethod() {
    }
    void method1() {
    }
    void method2() {
        this.method1();
    }
    public static void main(String[] args) {
        (new InvokeCurrentClassMethod()).method1();
    }
}      

這次看到了吧?

04、調用目前類的構造方法

this() 可用于調用目前類的構造方法——構造方法可以重用了。

/**
 * @author 微信搜「沉默王二」,回複關鍵字 PDF
 */
public class InvokeConstrutor {
    InvokeConstrutor() {
        System.out.println("hello");
    }
    InvokeConstrutor(int count) {
        this();
        System.out.println(count);
    }
    public static void main(String[] args) {
        InvokeConstrutor invokeConstrutor = new InvokeConstrutor(10);
    }
}      

在有參構造方法 InvokeConstrutor(int count) 中,使用了 this() 來調用無參構造方法 InvokeConstrutor()。來看一下輸出結果:

hello

10

果然,無參構造方法也被調用了,是以輸出了“hello”。

也可以在無參構造方法中使用 this() 并傳遞參數來調用有參構造方法,來看下面這段代碼:

/**
 * @author 微信搜「沉默王二」,回複關鍵字 PDF
 */
public class InvokeParamConstrutor {
    InvokeParamConstrutor() {
        this(10);
        System.out.println("hello");
    }
    InvokeParamConstrutor(int count) {
        System.out.println(count);
    }
    public static void main(String[] args) {
        InvokeParamConstrutor invokeConstrutor = new InvokeParamConstrutor();
    }
}      

來看一下程式的輸出結果。

注意,this() 必須放在構造方法的第一行,否則就報錯了,見下圖。

教妹學Java(二十 七):this 關鍵字的用法(1)