this的具體使用

DemoLiQi發表於2024-06-23

this表示的是當前物件
1.可以區分成員變數的區域性變數
2.呼叫當前物件中的成員
使用:
this()呼叫當前物件的無參構造
this(引數)呼叫當前物件的有參構造
this.成員變數名 呼叫當前物件的成員變數
this.方法名() 呼叫當前物件的成員方法

public class This {
int data=10;
public This(){
this(50);
System.out.println("我是This類中的無參構造");
}
public This(int a){
System.out.println("這是This的有參構造");
}
public void method(){
this.method01();
int data=20;
System.out.println(data);
System.out.println(this.data);
System.out.println("我是this類中的method方法");
}
public void method01(){
System.out.println("我是this類中的method01方法");
}
}
測試類
public class Testthis {
public static void main(String[] args) {
This this=new This();
this.method;
this.merhod01;
}
}

相關文章