在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 教程/ Java/ Java this關(guān)鍵字
Java for循環(huán)
Java接口
Java是什么?
Java命名約定
java中方法重載和方法重寫的區(qū)別
Java運算符
Java抽象類
Java快速入門
Java實例初始化程序塊
Java靜態(tài)綁定和動態(tài)綁定
Java do-while循環(huán)
Java對象克隆
Java Object類
Java聚合
Java繼承
Java this關(guān)鍵字
Java虛擬機內(nèi)部體系結(jié)構(gòu)
Java構(gòu)造器(構(gòu)造方法)
Eclipse安裝和配置
Java多態(tài)
Java方法重載
Java while循環(huán)
Java教程
Java按值調(diào)用和引用調(diào)用
Java strictfp關(guān)鍵字
Java封裝
Java語言特點
Java數(shù)組
Java instanceof運算符
Java包裝類
Java命令行參數(shù)
Java包
Java面向?qū)ο螅∣OP)概念
簡單Java程序
Java基礎(chǔ)實例程序
Java對象和類
Java continue語句
Java抽象類和接口的區(qū)別
C++ VS Java比較
Java if/else語句
Java switch語句
Java歷史
Java變量和數(shù)據(jù)類型
安裝Java環(huán)境(Linux)
Java JDK安裝和配置
Hello Java程序的內(nèi)部細節(jié)
Java break語句
Java方法重寫
Java Unicode編碼系統(tǒng)
怎么樣開始學(xué)習(xí)Java編程?
對象和類之間的區(qū)別
Java訪問修飾符
Java super關(guān)鍵字
Java注釋
JDK,JRE和JVM之間的區(qū)別
Java final關(guān)鍵字
Java static關(guān)鍵字

Java this關(guān)鍵字

在java中,this關(guān)鍵字有很多種用法。 在java中,這是一個引用當(dāng)前對象的引用變量。

java this關(guān)鍵字的用法如下:

  1. this關(guān)鍵字可用來引用當(dāng)前類的實例變量。
  2. this關(guān)鍵字可用于調(diào)用當(dāng)前類方法(隱式)。
  3. this()可以用來調(diào)用當(dāng)前類的構(gòu)造函數(shù)。
  4. this關(guān)鍵字可作為調(diào)用方法中的參數(shù)傳遞。
  5. this關(guān)鍵字可作為參數(shù)在構(gòu)造函數(shù)調(diào)用中傳遞。
  6. this關(guān)鍵字可用于從方法返回當(dāng)前類的實例。

建議:如果你是java初學(xué)者,只學(xué)習(xí) this 關(guān)鍵字的前三個用法就可以了。

1. this:引用當(dāng)前類的實例變量

this關(guān)鍵字可以用來引用當(dāng)前類的實例變量。如果實例變量和參數(shù)之間存在歧義,則 this 關(guān)鍵字可用于明確地指定類變量以解決歧義問題。

了解沒有 this 關(guān)鍵字的問題

下面先來理解一個不使用 this 關(guān)鍵字的示例:

class Student {
    int rollno;
    String name;
    float fee;

    Student(int rollno, String name, float fee) {
        rollno = rollno;
        name = name;
        fee = fee;
    }

    void display() {
        System.out.println(rollno + " " + name + " " + fee);
    }
}

class TestThis1 {
    public static void main(String args[]) {
        Student s1 = new Student(111, "ankit", 5000f);
        Student s2 = new Student(112, "sumit", 6000f);
        s1.display();
        s2.display();
    }
}

執(zhí)行上面代碼輸出結(jié)果如下 -

0 null 0.0
0 null 0.0

在上面的例子中,參數(shù)(形式參數(shù))和實例變量(rollnoname)是相同的。 所以要使用this關(guān)鍵字來區(qū)分局部變量和實例變量。

使用 this 關(guān)鍵字解決了上面的問題

class Student {
    int rollno;
    String name;
    float fee;

    Student(int rollno, String name, float fee) {
        this.rollno = rollno;
        this.name = name;
        this.fee = fee;
    }

    void display() {
        System.out.println(rollno + " " + name + " " + fee);
    }
}

class TestThis2 {
    public static void main(String args[]) {
        Student s1 = new Student(111, "ankit", 5000f);
        Student s2 = new Student(112, "sumit", 6000f);
        s1.display();
        s2.display();
    }
}

執(zhí)行上面代碼輸出結(jié)果如下 -

111 ankit 5000
112 sumit 6000

如果局部變量(形式參數(shù))和實例變量不同,則不需要像下面的程序一樣使用this關(guān)鍵字:

不需要 this 關(guān)鍵字的程序示例

class Student {
    int rollno;
    String name;
    float fee;

    Student(int r, String n, float f) {
        rollno = r;
        name = n;
        fee = f;
    }

    void display() {
        System.out.println(rollno + " " + name + " " + fee);
    }
}

class TestThis3 {
    public static void main(String args[]) {
        Student s1 = new Student(111, "ankit", 5000f);
        Student s2 = new Student(112, "sumit", 6000f);
        s1.display();
        s2.display();
    }
}

執(zhí)行上面代碼輸出結(jié)果如下 -

111 ankit 5000
112 sumit 6000

對變量使用有意義的名稱是一種好的編程習(xí)慣。所以使用相同名稱的實例變量和參數(shù),并且總是使用this關(guān)鍵字。

2. this:調(diào)用當(dāng)前類方法

可以使用this關(guān)鍵字調(diào)用當(dāng)前類的方法。如果不使用this關(guān)鍵字,編譯器會在調(diào)用方法時自動添加此 this 關(guān)鍵字。我們來看看這個例子。

執(zhí)行上面代碼輸出結(jié)果如下 -

hello n
hello m

3. this():調(diào)用當(dāng)前類的構(gòu)造函數(shù)

this()構(gòu)造函數(shù)調(diào)用可以用來調(diào)用當(dāng)前類的構(gòu)造函數(shù)。 它用于重用構(gòu)造函數(shù)。 換句話說,它用于構(gòu)造函數(shù)鏈接。

從參數(shù)化構(gòu)造函數(shù)調(diào)用默認構(gòu)造函數(shù):

class A {
    A() {
        System.out.println("hello a");
    }

    A(int x) {
        this();
        System.out.println(x);
    }
}

class TestThis5 {
    public static void main(String args[]) {
        A a = new A(10);
    }
}

執(zhí)行上面代碼輸出結(jié)果如下 -

hello a
10

從默認構(gòu)造函數(shù)調(diào)用參數(shù)化構(gòu)造函數(shù):

class A {
    A() {
        this(5);
        System.out.println("hello a");
    }

    A(int x) {
        System.out.println(x);
    }
}

class TestThis6 {
    public static void main(String args[]) {
        A a = new A();
    }
}

執(zhí)行上面代碼輸出結(jié)果如下 -

5
hello a

使用this()構(gòu)造函數(shù)調(diào)用

this()構(gòu)造函數(shù)調(diào)用用于從構(gòu)造函數(shù)重用構(gòu)造函數(shù)。 它維護構(gòu)造函數(shù)之間的鏈,即它用于構(gòu)造函數(shù)鏈接??纯聪旅娼o出的示例,顯示this關(guān)鍵字的實際使用。

class Student {
    int rollno;
    String name, course;
    float fee;

    Student(int rollno, String name, String course) {
        this.rollno = rollno;
        this.name = name;
        this.course = course;
    }

    Student(int rollno, String name, String course, float fee) {
        this(rollno, name, course);// reusing constructor
        this.fee = fee;
    }

    void display() {
        System.out.println(rollno + " " + name + " " + course + " " + fee);
    }
}

class TestThis7 {
    public static void main(String args[]) {
        Student s1 = new Student(111, "ankit", "java");
        Student s2 = new Student(112, "sumit", "java", 6000f);
        s1.display();
        s2.display();
    }
}

執(zhí)行上面代碼輸出結(jié)果如下 -

111 ankit java null
112 sumit java 6000

注意:調(diào)用this()必須是構(gòu)造函數(shù)中的第一個語句。

下面示例為不把 this() 語句放在第一行,因此編譯不通過。

class Student {
    int rollno;
    String name, course;
    float fee;

    Student(int rollno, String name, String course) {
        this.rollno = rollno;
        this.name = name;
        this.course = course;
    }

    Student(int rollno, String name, String course, float fee) {
        this.fee = fee;
        this(rollno, name, course);// C.T.Error
    }

    void display() {
        System.out.println(rollno + " " + name + " " + course + " " + fee);
    }
}

class TestThis8 {
    public static void main(String args[]) {
        Student s1 = new Student(111, "ankit", "java");
        Student s2 = new Student(112, "sumit", "java", 6000f);
        s1.display();
        s2.display();
    }
}

執(zhí)行上面代碼輸出結(jié)果如下 -

Compile Time Error: Call to this must be first statement in constructor

4. this:作為參數(shù)傳遞給方法

this關(guān)鍵字也可以作為方法中的參數(shù)傳遞。 它主要用于事件處理。 看看下面的一個例子:

class S2 {
    void m(S2 obj) {
        System.out.println("method is invoked");
    }

    void p() {
        m(this);
    }

    public static void main(String args[]) {
        S2 s1 = new S2();
        s1.p();
    }
}

執(zhí)行上面代碼輸出結(jié)果如下 -

method is invoked

這個應(yīng)用程序可以作為參數(shù)傳遞:

在事件處理(或)的情況下,必須提供一個類的引用到另一個。 它用于在多個方法中重用一個對象。

this:在構(gòu)造函數(shù)調(diào)用中作為參數(shù)傳遞

也可以在構(gòu)造函數(shù)中傳遞this關(guān)鍵字。 如果必須在多個類中使用一個對象,可以使用這種方式。 看看下面的一個例子:

class B {
    A4 obj;

    B(A4 obj) {
        this.obj = obj;
    }

    void display() {
        System.out.println(obj.data);// using data member of A4 class
    }
}

class A4 {
    int data = 10;

    A4() {
        B b = new B(this);
        b.display();
    }

    public static void main(String args[]) {
        A4 a = new A4();
    }
}

執(zhí)行上面代碼輸出結(jié)果如下 -

10

6. this關(guān)鍵字用來返回當(dāng)前類的實例

可以從方法中 this 關(guān)鍵字作為語句返回。 在這種情況下,方法的返回類型必須是類類型(非原始)。 看看下面的一個例子:

作為語句返回的語法

return_type method_name(){  
    return this;  
}

從方法中返回為語句的 this 關(guān)鍵字的示例

class A {
    A getA() {
        return this;
    }

    void msg() {
        System.out.println("Hello java");
    }
}

class Test1 {
    public static void main(String args[]) {
        new A().getA().msg();
    }
}

執(zhí)行上面代碼輸出結(jié)果如下 -

Hello java

驗證 this 關(guān)鍵字

現(xiàn)在來驗證 this 關(guān)鍵字引用當(dāng)前類的實例變量。 在這個程序中將打印參考變量,這兩個變量的輸出是相同的。

class A5 {
    void m() {
        System.out.println(this);// prints same reference ID
    }

    public static void main(String args[]) {
        A5 obj = new A5();
        System.out.println(obj);// prints the reference ID
        obj.m();
    }
}

執(zhí)行上面代碼輸出結(jié)果如下 -

A5@22b3ea59
A5@22b3ea59