这里如果我尝试覆盖静态方法而不在子类中使用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();
}
}
重写不适用领域。 –
http:// stackoverflow。com/questions/2223386/why-doesnt-java-allow-overriding-of-static-methods –
你不能覆盖静态方法,你会隐藏它们。 – Pshemo