<分区>
我有一段代码,我希望其中的输出与实际输出不同。由于静态变量是基于引用的,所以我希望输出是“父类(super class)”,但我得到的是“子类”。代码:
class TestClass {
public static void main(String args[] ) throws Exception {
A b = new B(); // Since the reference is A, "superclass" should be the output
b.test();
}
}
abstract class A{
static String a = "superclass";
abstract void test();
}
class B extends A{
static String a = "subclass";
void test(){
System.out.println(a); // Output subclass
}
}
请告诉我哪里错了..