按照惯例先吟诗一首:

《八阵图》–杜甫
功盖三分国,名成八阵图。
江流石不转,遗恨失吞吴。

1.成员变量和局部变量重名时,在方法中使用this时,this代表的是该方法所在类的成员变量。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class Hello {
String 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!

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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!

3.有时候,我们会用到一些内部类和匿名类,如事件处理。当在匿名类中用this时,这个this则指的是匿名类或内部类本身。这时如果我们要使用外部类的方法和变量的话,则应该加上外部类的类名。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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。

4.在构造函数中,调用本类的另一个构造函数。可以使用this调用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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:在一个构造函数内只能调用一个构造函数。
  4:不能通过this递归调用构造方法,即不能在一个构造方法中通过this直接或间接调用该构造方法本身。

5.this同时传递多个参数。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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();
}
}

6.super和this的异同

super在一个类中用来引用其父类的成员,它是在子类中访问父类成员的一个桥梁,并不是任何一个对象的引用,而this则表示当前类对象的引用。在代码中Object o = super;是错误的,Object o = this;则是允许的。
super关键字的作用在于当子类中覆盖了父类的某个成员变量,或者重写了父类的某个成员方法时还能够访问到父类的成员变量和成员方法。如果子类中没有重写父类的成员变量和成员方法,则子类会继承父类的所有非private的成员变量和成员方法。这时在子类中无论通过this来访问成员和通过super来访问成员,结果都是一样的。

super.getClass()和this.getClass()

getClass()是Object类定义的一个final方法,所有Java类的getClass()都继承自Object类。如前文所述,如果子类没有重写父类的某个成员方法,那么通过super来访问还是还是通过this来访问结果都是一样的。因此,super.getClass()和this.getClass()结果是一样的。Object类的getClass()方法返回的是该对象的运行时类,一个对象的运行时类是该对象通过new创建时指定的类。因此,super.getClass()和this.getClass()返回的都是new对象时指定的类。