Java的.通过此运算符访问实例变量值

Man*_*ark 4 java

我需要有人帮助这个小代码片段; 为什么输出:b 3而不是b 13如预期的那样?

public class Foo{ 
    int a = 3;
    public void addFive() { a+=5; System.out.println("f");}  
}

class Bar extends Foo{
int a = 8;
public void addFive() { this.a+=5; System.out.println("b");}  

public static void main(String[] args) {
    Foo f = new Bar();
    f.addFive();
    System.out.println(f.a);// why b 3 and not b 13 ??
    }
}
Run Code Online (Sandbox Code Playgroud)

SLa*_*aks 9

FooBar有两个不同的 a领域; Java没有任何字段覆盖的概念.

调用f.addFive()调用方法的派生版本(因为Java确实执行方法重写),这会修改Bar.a.
但是,访问f.a返回Foo.a(因为f声明为Foo),从未更改过.