【发布时间】:2015-09-08 11:29:10
【问题描述】:
节目是:
class A
{
int i = 10;
}
class B extends A
{
int j = 20;
}
class C extends B
{
int k = 30;
}
class D extends C
{
int m = 40;
}
public class asg2
{
public static void main(String[] args)
{
A[] a = {new A(),new B(), new C(), new D()};
System.out.println(a[3].i); //No error!!!
System.out.println(a[2].j); //throws error
System.out.println(a[1].k); //throws error (Understood why error)
System.out.println(a[0].m); //throws error (Understood why error)
}
}
我明白为什么最后两个抛出错误。 但我不明白为什么第二个打印语句会引发错误。 第一个运行顺利。
asg2.java:29: error: cannot find symbol
System.out.println(a[2].j);
^
symbol: variable j
location: class A
【问题讨论】:
标签: java inheritance scope