- 在Java中静态方法可以被继承,但是不能被覆盖,即不能重写。
- 如果子类中也含有一个返回类型、方法名、参数列表均与之相同的静态方法,那么该子类实际上只是将父类中的该同名方法进行了隐藏,而非重写。
- 父类引用指向子类对象时,只会调用父类的静态方法。此为静态分派
public class test {
public static void main(String[] args) {
Foo foo = new Foo();
Foo bar = new Bar();
bar.b(); // "fOO" 父类引用指向子类对象,输出父类的"fOO"
System.out.println(Bar.a);
System.out.println(Bar.a);
}
}
class Foo {
public static int a = 2;
public static void b(){
System.out.println("fOO");
}
}
class Bar extends Foo{
public static int a = 3; // 没有这句时,Bar.a = 2 ; 有这句时,Bar.a = 3
public static void b(){
System.out.println("bar");
}
}