this变量有两个作用:
1:用于区分成员变量和局部变量
class Example{
int x;
int y;
public Example(int x,int y){
this.x = x;
this.y = y; }
public static void main(String[] args){
Example a = new Example(12,12);
//实例化了一个对象a,此时this变量是指的a
}}
2:用于简化构造方法
class Example{
int x;
int y;
public Example(int x,int y){
this.x = x;
this.y = y; }
public Example(){
this(1,1);//此处表示调用带参的构造方法
}
public static void main(String[] args){
Example a = new Example();
}}
1:用于区分成员变量和局部变量
class Example{
int x;
int y;
public Example(int x,int y){
this.x = x;
this.y = y; }
public static void main(String[] args){
Example a = new Example(12,12);
//实例化了一个对象a,此时this变量是指的a
}}
2:用于简化构造方法
class Example{
int x;
int y;
public Example(int x,int y){
this.x = x;
this.y = y; }
public Example(){
this(1,1);//此处表示调用带参的构造方法
}
public static void main(String[] args){
Example a = new Example();
}}