朋友面試Java給淘汰了,原來(lái)竟然不懂Java中的this關(guān)鍵字

最近,朋友丟出來(lái)了一段Java代碼,這個(gè)程序的結(jié)果是什么?為什么會(huì)這樣?竟然說(shuō)面試不過(guò),這里給淘汰了。

package com.xttblog.demo;

class SuperClass {
    void method() {
        System.out.println("SuperClass::method");
    }
    void fun() {
        System.out.println("SuperClass::fun");
        this.method();
    }
}

class SubClass extends SuperClass {
    void method() {
        System.out.println("SubClass::method: Begin");
        super.fun();
        System.out.println("SubClass::method: End");
    }
}

class Test {
    public static void main(String[] args) {
        SubClass a = new SubClass();
        a.method();
    }
}

我可是新鮮的小白,雖然還沒(méi)面試,于是我看了看,不就是一個(gè)this嗎?

其實(shí)很容易知道,陷入了一個(gè)死循環(huán)。

image

運(yùn)行的結(jié)果的順序其實(shí)很簡(jiǎn)單,關(guān)鍵就是要理解this到底指著誰(shuí),this表示對(duì)當(dāng)前對(duì)象的引用,也就是subclass對(duì)象

image

如果在new一個(gè)Superclass對(duì)象,bug就解決了

image

但是你真正了解過(guò)this

this原理:

  • 代表的是當(dāng)前對(duì)象。

  • this就是所在函數(shù)的所屬對(duì)象的引用。

簡(jiǎn)單說(shuō),哪個(gè)對(duì)象調(diào)用了this關(guān)鍵字所在的函數(shù),this就代表哪個(gè)對(duì)象。

this有下面3中用法

引用成員變量

表示對(duì)當(dāng)前對(duì)象的引用!

/**
 * @author: 毛利
 */
public class Person1 {
    private  int age;
    private String name;
    void Sayhello(Integer age ,String name){
        //將局部變量的值傳遞給成員變量
        this.name = name;
        this.age = age;
        System.out.println("我的名字是" + this.name + ",年齡是" + this.age);
    }

    public static void main(String[] args) {
        Person1 p = new Person1();
        p.Sayhello(20,"maoli");
    }
}

表示用類的成員變量,而非函數(shù)參數(shù)

方法名來(lái)初始化對(duì)象,毛利到底 是19還是20呢?


/**
 * @author: 毛利
 */
class Person {
    private int age = 19;
    public Person(){
        System.out.println("初始化年齡:"+age);
    }
    public int GetAge(int age){
        this.age = age;
        return this.age;
    }
}

public class test1 {
    public static void main(String[] args) {
        Person maoli = new Person();
        System.out.println("maoli's age is "+maoli.GetAge(20));
    }
}

在Java中this這個(gè)關(guān)鍵字可以實(shí)現(xiàn)類屬性的調(diào)用,類方法的調(diào)用,表示當(dāng)前對(duì)象

初始化年齡:19
maoli's age is 20

毛利當(dāng)然是20,奔2的人

這個(gè)例子和草神的bug一樣的道理

形參與成員名字重名,用 this 來(lái)區(qū)分:

可以看到,這里 age 是 GetAge 成員方法的形參,this.age 是 Person 類的成員變量。

還有就是注意:this不能用在static方法中!

這跟jvm聯(lián)系到一起

在靜態(tài)函數(shù)是存在與類(class)一級(jí),并不是對(duì)象(object)的一部分,所以也就沒(méi)有this指針。因此,在靜態(tài)方法中使用this是錯(cuò)誤的。

[圖片上傳失敗...(image-f5b8b3-1586777429301)]

當(dāng)靜態(tài)方法加載進(jìn)內(nèi)存進(jìn)棧時(shí),如果在靜態(tài)方法中有this 和 super 關(guān)鍵字時(shí),this和super也被加載到了內(nèi)存,但是這個(gè)時(shí)候并沒(méi)有對(duì)象的引用,this和super沒(méi)有初始化,所有編譯會(huì)報(bào)錯(cuò)。

this參數(shù)類型的構(gòu)造器

如果為this提供了參數(shù)列表,那么即意味著對(duì)符合該參數(shù)列表的構(gòu)造器的調(diào)用。
通過(guò)this關(guān)鍵字調(diào)用構(gòu)造器有以下幾條規(guī)范:

  • 不能在普通方法中調(diào)用,只能在構(gòu)造器中調(diào)用。
  • 一個(gè)構(gòu)造器中只能調(diào)用一次。
  • 只能在構(gòu)造器的第一行調(diào)用。
/**
 * @author: 毛利
 */
public class test {
    public test(int a, int b, int c) {
        // 構(gòu)造器的調(diào)用必須在一個(gè)構(gòu)造器的第一行
        this(a, b);
        //this(a);   //此行報(bào)錯(cuò),意味著在一個(gè)構(gòu)造器里面只能調(diào)用一次構(gòu)造器。
        System.out.println(b);
    }

    public test(int a, int b) {
        this(a);
        System.out.println("a="+a);
    }

    public test(int a) {
        System.out.println("調(diào)用成功!");
        System.out.println(a);
    }

    public static void main(String[] args) {
        new test(1, 2, 3); //調(diào)用成功,意味著雖然一個(gè)構(gòu)造器中只能調(diào)用一次其它構(gòu)造器,但卻可以通過(guò)“一個(gè)調(diào)一個(gè)”的方式調(diào)用多個(gè)構(gòu)造器。
    }
}


調(diào)用成功!
1
a=1
2
其實(shí)this主要要三種用法:

1、表示對(duì)當(dāng)前對(duì)象的引用!

2、表示用類的成員變量,而非函數(shù)參數(shù),注意在函數(shù)參數(shù)和成員變量同名是進(jìn)行區(qū)分

3、用于在構(gòu)造方法中引用滿足指定參數(shù)類型的構(gòu)造器(其實(shí)也就是構(gòu)造方法)。但是這里必須非常注意:只能引用一個(gè)構(gòu)造方法且必須位于開始!

image
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容