来自社区问题!
public class Test {
public Test() {
}
public void aMethod() {
Base v = new Base();
System.out.println("v.x = " + v.x); //1
bMethod(v);
System.out.println("v.x = " + v.x); //6
}
public void bMethod(Base v) {
v.x = 6;
Base vh = new Base();
v = vh;
System.out.println("v.x = " + v.x); //1
}
public static void main(String[] args) {
Test tst=new Test();
tst.aMethod();
Base vv=new SubBase();
System.out.println("vv.x = " + vv.x); //1 域的隐藏
System.out.println("run vv.method() = " + vv.method()); //2 方法的覆盖
}
}
class Base {
int x = 1;
int method() {
return x;
}
}
class SubBase extends Base {
int x = 2;
int method() {
return x;
}
}
结果:
v.x = 1
v.x = 1
v.x = 6
vv.x = 1
run vv.method() = 2