【发布时间】:2014-06-25 21:12:35
【问题描述】:
如果我尝试在子类中不使用static 来覆盖静态方法,它会给我一个错误。虽然这不是静态变量的情况。为什么?
class A {
static int a;
static void a() {
System.out.println("in A");
}
}
class B extends A {
int a=9;/*this does not give an error*/
void a()/*this statement gives an error*/ {
System.out.println("In B"+(A.a));
}
}
class Test {
public static void main(String []args) {
B b1=new B();
b1.a();
}
}
【问题讨论】:
-
覆盖不适用于字段。
-
你不能覆盖静态方法,你隐藏它们。
标签: java inheritance static static-methods