关于this, 在这里this代表什么??
我写了一个测试程序,如下:
class base {
public boolean equals(Object obj) {
if (this == obj) return true;
if (this.getClass() != obj.getClass()) return false; // 1
return true;
}
}
class derived extends base {
public boolean equals(Object obj) {
if (this == obj) return true;
if (this.getClass() != obj.getClass()) return false;
return super.equals(obj); // 2
}
}
然后,用下面的方法试图运行上面两个类的equals方法(在// 2处调用了base的equals()):
derived d1 = new derived();
derived d2 = new derived();
System.out.println(d1.equals(d2));
我想问的是在上面的// 1处的this.getClass()返回的为什么是derived的Class,而不是base的Class,也就是说,为什么// 1处的this是derived,而不是base??