首頁  >  文章  >  Java  >  java中this關鍵字的三種用法

java中this關鍵字的三種用法

王林
王林轉載
2020-02-20 18:05:002574瀏覽

java中this關鍵字的三種用法

this是自身的一個對象,代表對象本身,可以理解為:指向對象本身的一個指標。

this的用法在java中大體可以分為3種:

1、普通的直接引用,this相當於是指向當前物件本身。

(推薦教學:java入門教學

2、形參與成員名字重名,用this來區分:

public Person(String name, int age) {
    this.name = name;
    this.age = age;
}

3、引用本類別的建構子

class Person{
    private String name;
    private int age;
    
    public Person() {
    }
 
    public Person(String name) {
        this.name = name;
    }
    public Person(String name, int age) {
        this(name);
        this.age = age;
    }
}

更多程式設計相關內容,請關注php中文網程式入門欄位!

以上是java中this關鍵字的三種用法的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:csdn.net。如有侵權,請聯絡admin@php.cn刪除