class A{
void fun1(){
System.out.println(this.fun2());
}
int fun2(){
return 123;
}
}
public class B extends A{
int fun2(){
return 456;
}
public static void main(String[] args) {
A a;
B b = new B();
b.fun1();
a = b;
a.fun1();
}
}
运行结果是:456
this.fun2();
this代表本类(A类)类型的引用,
但是指向的却是B类的对象,因为是B对象在调用它
this-->new B();//this指向的是B对象的地址
所以this.fun2();又可以表示为 A a = new B();
所以this.fun2()----------->a.fun2();
多态调用,结果:456.