一. this.成员变量
二. this.成员方法
三. this( )
this关键字就是引用当前对象以及构造方法调用
1. this.成员变量
我们先看一下,下面这段代码
public class Date {
public int year;
public int month;
public int day;
public void setDate(int y,int m,int d){
year = y;
month = m;
day = d;
}
public void printDate(){
System.out.println(year+"年"+month+"月"+day+"日");
}
}
public class Test {
public static void main(String[] args) {
Date date = new Date();
date.setDate(1111,11,1);
date.printDate();
System.out.println("===========");
Date date2 = new Date();
date2.setDate(2222,22,2);
date2.printDate();
}
}
运行结果如下:

看到这个结果后我们会思考: 这段代码没有用到this关键字,那 setDate和printDate是如何知道我每次赋值和打印的是那个对象。同时我们会发出疑问,既然这种写法我们可以成功为不同对象的成员变量赋值,那为啥还要引入this.成员变量。
其实this是"成员方法"第一个隐藏的参数,编译器会自动传递,在成员方法执行时,编译器会负责将调用成员方法对象的引用传递给该成员方法,this负责来接收,同时将this参数还原,在方法中的所有"成员变量"都通过this引用来访问。
接着通过下面代码,我们就可以明白为什么最好要添加this。
public class Date {
public int year;
public int month;
public int day;
public void setDate(int year,int month,int day){
year = year;
month = month;
day = day;
}
public void printDate(){
System.out.println(year+"年"+month+"月"+day+"日");
}
}
public class Test {
public static void main(String[] args) {
Date date = new Date();
date.setDate(1111,11,1);
date.printDate();
System.out.println("===========");
Date date2 = new Date();
date2.setDate(2222,22,2);
date2.printDate();
}
}

通过观察可以发现,在setDate这个方法中,形参和类的成员变量名相同。这时会出现局部变量优先的原则,出现形参给自己赋值,而成员变量里还是默认值。
最好的写法如下:
public class Date {
public int year;
public int month;
public int day;
public void setDate(int year,int month,int day){
this.year = year;
this.month = month;
this.day = day;
}
public void printDate(){
System.out.println(this.year+"年"+this.month+"月"+this.day+"日");
}
}
2. this.成员方法
public class Date {
public int year;
public int month;
public int day;
public void setDate(int year,int month,int day){
this.year = year;
this.month = month;
this.day = day;
}
public void printDate(){
System.out.println(this.year+"年"+this.month+"月"+this.day+"日");
}
public void show(){
this.printDate();
}
}
public class Test {
public static void main(String[] args) {
Date date = new Date();
date.setDate(1111,11,1);
date.printDate();
date.show();
System.out.println("===========");
Date date2 = new Date();
date2.setDate(2222,22,2);
date2.printDate();
date2.show();
}
}

本质上就是在一个成员方法中用this调用另一个成员方法,这里可以认为this具有传递性。
3. this( )
在这之前先知道对象的初始化。
我们知道,在Java方法内部定义一个局部变量时,必须初始化,否则在使用它时会编译失败。

![]()
而类中的成员变量即使未初始化,也能正常使用。
class Da_te {
public int year;
public int month;
public int day;
public void printDa_te() {
System.out.println(this.year + "年" + this.month + "月" + this.day + "日");
}
}
public class Test {
public static void main(String[] args) {
Da_te date = new Da_te();
date.printDa_te();
}
}
未初始化,显示的是默认值
如果我们想对对象的属性进行赋值,又不想每次赋值时都要创建一个类似setDate这样的函数,这时我们就可以用构造方法。
构造方法:(本质是创建对象时初始化对象状态)
1.名字与类名相同,没有返回值类型,设置为void也不行
2. 一般情况下使用public修饰,若被private修饰,该类不能在其他类中使用new关键字来创建对象
3. 在创建对象时由编译器自动调用,并且在对象的生命周期内只调用一次
4. 构造方法可以发生重载
注意:当自己没有写构造方法时,编译器会生成一份默认的构造方法,生成的默认构造方法一定是无参的(方法体中什么也没有)。但是一旦用户定义,编译器则不在生成。
public class Date {
public int year;
public int month;
public int day;
public Date(){
System.out.println("不带参数的构造方法");
this.year = 2024;
this.month = 10;
this.day = 22;
}
public Date(int year,int month,int day){
System.out.println("带参数的构造方法");
this.year = year;
this.month = month;
this.day = day;
}
public void printDate() {
System.out.println(this.year + "年" + this.month + "月" + this.day + "日");
}
public void show(){
this.printDate();
}
}
public class Test {
public static void main(String[] args) {
Date date = new Date();
date.show();
System.out.println("===============");
Date date2 = new Date(2024,10,22);
date2.show();
}
}

最后我们引入this( )。在构造方法中,可以通过this()调用其他构造方法来简化代码。
其实这就是this( )的主要用途。
public class Date {
public int year;
public int month;
public int day;
public Date() {
this(2024,10,22);
}
public Date(int year, int month, int day) {
System.out.println("带参数的构造方法");
this.year = year;
this.month = month;
this.day = day;
}
public void printDate() {
System.out.println(this.year + "年" + this.month + "月" + this.day + "日");
}
public void show(){
this.printDate();
}
}

需要注意的是:this( )必须是构造方法中的第一条语句!!!!!
同时使用this( )时,不能形成环!!!!!
这种代码编译都过不去
6万+






