当前位置 博文首页 > 沉默王二:教妹学Java(二十 七):this 关键字的用法
你好呀,我是沉默王二,(目前是)CSDN 周排名前十的博客专家。这是《教妹学 Java》专栏的第二十七篇,今天我们来谈谈 Java 的 this 关键字——this 关键字有哪些用法?
本专栏中的代码已收录到 GitHub github.com/itwanger ,里面还有我精心为你准备的一线大厂面试题。
三妹开学了,学的计算机软件编程。她学校离我家很近,坐公交车也就 10 站路的距离, 每逢周末她都会来找我,让我辅导她学习 Java。作为一名拥有十余年编程经验的程序员,再加上父母给我们的这份血缘关系,我觉得义不容辞。
“二哥,今天我们要学习的内容是‘this 关键字’,对吧?”看来三妹已经提前预习了我上次留给她的作业。
“是的,三妹。this 关键字有很多用法。 ”我面带着朴实无华的微笑回答着她,“最常用的一个是,它可以作为一引用变量,指向当前对象。”
----正儿八经的分割线,正文开始------------
下面给出了 this 关键字的六种用法:
this()
可以调用当前类的构造方法;如果参数名和实例变量名产生了冲突,this 关键字可以解决这个问题。先来看一个没有 this 关键字的例子:
/**
* @author 微信搜「沉默王二」,回复关键字 PDF
*/
public class WithoutThisStudent {
String name;
int age;
WithoutThisStudent(String name, int age) {
name = name;
age = age;
}
void out() {
System.out.println(name+" " + age);
}
public static void main(String[] args) {
WithoutThisStudent s1 = new WithoutThisStudent("沉默王二", 18);
WithoutThisStudent s2 = new WithoutThisStudent("沉默王三", 16);
s1.out();
s2.out();
}
}
在上面的例子中,构造方法的参数名和实例变量名相同,由于没有使用 this 关键字,所以无法为实例变量赋值。程序输出的结果如下所示:
null 0
null 0
从结果中可以看得出来,尽管创建对象的时候传递了参数,但实例变量并没有赋值。这是因为如果构造方法中没有使用 this 关键字的话,name 和 age 指向的并不是实例变量而是参数。
怎么解决这个问题呢?加上 this 关键字就好了。
/**
* @author 微信搜「沉默王二」,回复关键字 PDF
*/
public class WithThisStudent {
String name;
int age;
WithThisStudent(String name, int age) {
this.name = name;
this.age = age;
}
void out() {
System.out.println(name+" " + age);
}
public static void main(String[] args) {
WithThisStudent s1 = new WithThisStudent("沉默王二", 18);
WithThisStudent s2 = new WithThisStudent("沉默王三", 16);
s1.out();
s2.out();
}
}
再来看一下程序的输出结果:
沉默王二 18
沉默王三 16
当然了,如果参数名和实例变量名不同的话,就不必使用 this 关键字,但我建议使用 this 关键字,这样的代码更有意义。
我们可以在一个类中使用 this 关键字来调用另外一个方法,如果没有使用的话,编译器会自动帮我们加上。
/**
* @author 微信搜「沉默王二」,回复关键字 PDF
*/
public class InvokeCurrentClassMethod {
void method1() {}
void method2() {
method1();
}
public static void main(String[] args) {
new InvokeCurrentClassMethod().method1();
}
}
在源代码中,method2()
在调用 method1()
的时候并没有使用 this 关键字,但通过反编译后的字节码可以看得到。
public class InvokeCurrentClassMethod {
public InvokeCurrentClassMethod() {
}
void method1() {
}
void method2() {
this.method1();
}
public static void main(String[] args) {
(new InvokeCurrentClassMethod()).method1();
}
}
这次看到了吧?
this()
可用于调用当前类的构造方法——构造方法可以重用了。
/**
* @author 微信搜「沉默王二」,回复关键字 PDF
*/
public class InvokeConstrutor {
InvokeConstrutor() {
System.out.println("hello");
}
InvokeConstrutor(int count) {
this();
System.out.println(count);
}
public static void main(String[] args) {
InvokeConstrutor invokeConstrutor = new InvokeConstrutor(10);
}
}
在有参构造方法 InvokeConstrutor(int count)
中,使用了 this()
来调用无参构造方法 InvokeConstrutor()
。来看一下输出结果:
hello
10
果然,无参构造方法也被调用了,所以输出了“hello”。
也可以在无参构造方法中使用 this()
并传递参数来调用有参构造方法,来看下面这段代码:
/**
* @author 微信搜「沉默王二」,回复关键字 PDF
*/
public class InvokeParamConstrutor {
InvokeParamConstrutor() {
this(10);
System.out.println("hello");
}
InvokeParamConstrutor(int count) {
System.out.println(count);
}
public static void main(String[] args) {
InvokeParamConstrutor invokeConstrutor = new InvokeParamConstrutor();
}
}
来看一下程序的输出结果。
10
hello
注意,this()
必须放在构造方法的第一行,否则就报错了,见下图。
this
关键字可以作为参数在方法中传递,它指向的是当前类的对象。
/**
* @author 微信搜「沉默王二」,回复关键字 PDF
*/
public class ThisAsParam {
void method1(ThisAsParam p) {
System.out.println(p);
}
void method2() {
method1(this);
}
public static void main(String[] args) {
ThisAsParam thisAsParam = new ThisAsParam();
System.out.println(thisAsParam);
thisAsParam.method2();
}
}
来看一下输出结果:
com.itwanger.twentyseven.ThisAsParam@77459877
com.itwanger.twentyseven.ThisAsParam@77459877
method2()
调用了 method1()
,并传递了参数 this,method1()
中打印了当前对象的字符串。 main()
方法中打印了 thisAsParam 对象的字符串。从输出结果中可以看得出来,两者是同一个对象。
this
关键字也可以作为参数在构造方法中传递,它指向的是当前类的对象。当我们需要在多个类中使用一个对象的时候,这非常有用。
/**
* @author 微信搜「沉默王二」,回复关键字 PDF
*/
public class ThisAsConstrutorParam {
int count = 10;
ThisAsConstrutorParam() {
Data data = new Data(this);
data.out();
}
public static void main(String[] args) {
new ThisAsConstrutorParam();
}
}
class Data {
ThisAsConstrutorParam param;
Data(ThisAsConstrutorParam param) {
this.param = param;
}
void out() {
System.out.println(param.count);
}
}
在构造方法 ThisAsConstrutorParam()
中,我们使用 this 关键字作为参数传递给了 Data 对象,它其实指向的就是 new ThisAsConstrutorParam()
这个对象。
来看一下输出结果:
10
this
关键字可以作为方法的返回值,这时候,方法的返回类型为类的类型。
/**
* @author 微信搜「沉默王二」,回复关键字 PDF
*/
public class ThisAsMethodResult {
ThisAsMethodResult getThisAsMethodResult() {
return this;
}
void out() {
System.out.println("hello");
}
public static void main(String[] args) {
new ThisAsMethodResult().getThisAsMethodResult().out();
}
}
getThisAsMethodResult()
方法返回了 this 关键字,指向的就是 new ThisAsMethodResult()
这个对象,所以可以紧接着调用 out()
方法——达到了链式调用的目的。
来看一下输出结果:
hello
“三妹,this 关键字我们就学到这里吧,它的用法我相信你一定全部掌握了。”我揉一揉犯困的双眼,疲惫地给三妹说。
“好的,二哥,我这就去练习去。”三妹似乎意犹未尽,这种学习状态真令我感到开心。
二哥肝了两天两夜,《程序员不可或缺的软实力》第一版强势来袭,纯手敲,足足 20 万字精华文章,贯穿了我十余年的编程生涯,涉及到了生活和工作中的方方面面,如果你是迷茫的在校大学生,或者刚入职的新人,相信我的个人经历,可以给你带去一些思考,从而树立起正确的人生观和价值观。
那这份 PDF 该怎么获取呢?
链接:https://pan.baidu.com/s/1TA3txLvHxQNJEOJUHSZEqQ 密码:ps7z
如链接失效,请转至备用链接:https://shimo.im/docs/pJQv6qVcHqdYwrxx
最后,真心希望这份 PDF 能够对大家起到实质性的帮助,我也会在后面不断完善这本电子书,敬请期待。
cs