网上对this的描述很朦胧,有的说this表示的是当前对象自己,有的说this是当前对象的引用。
可是自己写了如下的测试代码,产生了几个问题:1 public class T3AboutThis {
2
3 public static void main(String[] args) {
4 new SubT3().func();
5 SuperT3 s = new SuperT3();
6 System.out.println(s instanceof SubT3);//false
7 }
8 }
9
10 class SuperT3 {
11 protected String name = "Jack";
12
13 protected void func() {
14 System.out.println(this.name);//Jack
15 System.out.println(this instanceof SuperT3);//true
16 System.out.println(this instanceof SubT3);//true
17 }
18 }
19 class SubT3 extends SuperT3 {
20 protected String name = "Peter";
21 }
1.this表示的是指向一个实例吗。如果不是指向一个实例,那么为什么this可以参与instanceof运算或当作引用参数传进方法里,super为什么不能?
2.如果this表示当前对象,那么我在第4行new的SubT3的实例,在第14行为什么没有打印出peter呢。
3.如果在第14行时,this只表示SuperT3的引用(我也不知道到底引用什么),那为什么第16行没有打印出与第6行相同的结果呢?
我真的是有点绕晕了。