class A
{
int a = 2, b = 3;
public void display()
{
int c = a + b;
System.out.println(c);
}
}
class B extends A
{
int a = 5, b = 6;
}
class Tester
{
public static void main(String arr[])
{
A x = new A();
B y = new B();
x.display();
y.display();
}
}
为什么输出为 5,5?而不是 5,11?。y.display()
方法如何工作?
最佳答案
why does the output comes 5,5?
因为 A.display()
只知道 A.a
和 A.b
字段。这些是 A
中的任何代码都知道的唯一字段。看起来您希望 B
中的声明“覆盖”现有的字段声明。他们没有。他们声明了新 字段,这些字段隐藏 现有字段。变量的行为实际上与方法的行为方式不同——覆盖变量的概念根本不存在。来自 the JLS section 8.3 :
If the class declares a field with a certain name, then the declaration of that field is said to hide any and all accessible declarations of fields with the same name in superclasses, and superinterfaces of the class.
您可以通过更改 B
来获得所需的效果,以便其构造函数更改它从 A
继承的现有字段的值:
class B extends A {
B() {
a = 5;
b = 6;
}
}
请注意,这些是不是变量声明。他们只是任务。当然,在大多数代码(好吧,我见过的大多数代码)中,A
中的字段都是私有(private)的,因此无法从 B
访问,但这是只是为了解释语言行为的例子。
关于java - 实例字段的继承如何在此特定代码中工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21377859/