专注Java教育14年 全国咨询/投诉热线:444-1124-454
星辉LOGO图
始于2009,口口相传的Java黄埔军校
首页 hot资讯 详解this关键字

详解this关键字

更新时间:2020-11-18 17:23:11 来源:星辉 浏览773次

this关键字是我们在学习Java中接触的为数不多的十分重要的关键字,无论是在Java面试题中还是实际的项目中都会经常遇到,因此,this关键字是我们学习Java的必修课。


this关键字表示当前这个对象,也就是说当前谁调用该方法则这个对象就是谁。每个对象都可以使用this关键字访问其自身的引用,this引用可以隐式的或显示的调用该方法的对象的实例变量和其他方法,this的参考名称并不属于实体变量,事实上在调用本对象方法时,this当成一个隐形的参数传给这个方法。

下面我们就来一一介绍this关键字的用法:


一、当成员变量和局部变量重名时,在方法中使用this时,表示的是该方法所在类中的成员变量。(this是当前对象自己)

public class Hello {

String s = "Hello";//这里的S与Hello()方法里面的成员变量重名

public Hello(String s) {

System.out.println("s = " + s);

System.out.println("1 -> this.s = " + this.s);

this.s = s;//把参数值赋给成员变量,成员变量的值改变

System.out.println("2 -> this.s = " + this.s);

}

public static void main(String[] args) {

Hello x = new Hello("HelloWorld!");

System.out.println("s=" + x.s);//验证成员变量值的改变

}

}

结果为:s = HelloWorld!

1 -> this.s = Hello

2 ->this.s = HelloWorld!s=HelloWorld!


二、把自己当作参数传递时,也可以用this.(this作当前参数进行传递)

class A {

public A() {

new B(this).print();// 调用B的方法

}

public void print() {

System.out.println("HelloAA from A!");

}

}

class B {

A a;

public B(A a) {

this.a = a;

}

public void print() {

a.print();//调用A的方法

System.out.println("HelloAB from B!");

}

}

public class HelloA {

public static void main(String[] args) {

A aaa = new A();

aaa.print();

B bbb = new B(aaa);

bbb.print();

}

}

结果为:

HelloAA from A!

HelloAB from B!

HelloAA from A!

HelloAA from A!

HelloAB from B!

在这个例子中,对象A的构造函数中,用new B(this)把对象A自己作为参数传递给了对象B的构造函数。


三、当在匿名类中用this时,这个this则指的是匿名类或内部类本身。

这时如果我们要使用外部类的方法和变量的话,则应该加上外部类的类名。如:

public class HelloB {

int i = 1;

public HelloB() {

Thread thread = new Thread() {

public void run() {

for (int j=0;j<20;j++) {

HelloB.this.run();//调用外部类的方法

try {

sleep(1000);

} catch (InterruptedException ie) {

}

}

}

}; // 注意这里有分号

thread.start();

}

public void run() {

System.out.println("i = " + i);

i++;

}

public static void main(String[] args) throws Exception {

new HelloB();

}

}

在上面这个例子中, thread 是一个匿名类对象,在它的定义中,它的 run 函数里用到了外部类的 run 函数。这时由于函数同名,直接调用就不行了。这时有两种办法,一种就是把外部的 run 函数换一个名字,但这种办法对于一个开发到中途的应用来说是不可取的。那么就可以用这个例子中的办法用外部类的类名加上 this 引用来说明要调用的是外部类的方法 run。


四、在构造函数中,通过this可以调用同一类中别的构造函数。如:

public class ThisTest {

ThisTest(String str) {

System.out.println(str);

}

ThisTest() {

this("this测试成功!");

}

public static void main(String[] args) {

ThisTest thistest = new ThisTest();

}

}

为了更确切的说明this用法,另外一个例子为:

public class ThisTest {

private int age;

private String str;

ThisTest(String str) {

this.str=str;

System.out.println(str);

}

ThisTest(String str,int age) {

this(str);

this.age=age;

System.out.println(age);

}

public static void main(String[] args) {

ThisTest thistest = new ThisTest("this测试成功",25);

}

}

结果为:this测试成功25

值得注意的是:

1:在构造调用另一个构造函数,调用动作必须置于最起始的位置。

2:不能在构造函数以外的任何函数内调用构造函数。

3:在一个构造函数内只能调用一个构造函数。这一点在第二个构造方法内可以看到,第一个this(str),第二个为this.age=age;


五、this同时传递多个参数

public class TestClass {

int x;

int y;

static void showtest(TestClass tc) {//实例化对象

System.out.println(tc.x + " " + tc.y);

}

void seeit() {

showtest(this);

}

public static void main(String[] args) {

TestClass p = new TestClass();

p.x = 9;

p.y = 10;

p.seeit();

}

}

结果为:9 10


希望大家可以通过上面对this关键字的介绍和用法实例的学习,可以在今后的Java学习和编程中熟练使用this关键字。在本站的Java SE教程中有着对众多Java关键字的全方位解读,感兴趣的小伙伴不容错过哦。


提交申请后,顾问老师会电话与您沟通安排学习

免费课程推荐 >>
技术文档推荐 >>